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.pde 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /*
  2. Reprap firmware based on Sprinter and grbl.
  3. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. This firmware is a mashup between Sprinter and grbl.
  17. (https://github.com/kliment/Sprinter)
  18. (https://github.com/simen/grbl/tree)
  19. It has preliminary support for Matthew Roberts advance algorithm
  20. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  21. */
  22. #include <math.h>
  23. #include <EEPROM.h>
  24. #include <stdio.h>
  25. #include "EEPROMwrite.h"
  26. #include "fastio.h"
  27. #include "Configuration.h"
  28. #include "pins.h"
  29. #include "Marlin.h"
  30. #include "ultralcd.h"
  31. #include "planner.h"
  32. #include "stepper.h"
  33. #include "temperature.h"
  34. #include "motion_control.h"
  35. #include "cardreader.h"
  36. #include "watchdog.h"
  37. #define VERSION_STRING "1.0.0 Beta 1"
  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. // G28 - Home all Axis
  48. // G90 - Use Absolute Coordinates
  49. // G91 - Use Relative Coordinates
  50. // G92 - Set current position to cordinates given
  51. //RepRap M Codes
  52. // M104 - Set extruder target temp
  53. // M105 - Read current temp
  54. // M106 - Fan on
  55. // M107 - Fan off
  56. // M109 - Wait for extruder current temp to reach target temp.
  57. // M114 - Display current position
  58. //Custom M Codes
  59. // M17 - Enable/Power all stepper motors
  60. // M18 - Disable all stepper motors; same as M84
  61. // M20 - List SD card
  62. // M21 - Init SD card
  63. // M22 - Release SD card
  64. // M23 - Select SD file (M23 filename.g)
  65. // M24 - Start/resume SD print
  66. // M25 - Pause SD print
  67. // M26 - Set SD position in bytes (M26 S12345)
  68. // M27 - Report SD print status
  69. // M28 - Start SD write (M28 filename.g)
  70. // M29 - Stop SD write
  71. // M30 - Output time since last M109 or SD card start to serial
  72. // M42 - Change pin status via gcode
  73. // M80 - Turn on Power Supply
  74. // M81 - Turn off Power Supply
  75. // M82 - Set E codes absolute (default)
  76. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  77. // M84 - Disable steppers until next move,
  78. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  79. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  80. // M92 - Set axis_steps_per_unit - same syntax as G92
  81. // M114 - Output current position to serial port
  82. // M115 - Capabilities string
  83. // M117 - display message
  84. // M119 - Output Endstop status to serial port
  85. // M140 - Set bed target temp
  86. // M190 - Wait for bed current temp to reach target temp.
  87. // M200 - Set filament diameter
  88. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  89. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  90. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  91. // 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
  92. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  93. // M206 - set additional homeing offset
  94. // M220 - set speed factor override percentage S:factor in percent
  95. // M301 - Set PID parameters P I and D
  96. // M400 - Finish all moves
  97. // M500 - stores paramters in EEPROM
  98. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  99. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  100. // M503 - print the current settings (from memory not from eeprom)
  101. //Stepper Movement Variables
  102. //===========================================================================
  103. //=============================imported variables============================
  104. //===========================================================================
  105. extern float HeaterPower;
  106. //===========================================================================
  107. //=============================public variables=============================
  108. //===========================================================================
  109. #ifdef SDSUPPORT
  110. CardReader card;
  111. #endif
  112. float homing_feedrate[] = HOMING_FEEDRATE;
  113. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  114. volatile int feedmultiply=100; //100->1 200->2
  115. int saved_feedmultiply;
  116. volatile bool feedmultiplychanged=false;
  117. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  118. float add_homeing[3]={0,0,0};
  119. //===========================================================================
  120. //=============================private variables=============================
  121. //===========================================================================
  122. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  123. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  124. static float offset[3] = {0.0, 0.0, 0.0};
  125. static bool home_all_axis = true;
  126. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  127. static long gcode_N, gcode_LastN;
  128. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  129. static bool relative_mode_e = false; //Determines Absolute or Relative E Codes while in Absolute Coordinates mode. E is always relative in Relative Coordinates mode.
  130. static uint8_t fanpwm=0;
  131. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  132. static bool fromsd[BUFSIZE];
  133. static int bufindr = 0;
  134. static int bufindw = 0;
  135. static int buflen = 0;
  136. static int i = 0;
  137. static char serial_char;
  138. static int serial_count = 0;
  139. static boolean comment_mode = false;
  140. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  141. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  142. //static float tt = 0;
  143. //static float bt = 0;
  144. //Inactivity shutdown variables
  145. static unsigned long previous_millis_cmd = 0;
  146. static unsigned long max_inactive_time = 0;
  147. static unsigned long stepper_inactive_time = 0;
  148. static unsigned long starttime=0;
  149. static unsigned long stoptime=0;
  150. static uint8_t tmp_extruder;
  151. //===========================================================================
  152. //=============================ROUTINES=============================
  153. //===========================================================================
  154. void get_arc_coordinates();
  155. extern "C"{
  156. extern unsigned int __bss_end;
  157. extern unsigned int __heap_start;
  158. extern void *__brkval;
  159. int freeMemory() {
  160. int free_memory;
  161. if((int)__brkval == 0)
  162. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  163. else
  164. free_memory = ((int)&free_memory) - ((int)__brkval);
  165. return free_memory;
  166. }
  167. }
  168. //adds an command to the main command buffer
  169. //thats really done in a non-safe way.
  170. //needs overworking someday
  171. void enquecommand(const char *cmd)
  172. {
  173. if(buflen < BUFSIZE)
  174. {
  175. //this is dangerous if a mixing of serial and this happsens
  176. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  177. SERIAL_ECHO_START;
  178. SERIAL_ECHOPGM("enqueing \"");
  179. SERIAL_ECHO(cmdbuffer[bufindw]);
  180. SERIAL_ECHOLNPGM("\"");
  181. bufindw= (bufindw + 1)%BUFSIZE;
  182. buflen += 1;
  183. }
  184. }
  185. void setup()
  186. {
  187. MSerial.begin(BAUDRATE);
  188. SERIAL_ECHO_START;
  189. SERIAL_ECHOLNPGM(VERSION_STRING);
  190. SERIAL_PROTOCOLLNPGM("start");
  191. SERIAL_ECHO_START;
  192. SERIAL_ECHOPGM("Free Memory:");
  193. SERIAL_ECHOLN(freeMemory());
  194. for(int8_t i = 0; i < BUFSIZE; i++)
  195. {
  196. fromsd[i] = false;
  197. }
  198. EEPROM_RetrieveSettings(); // loads data from EEPROM if available
  199. for(int8_t i=0; i < NUM_AXIS; i++)
  200. {
  201. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  202. }
  203. tp_init(); // Initialize temperature loop
  204. plan_init(); // Initialize planner;
  205. st_init(); // Initialize stepper;
  206. wd_init();
  207. }
  208. void loop()
  209. {
  210. if(buflen<3)
  211. get_command();
  212. #ifdef SDSUPPORT
  213. card.checkautostart(false);
  214. #endif
  215. if(buflen)
  216. {
  217. #ifdef SDSUPPORT
  218. if(card.saving)
  219. {
  220. if(strstr(cmdbuffer[bufindr],"M29") == NULL)
  221. {
  222. card.write_command(cmdbuffer[bufindr]);
  223. SERIAL_PROTOCOLLNPGM("ok");
  224. }
  225. else
  226. {
  227. card.closefile();
  228. SERIAL_PROTOCOLLNPGM("Done saving file.");
  229. }
  230. }
  231. else
  232. {
  233. process_commands();
  234. }
  235. #else
  236. process_commands();
  237. #endif //SDSUPPORT
  238. buflen = (buflen-1);
  239. bufindr = (bufindr + 1)%BUFSIZE;
  240. }
  241. //check heater every n milliseconds
  242. manage_heater();
  243. manage_inactivity(1);
  244. checkHitEndstops();
  245. LCD_STATUS;
  246. }
  247. FORCE_INLINE void get_command()
  248. {
  249. while( MSerial.available() > 0 && buflen < BUFSIZE) {
  250. serial_char = MSerial.read();
  251. if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) )
  252. {
  253. if(!serial_count) return; //if empty line
  254. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  255. if(!comment_mode){
  256. fromsd[bufindw] = false;
  257. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  258. {
  259. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  260. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  261. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  262. SERIAL_ERROR_START;
  263. SERIAL_ERRORPGM("Line Number is not Last Line Number+1, Last Line:");
  264. SERIAL_ERRORLN(gcode_LastN);
  265. //Serial.println(gcode_N);
  266. FlushSerialRequestResend();
  267. serial_count = 0;
  268. return;
  269. }
  270. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  271. {
  272. byte checksum = 0;
  273. byte count = 0;
  274. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  275. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  276. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  277. SERIAL_ERROR_START;
  278. SERIAL_ERRORPGM("checksum mismatch, Last Line:");
  279. SERIAL_ERRORLN(gcode_LastN);
  280. FlushSerialRequestResend();
  281. serial_count = 0;
  282. return;
  283. }
  284. //if no errors, continue parsing
  285. }
  286. else
  287. {
  288. SERIAL_ERROR_START;
  289. SERIAL_ERRORPGM("No Checksum with line number, Last Line:");
  290. SERIAL_ERRORLN(gcode_LastN);
  291. FlushSerialRequestResend();
  292. serial_count = 0;
  293. return;
  294. }
  295. gcode_LastN = gcode_N;
  296. //if no errors, continue parsing
  297. }
  298. else // if we don't receive 'N' but still see '*'
  299. {
  300. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  301. {
  302. SERIAL_ERROR_START;
  303. SERIAL_ERRORPGM("No Line Number with checksum, Last Line:");
  304. SERIAL_ERRORLN(gcode_LastN);
  305. serial_count = 0;
  306. return;
  307. }
  308. }
  309. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  310. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  311. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  312. case 0:
  313. case 1:
  314. case 2:
  315. case 3:
  316. #ifdef SDSUPPORT
  317. if(card.saving)
  318. break;
  319. #endif //SDSUPPORT
  320. SERIAL_PROTOCOLLNPGM("ok");
  321. break;
  322. default:
  323. break;
  324. }
  325. }
  326. bufindw = (bufindw + 1)%BUFSIZE;
  327. buflen += 1;
  328. }
  329. comment_mode = false; //for new command
  330. serial_count = 0; //clear buffer
  331. }
  332. else
  333. {
  334. if(serial_char == ';') comment_mode = true;
  335. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  336. }
  337. }
  338. #ifdef SDSUPPORT
  339. if(!card.sdprinting || serial_count!=0){
  340. return;
  341. }
  342. while( !card.eof() && buflen < BUFSIZE) {
  343. int16_t n=card.get();
  344. serial_char = (char)n;
  345. if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  346. {
  347. if(card.eof()){
  348. SERIAL_PROTOCOLLNPGM("Done printing file");
  349. stoptime=millis();
  350. char time[30];
  351. unsigned long t=(stoptime-starttime)/1000;
  352. int sec,min;
  353. min=t/60;
  354. sec=t%60;
  355. sprintf(time,"%i min, %i sec",min,sec);
  356. SERIAL_ECHO_START;
  357. SERIAL_ECHOLN(time);
  358. LCD_MESSAGE(time);
  359. card.printingHasFinished();
  360. card.checkautostart(true);
  361. }
  362. if(serial_char=='\n')
  363. comment_mode = false; //for new command
  364. if(!serial_count)
  365. {
  366. return; //if empty line
  367. }
  368. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  369. if(!comment_mode){
  370. fromsd[bufindw] = true;
  371. buflen += 1;
  372. bufindw = (bufindw + 1)%BUFSIZE;
  373. }
  374. serial_count = 0; //clear buffer
  375. }
  376. else
  377. {
  378. if(serial_char == ';') comment_mode = true;
  379. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  380. }
  381. }
  382. #endif //SDSUPPORT
  383. }
  384. FORCE_INLINE float code_value()
  385. {
  386. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  387. }
  388. FORCE_INLINE long code_value_long()
  389. {
  390. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  391. }
  392. FORCE_INLINE bool code_seen(char code_string[]) //Return True if the string was found
  393. {
  394. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  395. }
  396. FORCE_INLINE bool code_seen(char code)
  397. {
  398. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  399. return (strchr_pointer != NULL); //Return True if a character was found
  400. }
  401. #define HOMEAXIS(LETTER) \
  402. if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
  403. { \
  404. current_position[LETTER##_AXIS] = 0; \
  405. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); \
  406. destination[LETTER##_AXIS] = 1.5 * LETTER##_MAX_LENGTH * LETTER##_HOME_DIR; \
  407. feedrate = homing_feedrate[LETTER##_AXIS]; \
  408. prepare_move(); \
  409. \
  410. current_position[LETTER##_AXIS] = 0;\
  411. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  412. destination[LETTER##_AXIS] = -LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  413. prepare_move(); \
  414. \
  415. destination[LETTER##_AXIS] = 2*LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  416. feedrate = homing_feedrate[LETTER##_AXIS]/2 ; \
  417. prepare_move(); \
  418. \
  419. current_position[LETTER##_AXIS] = (LETTER##_HOME_DIR == -1) ? 0 : LETTER##_MAX_LENGTH;\
  420. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  421. destination[LETTER##_AXIS] = current_position[LETTER##_AXIS];\
  422. feedrate = 0.0;\
  423. st_synchronize();\
  424. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  425. endstops_hit_on_purpose();\
  426. }
  427. FORCE_INLINE void process_commands()
  428. {
  429. unsigned long codenum; //throw away variable
  430. char *starpos = NULL;
  431. if(code_seen('G'))
  432. {
  433. switch((int)code_value())
  434. {
  435. case 0: // G0 -> G1
  436. case 1: // G1
  437. get_coordinates(); // For X Y Z E F
  438. prepare_move();
  439. //ClearToSend();
  440. return;
  441. //break;
  442. case 2: // G2 - CW ARC
  443. get_arc_coordinates();
  444. prepare_arc_move(true);
  445. return;
  446. case 3: // G3 - CCW ARC
  447. get_arc_coordinates();
  448. prepare_arc_move(false);
  449. return;
  450. case 4: // G4 dwell
  451. LCD_MESSAGEPGM("DWELL...");
  452. codenum = 0;
  453. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  454. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  455. st_synchronize();
  456. codenum += millis(); // keep track of when we started waiting
  457. previous_millis_cmd = millis();
  458. while(millis() < codenum ){
  459. manage_heater();
  460. }
  461. break;
  462. case 28: //G28 Home all Axis one at a time
  463. saved_feedrate = feedrate;
  464. saved_feedmultiply = feedmultiply;
  465. feedmultiply = 100;
  466. enable_endstops(true);
  467. for(int8_t i=0; i < NUM_AXIS; i++) {
  468. destination[i] = current_position[i];
  469. }
  470. feedrate = 0.0;
  471. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  472. #ifdef QUICK_HOME
  473. if( code_seen(axis_codes[0]) && code_seen(axis_codes[1]) ) //first diagonal move
  474. {
  475. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  476. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  477. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  478. feedrate = homing_feedrate[X_AXIS];
  479. if(homing_feedrate[Y_AXIS]<feedrate)
  480. feedrate =homing_feedrate[Y_AXIS];
  481. prepare_move();
  482. current_position[X_AXIS] = (X_HOME_DIR == -1) ? 0 : X_MAX_LENGTH;
  483. current_position[Y_AXIS] = (Y_HOME_DIR == -1) ? 0 : Y_MAX_LENGTH;
  484. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  485. destination[X_AXIS] = current_position[X_AXIS];
  486. destination[Y_AXIS] = current_position[Y_AXIS];
  487. feedrate = 0.0;
  488. st_synchronize();
  489. plan_set_position(0, 0, current_position[Z_AXIS], current_position[E_AXIS]);
  490. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  491. endstops_hit_on_purpose();
  492. }
  493. #endif
  494. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  495. {
  496. HOMEAXIS(X);
  497. }
  498. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  499. HOMEAXIS(Y);
  500. }
  501. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  502. HOMEAXIS(Z);
  503. }
  504. if(code_seen(axis_codes[X_AXIS]))
  505. {
  506. current_position[0]=code_value()+add_homeing[0];
  507. }
  508. if(code_seen(axis_codes[Y_AXIS])) {
  509. current_position[1]=code_value()+add_homeing[1];
  510. }
  511. if(code_seen(axis_codes[Z_AXIS])) {
  512. current_position[2]=code_value()+add_homeing[2];
  513. }
  514. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  515. enable_endstops(false);
  516. #endif
  517. feedrate = saved_feedrate;
  518. feedmultiply = saved_feedmultiply;
  519. previous_millis_cmd = millis();
  520. endstops_hit_on_purpose();
  521. break;
  522. case 90: // G90
  523. relative_mode = false;
  524. break;
  525. case 91: // G91
  526. relative_mode = true;
  527. break;
  528. case 92: // G92
  529. if(!code_seen(axis_codes[E_AXIS]))
  530. st_synchronize();
  531. for(int8_t i=0; i < NUM_AXIS; i++) {
  532. if(code_seen(axis_codes[i])) {
  533. current_position[i] = code_value()+add_homeing[i];
  534. if(i == E_AXIS) {
  535. plan_set_e_position(current_position[E_AXIS]);
  536. }
  537. else {
  538. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  539. }
  540. }
  541. }
  542. break;
  543. }
  544. }
  545. else if(code_seen('M'))
  546. {
  547. switch( (int)code_value() )
  548. {
  549. case 17:
  550. LCD_MESSAGEPGM("No move.");
  551. enable_x();
  552. enable_y();
  553. enable_z();
  554. enable_e();
  555. break;
  556. #ifdef SDSUPPORT
  557. case 20: // M20 - list SD card
  558. SERIAL_PROTOCOLLNPGM("Begin file list");
  559. card.ls();
  560. SERIAL_PROTOCOLLNPGM("End file list");
  561. break;
  562. case 21: // M21 - init SD card
  563. card.initsd();
  564. break;
  565. case 22: //M22 - release SD card
  566. card.release();
  567. break;
  568. case 23: //M23 - Select file
  569. starpos = (strchr(strchr_pointer + 4,'*'));
  570. if(starpos!=NULL)
  571. *(starpos-1)='\0';
  572. card.openFile(strchr_pointer + 4,true);
  573. break;
  574. case 24: //M24 - Start SD print
  575. card.startFileprint();
  576. starttime=millis();
  577. break;
  578. case 25: //M25 - Pause SD print
  579. card.pauseSDPrint();
  580. break;
  581. case 26: //M26 - Set SD index
  582. if(card.cardOK && code_seen('S')){
  583. card.setIndex(code_value_long());
  584. }
  585. break;
  586. case 27: //M27 - Get SD status
  587. card.getStatus();
  588. break;
  589. case 28: //M28 - Start SD write
  590. starpos = (strchr(strchr_pointer + 4,'*'));
  591. if(starpos != NULL){
  592. char* npos = strchr(cmdbuffer[bufindr], 'N');
  593. strchr_pointer = strchr(npos,' ') + 1;
  594. *(starpos-1) = '\0';
  595. }
  596. card.openFile(strchr_pointer+4,false);
  597. break;
  598. case 29: //M29 - Stop SD write
  599. //processed in write to file routine above
  600. //card,saving = false;
  601. break;
  602. #endif //SDSUPPORT
  603. case 30: //M30 take time since the start of the SD print or an M109 command
  604. {
  605. stoptime=millis();
  606. char time[30];
  607. unsigned long t=(stoptime-starttime)/1000;
  608. int sec,min;
  609. min=t/60;
  610. sec=t%60;
  611. sprintf(time,"%i min, %i sec",min,sec);
  612. SERIAL_ECHO_START;
  613. SERIAL_ECHOLN(time);
  614. LCD_MESSAGE(time);
  615. autotempShutdown();
  616. }
  617. break;
  618. case 42: //M42 -Change pin status via gcode
  619. if (code_seen('S'))
  620. {
  621. int pin_status = code_value();
  622. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  623. {
  624. int pin_number = code_value();
  625. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  626. {
  627. if (sensitive_pins[i] == pin_number)
  628. {
  629. pin_number = -1;
  630. break;
  631. }
  632. }
  633. if (pin_number > -1)
  634. {
  635. pinMode(pin_number, OUTPUT);
  636. digitalWrite(pin_number, pin_status);
  637. analogWrite(pin_number, pin_status);
  638. }
  639. }
  640. }
  641. break;
  642. case 104: // M104
  643. tmp_extruder = active_extruder;
  644. if(code_seen('T')) {
  645. tmp_extruder = code_value();
  646. if(tmp_extruder >= EXTRUDERS) {
  647. SERIAL_ECHO_START;
  648. SERIAL_ECHO("M104 Invalid extruder ");
  649. SERIAL_ECHOLN(tmp_extruder);
  650. break;
  651. }
  652. }
  653. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  654. setWatch();
  655. break;
  656. case 140: // M140 set bed temp
  657. if (code_seen('S')) setTargetBed(code_value());
  658. break;
  659. case 105 : // M105
  660. tmp_extruder = active_extruder;
  661. if(code_seen('T')) {
  662. tmp_extruder = code_value();
  663. if(tmp_extruder >= EXTRUDERS) {
  664. SERIAL_ECHO_START;
  665. SERIAL_ECHO("M105 Invalid extruder ");
  666. SERIAL_ECHOLN(tmp_extruder);
  667. break;
  668. }
  669. }
  670. #if (TEMP_0_PIN > -1) || (TEMP_2_PIN > -1)
  671. SERIAL_PROTOCOLPGM("ok T:");
  672. SERIAL_PROTOCOL( degHotend(tmp_extruder));
  673. #if TEMP_1_PIN > -1
  674. SERIAL_PROTOCOLPGM(" B:");
  675. SERIAL_PROTOCOL(degBed());
  676. #endif //TEMP_1_PIN
  677. #else
  678. SERIAL_ERROR_START;
  679. SERIAL_ERRORLNPGM("No thermistors - no temp");
  680. #endif
  681. #ifdef PIDTEMP
  682. SERIAL_PROTOCOLPGM(" @:");
  683. SERIAL_PROTOCOL( HeaterPower);
  684. #endif
  685. SERIAL_PROTOCOLLN("");
  686. return;
  687. break;
  688. case 109:
  689. {// M109 - Wait for extruder heater to reach target.
  690. tmp_extruder = active_extruder;
  691. if(code_seen('T')) {
  692. tmp_extruder = code_value();
  693. if(tmp_extruder >= EXTRUDERS) {
  694. SERIAL_ECHO_START;
  695. SERIAL_ECHO("M109 Invalid extruder ");
  696. SERIAL_ECHOLN(tmp_extruder);
  697. break;
  698. }
  699. }
  700. LCD_MESSAGEPGM("Heating...");
  701. #ifdef AUTOTEMP
  702. autotemp_enabled=false;
  703. #endif
  704. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  705. #ifdef AUTOTEMP
  706. if (code_seen('S')) autotemp_min=code_value();
  707. if (code_seen('G')) autotemp_max=code_value();
  708. if (code_seen('F'))
  709. {
  710. autotemp_factor=code_value();
  711. autotemp_enabled=true;
  712. }
  713. #endif
  714. setWatch();
  715. codenum = millis();
  716. /* See if we are heating up or cooling down */
  717. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  718. #ifdef TEMP_RESIDENCY_TIME
  719. long residencyStart;
  720. residencyStart = -1;
  721. /* continue to loop until we have reached the target temp
  722. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  723. while((target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder))) ||
  724. (residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) {
  725. #else
  726. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  727. #endif //TEMP_RESIDENCY_TIME
  728. if( (millis() - codenum) > 1000 )
  729. { //Print Temp Reading every 1 second while heating up/cooling down
  730. SERIAL_PROTOCOLPGM("T:");
  731. SERIAL_PROTOCOLLN( degHotend(tmp_extruder) );
  732. codenum = millis();
  733. }
  734. manage_heater();
  735. LCD_STATUS;
  736. #ifdef TEMP_RESIDENCY_TIME
  737. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  738. or when current temp falls outside the hysteresis after target temp was reached */
  739. if ((residencyStart == -1 && target_direction && !isHeatingHotend(tmp_extruder)) ||
  740. (residencyStart == -1 && !target_direction && !isCoolingHotend(tmp_extruder)) ||
  741. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  742. {
  743. residencyStart = millis();
  744. }
  745. #endif //TEMP_RESIDENCY_TIME
  746. }
  747. LCD_MESSAGEPGM("Heating done.");
  748. starttime=millis();
  749. previous_millis_cmd = millis();
  750. }
  751. break;
  752. case 190: // M190 - Wait bed for heater to reach target.
  753. #if TEMP_1_PIN > -1
  754. LCD_MESSAGEPGM("Bed Heating.");
  755. if (code_seen('S')) setTargetBed(code_value());
  756. codenum = millis();
  757. while(isHeatingBed())
  758. {
  759. if( (millis()-codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  760. {
  761. float tt=degHotend0();
  762. SERIAL_PROTOCOLPGM("T:");
  763. SERIAL_PROTOCOLLN(tt );
  764. SERIAL_PROTOCOLPGM("ok T:");
  765. SERIAL_PROTOCOL(tt );
  766. SERIAL_PROTOCOLPGM(" B:");
  767. SERIAL_PROTOCOLLN(degBed() );
  768. codenum = millis();
  769. }
  770. manage_heater();
  771. }
  772. LCD_MESSAGEPGM("Bed done.");
  773. previous_millis_cmd = millis();
  774. #endif
  775. break;
  776. #if FAN_PIN > -1
  777. case 106: //M106 Fan On
  778. if (code_seen('S')){
  779. WRITE(FAN_PIN,HIGH);
  780. fanpwm=constrain(code_value(),0,255);
  781. analogWrite(FAN_PIN, fanpwm);
  782. }
  783. else {
  784. WRITE(FAN_PIN,HIGH);
  785. fanpwm=255;
  786. analogWrite(FAN_PIN, fanpwm);
  787. }
  788. break;
  789. case 107: //M107 Fan Off
  790. WRITE(FAN_PIN,LOW);
  791. analogWrite(FAN_PIN, 0);
  792. break;
  793. #endif //FAN_PIN
  794. #if (PS_ON_PIN > -1)
  795. case 80: // M80 - ATX Power On
  796. SET_OUTPUT(PS_ON_PIN); //GND
  797. break;
  798. case 81: // M81 - ATX Power Off
  799. SET_INPUT(PS_ON_PIN); //Floating
  800. break;
  801. #endif
  802. case 82:
  803. axis_relative_modes[3] = false;
  804. break;
  805. case 83:
  806. axis_relative_modes[3] = true;
  807. break;
  808. case 18: //compatibility
  809. case 84: // M84
  810. if(code_seen('S')){
  811. stepper_inactive_time = code_value() * 1000;
  812. }
  813. else
  814. {
  815. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  816. if(all_axis)
  817. {
  818. finishAndDisableSteppers();
  819. }
  820. else
  821. {
  822. st_synchronize();
  823. if(code_seen('X')) disable_x();
  824. if(code_seen('Y')) disable_y();
  825. if(code_seen('Z')) disable_z();
  826. #if ((E_ENABLE_PIN != X_ENABLE_PIN) && (E_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  827. if(code_seen('E')) disable_e();
  828. #endif
  829. LCD_MESSAGEPGM("Partial Release");
  830. }
  831. }
  832. break;
  833. case 85: // M85
  834. code_seen('S');
  835. max_inactive_time = code_value() * 1000;
  836. break;
  837. case 92: // M92
  838. for(int8_t i=0; i < NUM_AXIS; i++)
  839. {
  840. if(code_seen(axis_codes[i]))
  841. axis_steps_per_unit[i] = code_value();
  842. }
  843. break;
  844. case 115: // M115
  845. SerialprintPGM("FIRMWARE_NAME:Marlin; Sprinter/grbl mashup for gen6 FIRMWARE_URL:http://www.mendel-parts.com PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1");
  846. break;
  847. case 117: // M117 display message
  848. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  849. break;
  850. case 114: // M114
  851. SERIAL_PROTOCOLPGM("X:");
  852. SERIAL_PROTOCOL(current_position[X_AXIS]);
  853. SERIAL_PROTOCOLPGM("Y:");
  854. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  855. SERIAL_PROTOCOLPGM("Z:");
  856. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  857. SERIAL_PROTOCOLPGM("E:");
  858. SERIAL_PROTOCOL(current_position[E_AXIS]);
  859. SERIAL_PROTOCOLPGM(" Count X:");
  860. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  861. SERIAL_PROTOCOLPGM("Y:");
  862. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  863. SERIAL_PROTOCOLPGM("Z:");
  864. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  865. SERIAL_PROTOCOLLN("");
  866. break;
  867. case 119: // M119
  868. #if (X_MIN_PIN > -1)
  869. SERIAL_PROTOCOLPGM("x_min:");
  870. SERIAL_PROTOCOL(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  871. #endif
  872. #if (X_MAX_PIN > -1)
  873. SERIAL_PROTOCOLPGM("x_max:");
  874. SERIAL_PROTOCOL(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  875. #endif
  876. #if (Y_MIN_PIN > -1)
  877. SERIAL_PROTOCOLPGM("y_min:");
  878. SERIAL_PROTOCOL(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  879. #endif
  880. #if (Y_MAX_PIN > -1)
  881. SERIAL_PROTOCOLPGM("y_max:");
  882. SERIAL_PROTOCOL(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  883. #endif
  884. #if (Z_MIN_PIN > -1)
  885. SERIAL_PROTOCOLPGM("z_min:");
  886. SERIAL_PROTOCOL(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  887. #endif
  888. #if (Z_MAX_PIN > -1)
  889. SERIAL_PROTOCOLPGM("z_max:");
  890. SERIAL_PROTOCOL(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  891. #endif
  892. SERIAL_PROTOCOLLN("");
  893. break;
  894. //TODO: update for all axis, use for loop
  895. case 201: // M201
  896. for(int8_t i=0; i < NUM_AXIS; i++)
  897. {
  898. if(code_seen(axis_codes[i])) axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  899. }
  900. break;
  901. #if 0 // Not used for Sprinter/grbl gen6
  902. case 202: // M202
  903. for(int8_t i=0; i < NUM_AXIS; i++) {
  904. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  905. }
  906. break;
  907. #endif
  908. case 203: // M203 max feedrate mm/sec
  909. for(int8_t i=0; i < NUM_AXIS; i++) {
  910. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  911. }
  912. break;
  913. case 204: // M204 acclereration S normal moves T filmanent only moves
  914. {
  915. if(code_seen('S')) acceleration = code_value() ;
  916. if(code_seen('T')) retract_acceleration = code_value() ;
  917. }
  918. break;
  919. 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
  920. {
  921. if(code_seen('S')) minimumfeedrate = code_value();
  922. if(code_seen('T')) mintravelfeedrate = code_value();
  923. if(code_seen('B')) minsegmenttime = code_value() ;
  924. if(code_seen('X')) max_xy_jerk = code_value() ;
  925. if(code_seen('Z')) max_z_jerk = code_value() ;
  926. }
  927. break;
  928. case 206: // M206 additional homeing offset
  929. for(int8_t i=0; i < 3; i++)
  930. {
  931. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  932. }
  933. break;
  934. case 220: // M220 S<factor in percent>- set speed factor override percentage
  935. {
  936. if(code_seen('S'))
  937. {
  938. feedmultiply = code_value() ;
  939. feedmultiplychanged=true;
  940. }
  941. }
  942. break;
  943. #ifdef PIDTEMP
  944. case 301: // M301
  945. {
  946. if(code_seen('P')) Kp = code_value();
  947. if(code_seen('I')) Ki = code_value()*PID_dT;
  948. if(code_seen('D')) Kd = code_value()/PID_dT;
  949. #ifdef PID_ADD_EXTRUSION_RATE
  950. if(code_seen('C')) Kc = code_value();
  951. #endif
  952. updatePID();
  953. SERIAL_PROTOCOL("ok p:");
  954. SERIAL_PROTOCOL(Kp);
  955. SERIAL_PROTOCOL(" i:");
  956. SERIAL_PROTOCOL(Ki/PID_dT);
  957. SERIAL_PROTOCOL(" d:");
  958. SERIAL_PROTOCOL(Kd*PID_dT);
  959. #ifdef PID_ADD_EXTRUSION_RATE
  960. SERIAL_PROTOCOL(" c:");
  961. SERIAL_PROTOCOL(Kc*PID_dT);
  962. #endif
  963. SERIAL_PROTOCOLLN("");
  964. }
  965. break;
  966. #endif //PIDTEMP
  967. case 400: // finish all moves
  968. {
  969. st_synchronize();
  970. }
  971. break;
  972. case 500: // Store settings in EEPROM
  973. {
  974. EEPROM_StoreSettings();
  975. }
  976. break;
  977. case 501: // Read settings from EEPROM
  978. {
  979. EEPROM_RetrieveSettings();
  980. }
  981. break;
  982. case 502: // Revert to default settings
  983. {
  984. EEPROM_RetrieveSettings(true);
  985. }
  986. break;
  987. case 503: // print settings currently in memory
  988. {
  989. EEPROM_printSettings();
  990. }
  991. break;
  992. }
  993. }
  994. else if(code_seen('T')) {
  995. tmp_extruder = code_value();
  996. if(tmp_extruder >= EXTRUDERS) {
  997. SERIAL_ECHO_START;
  998. SERIAL_ECHO("T");
  999. SERIAL_ECHO(tmp_extruder);
  1000. SERIAL_ECHOLN("Invalid extruder");
  1001. }
  1002. else {
  1003. active_extruder = tmp_extruder;
  1004. }
  1005. }
  1006. else
  1007. {
  1008. SERIAL_ECHO_START;
  1009. SERIAL_ECHOPGM("Unknown command:\"");
  1010. SERIAL_ECHO(cmdbuffer[bufindr]);
  1011. SERIAL_ECHOLNPGM("\"");
  1012. }
  1013. ClearToSend();
  1014. }
  1015. void FlushSerialRequestResend()
  1016. {
  1017. //char cmdbuffer[bufindr][100]="Resend:";
  1018. MSerial.flush();
  1019. SERIAL_PROTOCOLPGM("Resend:");
  1020. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1021. ClearToSend();
  1022. }
  1023. void ClearToSend()
  1024. {
  1025. previous_millis_cmd = millis();
  1026. #ifdef SDSUPPORT
  1027. if(fromsd[bufindr])
  1028. return;
  1029. #endif //SDSUPPORT
  1030. SERIAL_PROTOCOLLNPGM("ok");
  1031. }
  1032. FORCE_INLINE void get_coordinates()
  1033. {
  1034. for(int8_t i=0; i < NUM_AXIS; i++) {
  1035. if(code_seen(axis_codes[i])) destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1036. else destination[i] = current_position[i]; //Are these else lines really needed?
  1037. }
  1038. if(code_seen('F')) {
  1039. next_feedrate = code_value();
  1040. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1041. }
  1042. }
  1043. FORCE_INLINE void get_arc_coordinates()
  1044. {
  1045. get_coordinates();
  1046. if(code_seen('I')) offset[0] = code_value();
  1047. if(code_seen('J')) offset[1] = code_value();
  1048. }
  1049. void prepare_move()
  1050. {
  1051. if (min_software_endstops) {
  1052. if (destination[X_AXIS] < 0) destination[X_AXIS] = 0.0;
  1053. if (destination[Y_AXIS] < 0) destination[Y_AXIS] = 0.0;
  1054. if (destination[Z_AXIS] < 0) destination[Z_AXIS] = 0.0;
  1055. }
  1056. if (max_software_endstops) {
  1057. if (destination[X_AXIS] > X_MAX_LENGTH) destination[X_AXIS] = X_MAX_LENGTH;
  1058. if (destination[Y_AXIS] > Y_MAX_LENGTH) destination[Y_AXIS] = Y_MAX_LENGTH;
  1059. if (destination[Z_AXIS] > Z_MAX_LENGTH) destination[Z_AXIS] = Z_MAX_LENGTH;
  1060. }
  1061. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1062. for(int8_t i=0; i < NUM_AXIS; i++) {
  1063. current_position[i] = destination[i];
  1064. }
  1065. previous_millis_cmd = millis();
  1066. }
  1067. void prepare_arc_move(char isclockwise) {
  1068. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1069. // Trace the arc
  1070. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1071. // As far as the parser is concerned, the position is now == target. In reality the
  1072. // motion control system might still be processing the action and the real tool position
  1073. // in any intermediate location.
  1074. for(int8_t i=0; i < NUM_AXIS; i++) {
  1075. current_position[i] = destination[i];
  1076. }
  1077. previous_millis_cmd = millis();
  1078. }
  1079. void manage_inactivity(byte debug)
  1080. {
  1081. if( (millis()-previous_millis_cmd) > max_inactive_time )
  1082. if(max_inactive_time)
  1083. kill();
  1084. if( (millis()-previous_millis_cmd) > stepper_inactive_time )
  1085. if(stepper_inactive_time)
  1086. {
  1087. disable_x();
  1088. disable_y();
  1089. disable_z();
  1090. disable_e();
  1091. }
  1092. #ifdef EXTRUDER_RUNOUT_PREVENT
  1093. if( (millis()-previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1094. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1095. {
  1096. enable_e();
  1097. float oldepos=current_position[E_AXIS];
  1098. float oldedes=destination[E_AXIS];
  1099. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1100. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1101. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1102. current_position[E_AXIS]=oldepos;
  1103. destination[E_AXIS]=oldedes;
  1104. plan_set_e_position(oldepos);
  1105. previous_millis_cmd=millis();
  1106. }
  1107. #endif
  1108. check_axes_activity();
  1109. }
  1110. void kill()
  1111. {
  1112. disable_heater();
  1113. disable_x();
  1114. disable_y();
  1115. disable_z();
  1116. disable_e();
  1117. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1118. SERIAL_ERROR_START;
  1119. SERIAL_ERRORLNPGM("Printer halted. kill() called !!");
  1120. LCD_MESSAGEPGM("KILLED. ");
  1121. while(1); // Wait for reset
  1122. }