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

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