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

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