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

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