My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin_main.cpp 125KB

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