My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Marlin.pde 41KB

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