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