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 133KB

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