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

Marlin_main.cpp 151KB

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