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

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