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

Marlin_main.cpp 121KB

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