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

Marlin_main.cpp 121KB

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