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

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