My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin.pde 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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 "Marlin.h"
  23. #include "ultralcd.h"
  24. #include "planner.h"
  25. #include "stepper.h"
  26. #include "temperature.h"
  27. #include "motion_control.h"
  28. #include "cardreader.h"
  29. #include "watchdog.h"
  30. #include "EEPROMwrite.h"
  31. #define VERSION_STRING "1.0.0 RC1"
  32. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  33. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  34. //Implemented Codes
  35. //-------------------
  36. // G0 -> G1
  37. // G1 - Coordinated Movement X Y Z E
  38. // G2 - CW ARC
  39. // G3 - CCW ARC
  40. // G4 - Dwell S<seconds> or P<milliseconds>
  41. // G28 - Home all Axis
  42. // G90 - Use Absolute Coordinates
  43. // G91 - Use Relative Coordinates
  44. // G92 - Set current position to cordinates given
  45. //RepRap M Codes
  46. // M104 - Set extruder target temp
  47. // M105 - Read current temp
  48. // M106 - Fan on
  49. // M107 - Fan off
  50. // M109 - Wait for extruder current temp to reach target temp.
  51. // M114 - Display current position
  52. //Custom M Codes
  53. // M17 - Enable/Power all stepper motors
  54. // M18 - Disable all stepper motors; same as M84
  55. // M20 - List SD card
  56. // M21 - Init SD card
  57. // M22 - Release SD card
  58. // M23 - Select SD file (M23 filename.g)
  59. // M24 - Start/resume SD print
  60. // M25 - Pause SD print
  61. // M26 - Set SD position in bytes (M26 S12345)
  62. // M27 - Report SD print status
  63. // M28 - Start SD write (M28 filename.g)
  64. // M29 - Stop SD write
  65. // M30 - Output time since last M109 or SD card start to serial
  66. // M42 - Change pin status via gcode
  67. // M80 - Turn on Power Supply
  68. // M81 - Turn off Power Supply
  69. // M82 - Set E codes absolute (default)
  70. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  71. // M84 - Disable steppers until next move,
  72. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  73. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  74. // M92 - Set axis_steps_per_unit - same syntax as G92
  75. // M114 - Output current position to serial port
  76. // M115 - Capabilities string
  77. // M117 - display message
  78. // M119 - Output Endstop status to serial port
  79. // M140 - Set bed target temp
  80. // M190 - Wait for bed current temp to reach target temp.
  81. // M200 - Set filament diameter
  82. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  83. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  84. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  85. // 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
  86. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  87. // M206 - set additional homeing offset
  88. // M220 - set speed factor override percentage S:factor in percent
  89. // M240 - Trigger a camera to take a photograph
  90. // M301 - Set PID parameters P I and D
  91. // M302 - Allow cold extrudes
  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. // M503 - print the current settings (from memory not from eeprom)
  97. //Stepper Movement Variables
  98. //===========================================================================
  99. //=============================imported variables============================
  100. //===========================================================================
  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. float add_homeing[3]={0,0,0};
  114. uint8_t active_extruder = 0;
  115. bool stop_heating_wait=false;
  116. //===========================================================================
  117. //=============================private variables=============================
  118. //===========================================================================
  119. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  120. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  121. static float offset[3] = {0.0, 0.0, 0.0};
  122. static bool home_all_axis = true;
  123. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  124. static long gcode_N, gcode_LastN;
  125. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  126. 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.
  127. static uint8_t fanpwm=0;
  128. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  129. static bool fromsd[BUFSIZE];
  130. static int bufindr = 0;
  131. static int bufindw = 0;
  132. static int buflen = 0;
  133. //static int i = 0;
  134. static char serial_char;
  135. static int serial_count = 0;
  136. static boolean comment_mode = false;
  137. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  138. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  139. //static float tt = 0;
  140. //static float bt = 0;
  141. //Inactivity shutdown variables
  142. static unsigned long previous_millis_cmd = 0;
  143. static unsigned long max_inactive_time = 0;
  144. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  145. static unsigned long starttime=0;
  146. static unsigned long stoptime=0;
  147. static uint8_t tmp_extruder;
  148. //===========================================================================
  149. //=============================ROUTINES=============================
  150. //===========================================================================
  151. void get_arc_coordinates();
  152. extern "C"{
  153. extern unsigned int __bss_end;
  154. extern unsigned int __heap_start;
  155. extern void *__brkval;
  156. int freeMemory() {
  157. int free_memory;
  158. if((int)__brkval == 0)
  159. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  160. else
  161. free_memory = ((int)&free_memory) - ((int)__brkval);
  162. return free_memory;
  163. }
  164. }
  165. //adds an command to the main command buffer
  166. //thats really done in a non-safe way.
  167. //needs overworking someday
  168. void enquecommand(const char *cmd)
  169. {
  170. if(buflen < BUFSIZE)
  171. {
  172. //this is dangerous if a mixing of serial and this happsens
  173. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  174. SERIAL_ECHO_START;
  175. SERIAL_ECHOPGM("enqueing \"");
  176. SERIAL_ECHO(cmdbuffer[bufindw]);
  177. SERIAL_ECHOLNPGM("\"");
  178. bufindw= (bufindw + 1)%BUFSIZE;
  179. buflen += 1;
  180. }
  181. }
  182. void setup_photpin()
  183. {
  184. #ifdef PHOTOGRAPH_PIN
  185. #if (PHOTOGRAPH_PIN > -1)
  186. SET_OUTPUT(PHOTOGRAPH_PIN);
  187. WRITE(PHOTOGRAPH_PIN, LOW);
  188. #endif
  189. #endif
  190. }
  191. void setup_powerhold()
  192. {
  193. #ifdef SUICIDE_PIN
  194. #if (SUICIDE_PIN> -1)
  195. SET_OUTPUT(SUICIDE_PIN);
  196. WRITE(SUICIDE_PIN, HIGH);
  197. #endif
  198. #endif
  199. }
  200. void suicide()
  201. {
  202. #ifdef SUICIDE_PIN
  203. #if (SUICIDE_PIN> -1)
  204. SET_OUTPUT(SUICIDE_PIN);
  205. WRITE(SUICIDE_PIN, LOW);
  206. #endif
  207. #endif
  208. }
  209. void setup()
  210. {
  211. setup_powerhold();
  212. MYSERIAL.begin(BAUDRATE);
  213. SERIAL_PROTOCOLLNPGM("start");
  214. SERIAL_ECHO_START;
  215. SERIAL_ECHOPGM("Marlin: ");
  216. SERIAL_ECHOLNPGM(VERSION_STRING);
  217. #ifdef STRING_VERSION_CONFIG_H
  218. #ifdef STRING_CONFIG_H_AUTHOR
  219. SERIAL_ECHO_START;
  220. SERIAL_ECHOPGM("Configuration.h: ");
  221. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  222. SERIAL_ECHOPGM(" | Author: ");
  223. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  224. #endif
  225. #endif
  226. SERIAL_ECHO_START;
  227. SERIAL_ECHOPGM("Free Memory:");
  228. SERIAL_ECHO(freeMemory());
  229. SERIAL_ECHOPGM(" PlannerBufferBytes:");
  230. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  231. for(int8_t i = 0; i < BUFSIZE; i++)
  232. {
  233. fromsd[i] = false;
  234. }
  235. EEPROM_RetrieveSettings(); // loads data from EEPROM if available
  236. for(int8_t i=0; i < NUM_AXIS; i++)
  237. {
  238. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  239. }
  240. tp_init(); // Initialize temperature loop
  241. plan_init(); // Initialize planner;
  242. st_init(); // Initialize stepper;
  243. wd_init();
  244. setup_photpin();
  245. }
  246. void loop()
  247. {
  248. if(buflen<3)
  249. get_command();
  250. #ifdef SDSUPPORT
  251. card.checkautostart(false);
  252. #endif
  253. if(buflen)
  254. {
  255. #ifdef SDSUPPORT
  256. if(card.saving)
  257. {
  258. if(strstr(cmdbuffer[bufindr],"M29") == NULL)
  259. {
  260. card.write_command(cmdbuffer[bufindr]);
  261. SERIAL_PROTOCOLLNPGM("ok");
  262. }
  263. else
  264. {
  265. card.closefile();
  266. SERIAL_PROTOCOLLNPGM("Done saving file.");
  267. }
  268. }
  269. else
  270. {
  271. process_commands();
  272. }
  273. #else
  274. process_commands();
  275. #endif //SDSUPPORT
  276. buflen = (buflen-1);
  277. bufindr = (bufindr + 1)%BUFSIZE;
  278. }
  279. //check heater every n milliseconds
  280. manage_heater();
  281. manage_inactivity(1);
  282. checkHitEndstops();
  283. LCD_STATUS;
  284. }
  285. void get_command()
  286. {
  287. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  288. serial_char = MYSERIAL.read();
  289. if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) )
  290. {
  291. if(!serial_count) return; //if empty line
  292. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  293. if(!comment_mode){
  294. fromsd[bufindw] = false;
  295. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  296. {
  297. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  298. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  299. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  300. SERIAL_ERROR_START;
  301. SERIAL_ERRORPGM("Line Number is not Last Line Number+1, Last Line:");
  302. SERIAL_ERRORLN(gcode_LastN);
  303. //Serial.println(gcode_N);
  304. FlushSerialRequestResend();
  305. serial_count = 0;
  306. return;
  307. }
  308. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  309. {
  310. byte checksum = 0;
  311. byte count = 0;
  312. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  313. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  314. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  315. SERIAL_ERROR_START;
  316. SERIAL_ERRORPGM("checksum mismatch, Last Line:");
  317. SERIAL_ERRORLN(gcode_LastN);
  318. FlushSerialRequestResend();
  319. serial_count = 0;
  320. return;
  321. }
  322. //if no errors, continue parsing
  323. }
  324. else
  325. {
  326. SERIAL_ERROR_START;
  327. SERIAL_ERRORPGM("No Checksum with line number, Last Line:");
  328. SERIAL_ERRORLN(gcode_LastN);
  329. FlushSerialRequestResend();
  330. serial_count = 0;
  331. return;
  332. }
  333. gcode_LastN = gcode_N;
  334. //if no errors, continue parsing
  335. }
  336. else // if we don't receive 'N' but still see '*'
  337. {
  338. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  339. {
  340. SERIAL_ERROR_START;
  341. SERIAL_ERRORPGM("No Line Number with checksum, Last Line:");
  342. SERIAL_ERRORLN(gcode_LastN);
  343. serial_count = 0;
  344. return;
  345. }
  346. }
  347. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  348. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  349. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  350. case 0:
  351. case 1:
  352. case 2:
  353. case 3:
  354. #ifdef SDSUPPORT
  355. if(card.saving)
  356. break;
  357. #endif //SDSUPPORT
  358. SERIAL_PROTOCOLLNPGM("ok");
  359. break;
  360. default:
  361. break;
  362. }
  363. }
  364. bufindw = (bufindw + 1)%BUFSIZE;
  365. buflen += 1;
  366. }
  367. comment_mode = false; //for new command
  368. serial_count = 0; //clear buffer
  369. }
  370. else
  371. {
  372. if(serial_char == ';') comment_mode = true;
  373. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  374. }
  375. }
  376. #ifdef SDSUPPORT
  377. if(!card.sdprinting || serial_count!=0){
  378. return;
  379. }
  380. while( !card.eof() && buflen < BUFSIZE) {
  381. int16_t n=card.get();
  382. serial_char = (char)n;
  383. if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  384. {
  385. if(card.eof()){
  386. SERIAL_PROTOCOLLNPGM("Done printing file");
  387. stoptime=millis();
  388. char time[30];
  389. unsigned long t=(stoptime-starttime)/1000;
  390. int sec,min;
  391. min=t/60;
  392. sec=t%60;
  393. sprintf(time,"%i min, %i sec",min,sec);
  394. SERIAL_ECHO_START;
  395. SERIAL_ECHOLN(time);
  396. LCD_MESSAGE(time);
  397. card.printingHasFinished();
  398. card.checkautostart(true);
  399. }
  400. if(serial_char=='\n')
  401. comment_mode = false; //for new command
  402. if(!serial_count)
  403. {
  404. return; //if empty line
  405. }
  406. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  407. if(!comment_mode){
  408. fromsd[bufindw] = true;
  409. buflen += 1;
  410. bufindw = (bufindw + 1)%BUFSIZE;
  411. }
  412. serial_count = 0; //clear buffer
  413. }
  414. else
  415. {
  416. if(serial_char == ';') comment_mode = true;
  417. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  418. }
  419. }
  420. #endif //SDSUPPORT
  421. }
  422. float code_value()
  423. {
  424. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  425. }
  426. long code_value_long()
  427. {
  428. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  429. }
  430. bool code_seen(char code_string[]) //Return True if the string was found
  431. {
  432. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  433. }
  434. bool code_seen(char code)
  435. {
  436. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  437. return (strchr_pointer != NULL); //Return True if a character was found
  438. }
  439. #define HOMEAXIS(LETTER) \
  440. if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
  441. { \
  442. current_position[LETTER##_AXIS] = 0; \
  443. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); \
  444. destination[LETTER##_AXIS] = 1.5 * LETTER##_MAX_LENGTH * LETTER##_HOME_DIR; \
  445. feedrate = homing_feedrate[LETTER##_AXIS]; \
  446. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  447. \
  448. current_position[LETTER##_AXIS] = 0;\
  449. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  450. destination[LETTER##_AXIS] = -LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  451. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  452. \
  453. destination[LETTER##_AXIS] = 2*LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  454. feedrate = homing_feedrate[LETTER##_AXIS]/2 ; \
  455. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  456. \
  457. current_position[LETTER##_AXIS] = (LETTER##_HOME_DIR == -1) ? 0 : LETTER##_MAX_LENGTH;\
  458. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  459. destination[LETTER##_AXIS] = current_position[LETTER##_AXIS];\
  460. feedrate = 0.0;\
  461. st_synchronize();\
  462. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  463. endstops_hit_on_purpose();\
  464. }
  465. void process_commands()
  466. {
  467. unsigned long codenum; //throw away variable
  468. char *starpos = NULL;
  469. if(code_seen('G'))
  470. {
  471. switch((int)code_value())
  472. {
  473. case 0: // G0 -> G1
  474. case 1: // G1
  475. get_coordinates(); // For X Y Z E F
  476. prepare_move();
  477. //ClearToSend();
  478. return;
  479. //break;
  480. case 2: // G2 - CW ARC
  481. get_arc_coordinates();
  482. prepare_arc_move(true);
  483. return;
  484. case 3: // G3 - CCW ARC
  485. get_arc_coordinates();
  486. prepare_arc_move(false);
  487. return;
  488. case 4: // G4 dwell
  489. LCD_MESSAGEPGM("DWELL...");
  490. codenum = 0;
  491. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  492. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  493. st_synchronize();
  494. codenum += millis(); // keep track of when we started waiting
  495. previous_millis_cmd = millis();
  496. while(millis() < codenum ){
  497. manage_heater();
  498. }
  499. break;
  500. case 28: //G28 Home all Axis one at a time
  501. saved_feedrate = feedrate;
  502. saved_feedmultiply = feedmultiply;
  503. feedmultiply = 100;
  504. enable_endstops(true);
  505. for(int8_t i=0; i < NUM_AXIS; i++) {
  506. destination[i] = current_position[i];
  507. }
  508. feedrate = 0.0;
  509. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  510. #ifdef QUICK_HOME
  511. if( code_seen(axis_codes[0]) && code_seen(axis_codes[1]) ) //first diagonal move
  512. {
  513. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  514. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  515. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  516. feedrate = homing_feedrate[X_AXIS];
  517. if(homing_feedrate[Y_AXIS]<feedrate)
  518. feedrate =homing_feedrate[Y_AXIS];
  519. prepare_move();
  520. current_position[X_AXIS] = (X_HOME_DIR == -1) ? 0 : X_MAX_LENGTH;
  521. current_position[Y_AXIS] = (Y_HOME_DIR == -1) ? 0 : Y_MAX_LENGTH;
  522. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  523. destination[X_AXIS] = current_position[X_AXIS];
  524. destination[Y_AXIS] = current_position[Y_AXIS];
  525. feedrate = 0.0;
  526. st_synchronize();
  527. plan_set_position(0, 0, current_position[Z_AXIS], current_position[E_AXIS]);
  528. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  529. endstops_hit_on_purpose();
  530. }
  531. #endif
  532. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  533. {
  534. HOMEAXIS(X);
  535. }
  536. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  537. HOMEAXIS(Y);
  538. }
  539. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  540. HOMEAXIS(Z);
  541. }
  542. if(code_seen(axis_codes[X_AXIS]))
  543. {
  544. current_position[0]=code_value()+add_homeing[0];
  545. }
  546. if(code_seen(axis_codes[Y_AXIS])) {
  547. current_position[1]=code_value()+add_homeing[1];
  548. }
  549. if(code_seen(axis_codes[Z_AXIS])) {
  550. current_position[2]=code_value()+add_homeing[2];
  551. }
  552. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  553. enable_endstops(false);
  554. #endif
  555. feedrate = saved_feedrate;
  556. feedmultiply = saved_feedmultiply;
  557. previous_millis_cmd = millis();
  558. endstops_hit_on_purpose();
  559. break;
  560. case 90: // G90
  561. relative_mode = false;
  562. break;
  563. case 91: // G91
  564. relative_mode = true;
  565. break;
  566. case 92: // G92
  567. if(!code_seen(axis_codes[E_AXIS]))
  568. st_synchronize();
  569. for(int8_t i=0; i < NUM_AXIS; i++) {
  570. if(code_seen(axis_codes[i])) {
  571. current_position[i] = code_value()+add_homeing[i];
  572. if(i == E_AXIS) {
  573. current_position[i] = code_value();
  574. plan_set_e_position(current_position[E_AXIS]);
  575. }
  576. else {
  577. current_position[i] = code_value()+add_homeing[i];
  578. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  579. }
  580. }
  581. }
  582. break;
  583. }
  584. }
  585. else if(code_seen('M'))
  586. {
  587. switch( (int)code_value() )
  588. {
  589. case 17:
  590. LCD_MESSAGEPGM("No move.");
  591. enable_x();
  592. enable_y();
  593. enable_z();
  594. enable_e0();
  595. enable_e1();
  596. enable_e2();
  597. break;
  598. #ifdef SDSUPPORT
  599. case 20: // M20 - list SD card
  600. SERIAL_PROTOCOLLNPGM("Begin file list");
  601. card.ls();
  602. SERIAL_PROTOCOLLNPGM("End file list");
  603. break;
  604. case 21: // M21 - init SD card
  605. card.initsd();
  606. break;
  607. case 22: //M22 - release SD card
  608. card.release();
  609. break;
  610. case 23: //M23 - Select file
  611. starpos = (strchr(strchr_pointer + 4,'*'));
  612. if(starpos!=NULL)
  613. *(starpos-1)='\0';
  614. card.openFile(strchr_pointer + 4,true);
  615. break;
  616. case 24: //M24 - Start SD print
  617. card.startFileprint();
  618. starttime=millis();
  619. break;
  620. case 25: //M25 - Pause SD print
  621. card.pauseSDPrint();
  622. break;
  623. case 26: //M26 - Set SD index
  624. if(card.cardOK && code_seen('S')) {
  625. card.setIndex(code_value_long());
  626. }
  627. break;
  628. case 27: //M27 - Get SD status
  629. card.getStatus();
  630. break;
  631. case 28: //M28 - Start SD write
  632. starpos = (strchr(strchr_pointer + 4,'*'));
  633. if(starpos != NULL){
  634. char* npos = strchr(cmdbuffer[bufindr], 'N');
  635. strchr_pointer = strchr(npos,' ') + 1;
  636. *(starpos-1) = '\0';
  637. }
  638. card.openFile(strchr_pointer+4,false);
  639. break;
  640. case 29: //M29 - Stop SD write
  641. //processed in write to file routine above
  642. //card,saving = false;
  643. break;
  644. #endif //SDSUPPORT
  645. case 30: //M30 take time since the start of the SD print or an M109 command
  646. {
  647. stoptime=millis();
  648. char time[30];
  649. unsigned long t=(stoptime-starttime)/1000;
  650. int sec,min;
  651. min=t/60;
  652. sec=t%60;
  653. sprintf(time,"%i min, %i sec",min,sec);
  654. SERIAL_ECHO_START;
  655. SERIAL_ECHOLN(time);
  656. LCD_MESSAGE(time);
  657. autotempShutdown();
  658. }
  659. break;
  660. case 42: //M42 -Change pin status via gcode
  661. if (code_seen('S'))
  662. {
  663. int pin_status = code_value();
  664. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  665. {
  666. int pin_number = code_value();
  667. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  668. {
  669. if (sensitive_pins[i] == pin_number)
  670. {
  671. pin_number = -1;
  672. break;
  673. }
  674. }
  675. if (pin_number > -1)
  676. {
  677. pinMode(pin_number, OUTPUT);
  678. digitalWrite(pin_number, pin_status);
  679. analogWrite(pin_number, pin_status);
  680. }
  681. }
  682. }
  683. break;
  684. case 104: // M104
  685. tmp_extruder = active_extruder;
  686. if(code_seen('T')) {
  687. tmp_extruder = code_value();
  688. if(tmp_extruder >= EXTRUDERS) {
  689. SERIAL_ECHO_START;
  690. SERIAL_ECHO("M104 Invalid extruder ");
  691. SERIAL_ECHOLN(tmp_extruder);
  692. break;
  693. }
  694. }
  695. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  696. setWatch();
  697. break;
  698. case 140: // M140 set bed temp
  699. if (code_seen('S')) setTargetBed(code_value());
  700. break;
  701. case 105 : // M105
  702. tmp_extruder = active_extruder;
  703. if(code_seen('T')) {
  704. tmp_extruder = code_value();
  705. if(tmp_extruder >= EXTRUDERS) {
  706. SERIAL_ECHO_START;
  707. SERIAL_ECHO("M105 Invalid extruder ");
  708. SERIAL_ECHOLN(tmp_extruder);
  709. break;
  710. }
  711. }
  712. #if (TEMP_0_PIN > -1)
  713. SERIAL_PROTOCOLPGM("ok T:");
  714. SERIAL_PROTOCOL(degHotend(tmp_extruder));
  715. #if TEMP_BED_PIN > -1
  716. SERIAL_PROTOCOLPGM(" B:");
  717. SERIAL_PROTOCOL(degBed());
  718. #endif //TEMP_BED_PIN
  719. #else
  720. SERIAL_ERROR_START;
  721. SERIAL_ERRORLNPGM("No thermistors - no temp");
  722. #endif
  723. #ifdef PIDTEMP
  724. SERIAL_PROTOCOLPGM(" @:");
  725. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  726. #endif
  727. SERIAL_PROTOCOLLN("");
  728. return;
  729. break;
  730. case 109:
  731. {// M109 - Wait for extruder heater to reach target.
  732. tmp_extruder = active_extruder;
  733. if(code_seen('T')) {
  734. tmp_extruder = code_value();
  735. if(tmp_extruder >= EXTRUDERS) {
  736. SERIAL_ECHO_START;
  737. SERIAL_ECHO("M109 Invalid extruder ");
  738. SERIAL_ECHOLN(tmp_extruder);
  739. break;
  740. }
  741. }
  742. LCD_MESSAGEPGM("Heating...");
  743. #ifdef AUTOTEMP
  744. autotemp_enabled=false;
  745. #endif
  746. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  747. #ifdef AUTOTEMP
  748. if (code_seen('S')) autotemp_min=code_value();
  749. if (code_seen('G')) autotemp_max=code_value();
  750. if (code_seen('F'))
  751. {
  752. autotemp_factor=code_value();
  753. autotemp_enabled=true;
  754. }
  755. #endif
  756. setWatch();
  757. codenum = millis();
  758. /* See if we are heating up or cooling down */
  759. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  760. #ifdef TEMP_RESIDENCY_TIME
  761. long residencyStart;
  762. residencyStart = -1;
  763. /* continue to loop until we have reached the target temp
  764. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  765. while((residencyStart == -1) ||
  766. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  767. #else
  768. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  769. #endif //TEMP_RESIDENCY_TIME
  770. if( (millis() - codenum) > 1000UL )
  771. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  772. SERIAL_PROTOCOLPGM("T:");
  773. SERIAL_PROTOCOL( degHotend(tmp_extruder) );
  774. SERIAL_PROTOCOLPGM(" E:");
  775. SERIAL_PROTOCOL( (int)tmp_extruder );
  776. #ifdef TEMP_RESIDENCY_TIME
  777. SERIAL_PROTOCOLPGM(" W:");
  778. if(residencyStart > -1)
  779. {
  780. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  781. SERIAL_PROTOCOLLN( codenum );
  782. }
  783. else
  784. {
  785. SERIAL_PROTOCOLLN( "?" );
  786. }
  787. #else
  788. SERIAL_PROTOCOLLN("");
  789. #endif
  790. codenum = millis();
  791. }
  792. manage_heater();
  793. LCD_STATUS;
  794. if(stop_heating_wait) break;
  795. #ifdef TEMP_RESIDENCY_TIME
  796. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  797. or when current temp falls outside the hysteresis after target temp was reached */
  798. if ((residencyStart == -1 && target_direction && !isHeatingHotend(tmp_extruder)) ||
  799. (residencyStart == -1 && !target_direction && !isCoolingHotend(tmp_extruder)) ||
  800. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  801. {
  802. residencyStart = millis();
  803. }
  804. #endif //TEMP_RESIDENCY_TIME
  805. }
  806. LCD_MESSAGEPGM("Heating done.");
  807. starttime=millis();
  808. previous_millis_cmd = millis();
  809. }
  810. break;
  811. case 190: // M190 - Wait for bed heater to reach target.
  812. #if TEMP_BED_PIN > -1
  813. LCD_MESSAGEPGM("Bed Heating.");
  814. if (code_seen('S')) setTargetBed(code_value());
  815. codenum = millis();
  816. while(isHeatingBed())
  817. {
  818. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  819. {
  820. float tt=degHotend(active_extruder);
  821. SERIAL_PROTOCOLPGM("T:");
  822. SERIAL_PROTOCOL(tt);
  823. SERIAL_PROTOCOLPGM(" E:");
  824. SERIAL_PROTOCOL( (int)active_extruder );
  825. SERIAL_PROTOCOLPGM(" B:");
  826. SERIAL_PROTOCOLLN(degBed());
  827. codenum = millis();
  828. }
  829. manage_heater();
  830. }
  831. LCD_MESSAGEPGM("Bed done.");
  832. previous_millis_cmd = millis();
  833. #endif
  834. break;
  835. #if FAN_PIN > -1
  836. case 106: //M106 Fan On
  837. if (code_seen('S')){
  838. WRITE(FAN_PIN,HIGH);
  839. fanpwm=constrain(code_value(),0,255);
  840. analogWrite(FAN_PIN, fanpwm);
  841. }
  842. else {
  843. WRITE(FAN_PIN,HIGH);
  844. fanpwm=255;
  845. analogWrite(FAN_PIN, fanpwm);
  846. }
  847. break;
  848. case 107: //M107 Fan Off
  849. WRITE(FAN_PIN,LOW);
  850. analogWrite(FAN_PIN, 0);
  851. break;
  852. #endif //FAN_PIN
  853. #if (PS_ON_PIN > -1)
  854. case 80: // M80 - ATX Power On
  855. SET_OUTPUT(PS_ON_PIN); //GND
  856. break;
  857. #endif
  858. case 81: // M81 - ATX Power Off
  859. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  860. st_synchronize();
  861. suicide();
  862. #elif (PS_ON_PIN > -1)
  863. SET_INPUT(PS_ON_PIN); //Floating
  864. #endif
  865. break;
  866. case 82:
  867. axis_relative_modes[3] = false;
  868. break;
  869. case 83:
  870. axis_relative_modes[3] = true;
  871. break;
  872. case 18: //compatibility
  873. case 84: // M84
  874. if(code_seen('S')){
  875. stepper_inactive_time = code_value() * 1000;
  876. }
  877. else
  878. {
  879. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  880. if(all_axis)
  881. {
  882. st_synchronize();
  883. disable_e0();
  884. disable_e1();
  885. disable_e2();
  886. finishAndDisableSteppers();
  887. }
  888. else
  889. {
  890. st_synchronize();
  891. if(code_seen('X')) disable_x();
  892. if(code_seen('Y')) disable_y();
  893. if(code_seen('Z')) disable_z();
  894. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  895. if(code_seen('E')) {
  896. disable_e0();
  897. disable_e1();
  898. disable_e2();
  899. }
  900. #endif
  901. LCD_MESSAGEPGM("Partial Release");
  902. }
  903. }
  904. break;
  905. case 85: // M85
  906. code_seen('S');
  907. max_inactive_time = code_value() * 1000;
  908. break;
  909. case 92: // M92
  910. for(int8_t i=0; i < NUM_AXIS; i++)
  911. {
  912. if(code_seen(axis_codes[i]))
  913. axis_steps_per_unit[i] = code_value();
  914. }
  915. break;
  916. case 115: // M115
  917. 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\n");
  918. break;
  919. case 117: // M117 display message
  920. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  921. break;
  922. case 114: // M114
  923. SERIAL_PROTOCOLPGM("X:");
  924. SERIAL_PROTOCOL(current_position[X_AXIS]);
  925. SERIAL_PROTOCOLPGM("Y:");
  926. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  927. SERIAL_PROTOCOLPGM("Z:");
  928. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  929. SERIAL_PROTOCOLPGM("E:");
  930. SERIAL_PROTOCOL(current_position[E_AXIS]);
  931. SERIAL_PROTOCOLPGM(" Count X:");
  932. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  933. SERIAL_PROTOCOLPGM("Y:");
  934. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  935. SERIAL_PROTOCOLPGM("Z:");
  936. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  937. SERIAL_PROTOCOLLN("");
  938. break;
  939. case 119: // M119
  940. #if (X_MIN_PIN > -1)
  941. SERIAL_PROTOCOLPGM("x_min:");
  942. SERIAL_PROTOCOL(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  943. #endif
  944. #if (X_MAX_PIN > -1)
  945. SERIAL_PROTOCOLPGM("x_max:");
  946. SERIAL_PROTOCOL(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  947. #endif
  948. #if (Y_MIN_PIN > -1)
  949. SERIAL_PROTOCOLPGM("y_min:");
  950. SERIAL_PROTOCOL(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  951. #endif
  952. #if (Y_MAX_PIN > -1)
  953. SERIAL_PROTOCOLPGM("y_max:");
  954. SERIAL_PROTOCOL(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  955. #endif
  956. #if (Z_MIN_PIN > -1)
  957. SERIAL_PROTOCOLPGM("z_min:");
  958. SERIAL_PROTOCOL(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  959. #endif
  960. #if (Z_MAX_PIN > -1)
  961. SERIAL_PROTOCOLPGM("z_max:");
  962. SERIAL_PROTOCOL(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  963. #endif
  964. SERIAL_PROTOCOLLN("");
  965. break;
  966. //TODO: update for all axis, use for loop
  967. case 201: // M201
  968. for(int8_t i=0; i < NUM_AXIS; i++)
  969. {
  970. if(code_seen(axis_codes[i]))
  971. {
  972. max_acceleration_units_per_sq_second[i] = code_value();
  973. axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  974. }
  975. }
  976. break;
  977. #if 0 // Not used for Sprinter/grbl gen6
  978. case 202: // M202
  979. for(int8_t i=0; i < NUM_AXIS; i++) {
  980. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  981. }
  982. break;
  983. #endif
  984. case 203: // M203 max feedrate mm/sec
  985. for(int8_t i=0; i < NUM_AXIS; i++) {
  986. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  987. }
  988. break;
  989. case 204: // M204 acclereration S normal moves T filmanent only moves
  990. {
  991. if(code_seen('S')) acceleration = code_value() ;
  992. if(code_seen('T')) retract_acceleration = code_value() ;
  993. }
  994. break;
  995. 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
  996. {
  997. if(code_seen('S')) minimumfeedrate = code_value();
  998. if(code_seen('T')) mintravelfeedrate = code_value();
  999. if(code_seen('B')) minsegmenttime = code_value() ;
  1000. if(code_seen('X')) max_xy_jerk = code_value() ;
  1001. if(code_seen('Z')) max_z_jerk = code_value() ;
  1002. }
  1003. break;
  1004. case 206: // M206 additional homeing offset
  1005. for(int8_t i=0; i < 3; i++)
  1006. {
  1007. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1008. }
  1009. break;
  1010. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1011. {
  1012. if(code_seen('S'))
  1013. {
  1014. feedmultiply = code_value() ;
  1015. feedmultiplychanged=true;
  1016. }
  1017. }
  1018. break;
  1019. #ifdef PIDTEMP
  1020. case 301: // M301
  1021. {
  1022. if(code_seen('P')) Kp = code_value();
  1023. if(code_seen('I')) Ki = code_value()*PID_dT;
  1024. if(code_seen('D')) Kd = code_value()/PID_dT;
  1025. #ifdef PID_ADD_EXTRUSION_RATE
  1026. if(code_seen('C')) Kc = code_value();
  1027. #endif
  1028. updatePID();
  1029. SERIAL_PROTOCOL("ok p:");
  1030. SERIAL_PROTOCOL(Kp);
  1031. SERIAL_PROTOCOL(" i:");
  1032. SERIAL_PROTOCOL(Ki/PID_dT);
  1033. SERIAL_PROTOCOL(" d:");
  1034. SERIAL_PROTOCOL(Kd*PID_dT);
  1035. #ifdef PID_ADD_EXTRUSION_RATE
  1036. SERIAL_PROTOCOL(" c:");
  1037. SERIAL_PROTOCOL(Kc*PID_dT);
  1038. #endif
  1039. SERIAL_PROTOCOLLN("");
  1040. }
  1041. break;
  1042. #endif //PIDTEMP
  1043. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1044. {
  1045. #ifdef PHOTOGRAPH_PIN
  1046. #if (PHOTOGRAPH_PIN > -1)
  1047. const uint8_t NUM_PULSES=16;
  1048. const float PULSE_LENGTH=0.01524;
  1049. for(int i=0; i < NUM_PULSES; i++) {
  1050. WRITE(PHOTOGRAPH_PIN, HIGH);
  1051. _delay_ms(PULSE_LENGTH);
  1052. WRITE(PHOTOGRAPH_PIN, LOW);
  1053. _delay_ms(PULSE_LENGTH);
  1054. }
  1055. delay(7.33);
  1056. for(int i=0; i < NUM_PULSES; i++) {
  1057. WRITE(PHOTOGRAPH_PIN, HIGH);
  1058. _delay_ms(PULSE_LENGTH);
  1059. WRITE(PHOTOGRAPH_PIN, LOW);
  1060. _delay_ms(PULSE_LENGTH);
  1061. }
  1062. #endif
  1063. #endif
  1064. }
  1065. break;
  1066. case 302: // finish all moves
  1067. {
  1068. allow_cold_extrudes(true);
  1069. }
  1070. break;
  1071. case 400: // finish all moves
  1072. {
  1073. st_synchronize();
  1074. }
  1075. break;
  1076. case 500: // Store settings in EEPROM
  1077. {
  1078. EEPROM_StoreSettings();
  1079. }
  1080. break;
  1081. case 501: // Read settings from EEPROM
  1082. {
  1083. EEPROM_RetrieveSettings();
  1084. }
  1085. break;
  1086. case 502: // Revert to default settings
  1087. {
  1088. EEPROM_RetrieveSettings(true);
  1089. }
  1090. break;
  1091. case 503: // print settings currently in memory
  1092. {
  1093. EEPROM_printSettings();
  1094. }
  1095. break;
  1096. }
  1097. }
  1098. else if(code_seen('T'))
  1099. {
  1100. tmp_extruder = code_value();
  1101. if(tmp_extruder >= EXTRUDERS) {
  1102. SERIAL_ECHO_START;
  1103. SERIAL_ECHO("T");
  1104. SERIAL_ECHO(tmp_extruder);
  1105. SERIAL_ECHOLN("Invalid extruder");
  1106. }
  1107. else {
  1108. active_extruder = tmp_extruder;
  1109. SERIAL_ECHO_START;
  1110. SERIAL_ECHO("Active Extruder: ");
  1111. SERIAL_PROTOCOLLN((int)active_extruder);
  1112. }
  1113. }
  1114. else
  1115. {
  1116. SERIAL_ECHO_START;
  1117. SERIAL_ECHOPGM("Unknown command:\"");
  1118. SERIAL_ECHO(cmdbuffer[bufindr]);
  1119. SERIAL_ECHOLNPGM("\"");
  1120. }
  1121. ClearToSend();
  1122. }
  1123. void FlushSerialRequestResend()
  1124. {
  1125. //char cmdbuffer[bufindr][100]="Resend:";
  1126. MYSERIAL.flush();
  1127. SERIAL_PROTOCOLPGM("Resend:");
  1128. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1129. ClearToSend();
  1130. }
  1131. void ClearToSend()
  1132. {
  1133. previous_millis_cmd = millis();
  1134. #ifdef SDSUPPORT
  1135. if(fromsd[bufindr])
  1136. return;
  1137. #endif //SDSUPPORT
  1138. SERIAL_PROTOCOLLNPGM("ok");
  1139. }
  1140. void get_coordinates()
  1141. {
  1142. for(int8_t i=0; i < NUM_AXIS; i++) {
  1143. if(code_seen(axis_codes[i])) destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1144. else destination[i] = current_position[i]; //Are these else lines really needed?
  1145. }
  1146. if(code_seen('F')) {
  1147. next_feedrate = code_value();
  1148. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1149. }
  1150. }
  1151. void get_arc_coordinates()
  1152. {
  1153. get_coordinates();
  1154. if(code_seen('I')) offset[0] = code_value();
  1155. if(code_seen('J')) offset[1] = code_value();
  1156. }
  1157. void prepare_move()
  1158. {
  1159. if (min_software_endstops) {
  1160. if (destination[X_AXIS] < 0) destination[X_AXIS] = 0.0;
  1161. if (destination[Y_AXIS] < 0) destination[Y_AXIS] = 0.0;
  1162. if (destination[Z_AXIS] < 0) destination[Z_AXIS] = 0.0;
  1163. }
  1164. if (max_software_endstops) {
  1165. if (destination[X_AXIS] > X_MAX_LENGTH) destination[X_AXIS] = X_MAX_LENGTH;
  1166. if (destination[Y_AXIS] > Y_MAX_LENGTH) destination[Y_AXIS] = Y_MAX_LENGTH;
  1167. if (destination[Z_AXIS] > Z_MAX_LENGTH) destination[Z_AXIS] = Z_MAX_LENGTH;
  1168. }
  1169. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1170. for(int8_t i=0; i < NUM_AXIS; i++) {
  1171. current_position[i] = destination[i];
  1172. }
  1173. previous_millis_cmd = millis();
  1174. }
  1175. void prepare_arc_move(char isclockwise) {
  1176. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1177. // Trace the arc
  1178. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1179. // As far as the parser is concerned, the position is now == target. In reality the
  1180. // motion control system might still be processing the action and the real tool position
  1181. // in any intermediate location.
  1182. for(int8_t i=0; i < NUM_AXIS; i++) {
  1183. current_position[i] = destination[i];
  1184. }
  1185. previous_millis_cmd = millis();
  1186. }
  1187. void manage_inactivity(byte debug)
  1188. {
  1189. if( (millis() - previous_millis_cmd) > max_inactive_time )
  1190. if(max_inactive_time)
  1191. kill();
  1192. if(stepper_inactive_time) {
  1193. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  1194. {
  1195. disable_x();
  1196. disable_y();
  1197. disable_z();
  1198. disable_e0();
  1199. disable_e1();
  1200. disable_e2();
  1201. }
  1202. }
  1203. #ifdef EXTRUDER_RUNOUT_PREVENT
  1204. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1205. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1206. {
  1207. bool oldstatus=READ(E0_ENABLE_PIN);
  1208. enable_e0();
  1209. float oldepos=current_position[E_AXIS];
  1210. float oldedes=destination[E_AXIS];
  1211. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1212. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1213. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1214. current_position[E_AXIS]=oldepos;
  1215. destination[E_AXIS]=oldedes;
  1216. plan_set_e_position(oldepos);
  1217. previous_millis_cmd=millis();
  1218. st_synchronize();
  1219. WRITE(E0_ENABLE_PIN,oldstatus);
  1220. }
  1221. #endif
  1222. check_axes_activity();
  1223. }
  1224. void kill()
  1225. {
  1226. cli(); // Stop interrupts
  1227. disable_heater();
  1228. disable_x();
  1229. disable_y();
  1230. disable_z();
  1231. disable_e0();
  1232. disable_e1();
  1233. disable_e2();
  1234. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1235. SERIAL_ERROR_START;
  1236. SERIAL_ERRORLNPGM("Printer halted. kill() called !!");
  1237. LCD_MESSAGEPGM("KILLED. ");
  1238. suicide();
  1239. while(1); // Wait for reset
  1240. }