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

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