My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Marlin.pde 31KB

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