My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Marlin.pde 32KB

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