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

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