My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Marlin.pde 30KB

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