Marlin_main.cpp 178 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188
  1. /* -*- c++ -*- */
  2. /*
  3. Reprap firmware based on Sprinter and grbl.
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. This firmware is a mashup between Sprinter and grbl.
  18. (https://github.com/kliment/Sprinter)
  19. (https://github.com/simen/grbl/tree)
  20. It has preliminary support for Matthew Roberts advance algorithm
  21. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  22. */
  23. #include "Marlin.h"
  24. #ifdef ENABLE_AUTO_BED_LEVELING
  25. #include "vector_3.h"
  26. #ifdef AUTO_BED_LEVELING_GRID
  27. #include "qr_solve.h"
  28. #endif
  29. #endif // ENABLE_AUTO_BED_LEVELING
  30. #ifdef MESH_BED_LEVELING
  31. #include "mesh_bed_leveling.h"
  32. #include "mesh_bed_calibration.h"
  33. #endif
  34. #include "ultralcd.h"
  35. #include "Configuration_prusa.h"
  36. #include "planner.h"
  37. #include "stepper.h"
  38. #include "temperature.h"
  39. #include "motion_control.h"
  40. #include "cardreader.h"
  41. #include "watchdog.h"
  42. #include "ConfigurationStore.h"
  43. #include "language.h"
  44. #include "pins_arduino.h"
  45. #include "math.h"
  46. #ifdef BLINKM
  47. #include "BlinkM.h"
  48. #include "Wire.h"
  49. #endif
  50. #ifdef ULTRALCD
  51. #include "ultralcd.h"
  52. #endif
  53. #if NUM_SERVOS > 0
  54. #include "Servo.h"
  55. #endif
  56. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  57. #include <SPI.h>
  58. #endif
  59. #define VERSION_STRING "1.0.2"
  60. #include "ultralcd.h"
  61. // Macros for bit masks
  62. #define BIT(b) (1<<(b))
  63. #define TEST(n,b) (((n)&BIT(b))!=0)
  64. #define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b))
  65. // look here for descriptions of G-codes: http://linuxcnc.org/handbook/gcode/g-code.html
  66. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  67. //Implemented Codes
  68. //-------------------
  69. // PRUSA CODES
  70. // P F - Returns FW versions
  71. // P R - Returns revision of printer
  72. // G0 -> G1
  73. // G1 - Coordinated Movement X Y Z E
  74. // G2 - CW ARC
  75. // G3 - CCW ARC
  76. // G4 - Dwell S<seconds> or P<milliseconds>
  77. // G10 - retract filament according to settings of M207
  78. // G11 - retract recover filament according to settings of M208
  79. // G28 - Home all Axis
  80. // G29 - Detailed Z-Probe, probes the bed at 3 or more points. Will fail if you haven't homed yet.
  81. // G30 - Single Z Probe, probes bed at current XY location.
  82. // G31 - Dock sled (Z_PROBE_SLED only)
  83. // G32 - Undock sled (Z_PROBE_SLED only)
  84. // G80 - Automatic mesh bed leveling
  85. // G81 - Print bed profile
  86. // G90 - Use Absolute Coordinates
  87. // G91 - Use Relative Coordinates
  88. // G92 - Set current position to coordinates given
  89. // M Codes
  90. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  91. // M1 - Same as M0
  92. // M17 - Enable/Power all stepper motors
  93. // M18 - Disable all stepper motors; same as M84
  94. // M20 - List SD card
  95. // M21 - Init SD card
  96. // M22 - Release SD card
  97. // M23 - Select SD file (M23 filename.g)
  98. // M24 - Start/resume SD print
  99. // M25 - Pause SD print
  100. // M26 - Set SD position in bytes (M26 S12345)
  101. // M27 - Report SD print status
  102. // M28 - Start SD write (M28 filename.g)
  103. // M29 - Stop SD write
  104. // M30 - Delete file from SD (M30 filename.g)
  105. // M31 - Output time since last M109 or SD card start to serial
  106. // M32 - Select file and start SD print (Can be used _while_ printing from SD card files):
  107. // syntax "M32 /path/filename#", or "M32 S<startpos bytes> !filename#"
  108. // Call gcode file : "M32 P !filename#" and return to caller file after finishing (similar to #include).
  109. // The '#' is necessary when calling from within sd files, as it stops buffer prereading
  110. // M42 - Change pin status via gcode Use M42 Px Sy to set pin x to value y, when omitting Px the onboard led will be used.
  111. // M80 - Turn on Power Supply
  112. // M81 - Turn off Power Supply
  113. // M82 - Set E codes absolute (default)
  114. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  115. // M84 - Disable steppers until next move,
  116. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  117. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  118. // M92 - Set axis_steps_per_unit - same syntax as G92
  119. // M104 - Set extruder target temp
  120. // M105 - Read current temp
  121. // M106 - Fan on
  122. // M107 - Fan off
  123. // M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating
  124. // Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
  125. // IF AUTOTEMP is enabled, S<mintemp> B<maxtemp> F<factor>. Exit autotemp by any M109 without F
  126. // M112 - Emergency stop
  127. // M114 - Output current position to serial port
  128. // M115 - Capabilities string
  129. // M117 - display message
  130. // M119 - Output Endstop status to serial port
  131. // M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
  132. // M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
  133. // M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  134. // M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  135. // M140 - Set bed target temp
  136. // M150 - Set BlinkM Color Output R: Red<0-255> U(!): Green<0-255> B: Blue<0-255> over i2c, G for green does not work.
  137. // M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating
  138. // Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
  139. // M200 D<millimeters>- set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  140. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  141. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  142. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  143. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) in mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer under-runs and M20 minimum feedrate
  144. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk
  145. // M206 - set additional homing offset
  146. // M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop], stays in mm regardless of M200 setting
  147. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  148. // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  149. // M218 - set hotend offset (in mm): T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  150. // M220 S<factor in percent>- set speed factor override percentage
  151. // M221 S<factor in percent>- set extrude factor override percentage
  152. // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  153. // M240 - Trigger a camera to take a photograph
  154. // M250 - Set LCD contrast C<contrast value> (value 0..63)
  155. // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  156. // M300 - Play beep sound S<frequency Hz> P<duration ms>
  157. // M301 - Set PID parameters P I and D
  158. // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
  159. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  160. // M304 - Set bed PID parameters P I and D
  161. // M400 - Finish all moves
  162. // M401 - Lower z-probe if present
  163. // M402 - Raise z-probe if present
  164. // M404 - N<dia in mm> Enter the nominal filament width (3mm, 1.75mm ) or will display nominal filament width without parameters
  165. // M405 - Turn on Filament Sensor extrusion control. Optional D<delay in cm> to set delay in centimeters between sensor and extruder
  166. // M406 - Turn off Filament Sensor extrusion control
  167. // M407 - Displays measured filament diameter
  168. // M500 - stores parameters in EEPROM
  169. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  170. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  171. // M503 - print the current settings (from memory not from EEPROM)
  172. // M509 - force language selection on next restart
  173. // M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  174. // M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  175. // M605 - Set dual x-carriage movement mode: S<mode> [ X<duplication x-offset> R<duplication temp offset> ]
  176. // M907 - Set digital trimpot motor current using axis codes.
  177. // M908 - Control digital trimpot directly.
  178. // M350 - Set microstepping mode.
  179. // M351 - Toggle MS1 MS2 pins directly.
  180. // M928 - Start SD logging (M928 filename.g) - ended by M29
  181. // M999 - Restart after being stopped by error
  182. //Stepper Movement Variables
  183. //===========================================================================
  184. //=============================imported variables============================
  185. //===========================================================================
  186. //===========================================================================
  187. //=============================public variables=============================
  188. //===========================================================================
  189. #ifdef SDSUPPORT
  190. CardReader card;
  191. #endif
  192. union Data
  193. {
  194. byte b[2];
  195. int value;
  196. };
  197. int babystepLoad[3];
  198. float homing_feedrate[] = HOMING_FEEDRATE;
  199. // Currently only the extruder axis may be switched to a relative mode.
  200. // Other axes are always absolute or relative based on the common relative_mode flag.
  201. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  202. int feedmultiply=100; //100->1 200->2
  203. int saved_feedmultiply;
  204. int extrudemultiply=100; //100->1 200->2
  205. int extruder_multiply[EXTRUDERS] = {100
  206. #if EXTRUDERS > 1
  207. , 100
  208. #if EXTRUDERS > 2
  209. , 100
  210. #endif
  211. #endif
  212. };
  213. bool is_usb_printing = false;
  214. bool _doMeshL = false;
  215. unsigned int usb_printing_counter;
  216. int lcd_change_fil_state = 0;
  217. int feedmultiplyBckp = 100;
  218. unsigned char lang_selected = 0;
  219. unsigned long total_filament_used;
  220. unsigned int heating_status;
  221. unsigned int heating_status_counter;
  222. bool custom_message;
  223. unsigned int custom_message_type;
  224. unsigned int custom_message_state;
  225. bool volumetric_enabled = false;
  226. float filament_size[EXTRUDERS] = { DEFAULT_NOMINAL_FILAMENT_DIA
  227. #if EXTRUDERS > 1
  228. , DEFAULT_NOMINAL_FILAMENT_DIA
  229. #if EXTRUDERS > 2
  230. , DEFAULT_NOMINAL_FILAMENT_DIA
  231. #endif
  232. #endif
  233. };
  234. float volumetric_multiplier[EXTRUDERS] = {1.0
  235. #if EXTRUDERS > 1
  236. , 1.0
  237. #if EXTRUDERS > 2
  238. , 1.0
  239. #endif
  240. #endif
  241. };
  242. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  243. float add_homing[3]={0,0,0};
  244. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  245. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  246. bool axis_known_position[3] = {false, false, false};
  247. float zprobe_zoffset;
  248. // Extruder offset
  249. #if EXTRUDERS > 1
  250. #ifndef DUAL_X_CARRIAGE
  251. #define NUM_EXTRUDER_OFFSETS 2 // only in XY plane
  252. #else
  253. #define NUM_EXTRUDER_OFFSETS 3 // supports offsets in XYZ plane
  254. #endif
  255. float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = {
  256. #if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
  257. EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
  258. #endif
  259. };
  260. #endif
  261. uint8_t active_extruder = 0;
  262. int fanSpeed=0;
  263. #ifdef FWRETRACT
  264. bool autoretract_enabled=false;
  265. bool retracted[EXTRUDERS]={false
  266. #if EXTRUDERS > 1
  267. , false
  268. #if EXTRUDERS > 2
  269. , false
  270. #endif
  271. #endif
  272. };
  273. bool retracted_swap[EXTRUDERS]={false
  274. #if EXTRUDERS > 1
  275. , false
  276. #if EXTRUDERS > 2
  277. , false
  278. #endif
  279. #endif
  280. };
  281. float retract_length = RETRACT_LENGTH;
  282. float retract_length_swap = RETRACT_LENGTH_SWAP;
  283. float retract_feedrate = RETRACT_FEEDRATE;
  284. float retract_zlift = RETRACT_ZLIFT;
  285. float retract_recover_length = RETRACT_RECOVER_LENGTH;
  286. float retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP;
  287. float retract_recover_feedrate = RETRACT_RECOVER_FEEDRATE;
  288. #endif
  289. #ifdef ULTIPANEL
  290. #ifdef PS_DEFAULT_OFF
  291. bool powersupply = false;
  292. #else
  293. bool powersupply = true;
  294. #endif
  295. #endif
  296. bool cancel_heatup = false ;
  297. #ifdef FILAMENT_SENSOR
  298. //Variables for Filament Sensor input
  299. float filament_width_nominal=DEFAULT_NOMINAL_FILAMENT_DIA; //Set nominal filament width, can be changed with M404
  300. bool filament_sensor=false; //M405 turns on filament_sensor control, M406 turns it off
  301. float filament_width_meas=DEFAULT_MEASURED_FILAMENT_DIA; //Stores the measured filament diameter
  302. signed char measurement_delay[MAX_MEASUREMENT_DELAY+1]; //ring buffer to delay measurement store extruder factor after subtracting 100
  303. int delay_index1=0; //index into ring buffer
  304. int delay_index2=-1; //index into ring buffer - set to -1 on startup to indicate ring buffer needs to be initialized
  305. float delay_dist=0; //delay distance counter
  306. int meas_delay_cm = MEASUREMENT_DELAY_CM; //distance delay setting
  307. #endif
  308. const char errormagic[] PROGMEM = "Error:";
  309. const char echomagic[] PROGMEM = "echo:";
  310. //===========================================================================
  311. //=============================Private Variables=============================
  312. //===========================================================================
  313. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  314. float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  315. static float delta[3] = {0.0, 0.0, 0.0};
  316. // For tracing an arc
  317. static float offset[3] = {0.0, 0.0, 0.0};
  318. static bool home_all_axis = true;
  319. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  320. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  321. // Determines Absolute or Relative Coordinates.
  322. // Also there is bool axis_relative_modes[] per axis flag.
  323. static bool relative_mode = false;
  324. // String circular buffer. Commands may be pushed to the buffer from both sides:
  325. // Chained commands will be pushed to the front, interactive (from LCD menu)
  326. // and printing commands (from serial line or from SD card) are pushed to the tail.
  327. // First character of each entry indicates the type of the entry:
  328. #define CMDBUFFER_CURRENT_TYPE_UNKNOWN 0
  329. // Command in cmdbuffer was sent over USB.
  330. #define CMDBUFFER_CURRENT_TYPE_USB 1
  331. // Command in cmdbuffer was read from SDCARD.
  332. #define CMDBUFFER_CURRENT_TYPE_SDCARD 2
  333. // Command in cmdbuffer was generated by the UI.
  334. #define CMDBUFFER_CURRENT_TYPE_UI 3
  335. // Command in cmdbuffer was generated by another G-code.
  336. #define CMDBUFFER_CURRENT_TYPE_CHAINED 4
  337. // How much space to reserve for the chained commands
  338. // of type CMDBUFFER_CURRENT_TYPE_CHAINED,
  339. // which are pushed to the front of the queue?
  340. // Maximum 5 commands of max length 20 + null terminator.
  341. #define CMDBUFFER_RESERVE_FRONT (5*21)
  342. // Reserve BUFSIZE lines of length MAX_CMD_SIZE plus CMDBUFFER_RESERVE_FRONT.
  343. static char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT];
  344. // Head of the circular buffer, where to read.
  345. static int bufindr = 0;
  346. // Tail of the buffer, where to write.
  347. static int bufindw = 0;
  348. // Number of lines in cmdbuffer.
  349. static int buflen = 0;
  350. // Flag for processing the current command inside the main Arduino loop().
  351. // If a new command was pushed to the front of a command buffer while
  352. // processing another command, this replaces the command on the top.
  353. // Therefore don't remove the command from the queue in the loop() function.
  354. static bool cmdbuffer_front_already_processed = false;
  355. // Type of a command, which is to be executed right now.
  356. #define CMDBUFFER_CURRENT_TYPE (cmdbuffer[bufindr])
  357. // String of a command, which is to be executed right now.
  358. #define CMDBUFFER_CURRENT_STRING (cmdbuffer+bufindr+1)
  359. // Enable debugging of the command buffer.
  360. // Debugging information will be sent to serial line.
  361. // #define CMDBUFFER_DEBUG
  362. static int serial_count = 0;
  363. static boolean comment_mode = false;
  364. static char *strchr_pointer; // just a pointer to find chars in the command string like X, Y, Z, E, etc
  365. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  366. //static float tt = 0;
  367. //static float bt = 0;
  368. //Inactivity shutdown variables
  369. static unsigned long previous_millis_cmd = 0;
  370. static unsigned long max_inactive_time = 0;
  371. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  372. unsigned long starttime=0;
  373. unsigned long stoptime=0;
  374. unsigned long _usb_timer = 0;
  375. static uint8_t tmp_extruder;
  376. bool Stopped=false;
  377. #if NUM_SERVOS > 0
  378. Servo servos[NUM_SERVOS];
  379. #endif
  380. bool CooldownNoWait = true;
  381. bool target_direction;
  382. //Insert variables if CHDK is defined
  383. #ifdef CHDK
  384. unsigned long chdkHigh = 0;
  385. boolean chdkActive = false;
  386. #endif
  387. //===========================================================================
  388. //=============================Routines======================================
  389. //===========================================================================
  390. void get_arc_coordinates();
  391. bool setTargetedHotend(int code);
  392. void serial_echopair_P(const char *s_P, float v)
  393. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  394. void serial_echopair_P(const char *s_P, double v)
  395. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  396. void serial_echopair_P(const char *s_P, unsigned long v)
  397. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  398. #ifdef SDSUPPORT
  399. #include "SdFatUtil.h"
  400. int freeMemory() { return SdFatUtil::FreeRam(); }
  401. #else
  402. extern "C" {
  403. extern unsigned int __bss_end;
  404. extern unsigned int __heap_start;
  405. extern void *__brkval;
  406. int freeMemory() {
  407. int free_memory;
  408. if ((int)__brkval == 0)
  409. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  410. else
  411. free_memory = ((int)&free_memory) - ((int)__brkval);
  412. return free_memory;
  413. }
  414. }
  415. #endif //!SDSUPPORT
  416. // Pop the currently processed command from the queue.
  417. // It is expected, that there is at least one command in the queue.
  418. void cmdqueue_pop_front()
  419. {
  420. if (buflen > 0) {
  421. #ifdef CMDBUFFER_DEBUG
  422. SERIAL_ECHOPGM("Dequeing ");
  423. SERIAL_ECHO(cmdbuffer+bufindr+1);
  424. SERIAL_ECHOLNPGM("");
  425. SERIAL_ECHOPGM("Old indices: buflen ");
  426. SERIAL_ECHO(buflen);
  427. SERIAL_ECHOPGM(", bufindr ");
  428. SERIAL_ECHO(bufindr);
  429. SERIAL_ECHOPGM(", bufindw ");
  430. SERIAL_ECHO(bufindw);
  431. SERIAL_ECHOPGM(", serial_count ");
  432. SERIAL_ECHO(serial_count);
  433. SERIAL_ECHOPGM(", bufsize ");
  434. SERIAL_ECHO(sizeof(cmdbuffer));
  435. SERIAL_ECHOLNPGM("");
  436. #endif /* CMDBUFFER_DEBUG */
  437. if (-- buflen == 0) {
  438. // Empty buffer.
  439. if (serial_count == 0)
  440. // No serial communication is pending. Reset both pointers to zero.
  441. bufindw = 0;
  442. bufindr = bufindw;
  443. } else {
  444. // There is at least one ready line in the buffer.
  445. // First skip the current command ID and iterate up to the end of the string.
  446. for (++ bufindr; cmdbuffer[bufindr] != 0; ++ bufindr) ;
  447. // Second, skip the end of string null character and iterate until a nonzero command ID is found.
  448. for (++ bufindr; bufindr < sizeof(cmdbuffer) && cmdbuffer[bufindr] == 0; ++ bufindr) ;
  449. // If the end of the buffer was empty,
  450. if (bufindr == sizeof(cmdbuffer)) {
  451. // skip to the start and find the nonzero command.
  452. for (bufindr = 0; cmdbuffer[bufindr] == 0; ++ bufindr) ;
  453. }
  454. #ifdef CMDBUFFER_DEBUG
  455. SERIAL_ECHOPGM("New indices: buflen ");
  456. SERIAL_ECHO(buflen);
  457. SERIAL_ECHOPGM(", bufindr ");
  458. SERIAL_ECHO(bufindr);
  459. SERIAL_ECHOPGM(", bufindw ");
  460. SERIAL_ECHO(bufindw);
  461. SERIAL_ECHOPGM(", serial_count ");
  462. SERIAL_ECHO(serial_count);
  463. SERIAL_ECHOPGM(" new command on the top: ");
  464. SERIAL_ECHO(cmdbuffer+bufindr+1);
  465. SERIAL_ECHOLNPGM("");
  466. #endif /* CMDBUFFER_DEBUG */
  467. }
  468. }
  469. }
  470. // How long a string could be pushed to the front of the command queue?
  471. // If yes, adjust bufindr to the new position, where the new command could be enqued.
  472. // len_asked does not contain the zero terminator size.
  473. bool cmdqueue_could_enqueue_front(int len_asked)
  474. {
  475. // MAX_CMD_SIZE has to accommodate the zero terminator.
  476. if (len_asked >= MAX_CMD_SIZE)
  477. return false;
  478. // Remove the currently processed command from the queue.
  479. if (! cmdbuffer_front_already_processed) {
  480. cmdqueue_pop_front();
  481. cmdbuffer_front_already_processed = true;
  482. }
  483. if (bufindr == bufindw && buflen > 0)
  484. // Full buffer.
  485. return false;
  486. // Adjust the end of the write buffer based on whether a partial line is in the receive buffer.
  487. int endw = (serial_count > 0) ? (bufindw + MAX_CMD_SIZE + 1) : bufindw;
  488. if (bufindw < bufindr) {
  489. int bufindr_new = bufindr - len_asked - 2;
  490. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  491. if (endw <= bufindr_new) {
  492. bufindr = bufindr_new;
  493. return true;
  494. }
  495. } else {
  496. // Otherwise the free space is split between the start and end.
  497. if (len_asked + 2 <= bufindr) {
  498. // Could fit at the start.
  499. bufindr -= len_asked + 2;
  500. return true;
  501. }
  502. int bufindr_new = sizeof(cmdbuffer) - len_asked - 2;
  503. if (endw <= bufindr_new) {
  504. memset(cmdbuffer, 0, bufindr);
  505. bufindr = bufindr_new;
  506. return true;
  507. }
  508. }
  509. return false;
  510. }
  511. // Could one enqueue a command of lenthg len_asked into the buffer,
  512. // while leaving CMDBUFFER_RESERVE_FRONT at the start?
  513. // If yes, adjust bufindw to the new position, where the new command could be enqued.
  514. // len_asked does not contain the zero terminator size.
  515. bool cmdqueue_could_enqueue_back(int len_asked)
  516. {
  517. // MAX_CMD_SIZE has to accommodate the zero terminator.
  518. if (len_asked >= MAX_CMD_SIZE)
  519. return false;
  520. if (bufindr == bufindw && buflen > 0)
  521. // Full buffer.
  522. return false;
  523. if (serial_count > 0) {
  524. // If there is some data stored starting at bufindw, len_asked is certainly smaller than
  525. // the allocated data buffer. Try to reserve a new buffer and to move the already received
  526. // serial data.
  527. // How much memory to reserve for the commands pushed to the front?
  528. // End of the queue, when pushing to the end.
  529. int endw = bufindw + len_asked + 2;
  530. if (bufindw < bufindr)
  531. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  532. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  533. // Otherwise the free space is split between the start and end.
  534. if (// Could one fit to the end, including the reserve?
  535. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  536. // Could one fit to the end, and the reserve to the start?
  537. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  538. return true;
  539. // Could one fit both to the start?
  540. if (len_asked + 2 + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  541. // Mark the rest of the buffer as used.
  542. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  543. // and point to the start.
  544. bufindw = 0;
  545. return true;
  546. }
  547. } else {
  548. // How much memory to reserve for the commands pushed to the front?
  549. // End of the queue, when pushing to the end.
  550. int endw = bufindw + len_asked + 2;
  551. if (bufindw < bufindr)
  552. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  553. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  554. // Otherwise the free space is split between the start and end.
  555. if (// Could one fit to the end, including the reserve?
  556. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  557. // Could one fit to the end, and the reserve to the start?
  558. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  559. return true;
  560. // Could one fit both to the start?
  561. if (len_asked + 2 + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  562. // Mark the rest of the buffer as used.
  563. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  564. // and point to the start.
  565. bufindw = 0;
  566. return true;
  567. }
  568. }
  569. return false;
  570. }
  571. #ifdef CMDBUFFER_DEBUG
  572. static void cmdqueue_dump_to_serial_single_line(int nr, const char *p)
  573. {
  574. SERIAL_ECHOPGM("Entry nr: ");
  575. SERIAL_ECHO(nr);
  576. SERIAL_ECHOPGM(", type: ");
  577. SERIAL_ECHO(int(*p));
  578. SERIAL_ECHOPGM(", cmd: ");
  579. SERIAL_ECHO(p+1);
  580. SERIAL_ECHOLNPGM("");
  581. }
  582. static void cmdqueue_dump_to_serial()
  583. {
  584. if (buflen == 0) {
  585. SERIAL_ECHOLNPGM("The command buffer is empty.");
  586. } else {
  587. SERIAL_ECHOPGM("Content of the buffer: entries ");
  588. SERIAL_ECHO(buflen);
  589. SERIAL_ECHOPGM(", indr ");
  590. SERIAL_ECHO(bufindr);
  591. SERIAL_ECHOPGM(", indw ");
  592. SERIAL_ECHO(bufindw);
  593. SERIAL_ECHOLNPGM("");
  594. int nr = 0;
  595. if (bufindr < bufindw) {
  596. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + bufindw; ++ nr) {
  597. cmdqueue_dump_to_serial_single_line(nr, p);
  598. // Skip the command.
  599. for (++p; *p != 0; ++ p);
  600. // Skip the gaps.
  601. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  602. }
  603. } else {
  604. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + sizeof(cmdbuffer); ++ nr) {
  605. cmdqueue_dump_to_serial_single_line(nr, p);
  606. // Skip the command.
  607. for (++p; *p != 0; ++ p);
  608. // Skip the gaps.
  609. for (++p; p < cmdbuffer + sizeof(cmdbuffer) && *p == 0; ++ p);
  610. }
  611. for (const char *p = cmdbuffer; p < cmdbuffer + bufindw; ++ nr) {
  612. cmdqueue_dump_to_serial_single_line(nr, p);
  613. // Skip the command.
  614. for (++p; *p != 0; ++ p);
  615. // Skip the gaps.
  616. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  617. }
  618. }
  619. SERIAL_ECHOLNPGM("End of the buffer.");
  620. }
  621. }
  622. #endif /* CMDBUFFER_DEBUG */
  623. //adds an command to the main command buffer
  624. //thats really done in a non-safe way.
  625. //needs overworking someday
  626. // Currently the maximum length of a command piped through this function is around 20 characters
  627. void enquecommand(const char *cmd, bool from_progmem)
  628. {
  629. int len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  630. // Does cmd fit the queue while leaving sufficient space at the front for the chained commands?
  631. // If it fits, it may move bufindw, so it points to a contiguous buffer, which fits cmd.
  632. if (cmdqueue_could_enqueue_back(len)) {
  633. // This is dangerous if a mixing of serial and this happens
  634. // This may easily be tested: If serial_count > 0, we have a problem.
  635. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_UI;
  636. if (from_progmem)
  637. strcpy_P(cmdbuffer + bufindw + 1, cmd);
  638. else
  639. strcpy(cmdbuffer + bufindw + 1, cmd);
  640. SERIAL_ECHO_START;
  641. SERIAL_ECHORPGM(MSG_Enqueing);
  642. SERIAL_ECHO(cmdbuffer + bufindw + 1);
  643. SERIAL_ECHOLNPGM("\"");
  644. bufindw += len + 2;
  645. if (bufindw == sizeof(cmdbuffer))
  646. bufindw = 0;
  647. ++ buflen;
  648. #ifdef CMDBUFFER_DEBUG
  649. cmdqueue_dump_to_serial();
  650. #endif /* CMDBUFFER_DEBUG */
  651. } else {
  652. SERIAL_ECHO_START;
  653. SERIAL_ECHORPGM(MSG_Enqueing);
  654. if (from_progmem)
  655. SERIAL_PROTOCOLRPGM(cmd);
  656. else
  657. SERIAL_ECHO(cmd);
  658. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  659. #ifdef CMDBUFFER_DEBUG
  660. cmdqueue_dump_to_serial();
  661. #endif /* CMDBUFFER_DEBUG */
  662. }
  663. }
  664. void enquecommand_front(const char *cmd, bool from_progmem)
  665. {
  666. int len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  667. // Does cmd fit the queue? This call shall move bufindr, so the command may be copied.
  668. if (cmdqueue_could_enqueue_front(len)) {
  669. cmdbuffer[bufindr] = CMDBUFFER_CURRENT_TYPE_UI;
  670. if (from_progmem)
  671. strcpy_P(cmdbuffer + bufindr + 1, cmd);
  672. else
  673. strcpy(cmdbuffer + bufindr + 1, cmd);
  674. ++ buflen;
  675. SERIAL_ECHO_START;
  676. SERIAL_ECHOPGM("Enqueing to the front: \"");
  677. SERIAL_ECHO(cmdbuffer + bufindr + 1);
  678. SERIAL_ECHOLNPGM("\"");
  679. #ifdef CMDBUFFER_DEBUG
  680. cmdqueue_dump_to_serial();
  681. #endif /* CMDBUFFER_DEBUG */
  682. } else {
  683. SERIAL_ECHO_START;
  684. SERIAL_ECHOPGM("Enqueing to the front: \"");
  685. if (from_progmem)
  686. SERIAL_PROTOCOLRPGM(cmd);
  687. else
  688. SERIAL_ECHO(cmd);
  689. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  690. #ifdef CMDBUFFER_DEBUG
  691. cmdqueue_dump_to_serial();
  692. #endif /* CMDBUFFER_DEBUG */
  693. }
  694. }
  695. // Mark the command at the top of the command queue as new.
  696. // Therefore it will not be removed from the queue.
  697. void repeatcommand_front()
  698. {
  699. cmdbuffer_front_already_processed = true;
  700. }
  701. void setup_killpin()
  702. {
  703. #if defined(KILL_PIN) && KILL_PIN > -1
  704. SET_INPUT(KILL_PIN);
  705. WRITE(KILL_PIN,HIGH);
  706. #endif
  707. }
  708. // Set home pin
  709. void setup_homepin(void)
  710. {
  711. #if defined(HOME_PIN) && HOME_PIN > -1
  712. SET_INPUT(HOME_PIN);
  713. WRITE(HOME_PIN,HIGH);
  714. #endif
  715. }
  716. void setup_photpin()
  717. {
  718. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  719. SET_OUTPUT(PHOTOGRAPH_PIN);
  720. WRITE(PHOTOGRAPH_PIN, LOW);
  721. #endif
  722. }
  723. void setup_powerhold()
  724. {
  725. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  726. SET_OUTPUT(SUICIDE_PIN);
  727. WRITE(SUICIDE_PIN, HIGH);
  728. #endif
  729. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  730. SET_OUTPUT(PS_ON_PIN);
  731. #if defined(PS_DEFAULT_OFF)
  732. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  733. #else
  734. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  735. #endif
  736. #endif
  737. }
  738. void suicide()
  739. {
  740. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  741. SET_OUTPUT(SUICIDE_PIN);
  742. WRITE(SUICIDE_PIN, LOW);
  743. #endif
  744. }
  745. void servo_init()
  746. {
  747. #if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1)
  748. servos[0].attach(SERVO0_PIN);
  749. #endif
  750. #if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1)
  751. servos[1].attach(SERVO1_PIN);
  752. #endif
  753. #if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1)
  754. servos[2].attach(SERVO2_PIN);
  755. #endif
  756. #if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1)
  757. servos[3].attach(SERVO3_PIN);
  758. #endif
  759. #if (NUM_SERVOS >= 5)
  760. #error "TODO: enter initalisation code for more servos"
  761. #endif
  762. }
  763. static void lcd_language_menu();
  764. #ifdef MESH_BED_LEVELING
  765. enum MeshLevelingState { MeshReport, MeshStart, MeshNext, MeshSet };
  766. #endif
  767. // "Setup" function is called by the Arduino framework on startup.
  768. // Before startup, the Timers-functions (PWM)/Analog RW and HardwareSerial provided by the Arduino-code
  769. // are initialized by the main() routine provided by the Arduino framework.
  770. void setup()
  771. {
  772. setup_killpin();
  773. setup_powerhold();
  774. MYSERIAL.begin(BAUDRATE);
  775. SERIAL_PROTOCOLLNPGM("start");
  776. SERIAL_ECHO_START;
  777. #if 0
  778. SERIAL_ECHOLN("Reading eeprom from 0 to 100: start");
  779. for (int i = 0; i < 4096; ++ i) {
  780. int b = eeprom_read_byte((unsigned char*)i);
  781. if (b != 255) {
  782. SERIAL_ECHO(i);
  783. SERIAL_ECHO(":");
  784. SERIAL_ECHO(b);
  785. SERIAL_ECHOLN("");
  786. }
  787. }
  788. SERIAL_ECHOLN("Reading eeprom from 0 to 100: done");
  789. #endif
  790. // Check startup - does nothing if bootloader sets MCUSR to 0
  791. byte mcu = MCUSR;
  792. if(mcu & 1) SERIAL_ECHOLNRPGM(MSG_POWERUP);
  793. if(mcu & 2) SERIAL_ECHOLNRPGM(MSG_EXTERNAL_RESET);
  794. if(mcu & 4) SERIAL_ECHOLNRPGM(MSG_BROWNOUT_RESET);
  795. if(mcu & 8) SERIAL_ECHOLNRPGM(MSG_WATCHDOG_RESET);
  796. if(mcu & 32) SERIAL_ECHOLNRPGM(MSG_SOFTWARE_RESET);
  797. MCUSR=0;
  798. //SERIAL_ECHORPGM(MSG_MARLIN);
  799. //SERIAL_ECHOLNRPGM(VERSION_STRING);
  800. #ifdef STRING_VERSION_CONFIG_H
  801. #ifdef STRING_CONFIG_H_AUTHOR
  802. SERIAL_ECHO_START;
  803. SERIAL_ECHORPGM(MSG_CONFIGURATION_VER);
  804. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  805. SERIAL_ECHORPGM(MSG_AUTHOR);
  806. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  807. SERIAL_ECHOPGM("Compiled: ");
  808. SERIAL_ECHOLNPGM(__DATE__);
  809. #endif
  810. #endif
  811. SERIAL_ECHO_START;
  812. SERIAL_ECHORPGM(MSG_FREE_MEMORY);
  813. SERIAL_ECHO(freeMemory());
  814. SERIAL_ECHORPGM(MSG_PLANNER_BUFFER_BYTES);
  815. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  816. // loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
  817. Config_RetrieveSettings();
  818. tp_init(); // Initialize temperature loop
  819. plan_init(); // Initialize planner;
  820. watchdog_init();
  821. st_init(); // Initialize stepper, this enables interrupts!
  822. setup_photpin();
  823. servo_init();
  824. // Reset the machine correction matrix.
  825. // It does not make sense to load the correction matrix until the machine is homed.
  826. world2machine_reset();
  827. lcd_init();
  828. if(!READ(BTN_ENC) ){
  829. _delay_ms(1000);
  830. if(!READ(BTN_ENC) ){
  831. SET_OUTPUT(BEEPER);
  832. WRITE(BEEPER,HIGH);
  833. lcd_force_language_selection();
  834. while(!READ(BTN_ENC));
  835. WRITE(BEEPER,LOW);
  836. }
  837. }else{
  838. _delay_ms(1000); // wait 1sec to display the splash screen
  839. }
  840. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  841. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  842. #endif
  843. #ifdef DIGIPOT_I2C
  844. digipot_i2c_init();
  845. #endif
  846. setup_homepin();
  847. #if defined(Z_AXIS_ALWAYS_ON)
  848. enable_z();
  849. #endif
  850. }
  851. // The loop() function is called in an endless loop by the Arduino framework from the default main() routine.
  852. // Before loop(), the setup() function is called by the main() routine.
  853. void loop()
  854. {
  855. if (usb_printing_counter > 0 && millis()-_usb_timer > 1000)
  856. {
  857. is_usb_printing = true;
  858. usb_printing_counter--;
  859. _usb_timer = millis();
  860. }
  861. if (usb_printing_counter == 0)
  862. {
  863. is_usb_printing = false;
  864. }
  865. get_command();
  866. #ifdef SDSUPPORT
  867. card.checkautostart(false);
  868. #endif
  869. if(buflen)
  870. {
  871. #ifdef SDSUPPORT
  872. if(card.saving)
  873. {
  874. // Saving a G-code file onto an SD-card is in progress.
  875. // Saving starts with M28, saving until M29 is seen.
  876. if(strstr_P(CMDBUFFER_CURRENT_STRING, PSTR("M29")) == NULL) {
  877. card.write_command(CMDBUFFER_CURRENT_STRING);
  878. if(card.logging)
  879. process_commands();
  880. else
  881. SERIAL_PROTOCOLLNRPGM(MSG_OK);
  882. } else {
  883. card.closefile();
  884. SERIAL_PROTOCOLLNRPGM(MSG_FILE_SAVED);
  885. }
  886. } else {
  887. process_commands();
  888. }
  889. #else
  890. process_commands();
  891. #endif //SDSUPPORT
  892. if (! cmdbuffer_front_already_processed)
  893. cmdqueue_pop_front();
  894. cmdbuffer_front_already_processed = false;
  895. }
  896. //check heater every n milliseconds
  897. manage_heater();
  898. manage_inactivity();
  899. checkHitEndstops();
  900. lcd_update();
  901. }
  902. void get_command()
  903. {
  904. // Test and reserve space for the new command string.
  905. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1))
  906. return;
  907. while (MYSERIAL.available() > 0) {
  908. char serial_char = MYSERIAL.read();
  909. if(serial_char == '\n' ||
  910. serial_char == '\r' ||
  911. (serial_char == ':' && comment_mode == false) ||
  912. serial_count >= (MAX_CMD_SIZE - 1) )
  913. {
  914. if(!serial_count) { //if empty line
  915. comment_mode = false; //for new command
  916. return;
  917. }
  918. cmdbuffer[bufindw+serial_count+1] = 0; //terminate string
  919. if(!comment_mode){
  920. comment_mode = false; //for new command
  921. if ((strchr_pointer = strchr(cmdbuffer+bufindw+1, 'N')) != NULL)
  922. {
  923. // Line number met. When sending a G-code over a serial line, each line may be stamped with its index,
  924. // and Marlin tests, whether the successive lines are stamped with an increasing line number ID.
  925. gcode_N = (strtol(strchr_pointer+1, NULL, 10));
  926. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer+bufindw+1, PSTR("M110")) == NULL) ) {
  927. // M110 - set current line number.
  928. // Line numbers not sent in succession.
  929. SERIAL_ERROR_START;
  930. SERIAL_ERRORRPGM(MSG_ERR_LINE_NO);
  931. SERIAL_ERRORLN(gcode_LastN);
  932. //Serial.println(gcode_N);
  933. FlushSerialRequestResend();
  934. serial_count = 0;
  935. return;
  936. }
  937. if((strchr_pointer = strchr(cmdbuffer+bufindw+1, '*')) != NULL)
  938. {
  939. byte checksum = 0;
  940. char *p = cmdbuffer+bufindw+1;
  941. while (p != strchr_pointer)
  942. checksum = checksum^(*p++);
  943. if (int(strtol(strchr_pointer+1, NULL, 10)) != int(checksum)) {
  944. SERIAL_ERROR_START;
  945. SERIAL_ERRORRPGM(MSG_ERR_CHECKSUM_MISMATCH);
  946. SERIAL_ERRORLN(gcode_LastN);
  947. FlushSerialRequestResend();
  948. serial_count = 0;
  949. return;
  950. }
  951. // If no errors, remove the checksum and continue parsing.
  952. *strchr_pointer = 0;
  953. }
  954. else
  955. {
  956. SERIAL_ERROR_START;
  957. SERIAL_ERRORRPGM(MSG_ERR_NO_CHECKSUM);
  958. SERIAL_ERRORLN(gcode_LastN);
  959. FlushSerialRequestResend();
  960. serial_count = 0;
  961. return;
  962. }
  963. gcode_LastN = gcode_N;
  964. //if no errors, continue parsing
  965. } // end of 'N' command
  966. else // if we don't receive 'N' but still see '*'
  967. {
  968. if((strchr(cmdbuffer+bufindw+1, '*') != NULL))
  969. {
  970. SERIAL_ERROR_START;
  971. SERIAL_ERRORRPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  972. SERIAL_ERRORLN(gcode_LastN);
  973. serial_count = 0;
  974. return;
  975. }
  976. } // end of '*' command
  977. if ((strchr_pointer = strchr(cmdbuffer+bufindw+1, 'G')) != NULL) {
  978. if (! IS_SD_PRINTING) {
  979. usb_printing_counter = 10;
  980. is_usb_printing = true;
  981. }
  982. if (Stopped == true) {
  983. int gcode = strtol(strchr_pointer+1, NULL, 10);
  984. if (gcode >= 0 && gcode <= 3) {
  985. SERIAL_ERRORLNRPGM(MSG_ERR_STOPPED);
  986. LCD_MESSAGERPGM(MSG_STOPPED);
  987. }
  988. }
  989. } // end of 'G' command
  990. //If command was e-stop process now
  991. if(strcmp(cmdbuffer+bufindw+1, "M112") == 0)
  992. kill();
  993. // Store the current line into buffer, move to the next line.
  994. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_USB;
  995. #ifdef CMDBUFFER_DEBUG
  996. SERIAL_ECHO_START;
  997. SERIAL_ECHOPGM("Storing a command line to buffer: ");
  998. SERIAL_ECHO(cmdbuffer+bufindw+1);
  999. SERIAL_ECHOLNPGM("");
  1000. #endif /* CMDBUFFER_DEBUG */
  1001. bufindw += strlen(cmdbuffer+bufindw+1) + 2;
  1002. if (bufindw == sizeof(cmdbuffer))
  1003. bufindw = 0;
  1004. ++ buflen;
  1005. #ifdef CMDBUFFER_DEBUG
  1006. SERIAL_ECHOPGM("Number of commands in the buffer: ");
  1007. SERIAL_ECHO(buflen);
  1008. SERIAL_ECHOLNPGM("");
  1009. #endif /* CMDBUFFER_DEBUG */
  1010. } // end of 'not comment mode'
  1011. serial_count = 0; //clear buffer
  1012. // Don't call cmdqueue_could_enqueue_back if there are no characters waiting
  1013. // in the queue, as this function will reserve the memory.
  1014. if (MYSERIAL.available() == 0 || ! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1))
  1015. return;
  1016. } // end of "end of line" processing
  1017. else {
  1018. // Not an "end of line" symbol. Store the new character into a buffer.
  1019. if(serial_char == ';') comment_mode = true;
  1020. if(!comment_mode) cmdbuffer[bufindw+1+serial_count++] = serial_char;
  1021. }
  1022. } // end of serial line processing loop
  1023. #ifdef SDSUPPORT
  1024. if(!card.sdprinting || serial_count!=0){
  1025. // If there is a half filled buffer from serial line, wait until return before
  1026. // continuing with the serial line.
  1027. return;
  1028. }
  1029. //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible
  1030. // if it occurs, stop_buffering is triggered and the buffer is ran dry.
  1031. // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing
  1032. static bool stop_buffering=false;
  1033. if(buflen==0) stop_buffering=false;
  1034. // Reads whole lines from the SD card. Never leaves a half-filled line in the cmdbuffer.
  1035. while( !card.eof() && !stop_buffering) {
  1036. int16_t n=card.get();
  1037. char serial_char = (char)n;
  1038. if(serial_char == '\n' ||
  1039. serial_char == '\r' ||
  1040. (serial_char == '#' && comment_mode == false) ||
  1041. (serial_char == ':' && comment_mode == false) ||
  1042. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  1043. {
  1044. if(card.eof()){
  1045. SERIAL_PROTOCOLLNRPGM(MSG_FILE_PRINTED);
  1046. stoptime=millis();
  1047. char time[30];
  1048. unsigned long t=(stoptime-starttime)/1000;
  1049. int hours, minutes;
  1050. minutes=(t/60)%60;
  1051. hours=t/60/60;
  1052. save_statistics(total_filament_used, t);
  1053. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  1054. SERIAL_ECHO_START;
  1055. SERIAL_ECHOLN(time);
  1056. lcd_setstatus(time);
  1057. card.printingHasFinished();
  1058. card.checkautostart(true);
  1059. }
  1060. if(serial_char=='#')
  1061. stop_buffering=true;
  1062. if(!serial_count)
  1063. {
  1064. comment_mode = false; //for new command
  1065. return; //if empty line
  1066. }
  1067. cmdbuffer[bufindw+serial_count+1] = 0; //terminate string
  1068. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_SDCARD;
  1069. ++ buflen;
  1070. bufindw += strlen(cmdbuffer+bufindw+1) + 2;
  1071. if (bufindw == sizeof(cmdbuffer))
  1072. bufindw = 0;
  1073. comment_mode = false; //for new command
  1074. serial_count = 0; //clear buffer
  1075. // The following line will reserve buffer space if available.
  1076. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1))
  1077. return;
  1078. }
  1079. else
  1080. {
  1081. if(serial_char == ';') comment_mode = true;
  1082. if(!comment_mode) cmdbuffer[bufindw+1+serial_count++] = serial_char;
  1083. }
  1084. }
  1085. #endif //SDSUPPORT
  1086. }
  1087. // Return True if a character was found
  1088. static inline bool code_seen(char code) { return (strchr_pointer = strchr(CMDBUFFER_CURRENT_STRING, code)) != NULL; }
  1089. static inline bool code_seen(const char *code) { return (strchr_pointer = strstr(CMDBUFFER_CURRENT_STRING, code)) != NULL; }
  1090. static inline float code_value() { return strtod(strchr_pointer+1, NULL); }
  1091. static inline long code_value_long() { return strtol(strchr_pointer+1, NULL, 10); }
  1092. static inline int16_t code_value_short() { return int16_t(strtol(strchr_pointer+1, NULL, 10)); };
  1093. #define DEFINE_PGM_READ_ANY(type, reader) \
  1094. static inline type pgm_read_any(const type *p) \
  1095. { return pgm_read_##reader##_near(p); }
  1096. DEFINE_PGM_READ_ANY(float, float);
  1097. DEFINE_PGM_READ_ANY(signed char, byte);
  1098. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  1099. static const PROGMEM type array##_P[3] = \
  1100. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  1101. static inline type array(int axis) \
  1102. { return pgm_read_any(&array##_P[axis]); } \
  1103. type array##_ext(int axis) \
  1104. { return pgm_read_any(&array##_P[axis]); }
  1105. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  1106. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  1107. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  1108. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  1109. XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
  1110. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  1111. #ifdef DUAL_X_CARRIAGE
  1112. #if EXTRUDERS == 1 || defined(COREXY) \
  1113. || !defined(X2_ENABLE_PIN) || !defined(X2_STEP_PIN) || !defined(X2_DIR_PIN) \
  1114. || !defined(X2_HOME_POS) || !defined(X2_MIN_POS) || !defined(X2_MAX_POS) \
  1115. || !defined(X_MAX_PIN) || X_MAX_PIN < 0
  1116. #error "Missing or invalid definitions for DUAL_X_CARRIAGE mode."
  1117. #endif
  1118. #if X_HOME_DIR != -1 || X2_HOME_DIR != 1
  1119. #error "Please use canonical x-carriage assignment" // the x-carriages are defined by their homing directions
  1120. #endif
  1121. #define DXC_FULL_CONTROL_MODE 0
  1122. #define DXC_AUTO_PARK_MODE 1
  1123. #define DXC_DUPLICATION_MODE 2
  1124. static int dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  1125. static float x_home_pos(int extruder) {
  1126. if (extruder == 0)
  1127. return base_home_pos(X_AXIS) + add_homing[X_AXIS];
  1128. else
  1129. // In dual carriage mode the extruder offset provides an override of the
  1130. // second X-carriage offset when homed - otherwise X2_HOME_POS is used.
  1131. // This allow soft recalibration of the second extruder offset position without firmware reflash
  1132. // (through the M218 command).
  1133. return (extruder_offset[X_AXIS][1] > 0) ? extruder_offset[X_AXIS][1] : X2_HOME_POS;
  1134. }
  1135. static int x_home_dir(int extruder) {
  1136. return (extruder == 0) ? X_HOME_DIR : X2_HOME_DIR;
  1137. }
  1138. static float inactive_extruder_x_pos = X2_MAX_POS; // used in mode 0 & 1
  1139. static bool active_extruder_parked = false; // used in mode 1 & 2
  1140. static float raised_parked_position[NUM_AXIS]; // used in mode 1
  1141. static unsigned long delayed_move_time = 0; // used in mode 1
  1142. static float duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  1143. static float duplicate_extruder_temp_offset = 0; // used in mode 2
  1144. bool extruder_duplication_enabled = false; // used in mode 2
  1145. #endif //DUAL_X_CARRIAGE
  1146. static void axis_is_at_home(int axis) {
  1147. #ifdef DUAL_X_CARRIAGE
  1148. if (axis == X_AXIS) {
  1149. if (active_extruder != 0) {
  1150. current_position[X_AXIS] = x_home_pos(active_extruder);
  1151. min_pos[X_AXIS] = X2_MIN_POS;
  1152. max_pos[X_AXIS] = max(extruder_offset[X_AXIS][1], X2_MAX_POS);
  1153. return;
  1154. }
  1155. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0) {
  1156. current_position[X_AXIS] = base_home_pos(X_AXIS) + add_homing[X_AXIS];
  1157. min_pos[X_AXIS] = base_min_pos(X_AXIS) + add_homing[X_AXIS];
  1158. max_pos[X_AXIS] = min(base_max_pos(X_AXIS) + add_homing[X_AXIS],
  1159. max(extruder_offset[X_AXIS][1], X2_MAX_POS) - duplicate_extruder_x_offset);
  1160. return;
  1161. }
  1162. }
  1163. #endif
  1164. current_position[axis] = base_home_pos(axis) + add_homing[axis];
  1165. min_pos[axis] = base_min_pos(axis) + add_homing[axis];
  1166. max_pos[axis] = base_max_pos(axis) + add_homing[axis];
  1167. }
  1168. inline void set_current_to_destination() { memcpy(current_position, destination, sizeof(current_position)); }
  1169. inline void set_destination_to_current() { memcpy(destination, current_position, sizeof(destination)); }
  1170. static void setup_for_endstop_move() {
  1171. saved_feedrate = feedrate;
  1172. saved_feedmultiply = feedmultiply;
  1173. feedmultiply = 100;
  1174. previous_millis_cmd = millis();
  1175. enable_endstops(true);
  1176. }
  1177. static void clean_up_after_endstop_move() {
  1178. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  1179. enable_endstops(false);
  1180. #endif
  1181. feedrate = saved_feedrate;
  1182. feedmultiply = saved_feedmultiply;
  1183. previous_millis_cmd = millis();
  1184. }
  1185. #ifdef ENABLE_AUTO_BED_LEVELING
  1186. #ifdef AUTO_BED_LEVELING_GRID
  1187. static void set_bed_level_equation_lsq(double *plane_equation_coefficients)
  1188. {
  1189. vector_3 planeNormal = vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1);
  1190. planeNormal.debug("planeNormal");
  1191. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  1192. //bedLevel.debug("bedLevel");
  1193. //plan_bed_level_matrix.debug("bed level before");
  1194. //vector_3 uncorrected_position = plan_get_position_mm();
  1195. //uncorrected_position.debug("position before");
  1196. vector_3 corrected_position = plan_get_position();
  1197. // corrected_position.debug("position after");
  1198. current_position[X_AXIS] = corrected_position.x;
  1199. current_position[Y_AXIS] = corrected_position.y;
  1200. current_position[Z_AXIS] = corrected_position.z;
  1201. // put the bed at 0 so we don't go below it.
  1202. current_position[Z_AXIS] = zprobe_zoffset; // in the lsq we reach here after raising the extruder due to the loop structure
  1203. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1204. }
  1205. #else // not AUTO_BED_LEVELING_GRID
  1206. static void set_bed_level_equation_3pts(float z_at_pt_1, float z_at_pt_2, float z_at_pt_3) {
  1207. plan_bed_level_matrix.set_to_identity();
  1208. vector_3 pt1 = vector_3(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, z_at_pt_1);
  1209. vector_3 pt2 = vector_3(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, z_at_pt_2);
  1210. vector_3 pt3 = vector_3(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, z_at_pt_3);
  1211. vector_3 from_2_to_1 = (pt1 - pt2).get_normal();
  1212. vector_3 from_2_to_3 = (pt3 - pt2).get_normal();
  1213. vector_3 planeNormal = vector_3::cross(from_2_to_1, from_2_to_3).get_normal();
  1214. planeNormal = vector_3(planeNormal.x, planeNormal.y, abs(planeNormal.z));
  1215. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  1216. vector_3 corrected_position = plan_get_position();
  1217. current_position[X_AXIS] = corrected_position.x;
  1218. current_position[Y_AXIS] = corrected_position.y;
  1219. current_position[Z_AXIS] = corrected_position.z;
  1220. // put the bed at 0 so we don't go below it.
  1221. current_position[Z_AXIS] = zprobe_zoffset;
  1222. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1223. }
  1224. #endif // AUTO_BED_LEVELING_GRID
  1225. static void run_z_probe() {
  1226. plan_bed_level_matrix.set_to_identity();
  1227. feedrate = homing_feedrate[Z_AXIS];
  1228. // move down until you find the bed
  1229. float zPosition = -10;
  1230. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  1231. st_synchronize();
  1232. // we have to let the planner know where we are right now as it is not where we said to go.
  1233. zPosition = st_get_position_mm(Z_AXIS);
  1234. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS]);
  1235. // move up the retract distance
  1236. zPosition += home_retract_mm(Z_AXIS);
  1237. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  1238. st_synchronize();
  1239. // move back down slowly to find bed
  1240. feedrate = homing_feedrate[Z_AXIS]/4;
  1241. zPosition -= home_retract_mm(Z_AXIS) * 2;
  1242. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  1243. st_synchronize();
  1244. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  1245. // make sure the planner knows where we are as it may be a bit different than we last said to move to
  1246. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1247. }
  1248. static void do_blocking_move_to(float x, float y, float z) {
  1249. float oldFeedRate = feedrate;
  1250. feedrate = homing_feedrate[Z_AXIS];
  1251. current_position[Z_AXIS] = z;
  1252. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  1253. st_synchronize();
  1254. feedrate = XY_TRAVEL_SPEED;
  1255. current_position[X_AXIS] = x;
  1256. current_position[Y_AXIS] = y;
  1257. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  1258. st_synchronize();
  1259. feedrate = oldFeedRate;
  1260. }
  1261. static void do_blocking_move_relative(float offset_x, float offset_y, float offset_z) {
  1262. do_blocking_move_to(current_position[X_AXIS] + offset_x, current_position[Y_AXIS] + offset_y, current_position[Z_AXIS] + offset_z);
  1263. }
  1264. /// Probe bed height at position (x,y), returns the measured z value
  1265. static float probe_pt(float x, float y, float z_before) {
  1266. // move to right place
  1267. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
  1268. do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
  1269. run_z_probe();
  1270. float measured_z = current_position[Z_AXIS];
  1271. SERIAL_PROTOCOLRPGM(MSG_BED);
  1272. SERIAL_PROTOCOLPGM(" x: ");
  1273. SERIAL_PROTOCOL(x);
  1274. SERIAL_PROTOCOLPGM(" y: ");
  1275. SERIAL_PROTOCOL(y);
  1276. SERIAL_PROTOCOLPGM(" z: ");
  1277. SERIAL_PROTOCOL(measured_z);
  1278. SERIAL_PROTOCOLPGM("\n");
  1279. return measured_z;
  1280. }
  1281. #endif // #ifdef ENABLE_AUTO_BED_LEVELING
  1282. void homeaxis(int axis) {
  1283. #define HOMEAXIS_DO(LETTER) \
  1284. ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
  1285. if (axis==X_AXIS ? HOMEAXIS_DO(X) :
  1286. axis==Y_AXIS ? HOMEAXIS_DO(Y) :
  1287. axis==Z_AXIS ? HOMEAXIS_DO(Z) :
  1288. 0) {
  1289. int axis_home_dir = home_dir(axis);
  1290. #ifdef DUAL_X_CARRIAGE
  1291. if (axis == X_AXIS)
  1292. axis_home_dir = x_home_dir(active_extruder);
  1293. #endif
  1294. current_position[axis] = 0;
  1295. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1296. destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
  1297. feedrate = homing_feedrate[axis];
  1298. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1299. st_synchronize();
  1300. current_position[axis] = 0;
  1301. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1302. destination[axis] = -home_retract_mm(axis) * axis_home_dir;
  1303. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1304. st_synchronize();
  1305. destination[axis] = 2*home_retract_mm(axis) * axis_home_dir;
  1306. feedrate = homing_feedrate[axis]/2 ;
  1307. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1308. st_synchronize();
  1309. axis_is_at_home(axis);
  1310. destination[axis] = current_position[axis];
  1311. feedrate = 0.0;
  1312. endstops_hit_on_purpose();
  1313. axis_known_position[axis] = true;
  1314. }
  1315. }
  1316. void home_xy()
  1317. {
  1318. set_destination_to_current();
  1319. homeaxis(X_AXIS);
  1320. homeaxis(Y_AXIS);
  1321. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1322. endstops_hit_on_purpose();
  1323. }
  1324. void refresh_cmd_timeout(void)
  1325. {
  1326. previous_millis_cmd = millis();
  1327. }
  1328. #ifdef FWRETRACT
  1329. void retract(bool retracting, bool swapretract = false) {
  1330. if(retracting && !retracted[active_extruder]) {
  1331. destination[X_AXIS]=current_position[X_AXIS];
  1332. destination[Y_AXIS]=current_position[Y_AXIS];
  1333. destination[Z_AXIS]=current_position[Z_AXIS];
  1334. destination[E_AXIS]=current_position[E_AXIS];
  1335. if (swapretract) {
  1336. current_position[E_AXIS]+=retract_length_swap/volumetric_multiplier[active_extruder];
  1337. } else {
  1338. current_position[E_AXIS]+=retract_length/volumetric_multiplier[active_extruder];
  1339. }
  1340. plan_set_e_position(current_position[E_AXIS]);
  1341. float oldFeedrate = feedrate;
  1342. feedrate=retract_feedrate*60;
  1343. retracted[active_extruder]=true;
  1344. prepare_move();
  1345. current_position[Z_AXIS]-=retract_zlift;
  1346. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1347. prepare_move();
  1348. feedrate = oldFeedrate;
  1349. } else if(!retracting && retracted[active_extruder]) {
  1350. destination[X_AXIS]=current_position[X_AXIS];
  1351. destination[Y_AXIS]=current_position[Y_AXIS];
  1352. destination[Z_AXIS]=current_position[Z_AXIS];
  1353. destination[E_AXIS]=current_position[E_AXIS];
  1354. current_position[Z_AXIS]+=retract_zlift;
  1355. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1356. //prepare_move();
  1357. if (swapretract) {
  1358. current_position[E_AXIS]-=(retract_length_swap+retract_recover_length_swap)/volumetric_multiplier[active_extruder];
  1359. } else {
  1360. current_position[E_AXIS]-=(retract_length+retract_recover_length)/volumetric_multiplier[active_extruder];
  1361. }
  1362. plan_set_e_position(current_position[E_AXIS]);
  1363. float oldFeedrate = feedrate;
  1364. feedrate=retract_recover_feedrate*60;
  1365. retracted[active_extruder]=false;
  1366. prepare_move();
  1367. feedrate = oldFeedrate;
  1368. }
  1369. } //retract
  1370. #endif //FWRETRACT
  1371. void process_commands()
  1372. {
  1373. #ifdef FILAMENT_RUNOUT_SUPPORT
  1374. SET_INPUT(FR_SENS);
  1375. #endif
  1376. #ifdef CMDBUFFER_DEBUG
  1377. SERIAL_ECHOPGM("Processing a GCODE command: ");
  1378. SERIAL_ECHO(cmdbuffer+bufindr+1);
  1379. SERIAL_ECHOLNPGM("");
  1380. SERIAL_ECHOPGM("In cmdqueue: ");
  1381. SERIAL_ECHO(buflen);
  1382. SERIAL_ECHOLNPGM("");
  1383. #endif /* CMDBUFFER_DEBUG */
  1384. unsigned long codenum; //throw away variable
  1385. char *starpos = NULL;
  1386. #ifdef ENABLE_AUTO_BED_LEVELING
  1387. float x_tmp, y_tmp, z_tmp, real_z;
  1388. #endif
  1389. // PRUSA GCODES
  1390. if(code_seen("PRUSA")){
  1391. if(code_seen("Fir")){
  1392. SERIAL_PROTOCOLLN(FW_version);
  1393. } else if(code_seen("Rev")){
  1394. SERIAL_PROTOCOLLN(FILAMENT_SIZE "-" ELECTRONICS "-" NOZZLE_TYPE );
  1395. } else if(code_seen("Lang")) {
  1396. lcd_force_language_selection();
  1397. } else if(code_seen("Lz")) {
  1398. EEPROM_save_B(EEPROM_BABYSTEP_Z,0);
  1399. }
  1400. //else if (code_seen('Cal')) {
  1401. // lcd_calibration();
  1402. // }
  1403. }
  1404. else
  1405. if(code_seen('G'))
  1406. {
  1407. switch((int)code_value())
  1408. {
  1409. case 0: // G0 -> G1
  1410. case 1: // G1
  1411. if(Stopped == false) {
  1412. #ifdef FILAMENT_RUNOUT_SUPPORT
  1413. if(READ(FR_SENS)){
  1414. feedmultiplyBckp=feedmultiply;
  1415. float target[4];
  1416. float lastpos[4];
  1417. target[X_AXIS]=current_position[X_AXIS];
  1418. target[Y_AXIS]=current_position[Y_AXIS];
  1419. target[Z_AXIS]=current_position[Z_AXIS];
  1420. target[E_AXIS]=current_position[E_AXIS];
  1421. lastpos[X_AXIS]=current_position[X_AXIS];
  1422. lastpos[Y_AXIS]=current_position[Y_AXIS];
  1423. lastpos[Z_AXIS]=current_position[Z_AXIS];
  1424. lastpos[E_AXIS]=current_position[E_AXIS];
  1425. //retract by E
  1426. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  1427. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 400, active_extruder);
  1428. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  1429. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 300, active_extruder);
  1430. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  1431. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  1432. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder);
  1433. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  1434. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder);
  1435. //finish moves
  1436. st_synchronize();
  1437. //disable extruder steppers so filament can be removed
  1438. disable_e0();
  1439. disable_e1();
  1440. disable_e2();
  1441. delay(100);
  1442. //LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
  1443. uint8_t cnt=0;
  1444. int counterBeep = 0;
  1445. lcd_wait_interact();
  1446. while(!lcd_clicked()){
  1447. cnt++;
  1448. manage_heater();
  1449. manage_inactivity(true);
  1450. //lcd_update();
  1451. if(cnt==0)
  1452. {
  1453. #if BEEPER > 0
  1454. if (counterBeep== 500){
  1455. counterBeep = 0;
  1456. }
  1457. SET_OUTPUT(BEEPER);
  1458. if (counterBeep== 0){
  1459. WRITE(BEEPER,HIGH);
  1460. }
  1461. if (counterBeep== 20){
  1462. WRITE(BEEPER,LOW);
  1463. }
  1464. counterBeep++;
  1465. #else
  1466. #if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
  1467. lcd_buzz(1000/6,100);
  1468. #else
  1469. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS,LCD_FEEDBACK_FREQUENCY_HZ);
  1470. #endif
  1471. #endif
  1472. }
  1473. }
  1474. WRITE(BEEPER,LOW);
  1475. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  1476. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder);
  1477. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  1478. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  1479. lcd_change_fil_state = 0;
  1480. lcd_loading_filament();
  1481. while ((lcd_change_fil_state == 0)||(lcd_change_fil_state != 1)){
  1482. lcd_change_fil_state = 0;
  1483. lcd_alright();
  1484. switch(lcd_change_fil_state){
  1485. case 2:
  1486. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  1487. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder);
  1488. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  1489. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  1490. lcd_loading_filament();
  1491. break;
  1492. case 3:
  1493. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  1494. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  1495. lcd_loading_color();
  1496. break;
  1497. default:
  1498. lcd_change_success();
  1499. break;
  1500. }
  1501. }
  1502. target[E_AXIS]+= 5;
  1503. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  1504. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT;
  1505. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 400, active_extruder);
  1506. //current_position[E_AXIS]=target[E_AXIS]; //the long retract of L is compensated by manual filament feeding
  1507. //plan_set_e_position(current_position[E_AXIS]);
  1508. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //should do nothing
  1509. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //move xy back
  1510. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], 200, active_extruder); //move z back
  1511. target[E_AXIS]= target[E_AXIS] - FILAMENTCHANGE_FIRSTRETRACT;
  1512. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], 5, active_extruder); //final untretract
  1513. plan_set_e_position(lastpos[E_AXIS]);
  1514. feedmultiply=feedmultiplyBckp;
  1515. char cmd[9];
  1516. sprintf_P(cmd, PSTR("M220 S%i"), feedmultiplyBckp);
  1517. enquecommand(cmd);
  1518. }
  1519. #endif
  1520. get_coordinates(); // For X Y Z E F
  1521. total_filament_used = total_filament_used + ((destination[E_AXIS] - current_position[E_AXIS])*100);
  1522. #ifdef FWRETRACT
  1523. if(autoretract_enabled)
  1524. if( !(code_seen('X') || code_seen('Y') || code_seen('Z')) && code_seen('E')) {
  1525. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1526. if((echange<-MIN_RETRACT && !retracted) || (echange>MIN_RETRACT && retracted)) { //move appears to be an attempt to retract or recover
  1527. current_position[E_AXIS] = destination[E_AXIS]; //hide the slicer-generated retract/recover from calculations
  1528. plan_set_e_position(current_position[E_AXIS]); //AND from the planner
  1529. retract(!retracted);
  1530. return;
  1531. }
  1532. }
  1533. #endif //FWRETRACT
  1534. prepare_move();
  1535. //ClearToSend();
  1536. }
  1537. break;
  1538. case 2: // G2 - CW ARC
  1539. if(Stopped == false) {
  1540. get_arc_coordinates();
  1541. prepare_arc_move(true);
  1542. }
  1543. break;
  1544. case 3: // G3 - CCW ARC
  1545. if(Stopped == false) {
  1546. get_arc_coordinates();
  1547. prepare_arc_move(false);
  1548. }
  1549. break;
  1550. case 4: // G4 dwell
  1551. LCD_MESSAGERPGM(MSG_DWELL);
  1552. codenum = 0;
  1553. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  1554. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  1555. st_synchronize();
  1556. codenum += millis(); // keep track of when we started waiting
  1557. previous_millis_cmd = millis();
  1558. while(millis() < codenum) {
  1559. manage_heater();
  1560. manage_inactivity();
  1561. lcd_update();
  1562. }
  1563. break;
  1564. #ifdef FWRETRACT
  1565. case 10: // G10 retract
  1566. #if EXTRUDERS > 1
  1567. retracted_swap[active_extruder]=(code_seen('S') && code_value_long() == 1); // checks for swap retract argument
  1568. retract(true,retracted_swap[active_extruder]);
  1569. #else
  1570. retract(true);
  1571. #endif
  1572. break;
  1573. case 11: // G11 retract_recover
  1574. #if EXTRUDERS > 1
  1575. retract(false,retracted_swap[active_extruder]);
  1576. #else
  1577. retract(false);
  1578. #endif
  1579. break;
  1580. #endif //FWRETRACT
  1581. case 28: //G28 Home all Axis one at a time
  1582. #ifdef ENABLE_AUTO_BED_LEVELING
  1583. plan_bed_level_matrix.set_to_identity(); //Reset the plane ("erase" all leveling data)
  1584. #endif //ENABLE_AUTO_BED_LEVELING
  1585. _doMeshL = false;
  1586. // For mesh bed leveling deactivate the matrix temporarily
  1587. #ifdef MESH_BED_LEVELING
  1588. mbl.active = 0;
  1589. #endif
  1590. // Reset world2machine_rotation_and_skew and world2machine_shift, therefore
  1591. // the planner will not perform any adjustments in the XY plane.
  1592. world2machine_reset();
  1593. saved_feedrate = feedrate;
  1594. saved_feedmultiply = feedmultiply;
  1595. feedmultiply = 100;
  1596. previous_millis_cmd = millis();
  1597. enable_endstops(true);
  1598. for(int8_t i=0; i < NUM_AXIS; i++) {
  1599. destination[i] = current_position[i];
  1600. }
  1601. feedrate = 0.0;
  1602. home_all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS])));
  1603. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  1604. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1605. homeaxis(Z_AXIS);
  1606. }
  1607. #endif
  1608. #ifdef QUICK_HOME
  1609. // In the quick mode, if both x and y are to be homed, a diagonal move will be performed initially.
  1610. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  1611. {
  1612. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  1613. #ifndef DUAL_X_CARRIAGE
  1614. int x_axis_home_dir = home_dir(X_AXIS);
  1615. #else
  1616. int x_axis_home_dir = x_home_dir(active_extruder);
  1617. extruder_duplication_enabled = false;
  1618. #endif
  1619. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1620. destination[X_AXIS] = 1.5 * max_length(X_AXIS) * x_axis_home_dir;destination[Y_AXIS] = 1.5 * max_length(Y_AXIS) * home_dir(Y_AXIS);
  1621. feedrate = homing_feedrate[X_AXIS];
  1622. if(homing_feedrate[Y_AXIS]<feedrate)
  1623. feedrate = homing_feedrate[Y_AXIS];
  1624. if (max_length(X_AXIS) > max_length(Y_AXIS)) {
  1625. feedrate *= sqrt(pow(max_length(Y_AXIS) / max_length(X_AXIS), 2) + 1);
  1626. } else {
  1627. feedrate *= sqrt(pow(max_length(X_AXIS) / max_length(Y_AXIS), 2) + 1);
  1628. }
  1629. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1630. st_synchronize();
  1631. axis_is_at_home(X_AXIS);
  1632. axis_is_at_home(Y_AXIS);
  1633. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1634. destination[X_AXIS] = current_position[X_AXIS];
  1635. destination[Y_AXIS] = current_position[Y_AXIS];
  1636. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1637. feedrate = 0.0;
  1638. st_synchronize();
  1639. endstops_hit_on_purpose();
  1640. current_position[X_AXIS] = destination[X_AXIS];
  1641. current_position[Y_AXIS] = destination[Y_AXIS];
  1642. current_position[Z_AXIS] = destination[Z_AXIS];
  1643. }
  1644. #endif /* QUICK_HOME */
  1645. if (home_all_axis)
  1646. {
  1647. _doMeshL = true;
  1648. }
  1649. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  1650. {
  1651. #ifdef DUAL_X_CARRIAGE
  1652. int tmp_extruder = active_extruder;
  1653. extruder_duplication_enabled = false;
  1654. active_extruder = !active_extruder;
  1655. homeaxis(X_AXIS);
  1656. inactive_extruder_x_pos = current_position[X_AXIS];
  1657. active_extruder = tmp_extruder;
  1658. homeaxis(X_AXIS);
  1659. // reset state used by the different modes
  1660. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  1661. delayed_move_time = 0;
  1662. active_extruder_parked = true;
  1663. #else
  1664. homeaxis(X_AXIS);
  1665. #endif
  1666. }
  1667. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  1668. homeaxis(Y_AXIS);
  1669. }
  1670. if(code_seen(axis_codes[X_AXIS]))
  1671. {
  1672. if(code_value_long() != 0) {
  1673. current_position[X_AXIS]=code_value()+add_homing[X_AXIS];
  1674. }
  1675. }
  1676. if(code_seen(axis_codes[Y_AXIS])) {
  1677. if(code_value_long() != 0) {
  1678. current_position[Y_AXIS]=code_value()+add_homing[Y_AXIS];
  1679. }
  1680. }
  1681. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  1682. #ifndef Z_SAFE_HOMING
  1683. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1684. #if defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0)
  1685. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  1686. feedrate = max_feedrate[Z_AXIS];
  1687. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  1688. st_synchronize();
  1689. #endif // defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0)
  1690. #ifdef MESH_BED_LEVELING // If Mesh bed leveling, moxve X&Y to safe position for home
  1691. if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] ))
  1692. {
  1693. homeaxis(X_AXIS);
  1694. homeaxis(Y_AXIS);
  1695. }
  1696. // 1st mesh bed leveling measurement point, corrected.
  1697. world2machine_initialize();
  1698. world2machine(pgm_read_float(bed_ref_points), pgm_read_float(bed_ref_points+1), destination[X_AXIS], destination[Y_AXIS]);
  1699. world2machine_reset();
  1700. destination[Z_AXIS] = MESH_HOME_Z_SEARCH; // Set destination away from bed
  1701. feedrate = homing_feedrate[Z_AXIS]/10;
  1702. current_position[Z_AXIS] = 0;
  1703. enable_endstops(false);
  1704. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1705. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  1706. st_synchronize();
  1707. current_position[X_AXIS] = destination[X_AXIS];
  1708. current_position[Y_AXIS] = destination[Y_AXIS];
  1709. enable_endstops(true);
  1710. endstops_hit_on_purpose();
  1711. homeaxis(Z_AXIS);
  1712. _doMeshL = true;
  1713. #else // MESH_BED_LEVELING
  1714. homeaxis(Z_AXIS);
  1715. #endif // MESH_BED_LEVELING
  1716. }
  1717. #else // defined(Z_SAFE_HOMING): Z Safe mode activated.
  1718. if(home_all_axis) {
  1719. destination[X_AXIS] = round(Z_SAFE_HOMING_X_POINT - X_PROBE_OFFSET_FROM_EXTRUDER);
  1720. destination[Y_AXIS] = round(Z_SAFE_HOMING_Y_POINT - Y_PROBE_OFFSET_FROM_EXTRUDER);
  1721. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  1722. feedrate = XY_TRAVEL_SPEED/60;
  1723. current_position[Z_AXIS] = 0;
  1724. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1725. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  1726. st_synchronize();
  1727. current_position[X_AXIS] = destination[X_AXIS];
  1728. current_position[Y_AXIS] = destination[Y_AXIS];
  1729. homeaxis(Z_AXIS);
  1730. }
  1731. // Let's see if X and Y are homed and probe is inside bed area.
  1732. if(code_seen(axis_codes[Z_AXIS])) {
  1733. if ( (axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]) \
  1734. && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER >= X_MIN_POS) \
  1735. && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER <= X_MAX_POS) \
  1736. && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER >= Y_MIN_POS) \
  1737. && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER <= Y_MAX_POS)) {
  1738. current_position[Z_AXIS] = 0;
  1739. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1740. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  1741. feedrate = max_feedrate[Z_AXIS];
  1742. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  1743. st_synchronize();
  1744. homeaxis(Z_AXIS);
  1745. } else if (!((axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]))) {
  1746. LCD_MESSAGERPGM(MSG_POSITION_UNKNOWN);
  1747. SERIAL_ECHO_START;
  1748. SERIAL_ECHOLNRPGM(MSG_POSITION_UNKNOWN);
  1749. } else {
  1750. LCD_MESSAGERPGM(MSG_ZPROBE_OUT);
  1751. SERIAL_ECHO_START;
  1752. SERIAL_ECHOLNRPGM(MSG_ZPROBE_OUT);
  1753. }
  1754. }
  1755. #endif // Z_SAFE_HOMING
  1756. #endif // Z_HOME_DIR < 0
  1757. if(code_seen(axis_codes[Z_AXIS])) {
  1758. if(code_value_long() != 0) {
  1759. current_position[Z_AXIS]=code_value()+add_homing[Z_AXIS];
  1760. }
  1761. }
  1762. #ifdef ENABLE_AUTO_BED_LEVELING
  1763. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1764. current_position[Z_AXIS] += zprobe_zoffset; //Add Z_Probe offset (the distance is negative)
  1765. }
  1766. #endif
  1767. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1768. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  1769. enable_endstops(false);
  1770. #endif
  1771. feedrate = saved_feedrate;
  1772. feedmultiply = saved_feedmultiply;
  1773. previous_millis_cmd = millis();
  1774. endstops_hit_on_purpose();
  1775. #ifndef MESH_BED_LEVELING
  1776. if(card.sdprinting) {
  1777. EEPROM_read_B(EEPROM_BABYSTEP_Z,&babystepLoad[2]);
  1778. if(babystepLoad[2] != 0){
  1779. lcd_adjust_z();
  1780. }
  1781. }
  1782. #endif
  1783. // Load the machine correction matrix
  1784. world2machine_initialize();
  1785. // and correct the current_position to match the transformed coordinate system.
  1786. world2machine_update_current();
  1787. #ifdef MESH_BED_LEVELING
  1788. if (code_seen('W'))
  1789. {
  1790. _doMeshL = false;
  1791. SERIAL_ECHOLN("G80 disabled");
  1792. }
  1793. if ( _doMeshL)
  1794. {
  1795. st_synchronize();
  1796. // Push the commands to the front of the message queue in the reverse order!
  1797. // There shall be always enough space reserved for these commands.
  1798. enquecommand_front_P((PSTR("G80")));
  1799. }
  1800. #endif
  1801. break;
  1802. #ifdef ENABLE_AUTO_BED_LEVELING
  1803. case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
  1804. {
  1805. #if Z_MIN_PIN == -1
  1806. #error "You must have a Z_MIN endstop in order to enable Auto Bed Leveling feature! Z_MIN_PIN must point to a valid hardware pin."
  1807. #endif
  1808. // Prevent user from running a G29 without first homing in X and Y
  1809. if (! (axis_known_position[X_AXIS] && axis_known_position[Y_AXIS]) )
  1810. {
  1811. LCD_MESSAGERPGM(MSG_POSITION_UNKNOWN);
  1812. SERIAL_ECHO_START;
  1813. SERIAL_ECHOLNRPGM(MSG_POSITION_UNKNOWN);
  1814. break; // abort G29, since we don't know where we are
  1815. }
  1816. st_synchronize();
  1817. // make sure the bed_level_rotation_matrix is identity or the planner will get it incorectly
  1818. //vector_3 corrected_position = plan_get_position_mm();
  1819. //corrected_position.debug("position before G29");
  1820. plan_bed_level_matrix.set_to_identity();
  1821. vector_3 uncorrected_position = plan_get_position();
  1822. //uncorrected_position.debug("position durring G29");
  1823. current_position[X_AXIS] = uncorrected_position.x;
  1824. current_position[Y_AXIS] = uncorrected_position.y;
  1825. current_position[Z_AXIS] = uncorrected_position.z;
  1826. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1827. setup_for_endstop_move();
  1828. feedrate = homing_feedrate[Z_AXIS];
  1829. #ifdef AUTO_BED_LEVELING_GRID
  1830. // probe at the points of a lattice grid
  1831. int xGridSpacing = (RIGHT_PROBE_BED_POSITION - LEFT_PROBE_BED_POSITION) / (AUTO_BED_LEVELING_GRID_POINTS-1);
  1832. int yGridSpacing = (BACK_PROBE_BED_POSITION - FRONT_PROBE_BED_POSITION) / (AUTO_BED_LEVELING_GRID_POINTS-1);
  1833. // solve the plane equation ax + by + d = z
  1834. // A is the matrix with rows [x y 1] for all the probed points
  1835. // B is the vector of the Z positions
  1836. // the normal vector to the plane is formed by the coefficients of the plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0
  1837. // so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
  1838. // "A" matrix of the linear system of equations
  1839. double eqnAMatrix[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS*3];
  1840. // "B" vector of Z points
  1841. double eqnBVector[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS];
  1842. int probePointCounter = 0;
  1843. bool zig = true;
  1844. for (int yProbe=FRONT_PROBE_BED_POSITION; yProbe <= BACK_PROBE_BED_POSITION; yProbe += yGridSpacing)
  1845. {
  1846. int xProbe, xInc;
  1847. if (zig)
  1848. {
  1849. xProbe = LEFT_PROBE_BED_POSITION;
  1850. //xEnd = RIGHT_PROBE_BED_POSITION;
  1851. xInc = xGridSpacing;
  1852. zig = false;
  1853. } else // zag
  1854. {
  1855. xProbe = RIGHT_PROBE_BED_POSITION;
  1856. //xEnd = LEFT_PROBE_BED_POSITION;
  1857. xInc = -xGridSpacing;
  1858. zig = true;
  1859. }
  1860. for (int xCount=0; xCount < AUTO_BED_LEVELING_GRID_POINTS; xCount++)
  1861. {
  1862. float z_before;
  1863. if (probePointCounter == 0)
  1864. {
  1865. // raise before probing
  1866. z_before = Z_RAISE_BEFORE_PROBING;
  1867. } else
  1868. {
  1869. // raise extruder
  1870. z_before = current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS;
  1871. }
  1872. float measured_z = probe_pt(xProbe, yProbe, z_before);
  1873. eqnBVector[probePointCounter] = measured_z;
  1874. eqnAMatrix[probePointCounter + 0*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = xProbe;
  1875. eqnAMatrix[probePointCounter + 1*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = yProbe;
  1876. eqnAMatrix[probePointCounter + 2*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = 1;
  1877. probePointCounter++;
  1878. xProbe += xInc;
  1879. }
  1880. }
  1881. clean_up_after_endstop_move();
  1882. // solve lsq problem
  1883. double *plane_equation_coefficients = qr_solve(AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS, 3, eqnAMatrix, eqnBVector);
  1884. SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
  1885. SERIAL_PROTOCOL(plane_equation_coefficients[0]);
  1886. SERIAL_PROTOCOLPGM(" b: ");
  1887. SERIAL_PROTOCOL(plane_equation_coefficients[1]);
  1888. SERIAL_PROTOCOLPGM(" d: ");
  1889. SERIAL_PROTOCOLLN(plane_equation_coefficients[2]);
  1890. set_bed_level_equation_lsq(plane_equation_coefficients);
  1891. free(plane_equation_coefficients);
  1892. #else // AUTO_BED_LEVELING_GRID not defined
  1893. // Probe at 3 arbitrary points
  1894. // probe 1
  1895. float z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING);
  1896. // probe 2
  1897. float z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
  1898. // probe 3
  1899. float z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
  1900. clean_up_after_endstop_move();
  1901. set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
  1902. #endif // AUTO_BED_LEVELING_GRID
  1903. st_synchronize();
  1904. // The following code correct the Z height difference from z-probe position and hotend tip position.
  1905. // The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend.
  1906. // When the bed is uneven, this height must be corrected.
  1907. real_z = float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]; //get the real Z (since the auto bed leveling is already correcting the plane)
  1908. x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER;
  1909. y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER;
  1910. z_tmp = current_position[Z_AXIS];
  1911. apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); //Apply the correction sending the probe offset
  1912. current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS]; //The difference is added to current position and sent to planner.
  1913. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1914. }
  1915. break;
  1916. #ifndef Z_PROBE_SLED
  1917. case 30: // G30 Single Z Probe
  1918. {
  1919. st_synchronize();
  1920. // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly
  1921. setup_for_endstop_move();
  1922. feedrate = homing_feedrate[Z_AXIS];
  1923. run_z_probe();
  1924. SERIAL_PROTOCOLPGM(MSG_BED);
  1925. SERIAL_PROTOCOLPGM(" X: ");
  1926. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1927. SERIAL_PROTOCOLPGM(" Y: ");
  1928. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1929. SERIAL_PROTOCOLPGM(" Z: ");
  1930. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1931. SERIAL_PROTOCOLPGM("\n");
  1932. clean_up_after_endstop_move();
  1933. }
  1934. break;
  1935. #else
  1936. case 31: // dock the sled
  1937. dock_sled(true);
  1938. break;
  1939. case 32: // undock the sled
  1940. dock_sled(false);
  1941. break;
  1942. #endif // Z_PROBE_SLED
  1943. #endif // ENABLE_AUTO_BED_LEVELING
  1944. #ifdef MESH_BED_LEVELING
  1945. /**
  1946. * G80: Mesh-based Z probe, probes a grid and produces a
  1947. * mesh to compensate for variable bed height
  1948. *
  1949. * The S0 report the points as below
  1950. *
  1951. * +----> X-axis
  1952. * |
  1953. * |
  1954. * v Y-axis
  1955. *
  1956. */
  1957. case 80:
  1958. {
  1959. if (!IS_SD_PRINTING)
  1960. {
  1961. custom_message = true;
  1962. custom_message_type = 1;
  1963. custom_message_state = (MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS) + 10;
  1964. }
  1965. // Firstly check if we know where we are
  1966. if ( !( axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS] ) ){
  1967. // We don't know where we are! HOME!
  1968. // Push the commands to the front of the message queue in the reverse order!
  1969. // There shall be always enough space reserved for these commands.
  1970. repeatcommand_front(); // repeat G80 with all its parameters
  1971. enquecommand_front_P((PSTR("G28 W0")));
  1972. break;
  1973. }
  1974. mbl.reset();
  1975. // Cycle through all points and probe them
  1976. // First move up.
  1977. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  1978. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder);
  1979. // The move to the first calibration point.
  1980. current_position[X_AXIS] = pgm_read_float(bed_ref_points);
  1981. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+1);
  1982. // mbl.get_meas_xy(0, 0, current_position[X_AXIS], current_position[Y_AXIS], false);
  1983. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[X_AXIS]/30, active_extruder);
  1984. // Wait until the move is finished.
  1985. st_synchronize();
  1986. int mesh_point = 0;
  1987. int ix = 0;
  1988. int iy = 0;
  1989. int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS]/20;
  1990. int Z_PROBE_FEEDRATE = homing_feedrate[Z_AXIS]/60;
  1991. int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS]/40;
  1992. bool has_z = is_bed_z_jitter_data_valid();
  1993. setup_for_endstop_move();
  1994. const char *kill_message = NULL;
  1995. while (mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS) {
  1996. // Get coords of a measuring point.
  1997. ix = mesh_point % MESH_MEAS_NUM_X_POINTS;
  1998. iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  1999. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; // Zig zag
  2000. float z0 = 0.f;
  2001. if (has_z && mesh_point > 0) {
  2002. uint16_t z_offset_u = eeprom_read_word((uint16_t*)(EEPROM_BED_CALIBRATION_Z_JITTER + 2 * (ix + iy * 3 - 1)));
  2003. z0 = mbl.z_values[0][0] + *reinterpret_cast<int16_t*>(&z_offset_u) * 0.01;
  2004. #if 0
  2005. SERIAL_ECHOPGM("Bed leveling, point: ");
  2006. MYSERIAL.print(mesh_point);
  2007. SERIAL_ECHOPGM(", calibration z: ");
  2008. MYSERIAL.print(z0, 5);
  2009. SERIAL_ECHOLNPGM("");
  2010. #endif
  2011. }
  2012. // Move Z to proper distance
  2013. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2014. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], Z_LIFT_FEEDRATE, active_extruder);
  2015. st_synchronize();
  2016. current_position[X_AXIS] = pgm_read_float(bed_ref_points+2*mesh_point);
  2017. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+2*mesh_point+1);
  2018. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2019. // mbl.get_meas_xy(ix, iy, current_position[X_AXIS], current_position[Y_AXIS], false);
  2020. enable_endstops(false);
  2021. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], XY_AXIS_FEEDRATE, active_extruder);
  2022. st_synchronize();
  2023. // Go down until endstop is hit
  2024. const float Z_CALIBRATION_THRESHOLD = 0.5f;
  2025. if (! find_bed_induction_sensor_point_z((has_z && mesh_point > 0) ? z0 - Z_CALIBRATION_THRESHOLD : -10.f)) {
  2026. kill_message = MSG_BED_LEVELING_FAILED_POINT_LOW;
  2027. break;
  2028. }
  2029. if (has_z && fabs(z0 - current_position[Z_AXIS]) > Z_CALIBRATION_THRESHOLD) {
  2030. kill_message = MSG_BED_LEVELING_FAILED_POINT_HIGH;
  2031. break;
  2032. }
  2033. mbl.set_z(ix, iy, current_position[Z_AXIS]);
  2034. if (!IS_SD_PRINTING)
  2035. {
  2036. custom_message_state--;
  2037. }
  2038. mesh_point++;
  2039. }
  2040. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2041. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], Z_LIFT_FEEDRATE, active_extruder);
  2042. if (mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS) {
  2043. st_synchronize();
  2044. kill(kill_message);
  2045. }
  2046. clean_up_after_endstop_move();
  2047. mbl.upsample_3x3();
  2048. mbl.active = 1;
  2049. current_position[X_AXIS] = X_MIN_POS+0.2;
  2050. current_position[Y_AXIS] = Y_MIN_POS+0.2;
  2051. current_position[Z_AXIS] = Z_MIN_POS;
  2052. plan_buffer_line(current_position[X_AXIS], current_position[X_AXIS], current_position[Z_AXIS], current_position[E_AXIS], XY_AXIS_FEEDRATE, active_extruder);
  2053. st_synchronize();
  2054. if(card.sdprinting || is_usb_printing )
  2055. {
  2056. if(eeprom_read_byte((unsigned char*)EEPROM_BABYSTEP_Z_SET) == 0x01)
  2057. {
  2058. EEPROM_read_B(EEPROM_BABYSTEP_Z,&babystepLoad[2]);
  2059. babystepsTodo[Z_AXIS] = babystepLoad[2];
  2060. }
  2061. }
  2062. }
  2063. break;
  2064. /**
  2065. * G81: Print mesh bed leveling status and bed profile if activated
  2066. */
  2067. case 81:
  2068. if (mbl.active) {
  2069. SERIAL_PROTOCOLPGM("Num X,Y: ");
  2070. SERIAL_PROTOCOL(MESH_NUM_X_POINTS);
  2071. SERIAL_PROTOCOLPGM(",");
  2072. SERIAL_PROTOCOL(MESH_NUM_Y_POINTS);
  2073. SERIAL_PROTOCOLPGM("\nZ search height: ");
  2074. SERIAL_PROTOCOL(MESH_HOME_Z_SEARCH);
  2075. SERIAL_PROTOCOLLNPGM("\nMeasured points:");
  2076. for (int y = MESH_NUM_Y_POINTS-1; y >= 0; y--) {
  2077. for (int x = 0; x < MESH_NUM_X_POINTS; x++) {
  2078. SERIAL_PROTOCOLPGM(" ");
  2079. SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5);
  2080. }
  2081. SERIAL_PROTOCOLPGM("\n");
  2082. }
  2083. }
  2084. else
  2085. SERIAL_PROTOCOLLNPGM("Mesh bed leveling not active.");
  2086. break;
  2087. /**
  2088. * G82: Single Z probe at current location
  2089. *
  2090. * WARNING! USE WITH CAUTION! If you'll try to probe where is no leveling pad, nasty things can happen!
  2091. *
  2092. */
  2093. case 82:
  2094. SERIAL_PROTOCOLLNPGM("Finding bed ");
  2095. setup_for_endstop_move();
  2096. find_bed_induction_sensor_point_z();
  2097. clean_up_after_endstop_move();
  2098. SERIAL_PROTOCOLPGM("Bed found at: ");
  2099. SERIAL_PROTOCOL_F(current_position[Z_AXIS], 5);
  2100. SERIAL_PROTOCOLPGM("\n");
  2101. break;
  2102. /**
  2103. * G83: Babystep in Z and store to EEPROM
  2104. */
  2105. case 83:
  2106. {
  2107. int babystepz = code_seen('S') ? code_value() : 0;
  2108. int BabyPosition = code_seen('P') ? code_value() : 0;
  2109. if (babystepz != 0) {
  2110. if (BabyPosition > 4) {
  2111. SERIAL_PROTOCOLLNPGM("Index out of bounds");
  2112. }else{
  2113. // Save it to the eeprom
  2114. babystepLoad[2] = babystepz;
  2115. EEPROM_save_B(EEPROM_BABYSTEP_Z0+(BabyPosition*2),&babystepLoad[2]);
  2116. // adjist the Z
  2117. babystepsTodo[Z_AXIS] = babystepLoad[2];
  2118. }
  2119. }
  2120. }
  2121. break;
  2122. /**
  2123. * G84: UNDO Babystep Z (move Z axis back)
  2124. */
  2125. case 84:
  2126. babystepsTodo[Z_AXIS] = -babystepLoad[2];
  2127. break;
  2128. /**
  2129. * G85: Pick best babystep
  2130. */
  2131. case 85:
  2132. lcd_pick_babystep();
  2133. break;
  2134. /**
  2135. * G86: Disable babystep correction after home
  2136. */
  2137. case 86:
  2138. eeprom_write_byte((unsigned char*)EEPROM_BABYSTEP_Z_SET, 0xFF);
  2139. break;
  2140. /**
  2141. * G87: Enable babystep correction after home
  2142. */
  2143. case 87:
  2144. eeprom_write_byte((unsigned char*)EEPROM_BABYSTEP_Z_SET, 0x01);
  2145. break;
  2146. case 88:
  2147. break;
  2148. #endif // ENABLE_MESH_BED_LEVELING
  2149. case 90: // G90
  2150. relative_mode = false;
  2151. break;
  2152. case 91: // G91
  2153. relative_mode = true;
  2154. break;
  2155. case 92: // G92
  2156. if(!code_seen(axis_codes[E_AXIS]))
  2157. st_synchronize();
  2158. for(int8_t i=0; i < NUM_AXIS; i++) {
  2159. if(code_seen(axis_codes[i])) {
  2160. if(i == E_AXIS) {
  2161. current_position[i] = code_value();
  2162. plan_set_e_position(current_position[E_AXIS]);
  2163. }
  2164. else {
  2165. current_position[i] = code_value()+add_homing[i];
  2166. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2167. }
  2168. }
  2169. }
  2170. break;
  2171. }
  2172. } // end if(code_seen('G'))
  2173. else if(code_seen('M'))
  2174. {
  2175. switch( (int)code_value() )
  2176. {
  2177. #ifdef ULTIPANEL
  2178. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  2179. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  2180. {
  2181. char *src = strchr_pointer + 2;
  2182. codenum = 0;
  2183. bool hasP = false, hasS = false;
  2184. if (code_seen('P')) {
  2185. codenum = code_value(); // milliseconds to wait
  2186. hasP = codenum > 0;
  2187. }
  2188. if (code_seen('S')) {
  2189. codenum = code_value() * 1000; // seconds to wait
  2190. hasS = codenum > 0;
  2191. }
  2192. starpos = strchr(src, '*');
  2193. if (starpos != NULL) *(starpos) = '\0';
  2194. while (*src == ' ') ++src;
  2195. if (!hasP && !hasS && *src != '\0') {
  2196. lcd_setstatus(src);
  2197. } else {
  2198. LCD_MESSAGERPGM(MSG_USERWAIT);
  2199. }
  2200. lcd_ignore_click();
  2201. st_synchronize();
  2202. previous_millis_cmd = millis();
  2203. if (codenum > 0){
  2204. codenum += millis(); // keep track of when we started waiting
  2205. while(millis() < codenum && !lcd_clicked()){
  2206. manage_heater();
  2207. manage_inactivity();
  2208. lcd_update();
  2209. }
  2210. lcd_ignore_click(false);
  2211. }else{
  2212. if (!lcd_detected())
  2213. break;
  2214. while(!lcd_clicked()){
  2215. manage_heater();
  2216. manage_inactivity();
  2217. lcd_update();
  2218. }
  2219. }
  2220. if (IS_SD_PRINTING)
  2221. LCD_MESSAGERPGM(MSG_RESUMING);
  2222. else
  2223. LCD_MESSAGERPGM(WELCOME_MSG);
  2224. }
  2225. break;
  2226. #endif
  2227. case 17:
  2228. LCD_MESSAGERPGM(MSG_NO_MOVE);
  2229. enable_x();
  2230. enable_y();
  2231. enable_z();
  2232. enable_e0();
  2233. enable_e1();
  2234. enable_e2();
  2235. break;
  2236. #ifdef SDSUPPORT
  2237. case 20: // M20 - list SD card
  2238. SERIAL_PROTOCOLLNRPGM(MSG_BEGIN_FILE_LIST);
  2239. card.ls();
  2240. SERIAL_PROTOCOLLNRPGM(MSG_END_FILE_LIST);
  2241. break;
  2242. case 21: // M21 - init SD card
  2243. card.initsd();
  2244. break;
  2245. case 22: //M22 - release SD card
  2246. card.release();
  2247. break;
  2248. case 23: //M23 - Select file
  2249. starpos = (strchr(strchr_pointer + 4,'*'));
  2250. if(starpos!=NULL)
  2251. *(starpos)='\0';
  2252. card.openFile(strchr_pointer + 4,true);
  2253. break;
  2254. case 24: //M24 - Start SD print
  2255. card.startFileprint();
  2256. starttime=millis();
  2257. break;
  2258. case 25: //M25 - Pause SD print
  2259. card.pauseSDPrint();
  2260. break;
  2261. case 26: //M26 - Set SD index
  2262. if(card.cardOK && code_seen('S')) {
  2263. card.setIndex(code_value_long());
  2264. }
  2265. break;
  2266. case 27: //M27 - Get SD status
  2267. card.getStatus();
  2268. break;
  2269. case 28: //M28 - Start SD write
  2270. starpos = (strchr(strchr_pointer + 4,'*'));
  2271. if(starpos != NULL){
  2272. char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
  2273. strchr_pointer = strchr(npos,' ') + 1;
  2274. *(starpos) = '\0';
  2275. }
  2276. card.openFile(strchr_pointer+4,false);
  2277. break;
  2278. case 29: //M29 - Stop SD write
  2279. //processed in write to file routine above
  2280. //card,saving = false;
  2281. break;
  2282. case 30: //M30 <filename> Delete File
  2283. if (card.cardOK){
  2284. card.closefile();
  2285. starpos = (strchr(strchr_pointer + 4,'*'));
  2286. if(starpos != NULL){
  2287. char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
  2288. strchr_pointer = strchr(npos,' ') + 1;
  2289. *(starpos) = '\0';
  2290. }
  2291. card.removeFile(strchr_pointer + 4);
  2292. }
  2293. break;
  2294. case 32: //M32 - Select file and start SD print
  2295. {
  2296. if(card.sdprinting) {
  2297. st_synchronize();
  2298. }
  2299. starpos = (strchr(strchr_pointer + 4,'*'));
  2300. char* namestartpos = (strchr(strchr_pointer + 4,'!')); //find ! to indicate filename string start.
  2301. if(namestartpos==NULL)
  2302. {
  2303. namestartpos=strchr_pointer + 4; //default name position, 4 letters after the M
  2304. }
  2305. else
  2306. namestartpos++; //to skip the '!'
  2307. if(starpos!=NULL)
  2308. *(starpos)='\0';
  2309. bool call_procedure=(code_seen('P'));
  2310. if(strchr_pointer>namestartpos)
  2311. call_procedure=false; //false alert, 'P' found within filename
  2312. if( card.cardOK )
  2313. {
  2314. card.openFile(namestartpos,true,!call_procedure);
  2315. if(code_seen('S'))
  2316. if(strchr_pointer<namestartpos) //only if "S" is occuring _before_ the filename
  2317. card.setIndex(code_value_long());
  2318. card.startFileprint();
  2319. if(!call_procedure)
  2320. starttime=millis(); //procedure calls count as normal print time.
  2321. }
  2322. } break;
  2323. case 928: //M928 - Start SD write
  2324. starpos = (strchr(strchr_pointer + 5,'*'));
  2325. if(starpos != NULL){
  2326. char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
  2327. strchr_pointer = strchr(npos,' ') + 1;
  2328. *(starpos) = '\0';
  2329. }
  2330. card.openLogFile(strchr_pointer+5);
  2331. break;
  2332. #endif //SDSUPPORT
  2333. case 31: //M31 take time since the start of the SD print or an M109 command
  2334. {
  2335. stoptime=millis();
  2336. char time[30];
  2337. unsigned long t=(stoptime-starttime)/1000;
  2338. int sec,min;
  2339. min=t/60;
  2340. sec=t%60;
  2341. sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
  2342. SERIAL_ECHO_START;
  2343. SERIAL_ECHOLN(time);
  2344. lcd_setstatus(time);
  2345. autotempShutdown();
  2346. }
  2347. break;
  2348. case 42: //M42 -Change pin status via gcode
  2349. if (code_seen('S'))
  2350. {
  2351. int pin_status = code_value();
  2352. int pin_number = LED_PIN;
  2353. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  2354. pin_number = code_value();
  2355. for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++)
  2356. {
  2357. if (sensitive_pins[i] == pin_number)
  2358. {
  2359. pin_number = -1;
  2360. break;
  2361. }
  2362. }
  2363. #if defined(FAN_PIN) && FAN_PIN > -1
  2364. if (pin_number == FAN_PIN)
  2365. fanSpeed = pin_status;
  2366. #endif
  2367. if (pin_number > -1)
  2368. {
  2369. pinMode(pin_number, OUTPUT);
  2370. digitalWrite(pin_number, pin_status);
  2371. analogWrite(pin_number, pin_status);
  2372. }
  2373. }
  2374. break;
  2375. case 44: // M45: Reset the bed skew and offset calibration.
  2376. // Reset the skew and offset in both RAM and EEPROM.
  2377. reset_bed_offset_and_skew();
  2378. world2machine_reset();
  2379. // Wait for the motors to stop and update the current position with the absolute values.
  2380. st_synchronize();
  2381. current_position[X_AXIS] = st_get_position_mm(X_AXIS);
  2382. current_position[Y_AXIS] = st_get_position_mm(Y_AXIS);
  2383. break;
  2384. case 45: // M46: bed skew and offset with manual Z up
  2385. {
  2386. // Disable the default update procedure of the display. We will do a modal dialog.
  2387. lcd_update_enable(false);
  2388. // Let the planner use the uncorrected coordinates.
  2389. mbl.reset();
  2390. world2machine_reset();
  2391. // Let the user move the Z axes up to the end stoppers.
  2392. if (lcd_calibrate_z_end_stop_manual()) {
  2393. refresh_cmd_timeout();
  2394. // Move the print head close to the bed.
  2395. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2396. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  2397. st_synchronize();
  2398. // Home in the XY plane.
  2399. set_destination_to_current();
  2400. setup_for_endstop_move();
  2401. home_xy();
  2402. int8_t verbosity_level = 0;
  2403. if (code_seen('V')) {
  2404. // Just 'V' without a number counts as V1.
  2405. char c = strchr_pointer[1];
  2406. verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
  2407. }
  2408. BedSkewOffsetDetectionResultType result = find_bed_offset_and_skew(verbosity_level);
  2409. uint8_t point_too_far_mask = 0;
  2410. clean_up_after_endstop_move();
  2411. // Print head up.
  2412. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2413. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  2414. st_synchronize();
  2415. if (result >= 0) {
  2416. // Second half: The fine adjustment.
  2417. // Let the planner use the uncorrected coordinates.
  2418. mbl.reset();
  2419. world2machine_reset();
  2420. // Home in the XY plane.
  2421. setup_for_endstop_move();
  2422. home_xy();
  2423. result = improve_bed_offset_and_skew(1, verbosity_level, point_too_far_mask);
  2424. clean_up_after_endstop_move();
  2425. // Print head up.
  2426. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2427. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  2428. st_synchronize();
  2429. }
  2430. lcd_bed_calibration_show_result(result, point_too_far_mask);
  2431. lcd_update_enable(true);
  2432. lcd_implementation_clear();
  2433. // lcd_return_to_status();
  2434. lcd_update();
  2435. }
  2436. break;
  2437. }
  2438. #if 1
  2439. case 48: // M48: scan the bed induction sensor points, print the sensor trigger coordinates to the serial line for visualization on the PC.
  2440. {
  2441. // Disable the default update procedure of the display. We will do a modal dialog.
  2442. lcd_update_enable(false);
  2443. // Let the planner use the uncorrected coordinates.
  2444. mbl.reset();
  2445. world2machine_reset();
  2446. // Move the print head close to the bed.
  2447. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2448. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  2449. st_synchronize();
  2450. // Home in the XY plane.
  2451. set_destination_to_current();
  2452. setup_for_endstop_move();
  2453. home_xy();
  2454. int8_t verbosity_level = 0;
  2455. if (code_seen('V')) {
  2456. // Just 'V' without a number counts as V1.
  2457. char c = strchr_pointer[1];
  2458. verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
  2459. }
  2460. bool success = scan_bed_induction_points(verbosity_level);
  2461. clean_up_after_endstop_move();
  2462. // Print head up.
  2463. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2464. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  2465. st_synchronize();
  2466. lcd_update_enable(true);
  2467. lcd_implementation_clear();
  2468. // lcd_return_to_status();
  2469. lcd_update();
  2470. break;
  2471. }
  2472. #endif
  2473. case 47:
  2474. lcd_diag_show_end_stops();
  2475. break;
  2476. // M48 Z-Probe repeatability measurement function.
  2477. //
  2478. // Usage: M48 <n #_samples> <X X_position_for_samples> <Y Y_position_for_samples> <V Verbose_Level> <L legs_of_movement_prior_to_doing_probe>
  2479. //
  2480. // This function assumes the bed has been homed. Specificaly, that a G28 command
  2481. // as been issued prior to invoking the M48 Z-Probe repeatability measurement function.
  2482. // Any information generated by a prior G29 Bed leveling command will be lost and need to be
  2483. // regenerated.
  2484. //
  2485. // The number of samples will default to 10 if not specified. You can use upper or lower case
  2486. // letters for any of the options EXCEPT n. n must be in lower case because Marlin uses a capital
  2487. // N for its communication protocol and will get horribly confused if you send it a capital N.
  2488. //
  2489. #ifdef ENABLE_AUTO_BED_LEVELING
  2490. #ifdef Z_PROBE_REPEATABILITY_TEST
  2491. case 48: // M48 Z-Probe repeatability
  2492. {
  2493. #if Z_MIN_PIN == -1
  2494. #error "You must have a Z_MIN endstop in order to enable calculation of Z-Probe repeatability."
  2495. #endif
  2496. double sum=0.0;
  2497. double mean=0.0;
  2498. double sigma=0.0;
  2499. double sample_set[50];
  2500. int verbose_level=1, n=0, j, n_samples = 10, n_legs=0;
  2501. double X_current, Y_current, Z_current;
  2502. double X_probe_location, Y_probe_location, Z_start_location, ext_position;
  2503. if (code_seen('V') || code_seen('v')) {
  2504. verbose_level = code_value();
  2505. if (verbose_level<0 || verbose_level>4 ) {
  2506. SERIAL_PROTOCOLPGM("?Verbose Level not plausable.\n");
  2507. goto Sigma_Exit;
  2508. }
  2509. }
  2510. if (verbose_level > 0) {
  2511. SERIAL_PROTOCOLPGM("M48 Z-Probe Repeatability test. Version 2.00\n");
  2512. SERIAL_PROTOCOLPGM("Full support at: http://3dprintboard.com/forum.php\n");
  2513. }
  2514. if (code_seen('n')) {
  2515. n_samples = code_value();
  2516. if (n_samples<4 || n_samples>50 ) {
  2517. SERIAL_PROTOCOLPGM("?Specified sample size not plausable.\n");
  2518. goto Sigma_Exit;
  2519. }
  2520. }
  2521. X_current = X_probe_location = st_get_position_mm(X_AXIS);
  2522. Y_current = Y_probe_location = st_get_position_mm(Y_AXIS);
  2523. Z_current = st_get_position_mm(Z_AXIS);
  2524. Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
  2525. ext_position = st_get_position_mm(E_AXIS);
  2526. if (code_seen('X') || code_seen('x') ) {
  2527. X_probe_location = code_value() - X_PROBE_OFFSET_FROM_EXTRUDER;
  2528. if (X_probe_location<X_MIN_POS || X_probe_location>X_MAX_POS ) {
  2529. SERIAL_PROTOCOLPGM("?Specified X position out of range.\n");
  2530. goto Sigma_Exit;
  2531. }
  2532. }
  2533. if (code_seen('Y') || code_seen('y') ) {
  2534. Y_probe_location = code_value() - Y_PROBE_OFFSET_FROM_EXTRUDER;
  2535. if (Y_probe_location<Y_MIN_POS || Y_probe_location>Y_MAX_POS ) {
  2536. SERIAL_PROTOCOLPGM("?Specified Y position out of range.\n");
  2537. goto Sigma_Exit;
  2538. }
  2539. }
  2540. if (code_seen('L') || code_seen('l') ) {
  2541. n_legs = code_value();
  2542. if ( n_legs==1 )
  2543. n_legs = 2;
  2544. if ( n_legs<0 || n_legs>15 ) {
  2545. SERIAL_PROTOCOLPGM("?Specified number of legs in movement not plausable.\n");
  2546. goto Sigma_Exit;
  2547. }
  2548. }
  2549. //
  2550. // Do all the preliminary setup work. First raise the probe.
  2551. //
  2552. st_synchronize();
  2553. plan_bed_level_matrix.set_to_identity();
  2554. plan_buffer_line( X_current, Y_current, Z_start_location,
  2555. ext_position,
  2556. homing_feedrate[Z_AXIS]/60,
  2557. active_extruder);
  2558. st_synchronize();
  2559. //
  2560. // Now get everything to the specified probe point So we can safely do a probe to
  2561. // get us close to the bed. If the Z-Axis is far from the bed, we don't want to
  2562. // use that as a starting point for each probe.
  2563. //
  2564. if (verbose_level > 2)
  2565. SERIAL_PROTOCOL("Positioning probe for the test.\n");
  2566. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  2567. ext_position,
  2568. homing_feedrate[X_AXIS]/60,
  2569. active_extruder);
  2570. st_synchronize();
  2571. current_position[X_AXIS] = X_current = st_get_position_mm(X_AXIS);
  2572. current_position[Y_AXIS] = Y_current = st_get_position_mm(Y_AXIS);
  2573. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  2574. current_position[E_AXIS] = ext_position = st_get_position_mm(E_AXIS);
  2575. //
  2576. // OK, do the inital probe to get us close to the bed.
  2577. // Then retrace the right amount and use that in subsequent probes
  2578. //
  2579. setup_for_endstop_move();
  2580. run_z_probe();
  2581. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  2582. Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
  2583. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  2584. ext_position,
  2585. homing_feedrate[X_AXIS]/60,
  2586. active_extruder);
  2587. st_synchronize();
  2588. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  2589. for( n=0; n<n_samples; n++) {
  2590. do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location); // Make sure we are at the probe location
  2591. if ( n_legs) {
  2592. double radius=0.0, theta=0.0, x_sweep, y_sweep;
  2593. int rotational_direction, l;
  2594. rotational_direction = (unsigned long) millis() & 0x0001; // clockwise or counter clockwise
  2595. radius = (unsigned long) millis() % (long) (X_MAX_LENGTH/4); // limit how far out to go
  2596. theta = (float) ((unsigned long) millis() % (long) 360) / (360./(2*3.1415926)); // turn into radians
  2597. //SERIAL_ECHOPAIR("starting radius: ",radius);
  2598. //SERIAL_ECHOPAIR(" theta: ",theta);
  2599. //SERIAL_ECHOPAIR(" direction: ",rotational_direction);
  2600. //SERIAL_PROTOCOLLNPGM("");
  2601. for( l=0; l<n_legs-1; l++) {
  2602. if (rotational_direction==1)
  2603. theta += (float) ((unsigned long) millis() % (long) 20) / (360.0/(2*3.1415926)); // turn into radians
  2604. else
  2605. theta -= (float) ((unsigned long) millis() % (long) 20) / (360.0/(2*3.1415926)); // turn into radians
  2606. radius += (float) ( ((long) ((unsigned long) millis() % (long) 10)) - 5);
  2607. if ( radius<0.0 )
  2608. radius = -radius;
  2609. X_current = X_probe_location + cos(theta) * radius;
  2610. Y_current = Y_probe_location + sin(theta) * radius;
  2611. if ( X_current<X_MIN_POS) // Make sure our X & Y are sane
  2612. X_current = X_MIN_POS;
  2613. if ( X_current>X_MAX_POS)
  2614. X_current = X_MAX_POS;
  2615. if ( Y_current<Y_MIN_POS) // Make sure our X & Y are sane
  2616. Y_current = Y_MIN_POS;
  2617. if ( Y_current>Y_MAX_POS)
  2618. Y_current = Y_MAX_POS;
  2619. if (verbose_level>3 ) {
  2620. SERIAL_ECHOPAIR("x: ", X_current);
  2621. SERIAL_ECHOPAIR("y: ", Y_current);
  2622. SERIAL_PROTOCOLLNPGM("");
  2623. }
  2624. do_blocking_move_to( X_current, Y_current, Z_current );
  2625. }
  2626. do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location); // Go back to the probe location
  2627. }
  2628. setup_for_endstop_move();
  2629. run_z_probe();
  2630. sample_set[n] = current_position[Z_AXIS];
  2631. //
  2632. // Get the current mean for the data points we have so far
  2633. //
  2634. sum=0.0;
  2635. for( j=0; j<=n; j++) {
  2636. sum = sum + sample_set[j];
  2637. }
  2638. mean = sum / (double (n+1));
  2639. //
  2640. // Now, use that mean to calculate the standard deviation for the
  2641. // data points we have so far
  2642. //
  2643. sum=0.0;
  2644. for( j=0; j<=n; j++) {
  2645. sum = sum + (sample_set[j]-mean) * (sample_set[j]-mean);
  2646. }
  2647. sigma = sqrt( sum / (double (n+1)) );
  2648. if (verbose_level > 1) {
  2649. SERIAL_PROTOCOL(n+1);
  2650. SERIAL_PROTOCOL(" of ");
  2651. SERIAL_PROTOCOL(n_samples);
  2652. SERIAL_PROTOCOLPGM(" z: ");
  2653. SERIAL_PROTOCOL_F(current_position[Z_AXIS], 6);
  2654. }
  2655. if (verbose_level > 2) {
  2656. SERIAL_PROTOCOL(" mean: ");
  2657. SERIAL_PROTOCOL_F(mean,6);
  2658. SERIAL_PROTOCOL(" sigma: ");
  2659. SERIAL_PROTOCOL_F(sigma,6);
  2660. }
  2661. if (verbose_level > 0)
  2662. SERIAL_PROTOCOLPGM("\n");
  2663. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  2664. current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder);
  2665. st_synchronize();
  2666. }
  2667. delay(1000);
  2668. clean_up_after_endstop_move();
  2669. // enable_endstops(true);
  2670. if (verbose_level > 0) {
  2671. SERIAL_PROTOCOLPGM("Mean: ");
  2672. SERIAL_PROTOCOL_F(mean, 6);
  2673. SERIAL_PROTOCOLPGM("\n");
  2674. }
  2675. SERIAL_PROTOCOLPGM("Standard Deviation: ");
  2676. SERIAL_PROTOCOL_F(sigma, 6);
  2677. SERIAL_PROTOCOLPGM("\n\n");
  2678. Sigma_Exit:
  2679. break;
  2680. }
  2681. #endif // Z_PROBE_REPEATABILITY_TEST
  2682. #endif // ENABLE_AUTO_BED_LEVELING
  2683. case 104: // M104
  2684. if(setTargetedHotend(104)){
  2685. break;
  2686. }
  2687. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  2688. #ifdef DUAL_X_CARRIAGE
  2689. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  2690. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  2691. #endif
  2692. setWatch();
  2693. break;
  2694. case 112: // M112 -Emergency Stop
  2695. kill();
  2696. break;
  2697. case 140: // M140 set bed temp
  2698. if (code_seen('S')) setTargetBed(code_value());
  2699. break;
  2700. case 105 : // M105
  2701. if(setTargetedHotend(105)){
  2702. break;
  2703. }
  2704. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  2705. SERIAL_PROTOCOLPGM("ok T:");
  2706. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  2707. SERIAL_PROTOCOLPGM(" /");
  2708. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  2709. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  2710. SERIAL_PROTOCOLPGM(" B:");
  2711. SERIAL_PROTOCOL_F(degBed(),1);
  2712. SERIAL_PROTOCOLPGM(" /");
  2713. SERIAL_PROTOCOL_F(degTargetBed(),1);
  2714. #endif //TEMP_BED_PIN
  2715. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  2716. SERIAL_PROTOCOLPGM(" T");
  2717. SERIAL_PROTOCOL(cur_extruder);
  2718. SERIAL_PROTOCOLPGM(":");
  2719. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  2720. SERIAL_PROTOCOLPGM(" /");
  2721. SERIAL_PROTOCOL_F(degTargetHotend(cur_extruder),1);
  2722. }
  2723. #else
  2724. SERIAL_ERROR_START;
  2725. SERIAL_ERRORLNRPGM(MSG_ERR_NO_THERMISTORS);
  2726. #endif
  2727. SERIAL_PROTOCOLPGM(" @:");
  2728. #ifdef EXTRUDER_WATTS
  2729. SERIAL_PROTOCOL((EXTRUDER_WATTS * getHeaterPower(tmp_extruder))/127);
  2730. SERIAL_PROTOCOLPGM("W");
  2731. #else
  2732. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  2733. #endif
  2734. SERIAL_PROTOCOLPGM(" B@:");
  2735. #ifdef BED_WATTS
  2736. SERIAL_PROTOCOL((BED_WATTS * getHeaterPower(-1))/127);
  2737. SERIAL_PROTOCOLPGM("W");
  2738. #else
  2739. SERIAL_PROTOCOL(getHeaterPower(-1));
  2740. #endif
  2741. #ifdef SHOW_TEMP_ADC_VALUES
  2742. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  2743. SERIAL_PROTOCOLPGM(" ADC B:");
  2744. SERIAL_PROTOCOL_F(degBed(),1);
  2745. SERIAL_PROTOCOLPGM("C->");
  2746. SERIAL_PROTOCOL_F(rawBedTemp()/OVERSAMPLENR,0);
  2747. #endif
  2748. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  2749. SERIAL_PROTOCOLPGM(" T");
  2750. SERIAL_PROTOCOL(cur_extruder);
  2751. SERIAL_PROTOCOLPGM(":");
  2752. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  2753. SERIAL_PROTOCOLPGM("C->");
  2754. SERIAL_PROTOCOL_F(rawHotendTemp(cur_extruder)/OVERSAMPLENR,0);
  2755. }
  2756. #endif
  2757. SERIAL_PROTOCOLLN("");
  2758. return;
  2759. break;
  2760. case 109:
  2761. {// M109 - Wait for extruder heater to reach target.
  2762. if(setTargetedHotend(109)){
  2763. break;
  2764. }
  2765. LCD_MESSAGERPGM(MSG_HEATING);
  2766. heating_status = 1;
  2767. #ifdef AUTOTEMP
  2768. autotemp_enabled=false;
  2769. #endif
  2770. if (code_seen('S')) {
  2771. setTargetHotend(code_value(), tmp_extruder);
  2772. #ifdef DUAL_X_CARRIAGE
  2773. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  2774. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  2775. #endif
  2776. CooldownNoWait = true;
  2777. } else if (code_seen('R')) {
  2778. setTargetHotend(code_value(), tmp_extruder);
  2779. #ifdef DUAL_X_CARRIAGE
  2780. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  2781. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  2782. #endif
  2783. CooldownNoWait = false;
  2784. }
  2785. #ifdef AUTOTEMP
  2786. if (code_seen('S')) autotemp_min=code_value();
  2787. if (code_seen('B')) autotemp_max=code_value();
  2788. if (code_seen('F'))
  2789. {
  2790. autotemp_factor=code_value();
  2791. autotemp_enabled=true;
  2792. }
  2793. #endif
  2794. setWatch();
  2795. codenum = millis();
  2796. /* See if we are heating up or cooling down */
  2797. target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  2798. cancel_heatup = false;
  2799. #ifdef TEMP_RESIDENCY_TIME
  2800. long residencyStart;
  2801. residencyStart = -1;
  2802. /* continue to loop until we have reached the target temp
  2803. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  2804. while((!cancel_heatup)&&((residencyStart == -1) ||
  2805. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL)))) ) {
  2806. #else
  2807. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  2808. #endif //TEMP_RESIDENCY_TIME
  2809. if( (millis() - codenum) > 1000UL )
  2810. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  2811. SERIAL_PROTOCOLPGM("T:");
  2812. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  2813. SERIAL_PROTOCOLPGM(" E:");
  2814. SERIAL_PROTOCOL((int)tmp_extruder);
  2815. #ifdef TEMP_RESIDENCY_TIME
  2816. SERIAL_PROTOCOLPGM(" W:");
  2817. if(residencyStart > -1)
  2818. {
  2819. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  2820. SERIAL_PROTOCOLLN( codenum );
  2821. }
  2822. else
  2823. {
  2824. SERIAL_PROTOCOLLN( "?" );
  2825. }
  2826. #else
  2827. SERIAL_PROTOCOLLN("");
  2828. #endif
  2829. codenum = millis();
  2830. }
  2831. manage_heater();
  2832. manage_inactivity();
  2833. lcd_update();
  2834. #ifdef TEMP_RESIDENCY_TIME
  2835. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  2836. or when current temp falls outside the hysteresis after target temp was reached */
  2837. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  2838. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  2839. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  2840. {
  2841. residencyStart = millis();
  2842. }
  2843. #endif //TEMP_RESIDENCY_TIME
  2844. }
  2845. LCD_MESSAGERPGM(MSG_HEATING_COMPLETE);
  2846. heating_status = 2;
  2847. starttime=millis();
  2848. previous_millis_cmd = millis();
  2849. }
  2850. break;
  2851. case 190: // M190 - Wait for bed heater to reach target.
  2852. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  2853. LCD_MESSAGERPGM(MSG_BED_HEATING);
  2854. heating_status = 3;
  2855. if (code_seen('S'))
  2856. {
  2857. setTargetBed(code_value());
  2858. CooldownNoWait = true;
  2859. }
  2860. else if (code_seen('R'))
  2861. {
  2862. setTargetBed(code_value());
  2863. CooldownNoWait = false;
  2864. }
  2865. codenum = millis();
  2866. cancel_heatup = false;
  2867. target_direction = isHeatingBed(); // true if heating, false if cooling
  2868. while ( (target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) )
  2869. {
  2870. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  2871. {
  2872. float tt=degHotend(active_extruder);
  2873. SERIAL_PROTOCOLPGM("T:");
  2874. SERIAL_PROTOCOL(tt);
  2875. SERIAL_PROTOCOLPGM(" E:");
  2876. SERIAL_PROTOCOL((int)active_extruder);
  2877. SERIAL_PROTOCOLPGM(" B:");
  2878. SERIAL_PROTOCOL_F(degBed(),1);
  2879. SERIAL_PROTOCOLLN("");
  2880. codenum = millis();
  2881. }
  2882. manage_heater();
  2883. manage_inactivity();
  2884. lcd_update();
  2885. }
  2886. LCD_MESSAGERPGM(MSG_BED_DONE);
  2887. heating_status = 4;
  2888. previous_millis_cmd = millis();
  2889. #endif
  2890. break;
  2891. #if defined(FAN_PIN) && FAN_PIN > -1
  2892. case 106: //M106 Fan On
  2893. if (code_seen('S')){
  2894. fanSpeed=constrain(code_value(),0,255);
  2895. }
  2896. else {
  2897. fanSpeed=255;
  2898. }
  2899. break;
  2900. case 107: //M107 Fan Off
  2901. fanSpeed = 0;
  2902. break;
  2903. #endif //FAN_PIN
  2904. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  2905. case 80: // M80 - Turn on Power Supply
  2906. SET_OUTPUT(PS_ON_PIN); //GND
  2907. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  2908. // If you have a switch on suicide pin, this is useful
  2909. // if you want to start another print with suicide feature after
  2910. // a print without suicide...
  2911. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  2912. SET_OUTPUT(SUICIDE_PIN);
  2913. WRITE(SUICIDE_PIN, HIGH);
  2914. #endif
  2915. #ifdef ULTIPANEL
  2916. powersupply = true;
  2917. LCD_MESSAGERPGM(WELCOME_MSG);
  2918. lcd_update();
  2919. #endif
  2920. break;
  2921. #endif
  2922. case 81: // M81 - Turn off Power Supply
  2923. disable_heater();
  2924. st_synchronize();
  2925. disable_e0();
  2926. disable_e1();
  2927. disable_e2();
  2928. finishAndDisableSteppers();
  2929. fanSpeed = 0;
  2930. delay(1000); // Wait a little before to switch off
  2931. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  2932. st_synchronize();
  2933. suicide();
  2934. #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
  2935. SET_OUTPUT(PS_ON_PIN);
  2936. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  2937. #endif
  2938. #ifdef ULTIPANEL
  2939. powersupply = false;
  2940. LCD_MESSAGERPGM(CAT4(CUSTOM_MENDEL_NAME,PSTR(" "),MSG_OFF,PSTR("."))); //!!
  2941. /*
  2942. MACHNAME = "Prusa i3"
  2943. MSGOFF = "Vypnuto"
  2944. "Prusai3"" ""vypnuto""."
  2945. "Prusa i3"" "MSG_ALL[lang_selected][50]"."
  2946. */
  2947. lcd_update();
  2948. #endif
  2949. break;
  2950. case 82:
  2951. axis_relative_modes[3] = false;
  2952. break;
  2953. case 83:
  2954. axis_relative_modes[3] = true;
  2955. break;
  2956. case 18: //compatibility
  2957. case 84: // M84
  2958. if(code_seen('S')){
  2959. stepper_inactive_time = code_value() * 1000;
  2960. }
  2961. else
  2962. {
  2963. bool all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS]))|| (code_seen(axis_codes[E_AXIS])));
  2964. if(all_axis)
  2965. {
  2966. st_synchronize();
  2967. disable_e0();
  2968. disable_e1();
  2969. disable_e2();
  2970. finishAndDisableSteppers();
  2971. }
  2972. else
  2973. {
  2974. st_synchronize();
  2975. if(code_seen('X')) disable_x();
  2976. if(code_seen('Y')) disable_y();
  2977. if(code_seen('Z')) disable_z();
  2978. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  2979. if(code_seen('E')) {
  2980. disable_e0();
  2981. disable_e1();
  2982. disable_e2();
  2983. }
  2984. #endif
  2985. }
  2986. }
  2987. break;
  2988. case 85: // M85
  2989. if(code_seen('S')) {
  2990. max_inactive_time = code_value() * 1000;
  2991. }
  2992. break;
  2993. case 92: // M92
  2994. for(int8_t i=0; i < NUM_AXIS; i++)
  2995. {
  2996. if(code_seen(axis_codes[i]))
  2997. {
  2998. if(i == 3) { // E
  2999. float value = code_value();
  3000. if(value < 20.0) {
  3001. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  3002. max_e_jerk *= factor;
  3003. max_feedrate[i] *= factor;
  3004. axis_steps_per_sqr_second[i] *= factor;
  3005. }
  3006. axis_steps_per_unit[i] = value;
  3007. }
  3008. else {
  3009. axis_steps_per_unit[i] = code_value();
  3010. }
  3011. }
  3012. }
  3013. break;
  3014. case 115: // M115
  3015. SERIAL_PROTOCOLRPGM(MSG_M115_REPORT);
  3016. break;
  3017. case 117: // M117 display message
  3018. starpos = (strchr(strchr_pointer + 5,'*'));
  3019. if(starpos!=NULL)
  3020. *(starpos)='\0';
  3021. lcd_setstatus(strchr_pointer + 5);
  3022. break;
  3023. case 114: // M114
  3024. SERIAL_PROTOCOLPGM("X:");
  3025. SERIAL_PROTOCOL(current_position[X_AXIS]);
  3026. SERIAL_PROTOCOLPGM(" Y:");
  3027. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  3028. SERIAL_PROTOCOLPGM(" Z:");
  3029. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  3030. SERIAL_PROTOCOLPGM(" E:");
  3031. SERIAL_PROTOCOL(current_position[E_AXIS]);
  3032. SERIAL_PROTOCOLRPGM(MSG_COUNT_X);
  3033. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  3034. SERIAL_PROTOCOLPGM(" Y:");
  3035. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  3036. SERIAL_PROTOCOLPGM(" Z:");
  3037. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  3038. SERIAL_PROTOCOLLN("");
  3039. break;
  3040. case 120: // M120
  3041. enable_endstops(false) ;
  3042. break;
  3043. case 121: // M121
  3044. enable_endstops(true) ;
  3045. break;
  3046. case 119: // M119
  3047. SERIAL_PROTOCOLRPGM(MSG_M119_REPORT);
  3048. SERIAL_PROTOCOLLN("");
  3049. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  3050. SERIAL_PROTOCOLRPGM(MSG_X_MIN);
  3051. if(READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING){
  3052. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3053. }else{
  3054. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3055. }
  3056. SERIAL_PROTOCOLLN("");
  3057. #endif
  3058. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  3059. SERIAL_PROTOCOLRPGM(MSG_X_MAX);
  3060. if(READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING){
  3061. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3062. }else{
  3063. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3064. }
  3065. SERIAL_PROTOCOLLN("");
  3066. #endif
  3067. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  3068. SERIAL_PROTOCOLRPGM(MSG_Y_MIN);
  3069. if(READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING){
  3070. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3071. }else{
  3072. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3073. }
  3074. SERIAL_PROTOCOLLN("");
  3075. #endif
  3076. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  3077. SERIAL_PROTOCOLRPGM(MSG_Y_MAX);
  3078. if(READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING){
  3079. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3080. }else{
  3081. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3082. }
  3083. SERIAL_PROTOCOLLN("");
  3084. #endif
  3085. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  3086. SERIAL_PROTOCOLRPGM(MSG_Z_MIN);
  3087. if(READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING){
  3088. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3089. }else{
  3090. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3091. }
  3092. SERIAL_PROTOCOLLN("");
  3093. #endif
  3094. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  3095. SERIAL_PROTOCOLRPGM(MSG_Z_MAX);
  3096. if(READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING){
  3097. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3098. }else{
  3099. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3100. }
  3101. SERIAL_PROTOCOLLN("");
  3102. #endif
  3103. break;
  3104. //TODO: update for all axis, use for loop
  3105. #ifdef BLINKM
  3106. case 150: // M150
  3107. {
  3108. byte red;
  3109. byte grn;
  3110. byte blu;
  3111. if(code_seen('R')) red = code_value();
  3112. if(code_seen('U')) grn = code_value();
  3113. if(code_seen('B')) blu = code_value();
  3114. SendColors(red,grn,blu);
  3115. }
  3116. break;
  3117. #endif //BLINKM
  3118. case 200: // M200 D<millimeters> set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  3119. {
  3120. tmp_extruder = active_extruder;
  3121. if(code_seen('T')) {
  3122. tmp_extruder = code_value();
  3123. if(tmp_extruder >= EXTRUDERS) {
  3124. SERIAL_ECHO_START;
  3125. SERIAL_ECHO(MSG_M200_INVALID_EXTRUDER);
  3126. break;
  3127. }
  3128. }
  3129. float area = .0;
  3130. if(code_seen('D')) {
  3131. float diameter = (float)code_value();
  3132. if (diameter == 0.0) {
  3133. // setting any extruder filament size disables volumetric on the assumption that
  3134. // slicers either generate in extruder values as cubic mm or as as filament feeds
  3135. // for all extruders
  3136. volumetric_enabled = false;
  3137. } else {
  3138. filament_size[tmp_extruder] = (float)code_value();
  3139. // make sure all extruders have some sane value for the filament size
  3140. filament_size[0] = (filament_size[0] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : filament_size[0]);
  3141. #if EXTRUDERS > 1
  3142. filament_size[1] = (filament_size[1] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : filament_size[1]);
  3143. #if EXTRUDERS > 2
  3144. filament_size[2] = (filament_size[2] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : filament_size[2]);
  3145. #endif
  3146. #endif
  3147. volumetric_enabled = true;
  3148. }
  3149. } else {
  3150. //reserved for setting filament diameter via UFID or filament measuring device
  3151. break;
  3152. }
  3153. calculate_volumetric_multipliers();
  3154. }
  3155. break;
  3156. case 201: // M201
  3157. for(int8_t i=0; i < NUM_AXIS; i++)
  3158. {
  3159. if(code_seen(axis_codes[i]))
  3160. {
  3161. max_acceleration_units_per_sq_second[i] = code_value();
  3162. }
  3163. }
  3164. // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner)
  3165. reset_acceleration_rates();
  3166. break;
  3167. #if 0 // Not used for Sprinter/grbl gen6
  3168. case 202: // M202
  3169. for(int8_t i=0; i < NUM_AXIS; i++) {
  3170. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  3171. }
  3172. break;
  3173. #endif
  3174. case 203: // M203 max feedrate mm/sec
  3175. for(int8_t i=0; i < NUM_AXIS; i++) {
  3176. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  3177. }
  3178. break;
  3179. case 204: // M204 acclereration S normal moves T filmanent only moves
  3180. {
  3181. if(code_seen('S')) acceleration = code_value() ;
  3182. if(code_seen('T')) retract_acceleration = code_value() ;
  3183. }
  3184. break;
  3185. case 205: //M205 advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  3186. {
  3187. if(code_seen('S')) minimumfeedrate = code_value();
  3188. if(code_seen('T')) mintravelfeedrate = code_value();
  3189. if(code_seen('B')) minsegmenttime = code_value() ;
  3190. if(code_seen('X')) max_xy_jerk = code_value() ;
  3191. if(code_seen('Z')) max_z_jerk = code_value() ;
  3192. if(code_seen('E')) max_e_jerk = code_value() ;
  3193. }
  3194. break;
  3195. case 206: // M206 additional homing offset
  3196. for(int8_t i=0; i < 3; i++)
  3197. {
  3198. if(code_seen(axis_codes[i])) add_homing[i] = code_value();
  3199. }
  3200. break;
  3201. #ifdef FWRETRACT
  3202. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop]
  3203. {
  3204. if(code_seen('S'))
  3205. {
  3206. retract_length = code_value() ;
  3207. }
  3208. if(code_seen('F'))
  3209. {
  3210. retract_feedrate = code_value()/60 ;
  3211. }
  3212. if(code_seen('Z'))
  3213. {
  3214. retract_zlift = code_value() ;
  3215. }
  3216. }break;
  3217. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min]
  3218. {
  3219. if(code_seen('S'))
  3220. {
  3221. retract_recover_length = code_value() ;
  3222. }
  3223. if(code_seen('F'))
  3224. {
  3225. retract_recover_feedrate = code_value()/60 ;
  3226. }
  3227. }break;
  3228. case 209: // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  3229. {
  3230. if(code_seen('S'))
  3231. {
  3232. int t= code_value() ;
  3233. switch(t)
  3234. {
  3235. case 0:
  3236. {
  3237. autoretract_enabled=false;
  3238. retracted[0]=false;
  3239. #if EXTRUDERS > 1
  3240. retracted[1]=false;
  3241. #endif
  3242. #if EXTRUDERS > 2
  3243. retracted[2]=false;
  3244. #endif
  3245. }break;
  3246. case 1:
  3247. {
  3248. autoretract_enabled=true;
  3249. retracted[0]=false;
  3250. #if EXTRUDERS > 1
  3251. retracted[1]=false;
  3252. #endif
  3253. #if EXTRUDERS > 2
  3254. retracted[2]=false;
  3255. #endif
  3256. }break;
  3257. default:
  3258. SERIAL_ECHO_START;
  3259. SERIAL_ECHORPGM(MSG_UNKNOWN_COMMAND);
  3260. SERIAL_ECHO(CMDBUFFER_CURRENT_STRING);
  3261. SERIAL_ECHOLNPGM("\"");
  3262. }
  3263. }
  3264. }break;
  3265. #endif // FWRETRACT
  3266. #if EXTRUDERS > 1
  3267. case 218: // M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  3268. {
  3269. if(setTargetedHotend(218)){
  3270. break;
  3271. }
  3272. if(code_seen('X'))
  3273. {
  3274. extruder_offset[X_AXIS][tmp_extruder] = code_value();
  3275. }
  3276. if(code_seen('Y'))
  3277. {
  3278. extruder_offset[Y_AXIS][tmp_extruder] = code_value();
  3279. }
  3280. #ifdef DUAL_X_CARRIAGE
  3281. if(code_seen('Z'))
  3282. {
  3283. extruder_offset[Z_AXIS][tmp_extruder] = code_value();
  3284. }
  3285. #endif
  3286. SERIAL_ECHO_START;
  3287. SERIAL_ECHORPGM(MSG_HOTEND_OFFSET);
  3288. for(tmp_extruder = 0; tmp_extruder < EXTRUDERS; tmp_extruder++)
  3289. {
  3290. SERIAL_ECHO(" ");
  3291. SERIAL_ECHO(extruder_offset[X_AXIS][tmp_extruder]);
  3292. SERIAL_ECHO(",");
  3293. SERIAL_ECHO(extruder_offset[Y_AXIS][tmp_extruder]);
  3294. #ifdef DUAL_X_CARRIAGE
  3295. SERIAL_ECHO(",");
  3296. SERIAL_ECHO(extruder_offset[Z_AXIS][tmp_extruder]);
  3297. #endif
  3298. }
  3299. SERIAL_ECHOLN("");
  3300. }break;
  3301. #endif
  3302. case 220: // M220 S<factor in percent>- set speed factor override percentage
  3303. {
  3304. if(code_seen('S'))
  3305. {
  3306. feedmultiply = code_value() ;
  3307. }
  3308. }
  3309. break;
  3310. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  3311. {
  3312. if(code_seen('S'))
  3313. {
  3314. int tmp_code = code_value();
  3315. if (code_seen('T'))
  3316. {
  3317. if(setTargetedHotend(221)){
  3318. break;
  3319. }
  3320. extruder_multiply[tmp_extruder] = tmp_code;
  3321. }
  3322. else
  3323. {
  3324. extrudemultiply = tmp_code ;
  3325. }
  3326. }
  3327. }
  3328. break;
  3329. case 226: // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  3330. {
  3331. if(code_seen('P')){
  3332. int pin_number = code_value(); // pin number
  3333. int pin_state = -1; // required pin state - default is inverted
  3334. if(code_seen('S')) pin_state = code_value(); // required pin state
  3335. if(pin_state >= -1 && pin_state <= 1){
  3336. for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++)
  3337. {
  3338. if (sensitive_pins[i] == pin_number)
  3339. {
  3340. pin_number = -1;
  3341. break;
  3342. }
  3343. }
  3344. if (pin_number > -1)
  3345. {
  3346. int target = LOW;
  3347. st_synchronize();
  3348. pinMode(pin_number, INPUT);
  3349. switch(pin_state){
  3350. case 1:
  3351. target = HIGH;
  3352. break;
  3353. case 0:
  3354. target = LOW;
  3355. break;
  3356. case -1:
  3357. target = !digitalRead(pin_number);
  3358. break;
  3359. }
  3360. while(digitalRead(pin_number) != target){
  3361. manage_heater();
  3362. manage_inactivity();
  3363. lcd_update();
  3364. }
  3365. }
  3366. }
  3367. }
  3368. }
  3369. break;
  3370. #if NUM_SERVOS > 0
  3371. case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  3372. {
  3373. int servo_index = -1;
  3374. int servo_position = 0;
  3375. if (code_seen('P'))
  3376. servo_index = code_value();
  3377. if (code_seen('S')) {
  3378. servo_position = code_value();
  3379. if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
  3380. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  3381. servos[servo_index].attach(0);
  3382. #endif
  3383. servos[servo_index].write(servo_position);
  3384. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  3385. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  3386. servos[servo_index].detach();
  3387. #endif
  3388. }
  3389. else {
  3390. SERIAL_ECHO_START;
  3391. SERIAL_ECHO("Servo ");
  3392. SERIAL_ECHO(servo_index);
  3393. SERIAL_ECHOLN(" out of range");
  3394. }
  3395. }
  3396. else if (servo_index >= 0) {
  3397. SERIAL_PROTOCOL(MSG_OK);
  3398. SERIAL_PROTOCOL(" Servo ");
  3399. SERIAL_PROTOCOL(servo_index);
  3400. SERIAL_PROTOCOL(": ");
  3401. SERIAL_PROTOCOL(servos[servo_index].read());
  3402. SERIAL_PROTOCOLLN("");
  3403. }
  3404. }
  3405. break;
  3406. #endif // NUM_SERVOS > 0
  3407. #if (LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) || defined(LCD_USE_I2C_BUZZER)))
  3408. case 300: // M300
  3409. {
  3410. int beepS = code_seen('S') ? code_value() : 110;
  3411. int beepP = code_seen('P') ? code_value() : 1000;
  3412. if (beepS > 0)
  3413. {
  3414. #if BEEPER > 0
  3415. tone(BEEPER, beepS);
  3416. delay(beepP);
  3417. noTone(BEEPER);
  3418. #elif defined(ULTRALCD)
  3419. lcd_buzz(beepS, beepP);
  3420. #elif defined(LCD_USE_I2C_BUZZER)
  3421. lcd_buzz(beepP, beepS);
  3422. #endif
  3423. }
  3424. else
  3425. {
  3426. delay(beepP);
  3427. }
  3428. }
  3429. break;
  3430. #endif // M300
  3431. #ifdef PIDTEMP
  3432. case 301: // M301
  3433. {
  3434. if(code_seen('P')) Kp = code_value();
  3435. if(code_seen('I')) Ki = scalePID_i(code_value());
  3436. if(code_seen('D')) Kd = scalePID_d(code_value());
  3437. #ifdef PID_ADD_EXTRUSION_RATE
  3438. if(code_seen('C')) Kc = code_value();
  3439. #endif
  3440. updatePID();
  3441. SERIAL_PROTOCOL(MSG_OK);
  3442. SERIAL_PROTOCOL(" p:");
  3443. SERIAL_PROTOCOL(Kp);
  3444. SERIAL_PROTOCOL(" i:");
  3445. SERIAL_PROTOCOL(unscalePID_i(Ki));
  3446. SERIAL_PROTOCOL(" d:");
  3447. SERIAL_PROTOCOL(unscalePID_d(Kd));
  3448. #ifdef PID_ADD_EXTRUSION_RATE
  3449. SERIAL_PROTOCOL(" c:");
  3450. //Kc does not have scaling applied above, or in resetting defaults
  3451. SERIAL_PROTOCOL(Kc);
  3452. #endif
  3453. SERIAL_PROTOCOLLN("");
  3454. }
  3455. break;
  3456. #endif //PIDTEMP
  3457. #ifdef PIDTEMPBED
  3458. case 304: // M304
  3459. {
  3460. if(code_seen('P')) bedKp = code_value();
  3461. if(code_seen('I')) bedKi = scalePID_i(code_value());
  3462. if(code_seen('D')) bedKd = scalePID_d(code_value());
  3463. updatePID();
  3464. SERIAL_PROTOCOL(MSG_OK);
  3465. SERIAL_PROTOCOL(" p:");
  3466. SERIAL_PROTOCOL(bedKp);
  3467. SERIAL_PROTOCOL(" i:");
  3468. SERIAL_PROTOCOL(unscalePID_i(bedKi));
  3469. SERIAL_PROTOCOL(" d:");
  3470. SERIAL_PROTOCOL(unscalePID_d(bedKd));
  3471. SERIAL_PROTOCOLLN("");
  3472. }
  3473. break;
  3474. #endif //PIDTEMP
  3475. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  3476. {
  3477. #ifdef CHDK
  3478. SET_OUTPUT(CHDK);
  3479. WRITE(CHDK, HIGH);
  3480. chdkHigh = millis();
  3481. chdkActive = true;
  3482. #else
  3483. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  3484. const uint8_t NUM_PULSES=16;
  3485. const float PULSE_LENGTH=0.01524;
  3486. for(int i=0; i < NUM_PULSES; i++) {
  3487. WRITE(PHOTOGRAPH_PIN, HIGH);
  3488. _delay_ms(PULSE_LENGTH);
  3489. WRITE(PHOTOGRAPH_PIN, LOW);
  3490. _delay_ms(PULSE_LENGTH);
  3491. }
  3492. delay(7.33);
  3493. for(int i=0; i < NUM_PULSES; i++) {
  3494. WRITE(PHOTOGRAPH_PIN, HIGH);
  3495. _delay_ms(PULSE_LENGTH);
  3496. WRITE(PHOTOGRAPH_PIN, LOW);
  3497. _delay_ms(PULSE_LENGTH);
  3498. }
  3499. #endif
  3500. #endif //chdk end if
  3501. }
  3502. break;
  3503. #ifdef DOGLCD
  3504. case 250: // M250 Set LCD contrast value: C<value> (value 0..63)
  3505. {
  3506. if (code_seen('C')) {
  3507. lcd_setcontrast( ((int)code_value())&63 );
  3508. }
  3509. SERIAL_PROTOCOLPGM("lcd contrast value: ");
  3510. SERIAL_PROTOCOL(lcd_contrast);
  3511. SERIAL_PROTOCOLLN("");
  3512. }
  3513. break;
  3514. #endif
  3515. #ifdef PREVENT_DANGEROUS_EXTRUDE
  3516. case 302: // allow cold extrudes, or set the minimum extrude temperature
  3517. {
  3518. float temp = .0;
  3519. if (code_seen('S')) temp=code_value();
  3520. set_extrude_min_temp(temp);
  3521. }
  3522. break;
  3523. #endif
  3524. case 303: // M303 PID autotune
  3525. {
  3526. float temp = 150.0;
  3527. int e=0;
  3528. int c=5;
  3529. if (code_seen('E')) e=code_value();
  3530. if (e<0)
  3531. temp=70;
  3532. if (code_seen('S')) temp=code_value();
  3533. if (code_seen('C')) c=code_value();
  3534. PID_autotune(temp, e, c);
  3535. }
  3536. break;
  3537. case 400: // M400 finish all moves
  3538. {
  3539. st_synchronize();
  3540. }
  3541. break;
  3542. #ifdef FILAMENT_SENSOR
  3543. case 404: //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or display nominal filament width
  3544. {
  3545. #if (FILWIDTH_PIN > -1)
  3546. if(code_seen('N')) filament_width_nominal=code_value();
  3547. else{
  3548. SERIAL_PROTOCOLPGM("Filament dia (nominal mm):");
  3549. SERIAL_PROTOCOLLN(filament_width_nominal);
  3550. }
  3551. #endif
  3552. }
  3553. break;
  3554. case 405: //M405 Turn on filament sensor for control
  3555. {
  3556. if(code_seen('D')) meas_delay_cm=code_value();
  3557. if(meas_delay_cm> MAX_MEASUREMENT_DELAY)
  3558. meas_delay_cm = MAX_MEASUREMENT_DELAY;
  3559. if(delay_index2 == -1) //initialize the ring buffer if it has not been done since startup
  3560. {
  3561. int temp_ratio = widthFil_to_size_ratio();
  3562. for (delay_index1=0; delay_index1<(MAX_MEASUREMENT_DELAY+1); ++delay_index1 ){
  3563. measurement_delay[delay_index1]=temp_ratio-100; //subtract 100 to scale within a signed byte
  3564. }
  3565. delay_index1=0;
  3566. delay_index2=0;
  3567. }
  3568. filament_sensor = true ;
  3569. //SERIAL_PROTOCOLPGM("Filament dia (measured mm):");
  3570. //SERIAL_PROTOCOL(filament_width_meas);
  3571. //SERIAL_PROTOCOLPGM("Extrusion ratio(%):");
  3572. //SERIAL_PROTOCOL(extrudemultiply);
  3573. }
  3574. break;
  3575. case 406: //M406 Turn off filament sensor for control
  3576. {
  3577. filament_sensor = false ;
  3578. }
  3579. break;
  3580. case 407: //M407 Display measured filament diameter
  3581. {
  3582. SERIAL_PROTOCOLPGM("Filament dia (measured mm):");
  3583. SERIAL_PROTOCOLLN(filament_width_meas);
  3584. }
  3585. break;
  3586. #endif
  3587. case 500: // M500 Store settings in EEPROM
  3588. {
  3589. Config_StoreSettings();
  3590. }
  3591. break;
  3592. case 501: // M501 Read settings from EEPROM
  3593. {
  3594. Config_RetrieveSettings();
  3595. }
  3596. break;
  3597. case 502: // M502 Revert to default settings
  3598. {
  3599. Config_ResetDefault();
  3600. }
  3601. break;
  3602. case 503: // M503 print settings currently in memory
  3603. {
  3604. Config_PrintSettings();
  3605. }
  3606. break;
  3607. case 509: //M509 Force language selection
  3608. {
  3609. lcd_force_language_selection();
  3610. SERIAL_ECHO_START;
  3611. SERIAL_PROTOCOLPGM(("LANG SEL FORCED"));
  3612. }
  3613. break;
  3614. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  3615. case 540:
  3616. {
  3617. if(code_seen('S')) abort_on_endstop_hit = code_value() > 0;
  3618. }
  3619. break;
  3620. #endif
  3621. #ifdef CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  3622. case CUSTOM_M_CODE_SET_Z_PROBE_OFFSET:
  3623. {
  3624. float value;
  3625. if (code_seen('Z'))
  3626. {
  3627. value = code_value();
  3628. if ((Z_PROBE_OFFSET_RANGE_MIN <= value) && (value <= Z_PROBE_OFFSET_RANGE_MAX))
  3629. {
  3630. zprobe_zoffset = -value; // compare w/ line 278 of ConfigurationStore.cpp
  3631. SERIAL_ECHO_START;
  3632. SERIAL_ECHOLNRPGM(CAT4(MSG_ZPROBE_ZOFFSET, " ", MSG_OK,PSTR("")));
  3633. SERIAL_PROTOCOLLN("");
  3634. }
  3635. else
  3636. {
  3637. SERIAL_ECHO_START;
  3638. SERIAL_ECHORPGM(MSG_ZPROBE_ZOFFSET);
  3639. SERIAL_ECHORPGM(MSG_Z_MIN);
  3640. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MIN);
  3641. SERIAL_ECHORPGM(MSG_Z_MAX);
  3642. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MAX);
  3643. SERIAL_PROTOCOLLN("");
  3644. }
  3645. }
  3646. else
  3647. {
  3648. SERIAL_ECHO_START;
  3649. SERIAL_ECHOLNRPGM(CAT2(MSG_ZPROBE_ZOFFSET, PSTR(" : ")));
  3650. SERIAL_ECHO(-zprobe_zoffset);
  3651. SERIAL_PROTOCOLLN("");
  3652. }
  3653. break;
  3654. }
  3655. #endif // CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  3656. #ifdef FILAMENTCHANGEENABLE
  3657. case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  3658. {
  3659. st_synchronize();
  3660. feedmultiplyBckp=feedmultiply;
  3661. int8_t TooLowZ = 0;
  3662. float target[4];
  3663. float lastpos[4];
  3664. target[X_AXIS]=current_position[X_AXIS];
  3665. target[Y_AXIS]=current_position[Y_AXIS];
  3666. target[Z_AXIS]=current_position[Z_AXIS];
  3667. target[E_AXIS]=current_position[E_AXIS];
  3668. lastpos[X_AXIS]=current_position[X_AXIS];
  3669. lastpos[Y_AXIS]=current_position[Y_AXIS];
  3670. lastpos[Z_AXIS]=current_position[Z_AXIS];
  3671. lastpos[E_AXIS]=current_position[E_AXIS];
  3672. //Restract extruder
  3673. if(code_seen('E'))
  3674. {
  3675. target[E_AXIS]+= code_value();
  3676. }
  3677. else
  3678. {
  3679. #ifdef FILAMENTCHANGE_FIRSTRETRACT
  3680. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  3681. #endif
  3682. }
  3683. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  3684. //Lift Z
  3685. if(code_seen('Z'))
  3686. {
  3687. target[Z_AXIS]+= code_value();
  3688. }
  3689. else
  3690. {
  3691. #ifdef FILAMENTCHANGE_ZADD
  3692. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  3693. if(target[Z_AXIS] < 10){
  3694. target[Z_AXIS]+= 10 ;
  3695. TooLowZ = 1;
  3696. }else{
  3697. TooLowZ = 0;
  3698. }
  3699. #endif
  3700. }
  3701. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_ZFEED, active_extruder);
  3702. //Move XY to side
  3703. if(code_seen('X'))
  3704. {
  3705. target[X_AXIS]+= code_value();
  3706. }
  3707. else
  3708. {
  3709. #ifdef FILAMENTCHANGE_XPOS
  3710. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  3711. #endif
  3712. }
  3713. if(code_seen('Y'))
  3714. {
  3715. target[Y_AXIS]= code_value();
  3716. }
  3717. else
  3718. {
  3719. #ifdef FILAMENTCHANGE_YPOS
  3720. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  3721. #endif
  3722. }
  3723. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_XYFEED, active_extruder);
  3724. // Unload filament
  3725. if(code_seen('L'))
  3726. {
  3727. target[E_AXIS]+= code_value();
  3728. }
  3729. else
  3730. {
  3731. #ifdef FILAMENTCHANGE_FINALRETRACT
  3732. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  3733. #endif
  3734. }
  3735. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  3736. //finish moves
  3737. st_synchronize();
  3738. //disable extruder steppers so filament can be removed
  3739. disable_e0();
  3740. disable_e1();
  3741. disable_e2();
  3742. delay(100);
  3743. //Wait for user to insert filament
  3744. uint8_t cnt=0;
  3745. int counterBeep = 0;
  3746. lcd_wait_interact();
  3747. while(!lcd_clicked()){
  3748. cnt++;
  3749. manage_heater();
  3750. manage_inactivity(true);
  3751. if(cnt==0)
  3752. {
  3753. #if BEEPER > 0
  3754. if (counterBeep== 500){
  3755. counterBeep = 0;
  3756. }
  3757. SET_OUTPUT(BEEPER);
  3758. if (counterBeep== 0){
  3759. WRITE(BEEPER,HIGH);
  3760. }
  3761. if (counterBeep== 20){
  3762. WRITE(BEEPER,LOW);
  3763. }
  3764. counterBeep++;
  3765. #else
  3766. #if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
  3767. lcd_buzz(1000/6,100);
  3768. #else
  3769. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS,LCD_FEEDBACK_FREQUENCY_HZ);
  3770. #endif
  3771. #endif
  3772. }
  3773. }
  3774. //Filament inserted
  3775. WRITE(BEEPER,LOW);
  3776. //Feed the filament to the end of nozzle quickly
  3777. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  3778. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EFEED, active_extruder);
  3779. //Extrude some filament
  3780. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  3781. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EXFEED, active_extruder);
  3782. //Wait for user to check the state
  3783. lcd_change_fil_state = 0;
  3784. lcd_loading_filament();
  3785. while ((lcd_change_fil_state == 0)||(lcd_change_fil_state != 1)){
  3786. lcd_change_fil_state = 0;
  3787. lcd_alright();
  3788. switch(lcd_change_fil_state){
  3789. // Filament failed to load so load it again
  3790. case 2:
  3791. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  3792. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EFEED, active_extruder);
  3793. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  3794. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EXFEED, active_extruder);
  3795. lcd_loading_filament();
  3796. break;
  3797. // Filament loaded properly but color is not clear
  3798. case 3:
  3799. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  3800. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  3801. lcd_loading_color();
  3802. break;
  3803. // Everything good
  3804. default:
  3805. lcd_change_success();
  3806. break;
  3807. }
  3808. }
  3809. //Not let's go back to print
  3810. //Feed a little of filament to stabilize pressure
  3811. target[E_AXIS]+= FILAMENTCHANGE_RECFEED;
  3812. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EXFEED, active_extruder);
  3813. //Retract
  3814. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT;
  3815. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  3816. //plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //should do nothing
  3817. //Move XY back
  3818. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_XYFEED, active_extruder);
  3819. //Move Z back
  3820. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_ZFEED, active_extruder);
  3821. target[E_AXIS]= target[E_AXIS] - FILAMENTCHANGE_FIRSTRETRACT;
  3822. //Unretract
  3823. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  3824. //Set E position to original
  3825. plan_set_e_position(lastpos[E_AXIS]);
  3826. //Recover feed rate
  3827. feedmultiply=feedmultiplyBckp;
  3828. char cmd[9];
  3829. sprintf_P(cmd, PSTR("M220 S%i"), feedmultiplyBckp);
  3830. enquecommand(cmd);
  3831. }
  3832. break;
  3833. #endif //FILAMENTCHANGEENABLE
  3834. #ifdef DUAL_X_CARRIAGE
  3835. case 605: // Set dual x-carriage movement mode:
  3836. // M605 S0: Full control mode. The slicer has full control over x-carriage movement
  3837. // M605 S1: Auto-park mode. The inactive head will auto park/unpark without slicer involvement
  3838. // M605 S2 [Xnnn] [Rmmm]: Duplication mode. The second extruder will duplicate the first with nnn
  3839. // millimeters x-offset and an optional differential hotend temperature of
  3840. // mmm degrees. E.g., with "M605 S2 X100 R2" the second extruder will duplicate
  3841. // the first with a spacing of 100mm in the x direction and 2 degrees hotter.
  3842. //
  3843. // Note: the X axis should be homed after changing dual x-carriage mode.
  3844. {
  3845. st_synchronize();
  3846. if (code_seen('S'))
  3847. dual_x_carriage_mode = code_value();
  3848. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  3849. {
  3850. if (code_seen('X'))
  3851. duplicate_extruder_x_offset = max(code_value(),X2_MIN_POS - x_home_pos(0));
  3852. if (code_seen('R'))
  3853. duplicate_extruder_temp_offset = code_value();
  3854. SERIAL_ECHO_START;
  3855. SERIAL_ECHORPGM(MSG_HOTEND_OFFSET);
  3856. SERIAL_ECHO(" ");
  3857. SERIAL_ECHO(extruder_offset[X_AXIS][0]);
  3858. SERIAL_ECHO(",");
  3859. SERIAL_ECHO(extruder_offset[Y_AXIS][0]);
  3860. SERIAL_ECHO(" ");
  3861. SERIAL_ECHO(duplicate_extruder_x_offset);
  3862. SERIAL_ECHO(",");
  3863. SERIAL_ECHOLN(extruder_offset[Y_AXIS][1]);
  3864. }
  3865. else if (dual_x_carriage_mode != DXC_FULL_CONTROL_MODE && dual_x_carriage_mode != DXC_AUTO_PARK_MODE)
  3866. {
  3867. dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  3868. }
  3869. active_extruder_parked = false;
  3870. extruder_duplication_enabled = false;
  3871. delayed_move_time = 0;
  3872. }
  3873. break;
  3874. #endif //DUAL_X_CARRIAGE
  3875. case 907: // M907 Set digital trimpot motor current using axis codes.
  3876. {
  3877. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  3878. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_current(i,code_value());
  3879. if(code_seen('B')) digipot_current(4,code_value());
  3880. if(code_seen('S')) for(int i=0;i<=4;i++) digipot_current(i,code_value());
  3881. #endif
  3882. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  3883. if(code_seen('X')) digipot_current(0, code_value());
  3884. #endif
  3885. #ifdef MOTOR_CURRENT_PWM_Z_PIN
  3886. if(code_seen('Z')) digipot_current(1, code_value());
  3887. #endif
  3888. #ifdef MOTOR_CURRENT_PWM_E_PIN
  3889. if(code_seen('E')) digipot_current(2, code_value());
  3890. #endif
  3891. #ifdef DIGIPOT_I2C
  3892. // this one uses actual amps in floating point
  3893. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_i2c_set_current(i, code_value());
  3894. // for each additional extruder (named B,C,D,E..., channels 4,5,6,7...)
  3895. for(int i=NUM_AXIS;i<DIGIPOT_I2C_NUM_CHANNELS;i++) if(code_seen('B'+i-NUM_AXIS)) digipot_i2c_set_current(i, code_value());
  3896. #endif
  3897. }
  3898. break;
  3899. case 908: // M908 Control digital trimpot directly.
  3900. {
  3901. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  3902. uint8_t channel,current;
  3903. if(code_seen('P')) channel=code_value();
  3904. if(code_seen('S')) current=code_value();
  3905. digitalPotWrite(channel, current);
  3906. #endif
  3907. }
  3908. break;
  3909. case 350: // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  3910. {
  3911. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  3912. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  3913. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  3914. if(code_seen('B')) microstep_mode(4,code_value());
  3915. microstep_readings();
  3916. #endif
  3917. }
  3918. break;
  3919. case 351: // M351 Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  3920. {
  3921. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  3922. if(code_seen('S')) switch((int)code_value())
  3923. {
  3924. case 1:
  3925. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
  3926. if(code_seen('B')) microstep_ms(4,code_value(),-1);
  3927. break;
  3928. case 2:
  3929. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
  3930. if(code_seen('B')) microstep_ms(4,-1,code_value());
  3931. break;
  3932. }
  3933. microstep_readings();
  3934. #endif
  3935. }
  3936. break;
  3937. case 999: // M999: Restart after being stopped
  3938. Stopped = false;
  3939. lcd_reset_alert_level();
  3940. gcode_LastN = Stopped_gcode_LastN;
  3941. FlushSerialRequestResend();
  3942. break;
  3943. }
  3944. } // end if(code_seen('M')) (end of M codes)
  3945. else if(code_seen('T'))
  3946. {
  3947. tmp_extruder = code_value();
  3948. if(tmp_extruder >= EXTRUDERS) {
  3949. SERIAL_ECHO_START;
  3950. SERIAL_ECHO("T");
  3951. SERIAL_ECHO(tmp_extruder);
  3952. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  3953. }
  3954. else {
  3955. boolean make_move = false;
  3956. if(code_seen('F')) {
  3957. make_move = true;
  3958. next_feedrate = code_value();
  3959. if(next_feedrate > 0.0) {
  3960. feedrate = next_feedrate;
  3961. }
  3962. }
  3963. #if EXTRUDERS > 1
  3964. if(tmp_extruder != active_extruder) {
  3965. // Save current position to return to after applying extruder offset
  3966. memcpy(destination, current_position, sizeof(destination));
  3967. #ifdef DUAL_X_CARRIAGE
  3968. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE && Stopped == false &&
  3969. (delayed_move_time != 0 || current_position[X_AXIS] != x_home_pos(active_extruder)))
  3970. {
  3971. // Park old head: 1) raise 2) move to park position 3) lower
  3972. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  3973. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3974. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  3975. current_position[E_AXIS], max_feedrate[X_AXIS], active_extruder);
  3976. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS],
  3977. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3978. st_synchronize();
  3979. }
  3980. // apply Y & Z extruder offset (x offset is already used in determining home pos)
  3981. current_position[Y_AXIS] = current_position[Y_AXIS] -
  3982. extruder_offset[Y_AXIS][active_extruder] +
  3983. extruder_offset[Y_AXIS][tmp_extruder];
  3984. current_position[Z_AXIS] = current_position[Z_AXIS] -
  3985. extruder_offset[Z_AXIS][active_extruder] +
  3986. extruder_offset[Z_AXIS][tmp_extruder];
  3987. active_extruder = tmp_extruder;
  3988. // This function resets the max/min values - the current position may be overwritten below.
  3989. axis_is_at_home(X_AXIS);
  3990. if (dual_x_carriage_mode == DXC_FULL_CONTROL_MODE)
  3991. {
  3992. current_position[X_AXIS] = inactive_extruder_x_pos;
  3993. inactive_extruder_x_pos = destination[X_AXIS];
  3994. }
  3995. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  3996. {
  3997. active_extruder_parked = (active_extruder == 0); // this triggers the second extruder to move into the duplication position
  3998. if (active_extruder == 0 || active_extruder_parked)
  3999. current_position[X_AXIS] = inactive_extruder_x_pos;
  4000. else
  4001. current_position[X_AXIS] = destination[X_AXIS] + duplicate_extruder_x_offset;
  4002. inactive_extruder_x_pos = destination[X_AXIS];
  4003. extruder_duplication_enabled = false;
  4004. }
  4005. else
  4006. {
  4007. // record raised toolhead position for use by unpark
  4008. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  4009. raised_parked_position[Z_AXIS] += TOOLCHANGE_UNPARK_ZLIFT;
  4010. active_extruder_parked = true;
  4011. delayed_move_time = 0;
  4012. }
  4013. #else
  4014. // Offset extruder (only by XY)
  4015. int i;
  4016. for(i = 0; i < 2; i++) {
  4017. current_position[i] = current_position[i] -
  4018. extruder_offset[i][active_extruder] +
  4019. extruder_offset[i][tmp_extruder];
  4020. }
  4021. // Set the new active extruder and position
  4022. active_extruder = tmp_extruder;
  4023. #endif //else DUAL_X_CARRIAGE
  4024. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  4025. // Move to the old position if 'F' was in the parameters
  4026. if(make_move && Stopped == false) {
  4027. prepare_move();
  4028. }
  4029. }
  4030. #endif
  4031. SERIAL_ECHO_START;
  4032. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  4033. SERIAL_PROTOCOLLN((int)active_extruder);
  4034. }
  4035. } // end if(code_seen('T')) (end of T codes)
  4036. else
  4037. {
  4038. SERIAL_ECHO_START;
  4039. SERIAL_ECHORPGM(MSG_UNKNOWN_COMMAND);
  4040. SERIAL_ECHO(CMDBUFFER_CURRENT_STRING);
  4041. SERIAL_ECHOLNPGM("\"");
  4042. }
  4043. ClearToSend();
  4044. }
  4045. void FlushSerialRequestResend()
  4046. {
  4047. //char cmdbuffer[bufindr][100]="Resend:";
  4048. MYSERIAL.flush();
  4049. SERIAL_PROTOCOLRPGM(MSG_RESEND);
  4050. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  4051. ClearToSend();
  4052. }
  4053. // Confirm the execution of a command, if sent from a serial line.
  4054. // Execution of a command from a SD card will not be confirmed.
  4055. void ClearToSend()
  4056. {
  4057. previous_millis_cmd = millis();
  4058. if (CMDBUFFER_CURRENT_TYPE == CMDBUFFER_CURRENT_TYPE_USB)
  4059. SERIAL_PROTOCOLLNRPGM(MSG_OK);
  4060. }
  4061. void get_coordinates()
  4062. {
  4063. bool seen[4]={false,false,false,false};
  4064. for(int8_t i=0; i < NUM_AXIS; i++) {
  4065. if(code_seen(axis_codes[i]))
  4066. {
  4067. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  4068. seen[i]=true;
  4069. }
  4070. else destination[i] = current_position[i]; //Are these else lines really needed?
  4071. }
  4072. if(code_seen('F')) {
  4073. next_feedrate = code_value();
  4074. if(next_feedrate > 0.0) feedrate = next_feedrate;
  4075. }
  4076. }
  4077. void get_arc_coordinates()
  4078. {
  4079. #ifdef SF_ARC_FIX
  4080. bool relative_mode_backup = relative_mode;
  4081. relative_mode = true;
  4082. #endif
  4083. get_coordinates();
  4084. #ifdef SF_ARC_FIX
  4085. relative_mode=relative_mode_backup;
  4086. #endif
  4087. if(code_seen('I')) {
  4088. offset[0] = code_value();
  4089. }
  4090. else {
  4091. offset[0] = 0.0;
  4092. }
  4093. if(code_seen('J')) {
  4094. offset[1] = code_value();
  4095. }
  4096. else {
  4097. offset[1] = 0.0;
  4098. }
  4099. }
  4100. void clamp_to_software_endstops(float target[3])
  4101. {
  4102. world2machine_clamp(target[0], target[1]);
  4103. // Clamp the Z coordinate.
  4104. if (min_software_endstops) {
  4105. float negative_z_offset = 0;
  4106. #ifdef ENABLE_AUTO_BED_LEVELING
  4107. if (Z_PROBE_OFFSET_FROM_EXTRUDER < 0) negative_z_offset = negative_z_offset + Z_PROBE_OFFSET_FROM_EXTRUDER;
  4108. if (add_homing[Z_AXIS] < 0) negative_z_offset = negative_z_offset + add_homing[Z_AXIS];
  4109. #endif
  4110. if (target[Z_AXIS] < min_pos[Z_AXIS]+negative_z_offset) target[Z_AXIS] = min_pos[Z_AXIS]+negative_z_offset;
  4111. }
  4112. if (max_software_endstops) {
  4113. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  4114. }
  4115. }
  4116. #ifdef MESH_BED_LEVELING
  4117. void mesh_plan_buffer_line(const float &x, const float &y, const float &z, const float &e, const float &feed_rate, const uint8_t extruder) {
  4118. float dx = x - current_position[X_AXIS];
  4119. float dy = y - current_position[Y_AXIS];
  4120. float dz = z - current_position[Z_AXIS];
  4121. int n_segments = 0;
  4122. if (mbl.active) {
  4123. float len = abs(dx) + abs(dy) + abs(dz);
  4124. if (len > 0)
  4125. n_segments = int(floor(len / 30.f));
  4126. }
  4127. if (n_segments > 1) {
  4128. float de = e - current_position[E_AXIS];
  4129. for (int i = 1; i < n_segments; ++ i) {
  4130. float t = float(i) / float(n_segments);
  4131. plan_buffer_line(
  4132. current_position[X_AXIS] + t * dx,
  4133. current_position[Y_AXIS] + t * dy,
  4134. current_position[Z_AXIS] + t * dz,
  4135. current_position[E_AXIS] + t * de,
  4136. feed_rate, extruder);
  4137. }
  4138. }
  4139. // The rest of the path.
  4140. plan_buffer_line(x, y, z, e, feed_rate, extruder);
  4141. set_current_to_destination();
  4142. }
  4143. #endif // MESH_BED_LEVELING
  4144. void prepare_move()
  4145. {
  4146. clamp_to_software_endstops(destination);
  4147. previous_millis_cmd = millis();
  4148. #ifdef DUAL_X_CARRIAGE
  4149. if (active_extruder_parked)
  4150. {
  4151. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0)
  4152. {
  4153. // move duplicate extruder into correct duplication position.
  4154. plan_set_position(inactive_extruder_x_pos, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  4155. plan_buffer_line(current_position[X_AXIS] + duplicate_extruder_x_offset, current_position[Y_AXIS], current_position[Z_AXIS],
  4156. current_position[E_AXIS], max_feedrate[X_AXIS], 1);
  4157. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  4158. st_synchronize();
  4159. extruder_duplication_enabled = true;
  4160. active_extruder_parked = false;
  4161. }
  4162. else if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE) // handle unparking of head
  4163. {
  4164. if (current_position[E_AXIS] == destination[E_AXIS])
  4165. {
  4166. // this is a travel move - skit it but keep track of current position (so that it can later
  4167. // be used as start of first non-travel move)
  4168. if (delayed_move_time != 0xFFFFFFFFUL)
  4169. {
  4170. memcpy(current_position, destination, sizeof(current_position));
  4171. if (destination[Z_AXIS] > raised_parked_position[Z_AXIS])
  4172. raised_parked_position[Z_AXIS] = destination[Z_AXIS];
  4173. delayed_move_time = millis();
  4174. return;
  4175. }
  4176. }
  4177. delayed_move_time = 0;
  4178. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  4179. plan_buffer_line(raised_parked_position[X_AXIS], raised_parked_position[Y_AXIS], raised_parked_position[Z_AXIS], current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  4180. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], raised_parked_position[Z_AXIS],
  4181. current_position[E_AXIS], min(max_feedrate[X_AXIS],max_feedrate[Y_AXIS]), active_extruder);
  4182. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  4183. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  4184. active_extruder_parked = false;
  4185. }
  4186. }
  4187. #endif //DUAL_X_CARRIAGE
  4188. // Do not use feedmultiply for E or Z only moves
  4189. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  4190. #ifdef MESH_BED_LEVELING
  4191. mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  4192. #else
  4193. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  4194. #endif
  4195. }
  4196. else {
  4197. #ifdef MESH_BED_LEVELING
  4198. mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  4199. #else
  4200. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  4201. #endif
  4202. }
  4203. for(int8_t i=0; i < NUM_AXIS; i++) {
  4204. current_position[i] = destination[i];
  4205. }
  4206. }
  4207. void prepare_arc_move(char isclockwise) {
  4208. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  4209. // Trace the arc
  4210. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  4211. // As far as the parser is concerned, the position is now == target. In reality the
  4212. // motion control system might still be processing the action and the real tool position
  4213. // in any intermediate location.
  4214. for(int8_t i=0; i < NUM_AXIS; i++) {
  4215. current_position[i] = destination[i];
  4216. }
  4217. previous_millis_cmd = millis();
  4218. }
  4219. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  4220. #if defined(FAN_PIN)
  4221. #if CONTROLLERFAN_PIN == FAN_PIN
  4222. #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN"
  4223. #endif
  4224. #endif
  4225. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  4226. unsigned long lastMotorCheck = 0;
  4227. void controllerFan()
  4228. {
  4229. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  4230. {
  4231. lastMotorCheck = millis();
  4232. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN) || (soft_pwm_bed > 0)
  4233. #if EXTRUDERS > 2
  4234. || !READ(E2_ENABLE_PIN)
  4235. #endif
  4236. #if EXTRUDER > 1
  4237. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  4238. || !READ(X2_ENABLE_PIN)
  4239. #endif
  4240. || !READ(E1_ENABLE_PIN)
  4241. #endif
  4242. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  4243. {
  4244. lastMotor = millis(); //... set time to NOW so the fan will turn on
  4245. }
  4246. if ((millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  4247. {
  4248. digitalWrite(CONTROLLERFAN_PIN, 0);
  4249. analogWrite(CONTROLLERFAN_PIN, 0);
  4250. }
  4251. else
  4252. {
  4253. // allows digital or PWM fan output to be used (see M42 handling)
  4254. digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  4255. analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  4256. }
  4257. }
  4258. }
  4259. #endif
  4260. #ifdef TEMP_STAT_LEDS
  4261. static bool blue_led = false;
  4262. static bool red_led = false;
  4263. static uint32_t stat_update = 0;
  4264. void handle_status_leds(void) {
  4265. float max_temp = 0.0;
  4266. if(millis() > stat_update) {
  4267. stat_update += 500; // Update every 0.5s
  4268. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  4269. max_temp = max(max_temp, degHotend(cur_extruder));
  4270. max_temp = max(max_temp, degTargetHotend(cur_extruder));
  4271. }
  4272. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  4273. max_temp = max(max_temp, degTargetBed());
  4274. max_temp = max(max_temp, degBed());
  4275. #endif
  4276. if((max_temp > 55.0) && (red_led == false)) {
  4277. digitalWrite(STAT_LED_RED, 1);
  4278. digitalWrite(STAT_LED_BLUE, 0);
  4279. red_led = true;
  4280. blue_led = false;
  4281. }
  4282. if((max_temp < 54.0) && (blue_led == false)) {
  4283. digitalWrite(STAT_LED_RED, 0);
  4284. digitalWrite(STAT_LED_BLUE, 1);
  4285. red_led = false;
  4286. blue_led = true;
  4287. }
  4288. }
  4289. }
  4290. #endif
  4291. void manage_inactivity(bool ignore_stepper_queue/*=false*/) //default argument set in Marlin.h
  4292. {
  4293. #if defined(KILL_PIN) && KILL_PIN > -1
  4294. static int killCount = 0; // make the inactivity button a bit less responsive
  4295. const int KILL_DELAY = 10000;
  4296. #endif
  4297. if(buflen < (BUFSIZE-1))
  4298. get_command();
  4299. if( (millis() - previous_millis_cmd) > max_inactive_time )
  4300. if(max_inactive_time)
  4301. kill();
  4302. if(stepper_inactive_time) {
  4303. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  4304. {
  4305. if(blocks_queued() == false && ignore_stepper_queue == false) {
  4306. disable_x();
  4307. // SERIAL_ECHOLNPGM("manage_inactivity - disable Y");
  4308. disable_y();
  4309. disable_z();
  4310. disable_e0();
  4311. disable_e1();
  4312. disable_e2();
  4313. }
  4314. }
  4315. }
  4316. #ifdef CHDK //Check if pin should be set to LOW after M240 set it to HIGH
  4317. if (chdkActive && (millis() - chdkHigh > CHDK_DELAY))
  4318. {
  4319. chdkActive = false;
  4320. WRITE(CHDK, LOW);
  4321. }
  4322. #endif
  4323. #if defined(KILL_PIN) && KILL_PIN > -1
  4324. // Check if the kill button was pressed and wait just in case it was an accidental
  4325. // key kill key press
  4326. // -------------------------------------------------------------------------------
  4327. if( 0 == READ(KILL_PIN) )
  4328. {
  4329. killCount++;
  4330. }
  4331. else if (killCount > 0)
  4332. {
  4333. killCount--;
  4334. }
  4335. // Exceeded threshold and we can confirm that it was not accidental
  4336. // KILL the machine
  4337. // ----------------------------------------------------------------
  4338. if ( killCount >= KILL_DELAY)
  4339. {
  4340. kill();
  4341. }
  4342. #endif
  4343. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  4344. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  4345. #endif
  4346. #ifdef EXTRUDER_RUNOUT_PREVENT
  4347. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  4348. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  4349. {
  4350. bool oldstatus=READ(E0_ENABLE_PIN);
  4351. enable_e0();
  4352. float oldepos=current_position[E_AXIS];
  4353. float oldedes=destination[E_AXIS];
  4354. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS],
  4355. destination[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  4356. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  4357. current_position[E_AXIS]=oldepos;
  4358. destination[E_AXIS]=oldedes;
  4359. plan_set_e_position(oldepos);
  4360. previous_millis_cmd=millis();
  4361. st_synchronize();
  4362. WRITE(E0_ENABLE_PIN,oldstatus);
  4363. }
  4364. #endif
  4365. #if defined(DUAL_X_CARRIAGE)
  4366. // handle delayed move timeout
  4367. if (delayed_move_time != 0 && (millis() - delayed_move_time) > 1000 && Stopped == false)
  4368. {
  4369. // travel moves have been received so enact them
  4370. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  4371. memcpy(destination,current_position,sizeof(destination));
  4372. prepare_move();
  4373. }
  4374. #endif
  4375. #ifdef TEMP_STAT_LEDS
  4376. handle_status_leds();
  4377. #endif
  4378. check_axes_activity();
  4379. }
  4380. void kill(const char *full_screen_message)
  4381. {
  4382. cli(); // Stop interrupts
  4383. disable_heater();
  4384. disable_x();
  4385. // SERIAL_ECHOLNPGM("kill - disable Y");
  4386. disable_y();
  4387. disable_z();
  4388. disable_e0();
  4389. disable_e1();
  4390. disable_e2();
  4391. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  4392. pinMode(PS_ON_PIN,INPUT);
  4393. #endif
  4394. SERIAL_ERROR_START;
  4395. SERIAL_ERRORLNRPGM(MSG_ERR_KILLED);
  4396. if (full_screen_message != NULL) {
  4397. SERIAL_ERRORLNRPGM(full_screen_message);
  4398. lcd_display_message_fullscreen_P(full_screen_message);
  4399. } else {
  4400. LCD_ALERTMESSAGERPGM(MSG_KILLED);
  4401. }
  4402. // FMC small patch to update the LCD before ending
  4403. sei(); // enable interrupts
  4404. for ( int i=5; i--; lcd_update())
  4405. {
  4406. delay(200);
  4407. }
  4408. cli(); // disable interrupts
  4409. suicide();
  4410. while(1) { /* Intentionally left empty */ } // Wait for reset
  4411. }
  4412. void Stop()
  4413. {
  4414. disable_heater();
  4415. if(Stopped == false) {
  4416. Stopped = true;
  4417. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  4418. SERIAL_ERROR_START;
  4419. SERIAL_ERRORLNRPGM(MSG_ERR_STOPPED);
  4420. LCD_MESSAGERPGM(MSG_STOPPED);
  4421. }
  4422. }
  4423. bool IsStopped() { return Stopped; };
  4424. #ifdef FAST_PWM_FAN
  4425. void setPwmFrequency(uint8_t pin, int val)
  4426. {
  4427. val &= 0x07;
  4428. switch(digitalPinToTimer(pin))
  4429. {
  4430. #if defined(TCCR0A)
  4431. case TIMER0A:
  4432. case TIMER0B:
  4433. // TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02));
  4434. // TCCR0B |= val;
  4435. break;
  4436. #endif
  4437. #if defined(TCCR1A)
  4438. case TIMER1A:
  4439. case TIMER1B:
  4440. // TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  4441. // TCCR1B |= val;
  4442. break;
  4443. #endif
  4444. #if defined(TCCR2)
  4445. case TIMER2:
  4446. case TIMER2:
  4447. TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  4448. TCCR2 |= val;
  4449. break;
  4450. #endif
  4451. #if defined(TCCR2A)
  4452. case TIMER2A:
  4453. case TIMER2B:
  4454. TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
  4455. TCCR2B |= val;
  4456. break;
  4457. #endif
  4458. #if defined(TCCR3A)
  4459. case TIMER3A:
  4460. case TIMER3B:
  4461. case TIMER3C:
  4462. TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
  4463. TCCR3B |= val;
  4464. break;
  4465. #endif
  4466. #if defined(TCCR4A)
  4467. case TIMER4A:
  4468. case TIMER4B:
  4469. case TIMER4C:
  4470. TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
  4471. TCCR4B |= val;
  4472. break;
  4473. #endif
  4474. #if defined(TCCR5A)
  4475. case TIMER5A:
  4476. case TIMER5B:
  4477. case TIMER5C:
  4478. TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
  4479. TCCR5B |= val;
  4480. break;
  4481. #endif
  4482. }
  4483. }
  4484. #endif //FAST_PWM_FAN
  4485. bool setTargetedHotend(int code){
  4486. tmp_extruder = active_extruder;
  4487. if(code_seen('T')) {
  4488. tmp_extruder = code_value();
  4489. if(tmp_extruder >= EXTRUDERS) {
  4490. SERIAL_ECHO_START;
  4491. switch(code){
  4492. case 104:
  4493. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  4494. break;
  4495. case 105:
  4496. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  4497. break;
  4498. case 109:
  4499. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  4500. break;
  4501. case 218:
  4502. SERIAL_ECHO(MSG_M218_INVALID_EXTRUDER);
  4503. break;
  4504. case 221:
  4505. SERIAL_ECHO(MSG_M221_INVALID_EXTRUDER);
  4506. break;
  4507. }
  4508. SERIAL_ECHOLN(tmp_extruder);
  4509. return true;
  4510. }
  4511. }
  4512. return false;
  4513. }
  4514. void save_statistics(unsigned long _total_filament_used, unsigned long _total_print_time)
  4515. {
  4516. if (eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 1) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 2) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 3) == 255)
  4517. {
  4518. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
  4519. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
  4520. }
  4521. unsigned long _previous_filament = eeprom_read_dword((uint32_t *)EEPROM_FILAMENTUSED);
  4522. unsigned long _previous_time = eeprom_read_dword((uint32_t *)EEPROM_TOTALTIME);
  4523. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, _previous_time + (_total_print_time/60));
  4524. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, _previous_filament + (_total_filament_used / 1000));
  4525. total_filament_used = 0;
  4526. }
  4527. float calculate_volumetric_multiplier(float diameter) {
  4528. float area = .0;
  4529. float radius = .0;
  4530. radius = diameter * .5;
  4531. if (! volumetric_enabled || radius == 0) {
  4532. area = 1;
  4533. }
  4534. else {
  4535. area = M_PI * pow(radius, 2);
  4536. }
  4537. return 1.0 / area;
  4538. }
  4539. void calculate_volumetric_multipliers() {
  4540. volumetric_multiplier[0] = calculate_volumetric_multiplier(filament_size[0]);
  4541. #if EXTRUDERS > 1
  4542. volumetric_multiplier[1] = calculate_volumetric_multiplier(filament_size[1]);
  4543. #if EXTRUDERS > 2
  4544. volumetric_multiplier[2] = calculate_volumetric_multiplier(filament_size[2]);
  4545. #endif
  4546. #endif
  4547. }
  4548. void delay_keep_alive(int ms)
  4549. {
  4550. for (;;) {
  4551. manage_heater();
  4552. // Manage inactivity, but don't disable steppers on timeout.
  4553. manage_inactivity(true);
  4554. lcd_update();
  4555. if (ms == 0)
  4556. break;
  4557. else if (ms >= 50) {
  4558. delay(50);
  4559. ms -= 50;
  4560. } else {
  4561. delay(ms);
  4562. ms = 0;
  4563. }
  4564. }
  4565. }