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

Marlin.pde 41KB

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