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

Marlin.pde 35KB

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