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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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. }
  514. break;
  515. case 28: //G28 Home all Axis one at a time
  516. saved_feedrate = feedrate;
  517. saved_feedmultiply = feedmultiply;
  518. feedmultiply = 100;
  519. previous_millis_cmd = millis();
  520. enable_endstops(true);
  521. for(int8_t i=0; i < NUM_AXIS; i++) {
  522. destination[i] = current_position[i];
  523. }
  524. feedrate = 0.0;
  525. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  526. #ifdef QUICK_HOME
  527. if( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS]) ) //first diagonal move
  528. {
  529. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  530. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  531. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  532. feedrate = homing_feedrate[X_AXIS];
  533. if(homing_feedrate[Y_AXIS]<feedrate)
  534. feedrate =homing_feedrate[Y_AXIS];
  535. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  536. st_synchronize();
  537. current_position[X_AXIS] = (X_HOME_DIR == -1) ? X_HOME_POS : X_MAX_LENGTH;
  538. current_position[Y_AXIS] = (Y_HOME_DIR == -1) ? Y_HOME_POS : Y_MAX_LENGTH;
  539. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  540. destination[X_AXIS] = current_position[X_AXIS];
  541. destination[Y_AXIS] = current_position[Y_AXIS];
  542. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  543. feedrate = 0.0;
  544. st_synchronize();
  545. endstops_hit_on_purpose();
  546. }
  547. #endif
  548. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  549. {
  550. HOMEAXIS(X);
  551. }
  552. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  553. HOMEAXIS(Y);
  554. }
  555. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  556. HOMEAXIS(Z);
  557. }
  558. if(code_seen(axis_codes[X_AXIS]))
  559. {
  560. if(code_value_long() != 0) {
  561. current_position[X_AXIS]=code_value()+add_homeing[0];
  562. }
  563. }
  564. if(code_seen(axis_codes[Y_AXIS])) {
  565. if(code_value_long() != 0) {
  566. current_position[Y_AXIS]=code_value()+add_homeing[1];
  567. }
  568. }
  569. if(code_seen(axis_codes[Z_AXIS])) {
  570. if(code_value_long() != 0) {
  571. current_position[Z_AXIS]=code_value()+add_homeing[2];
  572. }
  573. }
  574. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  575. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  576. enable_endstops(false);
  577. #endif
  578. feedrate = saved_feedrate;
  579. feedmultiply = saved_feedmultiply;
  580. previous_millis_cmd = millis();
  581. endstops_hit_on_purpose();
  582. break;
  583. case 90: // G90
  584. relative_mode = false;
  585. break;
  586. case 91: // G91
  587. relative_mode = true;
  588. break;
  589. case 92: // G92
  590. if(!code_seen(axis_codes[E_AXIS]))
  591. st_synchronize();
  592. for(int8_t i=0; i < NUM_AXIS; i++) {
  593. if(code_seen(axis_codes[i])) {
  594. current_position[i] = code_value()+add_homeing[i];
  595. if(i == E_AXIS) {
  596. current_position[i] = code_value();
  597. plan_set_e_position(current_position[E_AXIS]);
  598. }
  599. else {
  600. current_position[i] = code_value()+add_homeing[i];
  601. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  602. }
  603. }
  604. }
  605. break;
  606. }
  607. }
  608. else if(code_seen('M'))
  609. {
  610. switch( (int)code_value() )
  611. {
  612. case 17:
  613. LCD_MESSAGEPGM(MSG_NO_MOVE);
  614. enable_x();
  615. enable_y();
  616. enable_z();
  617. enable_e0();
  618. enable_e1();
  619. enable_e2();
  620. break;
  621. #ifdef SDSUPPORT
  622. case 20: // M20 - list SD card
  623. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  624. card.ls();
  625. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  626. break;
  627. case 21: // M21 - init SD card
  628. card.initsd();
  629. break;
  630. case 22: //M22 - release SD card
  631. card.release();
  632. break;
  633. case 23: //M23 - Select file
  634. starpos = (strchr(strchr_pointer + 4,'*'));
  635. if(starpos!=NULL)
  636. *(starpos-1)='\0';
  637. card.openFile(strchr_pointer + 4,true);
  638. break;
  639. case 24: //M24 - Start SD print
  640. card.startFileprint();
  641. starttime=millis();
  642. break;
  643. case 25: //M25 - Pause SD print
  644. card.pauseSDPrint();
  645. break;
  646. case 26: //M26 - Set SD index
  647. if(card.cardOK && code_seen('S')) {
  648. card.setIndex(code_value_long());
  649. }
  650. break;
  651. case 27: //M27 - Get SD status
  652. card.getStatus();
  653. break;
  654. case 28: //M28 - Start SD write
  655. starpos = (strchr(strchr_pointer + 4,'*'));
  656. if(starpos != NULL){
  657. char* npos = strchr(cmdbuffer[bufindr], 'N');
  658. strchr_pointer = strchr(npos,' ') + 1;
  659. *(starpos-1) = '\0';
  660. }
  661. card.openFile(strchr_pointer+4,false);
  662. break;
  663. case 29: //M29 - Stop SD write
  664. //processed in write to file routine above
  665. //card,saving = false;
  666. break;
  667. case 30: //M30 <filename> Delete File
  668. if (card.cardOK){
  669. card.closefile();
  670. starpos = (strchr(strchr_pointer + 4,'*'));
  671. if(starpos != NULL){
  672. char* npos = strchr(cmdbuffer[bufindr], 'N');
  673. strchr_pointer = strchr(npos,' ') + 1;
  674. *(starpos-1) = '\0';
  675. }
  676. card.removeFile(strchr_pointer + 4);
  677. }
  678. break;
  679. #endif //SDSUPPORT
  680. case 31: //M31 take time since the start of the SD print or an M109 command
  681. {
  682. stoptime=millis();
  683. char time[30];
  684. unsigned long t=(stoptime-starttime)/1000;
  685. int sec,min;
  686. min=t/60;
  687. sec=t%60;
  688. sprintf(time,"%i min, %i sec",min,sec);
  689. SERIAL_ECHO_START;
  690. SERIAL_ECHOLN(time);
  691. LCD_MESSAGE(time);
  692. autotempShutdown();
  693. }
  694. break;
  695. case 42: //M42 -Change pin status via gcode
  696. if (code_seen('S'))
  697. {
  698. int pin_status = code_value();
  699. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  700. {
  701. int pin_number = code_value();
  702. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  703. {
  704. if (sensitive_pins[i] == pin_number)
  705. {
  706. pin_number = -1;
  707. break;
  708. }
  709. }
  710. if (pin_number > -1)
  711. {
  712. pinMode(pin_number, OUTPUT);
  713. digitalWrite(pin_number, pin_status);
  714. analogWrite(pin_number, pin_status);
  715. }
  716. }
  717. }
  718. break;
  719. case 104: // M104
  720. tmp_extruder = active_extruder;
  721. if(code_seen('T')) {
  722. tmp_extruder = code_value();
  723. if(tmp_extruder >= EXTRUDERS) {
  724. SERIAL_ECHO_START;
  725. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  726. SERIAL_ECHOLN(tmp_extruder);
  727. break;
  728. }
  729. }
  730. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  731. setWatch();
  732. break;
  733. case 140: // M140 set bed temp
  734. if (code_seen('S')) setTargetBed(code_value());
  735. break;
  736. case 105 : // M105
  737. tmp_extruder = active_extruder;
  738. if(code_seen('T')) {
  739. tmp_extruder = code_value();
  740. if(tmp_extruder >= EXTRUDERS) {
  741. SERIAL_ECHO_START;
  742. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  743. SERIAL_ECHOLN(tmp_extruder);
  744. break;
  745. }
  746. }
  747. #if (TEMP_0_PIN > -1)
  748. SERIAL_PROTOCOLPGM("ok T:");
  749. SERIAL_PROTOCOL(degHotend(tmp_extruder));
  750. #if TEMP_BED_PIN > -1
  751. SERIAL_PROTOCOLPGM(" B:");
  752. SERIAL_PROTOCOL(degBed());
  753. #endif //TEMP_BED_PIN
  754. #else
  755. SERIAL_ERROR_START;
  756. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  757. #endif
  758. #ifdef PIDTEMP
  759. SERIAL_PROTOCOLPGM(" @:");
  760. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  761. #endif
  762. SERIAL_PROTOCOLLN("");
  763. return;
  764. break;
  765. case 109:
  766. {// M109 - Wait for extruder heater to reach target.
  767. tmp_extruder = active_extruder;
  768. if(code_seen('T')) {
  769. tmp_extruder = code_value();
  770. if(tmp_extruder >= EXTRUDERS) {
  771. SERIAL_ECHO_START;
  772. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  773. SERIAL_ECHOLN(tmp_extruder);
  774. break;
  775. }
  776. }
  777. LCD_MESSAGEPGM(MSG_HEATING);
  778. #ifdef AUTOTEMP
  779. autotemp_enabled=false;
  780. #endif
  781. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  782. #ifdef AUTOTEMP
  783. if (code_seen('S')) autotemp_min=code_value();
  784. if (code_seen('G')) autotemp_max=code_value();
  785. if (code_seen('F'))
  786. {
  787. autotemp_factor=code_value();
  788. autotemp_enabled=true;
  789. }
  790. #endif
  791. setWatch();
  792. codenum = millis();
  793. /* See if we are heating up or cooling down */
  794. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  795. #ifdef TEMP_RESIDENCY_TIME
  796. long residencyStart;
  797. residencyStart = -1;
  798. /* continue to loop until we have reached the target temp
  799. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  800. while((residencyStart == -1) ||
  801. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  802. #else
  803. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  804. #endif //TEMP_RESIDENCY_TIME
  805. if( (millis() - codenum) > 1000UL )
  806. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  807. SERIAL_PROTOCOLPGM("T:");
  808. SERIAL_PROTOCOL( degHotend(tmp_extruder) );
  809. SERIAL_PROTOCOLPGM(" E:");
  810. SERIAL_PROTOCOL( (int)tmp_extruder );
  811. #ifdef TEMP_RESIDENCY_TIME
  812. SERIAL_PROTOCOLPGM(" W:");
  813. if(residencyStart > -1)
  814. {
  815. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  816. SERIAL_PROTOCOLLN( codenum );
  817. }
  818. else
  819. {
  820. SERIAL_PROTOCOLLN( "?" );
  821. }
  822. #else
  823. SERIAL_PROTOCOLLN("");
  824. #endif
  825. codenum = millis();
  826. }
  827. manage_heater();
  828. LCD_STATUS;
  829. #ifdef TEMP_RESIDENCY_TIME
  830. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  831. or when current temp falls outside the hysteresis after target temp was reached */
  832. if ((residencyStart == -1 && target_direction && !isHeatingHotend(tmp_extruder)) ||
  833. (residencyStart == -1 && !target_direction && !isCoolingHotend(tmp_extruder)) ||
  834. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  835. {
  836. residencyStart = millis();
  837. }
  838. #endif //TEMP_RESIDENCY_TIME
  839. }
  840. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  841. starttime=millis();
  842. previous_millis_cmd = millis();
  843. }
  844. break;
  845. case 190: // M190 - Wait for bed heater to reach target.
  846. #if TEMP_BED_PIN > -1
  847. LCD_MESSAGEPGM(MSG_BED_HEATING);
  848. if (code_seen('S')) setTargetBed(code_value());
  849. codenum = millis();
  850. while(isHeatingBed())
  851. {
  852. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  853. {
  854. float tt=degHotend(active_extruder);
  855. SERIAL_PROTOCOLPGM("T:");
  856. SERIAL_PROTOCOL(tt);
  857. SERIAL_PROTOCOLPGM(" E:");
  858. SERIAL_PROTOCOL( (int)active_extruder );
  859. SERIAL_PROTOCOLPGM(" B:");
  860. SERIAL_PROTOCOLLN(degBed());
  861. codenum = millis();
  862. }
  863. manage_heater();
  864. LCD_STATUS;
  865. }
  866. LCD_MESSAGEPGM(MSG_BED_DONE);
  867. previous_millis_cmd = millis();
  868. #endif
  869. break;
  870. #if FAN_PIN > -1
  871. case 106: //M106 Fan On
  872. if (code_seen('S')){
  873. FanSpeed=constrain(code_value(),0,255);
  874. }
  875. else {
  876. FanSpeed=255;
  877. }
  878. break;
  879. case 107: //M107 Fan Off
  880. FanSpeed = 0;
  881. break;
  882. #endif //FAN_PIN
  883. #if (PS_ON_PIN > -1)
  884. case 80: // M80 - ATX Power On
  885. SET_OUTPUT(PS_ON_PIN); //GND
  886. break;
  887. #endif
  888. case 81: // M81 - ATX Power Off
  889. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  890. st_synchronize();
  891. suicide();
  892. #elif (PS_ON_PIN > -1)
  893. SET_INPUT(PS_ON_PIN); //Floating
  894. #endif
  895. break;
  896. case 82:
  897. axis_relative_modes[3] = false;
  898. break;
  899. case 83:
  900. axis_relative_modes[3] = true;
  901. break;
  902. case 18: //compatibility
  903. case 84: // M84
  904. if(code_seen('S')){
  905. stepper_inactive_time = code_value() * 1000;
  906. }
  907. else
  908. {
  909. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  910. if(all_axis)
  911. {
  912. st_synchronize();
  913. disable_e0();
  914. disable_e1();
  915. disable_e2();
  916. finishAndDisableSteppers();
  917. }
  918. else
  919. {
  920. st_synchronize();
  921. if(code_seen('X')) disable_x();
  922. if(code_seen('Y')) disable_y();
  923. if(code_seen('Z')) disable_z();
  924. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  925. if(code_seen('E')) {
  926. disable_e0();
  927. disable_e1();
  928. disable_e2();
  929. }
  930. #endif
  931. LCD_MESSAGEPGM(MSG_PART_RELEASE);
  932. }
  933. }
  934. break;
  935. case 85: // M85
  936. code_seen('S');
  937. max_inactive_time = code_value() * 1000;
  938. break;
  939. case 92: // M92
  940. for(int8_t i=0; i < NUM_AXIS; i++)
  941. {
  942. if(code_seen(axis_codes[i]))
  943. axis_steps_per_unit[i] = code_value();
  944. }
  945. break;
  946. case 115: // M115
  947. SerialprintPGM(MSG_M115_REPORT);
  948. break;
  949. case 117: // M117 display message
  950. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  951. break;
  952. case 114: // M114
  953. SERIAL_PROTOCOLPGM("X:");
  954. SERIAL_PROTOCOL(current_position[X_AXIS]);
  955. SERIAL_PROTOCOLPGM("Y:");
  956. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  957. SERIAL_PROTOCOLPGM("Z:");
  958. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  959. SERIAL_PROTOCOLPGM("E:");
  960. SERIAL_PROTOCOL(current_position[E_AXIS]);
  961. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  962. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  963. SERIAL_PROTOCOLPGM("Y:");
  964. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  965. SERIAL_PROTOCOLPGM("Z:");
  966. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  967. SERIAL_PROTOCOLLN("");
  968. break;
  969. case 119: // M119
  970. #if (X_MIN_PIN > -1)
  971. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  972. SERIAL_PROTOCOL(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  973. #endif
  974. #if (X_MAX_PIN > -1)
  975. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  976. SERIAL_PROTOCOL(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  977. #endif
  978. #if (Y_MIN_PIN > -1)
  979. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  980. SERIAL_PROTOCOL(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  981. #endif
  982. #if (Y_MAX_PIN > -1)
  983. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  984. SERIAL_PROTOCOL(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  985. #endif
  986. #if (Z_MIN_PIN > -1)
  987. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  988. SERIAL_PROTOCOL(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  989. #endif
  990. #if (Z_MAX_PIN > -1)
  991. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  992. SERIAL_PROTOCOL(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  993. #endif
  994. SERIAL_PROTOCOLLN("");
  995. break;
  996. //TODO: update for all axis, use for loop
  997. case 201: // M201
  998. for(int8_t i=0; i < NUM_AXIS; i++)
  999. {
  1000. if(code_seen(axis_codes[i]))
  1001. {
  1002. max_acceleration_units_per_sq_second[i] = code_value();
  1003. axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1004. }
  1005. }
  1006. break;
  1007. #if 0 // Not used for Sprinter/grbl gen6
  1008. case 202: // M202
  1009. for(int8_t i=0; i < NUM_AXIS; i++) {
  1010. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1011. }
  1012. break;
  1013. #endif
  1014. case 203: // M203 max feedrate mm/sec
  1015. for(int8_t i=0; i < NUM_AXIS; i++) {
  1016. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1017. }
  1018. break;
  1019. case 204: // M204 acclereration S normal moves T filmanent only moves
  1020. {
  1021. if(code_seen('S')) acceleration = code_value() ;
  1022. if(code_seen('T')) retract_acceleration = code_value() ;
  1023. }
  1024. break;
  1025. 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
  1026. {
  1027. if(code_seen('S')) minimumfeedrate = code_value();
  1028. if(code_seen('T')) mintravelfeedrate = code_value();
  1029. if(code_seen('B')) minsegmenttime = code_value() ;
  1030. if(code_seen('X')) max_xy_jerk = code_value() ;
  1031. if(code_seen('Z')) max_z_jerk = code_value() ;
  1032. if(code_seen('E')) max_e_jerk = code_value() ;
  1033. }
  1034. break;
  1035. case 206: // M206 additional homeing offset
  1036. for(int8_t i=0; i < 3; i++)
  1037. {
  1038. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1039. }
  1040. break;
  1041. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1042. {
  1043. if(code_seen('S'))
  1044. {
  1045. feedmultiply = code_value() ;
  1046. feedmultiplychanged=true;
  1047. }
  1048. }
  1049. break;
  1050. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1051. {
  1052. if(code_seen('S'))
  1053. {
  1054. extrudemultiply = code_value() ;
  1055. }
  1056. }
  1057. break;
  1058. #ifdef PIDTEMP
  1059. case 301: // M301
  1060. {
  1061. if(code_seen('P')) Kp = code_value();
  1062. if(code_seen('I')) Ki = code_value()*PID_dT;
  1063. if(code_seen('D')) Kd = code_value()/PID_dT;
  1064. #ifdef PID_ADD_EXTRUSION_RATE
  1065. if(code_seen('C')) Kc = code_value();
  1066. #endif
  1067. updatePID();
  1068. SERIAL_PROTOCOL(MSG_OK);
  1069. SERIAL_PROTOCOL(" p:");
  1070. SERIAL_PROTOCOL(Kp);
  1071. SERIAL_PROTOCOL(" i:");
  1072. SERIAL_PROTOCOL(Ki/PID_dT);
  1073. SERIAL_PROTOCOL(" d:");
  1074. SERIAL_PROTOCOL(Kd*PID_dT);
  1075. #ifdef PID_ADD_EXTRUSION_RATE
  1076. SERIAL_PROTOCOL(" c:");
  1077. SERIAL_PROTOCOL(Kc*PID_dT);
  1078. #endif
  1079. SERIAL_PROTOCOLLN("");
  1080. }
  1081. break;
  1082. #endif //PIDTEMP
  1083. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1084. {
  1085. #ifdef PHOTOGRAPH_PIN
  1086. #if (PHOTOGRAPH_PIN > -1)
  1087. const uint8_t NUM_PULSES=16;
  1088. const float PULSE_LENGTH=0.01524;
  1089. for(int i=0; i < NUM_PULSES; i++) {
  1090. WRITE(PHOTOGRAPH_PIN, HIGH);
  1091. _delay_ms(PULSE_LENGTH);
  1092. WRITE(PHOTOGRAPH_PIN, LOW);
  1093. _delay_ms(PULSE_LENGTH);
  1094. }
  1095. delay(7.33);
  1096. for(int i=0; i < NUM_PULSES; i++) {
  1097. WRITE(PHOTOGRAPH_PIN, HIGH);
  1098. _delay_ms(PULSE_LENGTH);
  1099. WRITE(PHOTOGRAPH_PIN, LOW);
  1100. _delay_ms(PULSE_LENGTH);
  1101. }
  1102. #endif
  1103. #endif
  1104. }
  1105. break;
  1106. case 302: // finish all moves
  1107. {
  1108. allow_cold_extrudes(true);
  1109. }
  1110. break;
  1111. case 303: // M303 PID autotune
  1112. {
  1113. float temp = 150.0;
  1114. if (code_seen('S')) temp=code_value();
  1115. PID_autotune(temp);
  1116. }
  1117. break;
  1118. case 400: // finish all moves
  1119. {
  1120. st_synchronize();
  1121. }
  1122. break;
  1123. case 500: // Store settings in EEPROM
  1124. {
  1125. EEPROM_StoreSettings();
  1126. }
  1127. break;
  1128. case 501: // Read settings from EEPROM
  1129. {
  1130. EEPROM_RetrieveSettings();
  1131. }
  1132. break;
  1133. case 502: // Revert to default settings
  1134. {
  1135. EEPROM_RetrieveSettings(true);
  1136. }
  1137. break;
  1138. case 503: // print settings currently in memory
  1139. {
  1140. EEPROM_printSettings();
  1141. }
  1142. break;
  1143. }
  1144. }
  1145. else if(code_seen('T'))
  1146. {
  1147. tmp_extruder = code_value();
  1148. if(tmp_extruder >= EXTRUDERS) {
  1149. SERIAL_ECHO_START;
  1150. SERIAL_ECHO("T");
  1151. SERIAL_ECHO(tmp_extruder);
  1152. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  1153. }
  1154. else {
  1155. active_extruder = tmp_extruder;
  1156. SERIAL_ECHO_START;
  1157. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  1158. SERIAL_PROTOCOLLN((int)active_extruder);
  1159. }
  1160. }
  1161. else
  1162. {
  1163. SERIAL_ECHO_START;
  1164. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1165. SERIAL_ECHO(cmdbuffer[bufindr]);
  1166. SERIAL_ECHOLNPGM("\"");
  1167. }
  1168. ClearToSend();
  1169. }
  1170. void FlushSerialRequestResend()
  1171. {
  1172. //char cmdbuffer[bufindr][100]="Resend:";
  1173. MYSERIAL.flush();
  1174. SERIAL_PROTOCOLPGM(MSG_RESEND);
  1175. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1176. ClearToSend();
  1177. }
  1178. void ClearToSend()
  1179. {
  1180. previous_millis_cmd = millis();
  1181. #ifdef SDSUPPORT
  1182. if(fromsd[bufindr])
  1183. return;
  1184. #endif //SDSUPPORT
  1185. SERIAL_PROTOCOLLNPGM(MSG_OK);
  1186. }
  1187. void get_coordinates()
  1188. {
  1189. for(int8_t i=0; i < NUM_AXIS; i++) {
  1190. if(code_seen(axis_codes[i])) destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1191. else destination[i] = current_position[i]; //Are these else lines really needed?
  1192. }
  1193. if(code_seen('F')) {
  1194. next_feedrate = code_value();
  1195. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1196. }
  1197. }
  1198. void get_arc_coordinates()
  1199. {
  1200. get_coordinates();
  1201. if(code_seen('I')) offset[0] = code_value();
  1202. if(code_seen('J')) offset[1] = code_value();
  1203. }
  1204. void prepare_move()
  1205. {
  1206. if (min_software_endstops) {
  1207. if (destination[X_AXIS] < X_HOME_POS) destination[X_AXIS] = X_HOME_POS;
  1208. if (destination[Y_AXIS] < Y_HOME_POS) destination[Y_AXIS] = Y_HOME_POS;
  1209. if (destination[Z_AXIS] < Z_HOME_POS) destination[Z_AXIS] = Z_HOME_POS;
  1210. }
  1211. if (max_software_endstops) {
  1212. if (destination[X_AXIS] > X_MAX_LENGTH) destination[X_AXIS] = X_MAX_LENGTH;
  1213. if (destination[Y_AXIS] > Y_MAX_LENGTH) destination[Y_AXIS] = Y_MAX_LENGTH;
  1214. if (destination[Z_AXIS] > Z_MAX_LENGTH) destination[Z_AXIS] = Z_MAX_LENGTH;
  1215. }
  1216. previous_millis_cmd = millis();
  1217. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1218. for(int8_t i=0; i < NUM_AXIS; i++) {
  1219. current_position[i] = destination[i];
  1220. }
  1221. }
  1222. void prepare_arc_move(char isclockwise) {
  1223. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1224. // Trace the arc
  1225. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1226. // As far as the parser is concerned, the position is now == target. In reality the
  1227. // motion control system might still be processing the action and the real tool position
  1228. // in any intermediate location.
  1229. for(int8_t i=0; i < NUM_AXIS; i++) {
  1230. current_position[i] = destination[i];
  1231. }
  1232. previous_millis_cmd = millis();
  1233. }
  1234. #ifdef CONTROLLERFAN_PIN
  1235. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  1236. unsigned long lastMotorCheck = 0;
  1237. void controllerFan()
  1238. {
  1239. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  1240. {
  1241. lastMotorCheck = millis();
  1242. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  1243. #if EXTRUDERS > 2
  1244. || !READ(E2_ENABLE_PIN)
  1245. #endif
  1246. #if EXTRUDER > 1
  1247. || !READ(E2_ENABLE_PIN)
  1248. #endif
  1249. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  1250. {
  1251. lastMotor = millis(); //... set time to NOW so the fan will turn on
  1252. }
  1253. if ((millis() - lastMotor) >= (CONTROLLERFAN_SEC*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  1254. {
  1255. WRITE(CONTROLLERFAN_PIN, LOW); //... turn the fan off
  1256. }
  1257. else
  1258. {
  1259. WRITE(CONTROLLERFAN_PIN, HIGH); //... turn the fan on
  1260. }
  1261. }
  1262. }
  1263. #endif
  1264. void manage_inactivity(byte debug)
  1265. {
  1266. if( (millis() - previous_millis_cmd) > max_inactive_time )
  1267. if(max_inactive_time)
  1268. kill();
  1269. if(stepper_inactive_time) {
  1270. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  1271. {
  1272. if(blocks_queued() == false) {
  1273. disable_x();
  1274. disable_y();
  1275. disable_z();
  1276. disable_e0();
  1277. disable_e1();
  1278. disable_e2();
  1279. }
  1280. }
  1281. }
  1282. #ifdef CONTROLLERFAN_PIN
  1283. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  1284. #endif
  1285. #ifdef EXTRUDER_RUNOUT_PREVENT
  1286. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1287. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1288. {
  1289. bool oldstatus=READ(E0_ENABLE_PIN);
  1290. enable_e0();
  1291. float oldepos=current_position[E_AXIS];
  1292. float oldedes=destination[E_AXIS];
  1293. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1294. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1295. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1296. current_position[E_AXIS]=oldepos;
  1297. destination[E_AXIS]=oldedes;
  1298. plan_set_e_position(oldepos);
  1299. previous_millis_cmd=millis();
  1300. st_synchronize();
  1301. WRITE(E0_ENABLE_PIN,oldstatus);
  1302. }
  1303. #endif
  1304. check_axes_activity();
  1305. }
  1306. void kill()
  1307. {
  1308. cli(); // Stop interrupts
  1309. disable_heater();
  1310. disable_x();
  1311. disable_y();
  1312. disable_z();
  1313. disable_e0();
  1314. disable_e1();
  1315. disable_e2();
  1316. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1317. SERIAL_ERROR_START;
  1318. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  1319. LCD_MESSAGEPGM(MSG_KILLED);
  1320. suicide();
  1321. while(1); // Wait for reset
  1322. }