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

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