My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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