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

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