My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Marlin_main.cpp 133KB

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