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

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