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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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 "fastio.h"
  24. #include "Configuration.h"
  25. #include "pins.h"
  26. #include "Marlin.h"
  27. #include "ultralcd.h"
  28. #include "streaming.h"
  29. #include "planner.h"
  30. #include "stepper.h"
  31. #include "temperature.h"
  32. #ifdef SIMPLE_LCD
  33. #include "Simplelcd.h"
  34. #endif
  35. char version_string[] = "1.0.0 Alpha 1";
  36. #ifdef SDSUPPORT
  37. #include "SdFat.h"
  38. #endif //SDSUPPORT
  39. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  40. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  41. //Implemented Codes
  42. //-------------------
  43. // G0 -> G1
  44. // G1 - Coordinated Movement X Y Z E
  45. // G4 - Dwell S<seconds> or P<milliseconds>
  46. // G28 - Home all Axis
  47. // G90 - Use Absolute Coordinates
  48. // G91 - Use Relative Coordinates
  49. // G92 - Set current position to cordinates given
  50. //RepRap M Codes
  51. // M104 - Set extruder target temp
  52. // M105 - Read current temp
  53. // M106 - Fan on
  54. // M107 - Fan off
  55. // M109 - Wait for extruder current temp to reach target temp.
  56. // M114 - Display current position
  57. //Custom M Codes
  58. // M80 - Turn on Power Supply
  59. // M20 - List SD card
  60. // M21 - Init SD card
  61. // M22 - Release SD card
  62. // M23 - Select SD file (M23 filename.g)
  63. // M24 - Start/resume SD print
  64. // M25 - Pause SD print
  65. // M26 - Set SD position in bytes (M26 S12345)
  66. // M27 - Report SD print status
  67. // M28 - Start SD write (M28 filename.g)
  68. // M29 - Stop SD write
  69. // M81 - Turn off Power Supply
  70. // M82 - Set E codes absolute (default)
  71. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  72. // M84 - Disable steppers until next move,
  73. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  74. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  75. // M92 - Set axis_steps_per_unit - same syntax as G92
  76. // M115 - Capabilities string
  77. // M140 - Set bed target temp
  78. // M190 - Wait for bed current temp to reach target temp.
  79. // M200 - Set filament diameter
  80. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  81. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  82. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  83. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) im mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate
  84. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  85. // M220 - set speed factor override percentage S:factor in percent
  86. // M301 - Set PID parameters P I and D
  87. // M500 - stores paramters in EEPROM
  88. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily). D
  89. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  90. //Stepper Movement Variables
  91. char axis_codes[NUM_AXIS] = {
  92. 'X', 'Y', 'Z', 'E'};
  93. float destination[NUM_AXIS] = {
  94. 0.0, 0.0, 0.0, 0.0};
  95. float current_position[NUM_AXIS] = {
  96. 0.0, 0.0, 0.0, 0.0};
  97. bool home_all_axis = true;
  98. float feedrate = 1500.0, next_feedrate, saved_feedrate;
  99. long gcode_N, gcode_LastN;
  100. float homing_feedrate[] = HOMING_FEEDRATE;
  101. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  102. bool relative_mode = false; //Determines Absolute or Relative Coordinates
  103. bool relative_mode_e = false; //Determines Absolute or Relative E Codes while in Absolute Coordinates mode. E is always relative in Relative Coordinates mode.
  104. uint8_t fanpwm=0;
  105. volatile int feedmultiply=100; //100->1 200->2
  106. int saved_feedmultiply;
  107. volatile bool feedmultiplychanged=false;
  108. // comm variables
  109. #define MAX_CMD_SIZE 96
  110. #define BUFSIZE 4
  111. char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  112. bool fromsd[BUFSIZE];
  113. int bufindr = 0;
  114. int bufindw = 0;
  115. int buflen = 0;
  116. int i = 0;
  117. char serial_char;
  118. int serial_count = 0;
  119. boolean comment_mode = false;
  120. char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  121. extern float HeaterPower;
  122. #include "EEPROM.h"
  123. float tt = 0, bt = 0;
  124. #ifdef WATCHPERIOD
  125. int watch_raw = -1000;
  126. unsigned long watchmillis = 0;
  127. #endif //WATCHPERIOD
  128. //Inactivity shutdown variables
  129. unsigned long previous_millis_cmd = 0;
  130. unsigned long max_inactive_time = 0;
  131. unsigned long stepper_inactive_time = 0;
  132. unsigned long starttime=0;
  133. unsigned long stoptime=0;
  134. #ifdef SDSUPPORT
  135. Sd2Card card;
  136. SdVolume volume;
  137. SdFile root;
  138. SdFile file;
  139. uint32_t filesize = 0;
  140. uint32_t sdpos = 0;
  141. bool sdmode = false;
  142. bool sdactive = false;
  143. bool savetosd = false;
  144. int16_t n;
  145. long autostart_atmillis=0;
  146. void initsd(){
  147. sdactive = false;
  148. #if SDSS >- 1
  149. if(root.isOpen())
  150. root.close();
  151. if (!card.init(SPI_FULL_SPEED,SDSS)){
  152. //if (!card.init(SPI_HALF_SPEED,SDSS))
  153. Serial.println("SD init fail");
  154. }
  155. else if (!volume.init(&card))
  156. Serial.println("volume.init failed");
  157. else if (!root.openRoot(&volume))
  158. Serial.println("openRoot failed");
  159. else
  160. {
  161. sdactive = true;
  162. Serial.println("SD card ok");
  163. }
  164. #endif //SDSS
  165. }
  166. void quickinitsd(){
  167. sdactive=false;
  168. autostart_atmillis=millis()+5000;
  169. }
  170. inline void write_command(char *buf){
  171. char* begin = buf;
  172. char* npos = 0;
  173. char* end = buf + strlen(buf) - 1;
  174. file.writeError = false;
  175. if((npos = strchr(buf, 'N')) != NULL){
  176. begin = strchr(npos, ' ') + 1;
  177. end = strchr(npos, '*') - 1;
  178. }
  179. end[1] = '\r';
  180. end[2] = '\n';
  181. end[3] = '\0';
  182. //Serial.println(begin);
  183. file.write(begin);
  184. if (file.writeError){
  185. Serial.println("error writing to file");
  186. }
  187. }
  188. #endif //SDSUPPORT
  189. ///adds an command to the main command buffer
  190. void enquecommand(const char *cmd)
  191. {
  192. if(buflen < BUFSIZE)
  193. {
  194. //this is dangerous if a mixing of serial and this happsens
  195. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  196. Serial.print("en:");Serial.println(cmdbuffer[bufindw]);
  197. bufindw= (bufindw + 1)%BUFSIZE;
  198. buflen += 1;
  199. }
  200. }
  201. void setup()
  202. {
  203. Serial.begin(BAUDRATE);
  204. ECHOLN("Marlin "<<version_string);
  205. Serial.println("start");
  206. #if defined FANCY_LCD || defined SIMPLE_LCD
  207. lcd_init();
  208. #endif
  209. for(int i = 0; i < BUFSIZE; i++){
  210. fromsd[i] = false;
  211. }
  212. RetrieveSettings(); // loads data from EEPROM if available
  213. for(int i=0; i < NUM_AXIS; i++){
  214. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  215. }
  216. #ifdef SDSUPPORT
  217. //power to SD reader
  218. #if SDPOWER > -1
  219. SET_OUTPUT(SDPOWER);
  220. WRITE(SDPOWER,HIGH);
  221. #endif //SDPOWER
  222. quickinitsd();
  223. #endif //SDSUPPORT
  224. plan_init(); // Initialize planner;
  225. st_init(); // Initialize stepper;
  226. tp_init(); // Initialize temperature loop
  227. //checkautostart();
  228. }
  229. #ifdef SDSUPPORT
  230. bool autostart_stilltocheck=true;
  231. void checkautostart(bool force)
  232. {
  233. //this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset
  234. if(!force)
  235. {
  236. if(!autostart_stilltocheck)
  237. return;
  238. if(autostart_atmillis<millis())
  239. return;
  240. }
  241. autostart_stilltocheck=false;
  242. if(!sdactive)
  243. {
  244. initsd();
  245. if(!sdactive) //fail
  246. return;
  247. }
  248. static int lastnr=0;
  249. char autoname[30];
  250. sprintf(autoname,"auto%i.g",lastnr);
  251. for(int i=0;i<strlen(autoname);i++)
  252. autoname[i]=tolower(autoname[i]);
  253. dir_t p;
  254. root.rewind();
  255. char filename[11];
  256. int cnt=0;
  257. bool found=false;
  258. while (root.readDir(p) > 0)
  259. {
  260. for(int i=0;i<strlen((char*)p.name);i++)
  261. p.name[i]=tolower(p.name[i]);
  262. //Serial.print((char*)p.name);
  263. //Serial.print(" ");
  264. //Serial.println(autoname);
  265. if(p.name[9]!='~') //skip safety copies
  266. if(strncmp((char*)p.name,autoname,5)==0)
  267. {
  268. char cmd[30];
  269. sprintf(cmd,"M23 %s",autoname);
  270. //sprintf(cmd,"M115");
  271. //enquecommand("G92 Z0");
  272. //enquecommand("G1 Z10 F2000");
  273. //enquecommand("G28 X-105 Y-105");
  274. enquecommand(cmd);
  275. enquecommand("M24");
  276. found=true;
  277. }
  278. }
  279. if(!found)
  280. lastnr=-1;
  281. else
  282. lastnr++;
  283. }
  284. #else
  285. inline void checkautostart(bool x)
  286. {
  287. }
  288. #endif
  289. void loop()
  290. {
  291. if(buflen<3)
  292. get_command();
  293. checkautostart(false);
  294. if(buflen)
  295. {
  296. #ifdef SDSUPPORT
  297. if(savetosd){
  298. if(strstr(cmdbuffer[bufindr],"M29") == NULL){
  299. write_command(cmdbuffer[bufindr]);
  300. Serial.println("ok");
  301. }
  302. else{
  303. file.sync();
  304. file.close();
  305. savetosd = false;
  306. Serial.println("Done saving file.");
  307. }
  308. }
  309. else{
  310. process_commands();
  311. }
  312. #else
  313. process_commands();
  314. #endif //SDSUPPORT
  315. buflen = (buflen-1);
  316. bufindr = (bufindr + 1)%BUFSIZE;
  317. }
  318. //check heater every n milliseconds
  319. manage_heater();
  320. manage_inactivity(1);
  321. LCD_STATUS;
  322. }
  323. inline void get_command()
  324. {
  325. while( Serial.available() > 0 && buflen < BUFSIZE) {
  326. serial_char = Serial.read();
  327. if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) )
  328. {
  329. if(!serial_count) return; //if empty line
  330. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  331. if(!comment_mode){
  332. fromsd[bufindw] = false;
  333. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  334. {
  335. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  336. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  337. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  338. Serial.print("Serial Error: Line Number is not Last Line Number+1, Last Line:");
  339. Serial.println(gcode_LastN);
  340. //Serial.println(gcode_N);
  341. FlushSerialRequestResend();
  342. serial_count = 0;
  343. return;
  344. }
  345. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  346. {
  347. byte checksum = 0;
  348. byte count = 0;
  349. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  350. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  351. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  352. Serial.print("Error: checksum mismatch, Last Line:");
  353. Serial.println(gcode_LastN);
  354. FlushSerialRequestResend();
  355. serial_count = 0;
  356. return;
  357. }
  358. //if no errors, continue parsing
  359. }
  360. else
  361. {
  362. Serial.print("Error: No Checksum with line number, Last Line:");
  363. Serial.println(gcode_LastN);
  364. FlushSerialRequestResend();
  365. serial_count = 0;
  366. return;
  367. }
  368. gcode_LastN = gcode_N;
  369. //if no errors, continue parsing
  370. }
  371. else // if we don't receive 'N' but still see '*'
  372. {
  373. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  374. {
  375. Serial.print("Error: No Line Number with checksum, Last Line:");
  376. Serial.println(gcode_LastN);
  377. serial_count = 0;
  378. return;
  379. }
  380. }
  381. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  382. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  383. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  384. case 0:
  385. case 1:
  386. #ifdef SDSUPPORT
  387. if(savetosd)
  388. break;
  389. #endif //SDSUPPORT
  390. Serial.println("ok");
  391. break;
  392. default:
  393. break;
  394. }
  395. }
  396. bufindw = (bufindw + 1)%BUFSIZE;
  397. buflen += 1;
  398. }
  399. comment_mode = false; //for new command
  400. serial_count = 0; //clear buffer
  401. }
  402. else
  403. {
  404. if(serial_char == ';') comment_mode = true;
  405. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  406. }
  407. }
  408. #ifdef SDSUPPORT
  409. if(!sdmode || serial_count!=0){
  410. return;
  411. }
  412. while( filesize > sdpos && buflen < BUFSIZE) {
  413. n = file.read();
  414. serial_char = (char)n;
  415. if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) || n == -1)
  416. {
  417. sdpos = file.curPosition();
  418. if(sdpos >= filesize){
  419. sdmode = false;
  420. Serial.println("Done printing file");
  421. stoptime=millis();
  422. char time[30];
  423. unsigned long t=(stoptime-starttime)/1000;
  424. int sec,min;
  425. min=t/60;
  426. sec=t%60;
  427. sprintf(time,"%i min, %i sec",min,sec);
  428. Serial.println(time);
  429. LCD_MESSAGE(time);
  430. checkautostart(true);
  431. }
  432. if(!serial_count) return; //if empty line
  433. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  434. if(!comment_mode){
  435. fromsd[bufindw] = true;
  436. buflen += 1;
  437. bufindw = (bufindw + 1)%BUFSIZE;
  438. }
  439. comment_mode = false; //for new command
  440. serial_count = 0; //clear buffer
  441. }
  442. else
  443. {
  444. if(serial_char == ';') comment_mode = true;
  445. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  446. }
  447. }
  448. #endif //SDSUPPORT
  449. }
  450. inline float code_value() {
  451. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  452. }
  453. inline long code_value_long() {
  454. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  455. }
  456. inline bool code_seen(char code_string[]) {
  457. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  458. } //Return True if the string was found
  459. inline bool code_seen(char code)
  460. {
  461. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  462. return (strchr_pointer != NULL); //Return True if a character was found
  463. }
  464. inline void process_commands()
  465. {
  466. unsigned long codenum; //throw away variable
  467. char *starpos = NULL;
  468. if(code_seen('G'))
  469. {
  470. switch((int)code_value())
  471. {
  472. case 0: // G0 -> G1
  473. case 1: // G1
  474. get_coordinates(); // For X Y Z E F
  475. prepare_move();
  476. previous_millis_cmd = millis();
  477. //ClearToSend();
  478. return;
  479. //break;
  480. case 4: // G4 dwell
  481. codenum = 0;
  482. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  483. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  484. codenum += millis(); // keep track of when we started waiting
  485. while(millis() < codenum ){
  486. manage_heater();
  487. }
  488. break;
  489. case 28: //G28 Home all Axis one at a time
  490. saved_feedrate = feedrate;
  491. saved_feedmultiply = feedmultiply;
  492. feedmultiply = 100;
  493. for(int i=0; i < NUM_AXIS; i++) {
  494. destination[i] = current_position[i];
  495. }
  496. feedrate = 0.0;
  497. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  498. if((home_all_axis) || (code_seen(axis_codes[X_AXIS]))) {
  499. if ((X_MIN_PIN > -1 && X_HOME_DIR==-1) || (X_MAX_PIN > -1 && X_HOME_DIR==1)){
  500. // st_synchronize();
  501. current_position[X_AXIS] = 0;
  502. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  503. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;
  504. feedrate = homing_feedrate[X_AXIS];
  505. prepare_move();
  506. // st_synchronize();
  507. current_position[X_AXIS] = 0;
  508. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  509. destination[X_AXIS] = -5 * X_HOME_DIR;
  510. prepare_move();
  511. // st_synchronize();
  512. destination[X_AXIS] = 10 * X_HOME_DIR;
  513. feedrate = homing_feedrate[X_AXIS]/2 ;
  514. prepare_move();
  515. // st_synchronize();
  516. current_position[X_AXIS] = (X_HOME_DIR == -1) ? 0 : X_MAX_LENGTH;
  517. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  518. destination[X_AXIS] = current_position[X_AXIS];
  519. feedrate = 0.0;
  520. }
  521. }
  522. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  523. if ((Y_MIN_PIN > -1 && Y_HOME_DIR==-1) || (Y_MAX_PIN > -1 && Y_HOME_DIR==1)){
  524. current_position[Y_AXIS] = 0;
  525. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  526. destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  527. feedrate = homing_feedrate[Y_AXIS];
  528. prepare_move();
  529. // st_synchronize();
  530. current_position[Y_AXIS] = 0;
  531. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  532. destination[Y_AXIS] = -5 * Y_HOME_DIR;
  533. prepare_move();
  534. // st_synchronize();
  535. destination[Y_AXIS] = 10 * Y_HOME_DIR;
  536. feedrate = homing_feedrate[Y_AXIS]/2;
  537. prepare_move();
  538. // st_synchronize();
  539. current_position[Y_AXIS] = (Y_HOME_DIR == -1) ? 0 : Y_MAX_LENGTH;
  540. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  541. destination[Y_AXIS] = current_position[Y_AXIS];
  542. feedrate = 0.0;
  543. }
  544. }
  545. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  546. if ((Z_MIN_PIN > -1 && Z_HOME_DIR==-1) || (Z_MAX_PIN > -1 && Z_HOME_DIR==1)){
  547. current_position[Z_AXIS] = 0;
  548. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  549. destination[Z_AXIS] = 1.5 * Z_MAX_LENGTH * Z_HOME_DIR;
  550. feedrate = homing_feedrate[Z_AXIS];
  551. prepare_move();
  552. // st_synchronize();
  553. current_position[Z_AXIS] = 0;
  554. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  555. destination[Z_AXIS] = -2 * Z_HOME_DIR;
  556. prepare_move();
  557. // st_synchronize();
  558. destination[Z_AXIS] = 3 * Z_HOME_DIR;
  559. feedrate = homing_feedrate[Z_AXIS]/2;
  560. prepare_move();
  561. // st_synchronize();
  562. current_position[Z_AXIS] = (Z_HOME_DIR == -1) ? 0 : Z_MAX_LENGTH;
  563. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  564. destination[Z_AXIS] = current_position[Z_AXIS];
  565. feedrate = 0.0;
  566. }
  567. }
  568. feedrate = saved_feedrate;
  569. feedmultiply = saved_feedmultiply;
  570. previous_millis_cmd = millis();
  571. break;
  572. case 90: // G90
  573. relative_mode = false;
  574. break;
  575. case 91: // G91
  576. relative_mode = true;
  577. break;
  578. case 92: // G92
  579. if(!code_seen(axis_codes[E_AXIS]))
  580. st_synchronize();
  581. for(int i=0; i < NUM_AXIS; i++) {
  582. if(code_seen(axis_codes[i])) current_position[i] = code_value();
  583. }
  584. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  585. break;
  586. }
  587. }
  588. else if(code_seen('M'))
  589. {
  590. switch( (int)code_value() )
  591. {
  592. #ifdef SDSUPPORT
  593. case 20: // M20 - list SD card
  594. Serial.println("Begin file list");
  595. root.ls();
  596. Serial.println("End file list");
  597. break;
  598. case 21: // M21 - init SD card
  599. sdmode = false;
  600. initsd();
  601. break;
  602. case 22: //M22 - release SD card
  603. sdmode = false;
  604. sdactive = false;
  605. break;
  606. case 23: //M23 - Select file
  607. if(sdactive){
  608. sdmode = false;
  609. file.close();
  610. starpos = (strchr(strchr_pointer + 4,'*'));
  611. if(starpos!=NULL)
  612. *(starpos-1)='\0';
  613. if (file.open(&root, strchr_pointer + 4, O_READ)) {
  614. Serial.print("File opened:");
  615. Serial.print(strchr_pointer + 4);
  616. Serial.print(" Size:");
  617. Serial.println(file.fileSize());
  618. sdpos = 0;
  619. filesize = file.fileSize();
  620. Serial.println("File selected");
  621. }
  622. else{
  623. Serial.println("file.open failed");
  624. }
  625. }
  626. break;
  627. case 24: //M24 - Start SD print
  628. if(sdactive){
  629. sdmode = true;
  630. starttime=millis();
  631. }
  632. break;
  633. case 25: //M25 - Pause SD print
  634. if(sdmode){
  635. sdmode = false;
  636. }
  637. break;
  638. case 26: //M26 - Set SD index
  639. if(sdactive && code_seen('S')){
  640. sdpos = code_value_long();
  641. file.seekSet(sdpos);
  642. }
  643. break;
  644. case 27: //M27 - Get SD status
  645. if(sdactive){
  646. Serial.print("SD printing byte ");
  647. Serial.print(sdpos);
  648. Serial.print("/");
  649. Serial.println(filesize);
  650. }
  651. else{
  652. Serial.println("Not SD printing");
  653. }
  654. break;
  655. case 28: //M28 - Start SD write
  656. if(sdactive){
  657. char* npos = 0;
  658. file.close();
  659. sdmode = false;
  660. starpos = (strchr(strchr_pointer + 4,'*'));
  661. if(starpos != NULL){
  662. npos = strchr(cmdbuffer[bufindr], 'N');
  663. strchr_pointer = strchr(npos,' ') + 1;
  664. *(starpos-1) = '\0';
  665. }
  666. if (!file.open(&root, strchr_pointer+4, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  667. {
  668. Serial.print("open failed, File: ");
  669. Serial.print(strchr_pointer + 4);
  670. Serial.print(".");
  671. }
  672. else{
  673. savetosd = true;
  674. Serial.print("Writing to file: ");
  675. Serial.println(strchr_pointer + 4);
  676. }
  677. }
  678. break;
  679. case 29: //M29 - Stop SD write
  680. //processed in write to file routine above
  681. //savetosd = false;
  682. break;
  683. case 30:
  684. {
  685. stoptime=millis();
  686. char time[30];
  687. unsigned long t=(stoptime-starttime)/1000;
  688. int sec,min;
  689. min=t/60;
  690. sec=t%60;
  691. sprintf(time,"%i min, %i sec",min,sec);
  692. Serial.println(time);
  693. LCD_MESSAGE(time);
  694. }
  695. break;
  696. #endif //SDSUPPORT
  697. case 104: // M104
  698. if (code_seen('S')) target_raw[0] = temp2analog(code_value());
  699. #ifdef PIDTEMP
  700. pid_setpoint = code_value();
  701. #endif //PIDTEM
  702. #ifdef WATCHPERIOD
  703. if(target_raw[0] > current_raw[0]){
  704. watchmillis = max(1,millis());
  705. watch_raw[0] = current_raw[0];
  706. }else{
  707. watchmillis = 0;
  708. }
  709. #endif
  710. break;
  711. case 140: // M140 set bed temp
  712. if (code_seen('S')) target_raw[1] = temp2analogBed(code_value());
  713. break;
  714. case 105: // M105
  715. #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
  716. tt = analog2temp(current_raw[0]);
  717. #endif
  718. #if TEMP_1_PIN > -1
  719. bt = analog2tempBed(current_raw[1]);
  720. #endif
  721. #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
  722. Serial.print("ok T:");
  723. Serial.print(tt);
  724. // Serial.print(", raw:");
  725. // Serial.print(current_raw);
  726. #if TEMP_1_PIN > -1
  727. #ifdef PIDTEMP
  728. Serial.print(" B:");
  729. #if TEMP_1_PIN > -1
  730. Serial.println(bt);
  731. #else
  732. Serial.println(HeaterPower);
  733. #endif
  734. #else
  735. Serial.println();
  736. #endif
  737. #else
  738. Serial.println();
  739. #endif
  740. #else
  741. Serial.println("No thermistors - no temp");
  742. #endif
  743. return;
  744. //break;
  745. case 109: // M109 - Wait for extruder heater to reach target.
  746. LCD_MESSAGE("Heating...");
  747. if (code_seen('S')) target_raw[0] = temp2analog(code_value());
  748. #ifdef PIDTEMP
  749. pid_setpoint = code_value();
  750. #endif //PIDTEM
  751. #ifdef WATCHPERIOD
  752. if(target_raw[0]>current_raw[0]){
  753. watchmillis = max(1,millis());
  754. watch_raw[0] = current_raw[0];
  755. }else{
  756. watchmillis = 0;
  757. }
  758. #endif
  759. codenum = millis();
  760. starttime=millis();
  761. while(current_raw[0] < target_raw[0]) {
  762. if( (millis() - codenum) > 1000 ) { //Print Temp Reading every 1 second while heating up.
  763. Serial.print("T:");
  764. Serial.println( analog2temp(current_raw[0]) );
  765. codenum = millis();
  766. }
  767. LCD_STATUS;
  768. manage_heater();
  769. }
  770. LCD_MESSAGE("UltiMarlin ready.");
  771. break;
  772. case 190: // M190 - Wait bed for heater to reach target.
  773. #if TEMP_1_PIN > -1
  774. if (code_seen('S')) target_raw[1] = temp2analog(code_value());
  775. codenum = millis();
  776. while(current_raw[1] < target_raw[1])
  777. {
  778. if( (millis()-codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  779. {
  780. float tt=analog2temp(current_raw[0]);
  781. Serial.print("T:");
  782. Serial.println( tt );
  783. Serial.print("ok T:");
  784. Serial.print( tt );
  785. Serial.print(" B:");
  786. Serial.println( analog2temp(current_raw[1]) );
  787. codenum = millis();
  788. }
  789. manage_heater();
  790. }
  791. #endif
  792. break;
  793. #if FAN_PIN > -1
  794. case 106: //M106 Fan On
  795. if (code_seen('S')){
  796. WRITE(FAN_PIN,HIGH);
  797. fanpwm=constrain(code_value(),0,255);
  798. analogWrite(FAN_PIN, fanpwm);
  799. }
  800. else {
  801. WRITE(FAN_PIN,HIGH);
  802. fanpwm=255;
  803. analogWrite(FAN_PIN, fanpwm);
  804. }
  805. break;
  806. case 107: //M107 Fan Off
  807. WRITE(FAN_PIN,LOW);
  808. analogWrite(FAN_PIN, 0);
  809. break;
  810. #endif
  811. case 82:
  812. axis_relative_modes[3] = false;
  813. break;
  814. case 83:
  815. axis_relative_modes[3] = true;
  816. break;
  817. case 18:
  818. case 84:
  819. if(code_seen('S')){
  820. stepper_inactive_time = code_value() * 1000;
  821. }
  822. else{
  823. st_synchronize();
  824. disable_x();
  825. disable_y();
  826. disable_z();
  827. disable_e();
  828. }
  829. break;
  830. case 85: // M85
  831. code_seen('S');
  832. max_inactive_time = code_value() * 1000;
  833. break;
  834. case 92: // M92
  835. for(int i=0; i < NUM_AXIS; i++) {
  836. if(code_seen(axis_codes[i])) axis_steps_per_unit[i] = code_value();
  837. }
  838. break;
  839. case 115: // M115
  840. Serial.println("FIRMWARE_NAME:Sprinter/grbl mashup for gen6 FIRMWARE_URL:http://www.mendel-parts.com PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1");
  841. break;
  842. case 114: // M114
  843. Serial.print("X:");
  844. Serial.print(current_position[X_AXIS]);
  845. Serial.print("Y:");
  846. Serial.print(current_position[Y_AXIS]);
  847. Serial.print("Z:");
  848. Serial.print(current_position[Z_AXIS]);
  849. Serial.print("E:");
  850. Serial.print(current_position[E_AXIS]);
  851. #ifdef DEBUG_STEPS
  852. Serial.print(" Count X:");
  853. Serial.print(float(count_position[X_AXIS])/axis_steps_per_unit[X_AXIS]);
  854. Serial.print("Y:");
  855. Serial.print(float(count_position[Y_AXIS])/axis_steps_per_unit[Y_AXIS]);
  856. Serial.print("Z:");
  857. Serial.println(float(count_position[Z_AXIS])/axis_steps_per_unit[Z_AXIS]);
  858. #endif
  859. Serial.println("");
  860. break;
  861. case 119: // M119
  862. #if (X_MIN_PIN > -1)
  863. Serial.print("x_min:");
  864. Serial.print((READ(X_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  865. #endif
  866. #if (X_MAX_PIN > -1)
  867. Serial.print("x_max:");
  868. Serial.print((READ(X_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  869. #endif
  870. #if (Y_MIN_PIN > -1)
  871. Serial.print("y_min:");
  872. Serial.print((READ(Y_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  873. #endif
  874. #if (Y_MAX_PIN > -1)
  875. Serial.print("y_max:");
  876. Serial.print((READ(Y_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  877. #endif
  878. #if (Z_MIN_PIN > -1)
  879. Serial.print("z_min:");
  880. Serial.print((READ(Z_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  881. #endif
  882. #if (Z_MAX_PIN > -1)
  883. Serial.print("z_max:");
  884. Serial.print((READ(Z_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  885. #endif
  886. Serial.println("");
  887. break;
  888. //TODO: update for all axis, use for loop
  889. case 201: // M201
  890. for(int i=0; i < NUM_AXIS; i++) {
  891. if(code_seen(axis_codes[i])) axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  892. }
  893. break;
  894. #if 0 // Not used for Sprinter/grbl gen6
  895. case 202: // M202
  896. for(int i=0; i < NUM_AXIS; i++) {
  897. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  898. }
  899. break;
  900. #endif
  901. case 203: // M203 max feedrate mm/sec
  902. for(int i=0; i < NUM_AXIS; i++) {
  903. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value()*60 ;
  904. }
  905. break;
  906. case 204: // M204 acclereration S normal moves T filmanent only moves
  907. {
  908. if(code_seen('S')) acceleration = code_value() ;
  909. if(code_seen('T')) retract_acceleration = code_value() ;
  910. }
  911. break;
  912. 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
  913. {
  914. if(code_seen('S')) minimumfeedrate = code_value()*60 ;
  915. if(code_seen('T')) mintravelfeedrate = code_value()*60 ;
  916. if(code_seen('B')) minsegmenttime = code_value() ;
  917. if(code_seen('X')) max_xy_jerk = code_value()*60 ;
  918. if(code_seen('Z')) max_z_jerk = code_value()*60 ;
  919. }
  920. break;
  921. case 220: // M220 S<factor in percent>- set speed factor override percentage
  922. {
  923. if(code_seen('S'))
  924. {
  925. feedmultiply = code_value() ;
  926. feedmultiplychanged=true;
  927. }
  928. }
  929. break;
  930. #ifdef PIDTEMP
  931. case 301: // M301
  932. if(code_seen('P')) Kp = code_value();
  933. if(code_seen('I')) Ki = code_value()*PID_dT;
  934. if(code_seen('D')) Kd = code_value()/PID_dT;
  935. // ECHOLN("Kp "<<_FLOAT(Kp,2));
  936. // ECHOLN("Ki "<<_FLOAT(Ki/PID_dT,2));
  937. // ECHOLN("Kd "<<_FLOAT(Kd*PID_dT,2));
  938. // temp_iState_min = 0.0;
  939. // if (Ki!=0) {
  940. // temp_iState_max = PID_INTEGRAL_DRIVE_MAX / (Ki/100.0);
  941. // }
  942. // else temp_iState_max = 1.0e10;
  943. break;
  944. #endif //PIDTEMP
  945. case 500: // Store settings in EEPROM
  946. {
  947. StoreSettings();
  948. }
  949. break;
  950. case 501: // Read settings from EEPROM
  951. {
  952. RetrieveSettings();
  953. }
  954. break;
  955. case 502: // Revert to default settings
  956. {
  957. RetrieveSettings(true);
  958. }
  959. break;
  960. }
  961. }
  962. else{
  963. Serial.println("Unknown command:");
  964. Serial.println(cmdbuffer[bufindr]);
  965. }
  966. ClearToSend();
  967. }
  968. void FlushSerialRequestResend()
  969. {
  970. //char cmdbuffer[bufindr][100]="Resend:";
  971. Serial.flush();
  972. Serial.print("Resend:");
  973. Serial.println(gcode_LastN + 1);
  974. ClearToSend();
  975. }
  976. void ClearToSend()
  977. {
  978. previous_millis_cmd = millis();
  979. #ifdef SDSUPPORT
  980. if(fromsd[bufindr])
  981. return;
  982. #endif //SDSUPPORT
  983. Serial.println("ok");
  984. }
  985. inline void get_coordinates()
  986. {
  987. for(int i=0; i < NUM_AXIS; i++) {
  988. if(code_seen(axis_codes[i])) destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  989. else destination[i] = current_position[i]; //Are these else lines really needed?
  990. }
  991. if(code_seen('F')) {
  992. next_feedrate = code_value();
  993. if(next_feedrate > 0.0) feedrate = next_feedrate;
  994. }
  995. }
  996. void prepare_move()
  997. {
  998. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60.0/100.0);
  999. for(int i=0; i < NUM_AXIS; i++) {
  1000. current_position[i] = destination[i];
  1001. }
  1002. }
  1003. #ifdef USE_WATCHDOG
  1004. #include <avr/wdt.h>
  1005. #include <avr/interrupt.h>
  1006. volatile uint8_t timeout_seconds=0;
  1007. void(* ctrlaltdelete) (void) = 0;
  1008. ISR(WDT_vect) { //Watchdog timer interrupt, called if main program blocks >1sec
  1009. if(timeout_seconds++ >= WATCHDOG_TIMEOUT)
  1010. {
  1011. kill();
  1012. #ifdef RESET_MANUAL
  1013. LCD_MESSAGE("Please Reset!");
  1014. ECHOLN("echo_: Something is wrong, please turn off the printer.");
  1015. #else
  1016. LCD_MESSAGE("Timeout, resetting!");
  1017. #endif
  1018. //disable watchdog, it will survife reboot.
  1019. WDTCSR |= (1<<WDCE) | (1<<WDE);
  1020. WDTCSR = 0;
  1021. #ifdef RESET_MANUAL
  1022. while(1); //wait for user or serial reset
  1023. #else
  1024. ctrlaltdelete();
  1025. #endif
  1026. }
  1027. }
  1028. /// intialise watch dog with a 1 sec interrupt time
  1029. void wd_init() {
  1030. WDTCSR = (1<<WDCE )|(1<<WDE ); //allow changes
  1031. WDTCSR = (1<<WDIF)|(1<<WDIE)| (1<<WDCE )|(1<<WDE )| (1<<WDP2 )|(1<<WDP1)|(0<<WDP0);
  1032. }
  1033. /// reset watchdog. MUST be called every 1s after init or avr will reset.
  1034. void wd_reset() {
  1035. wdt_reset();
  1036. timeout_seconds=0; //reset counter for resets
  1037. }
  1038. #endif /* USE_WATCHDOG */
  1039. inline void kill()
  1040. {
  1041. #if TEMP_0_PIN > -1
  1042. target_raw[0]=0;
  1043. #if HEATER_0_PIN > -1
  1044. WRITE(HEATER_0_PIN,LOW);
  1045. #endif
  1046. #endif
  1047. #if TEMP_1_PIN > -1
  1048. target_raw[1]=0;
  1049. #if HEATER_1_PIN > -1
  1050. WRITE(HEATER_1_PIN,LOW);
  1051. #endif
  1052. #endif
  1053. #if TEMP_2_PIN > -1
  1054. target_raw[2]=0;
  1055. #if HEATER_2_PIN > -1
  1056. WRITE(HEATER_2_PIN,LOW);
  1057. #endif
  1058. #endif
  1059. disable_x();
  1060. disable_y();
  1061. disable_z();
  1062. disable_e();
  1063. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1064. Serial.println("!! Printer halted. kill() called!!");
  1065. while(1); // Wait for reset
  1066. }
  1067. void manage_inactivity(byte debug) {
  1068. if( (millis()-previous_millis_cmd) > max_inactive_time ) if(max_inactive_time) kill();
  1069. if( (millis()-previous_millis_cmd) > stepper_inactive_time ) if(stepper_inactive_time) {
  1070. disable_x();
  1071. disable_y();
  1072. disable_z();
  1073. disable_e();
  1074. }
  1075. check_axes_activity();
  1076. }