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

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