My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Marlin_main.cpp 151KB

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