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

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