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

Marlin_main.cpp 112KB

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