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

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