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

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