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

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