My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Marlin.pde 34KB

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