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

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