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

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