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

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