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

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