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

Marlin.pde 41KB

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