My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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