4 Reprap firmware based on Sprinter and grbl.
5 Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 This firmware is a mashup between Sprinter and grbl.
23 (https://github.com/kliment/Sprinter)
24 (https://github.com/simen/grbl/tree)
26 It has preliminary support for Matthew Roberts advance algorithm
27 http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
35 #include "temperature.h"
36 #include "motion_control.h"
37 #include "cardreader.h"
39 #include "EEPROMwrite.h"
41 #include "pins_arduino.h"
43 #define VERSION_STRING "1.0.0 RC2"
45 // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
46 // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
51 // G1 - Coordinated Movement X Y Z E
54 // G4 - Dwell S<seconds> or P<milliseconds>
55 // G10 - retract filament according to settings of M207
56 // G11 - retract recover filament according to settings of M208
57 // G28 - Home all Axis
58 // G90 - Use Absolute Coordinates
59 // G91 - Use Relative Coordinates
60 // G92 - Set current position to cordinates given
63 // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
65 // M104 - Set extruder target temp
66 // M105 - Read current temp
69 // M109 - Wait for extruder current temp to reach target temp.
70 // M114 - Display current position
73 // M17 - Enable/Power all stepper motors
74 // M18 - Disable all stepper motors; same as M84
77 // M22 - Release SD card
78 // M23 - Select SD file (M23 filename.g)
79 // M24 - Start/resume SD print
80 // M25 - Pause SD print
81 // M26 - Set SD position in bytes (M26 S12345)
82 // M27 - Report SD print status
83 // M28 - Start SD write (M28 filename.g)
84 // M29 - Stop SD write
85 // M30 - Delete file from SD (M30 filename.g)
86 // M31 - Output time since last M109 or SD card start to serial
87 // M42 - Change pin status via gcode
88 // M80 - Turn on Power Supply
89 // M81 - Turn off Power Supply
90 // M82 - Set E codes absolute (default)
91 // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
92 // M84 - Disable steppers until next move,
93 // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
94 // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
95 // M92 - Set axis_steps_per_unit - same syntax as G92
96 // M114 - Output current position to serial port
97 // M115 - Capabilities string
98 // M117 - display message
99 // M119 - Output Endstop status to serial port
100 // M140 - Set bed target temp
101 // M190 - Wait for bed current temp to reach target temp.
102 // M200 - Set filament diameter
103 // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
104 // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
105 // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
106 // 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
107 // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk
108 // M206 - set additional homeing offset
109 // M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
110 // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
111 // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
112 // M220 S<factor in percent>- set speed factor override percentage
113 // M221 S<factor in percent>- set extrude factor override percentage
114 // M240 - Trigger a camera to take a photograph
115 // M301 - Set PID parameters P I and D
116 // M302 - Allow cold extrudes
117 // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
118 // M400 - Finish all moves
119 // M500 - stores paramters in EEPROM
120 // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
121 // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
122 // M503 - print the current settings (from memory not from eeprom)
123 // M999 - Restart after being stopped by error
125 //Stepper Movement Variables
127 //===========================================================================
128 //=============================imported variables============================
129 //===========================================================================
132 //===========================================================================
133 //=============================public variables=============================
134 //===========================================================================
138 float homing_feedrate[] = HOMING_FEEDRATE;
139 bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
140 volatile int feedmultiply=100; //100->1 200->2
141 int saved_feedmultiply;
142 volatile bool feedmultiplychanged=false;
143 volatile int extrudemultiply=100; //100->1 200->2
144 float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
145 float add_homeing[3]={0,0,0};
146 float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
147 float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
148 uint8_t active_extruder = 0;
149 unsigned char FanSpeed=0;
152 bool autoretract_enabled=true;
153 bool retracted=false;
154 float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
155 float retract_recover_length=0, retract_recover_feedrate=8*60;
158 //===========================================================================
159 //=============================private variables=============================
160 //===========================================================================
161 const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
162 static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
163 static float offset[3] = {0.0, 0.0, 0.0};
164 static bool home_all_axis = true;
165 static float feedrate = 1500.0, next_feedrate, saved_feedrate;
166 static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
168 static bool relative_mode = false; //Determines Absolute or Relative Coordinates
169 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.
171 static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
172 static bool fromsd[BUFSIZE];
173 static int bufindr = 0;
174 static int bufindw = 0;
175 static int buflen = 0;
177 static char serial_char;
178 static int serial_count = 0;
179 static boolean comment_mode = false;
180 static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
182 const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
184 //static float tt = 0;
185 //static float bt = 0;
187 //Inactivity shutdown variables
188 static unsigned long previous_millis_cmd = 0;
189 static unsigned long max_inactive_time = 0;
190 static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
192 static unsigned long starttime=0;
193 static unsigned long stoptime=0;
195 static uint8_t tmp_extruder;
200 //===========================================================================
201 //=============================ROUTINES=============================
202 //===========================================================================
204 void get_arc_coordinates();
207 extern unsigned int __bss_end;
208 extern unsigned int __heap_start;
209 extern void *__brkval;
214 if((int)__brkval == 0)
215 free_memory = ((int)&free_memory) - ((int)&__bss_end);
217 free_memory = ((int)&free_memory) - ((int)__brkval);
223 //adds an command to the main command buffer
224 //thats really done in a non-safe way.
225 //needs overworking someday
226 void enquecommand(const char *cmd)
230 //this is dangerous if a mixing of serial and this happsens
231 strcpy(&(cmdbuffer[bufindw][0]),cmd);
233 SERIAL_ECHOPGM("enqueing \"");
234 SERIAL_ECHO(cmdbuffer[bufindw]);
235 SERIAL_ECHOLNPGM("\"");
236 bufindw= (bufindw + 1)%BUFSIZE;
243 #ifdef PHOTOGRAPH_PIN
244 #if (PHOTOGRAPH_PIN > -1)
245 SET_OUTPUT(PHOTOGRAPH_PIN);
246 WRITE(PHOTOGRAPH_PIN, LOW);
251 void setup_powerhold()
254 #if (SUICIDE_PIN> -1)
255 SET_OUTPUT(SUICIDE_PIN);
256 WRITE(SUICIDE_PIN, HIGH);
264 #if (SUICIDE_PIN> -1)
265 SET_OUTPUT(SUICIDE_PIN);
266 WRITE(SUICIDE_PIN, LOW);
274 MYSERIAL.begin(BAUDRATE);
275 SERIAL_PROTOCOLLNPGM("start");
278 // Check startup - does nothing if bootloader sets MCUSR to 0
280 if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
281 if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
282 if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
283 if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
284 if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
287 SERIAL_ECHOPGM(MSG_MARLIN);
288 SERIAL_ECHOLNPGM(VERSION_STRING);
289 #ifdef STRING_VERSION_CONFIG_H
290 #ifdef STRING_CONFIG_H_AUTHOR
292 SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
293 SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
294 SERIAL_ECHOPGM(MSG_AUTHOR);
295 SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
299 SERIAL_ECHOPGM(MSG_FREE_MEMORY);
300 SERIAL_ECHO(freeMemory());
301 SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
302 SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
303 for(int8_t i = 0; i < BUFSIZE; i++)
308 EEPROM_RetrieveSettings(); // loads data from EEPROM if available
310 for(int8_t i=0; i < NUM_AXIS; i++)
312 axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
316 tp_init(); // Initialize temperature loop
317 plan_init(); // Initialize planner;
318 st_init(); // Initialize stepper;
328 if(buflen < (BUFSIZE-1))
331 card.checkautostart(false);
338 if(strstr(cmdbuffer[bufindr],"M29") == NULL)
340 card.write_command(cmdbuffer[bufindr]);
341 SERIAL_PROTOCOLLNPGM(MSG_OK);
346 SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
357 bufindr = (bufindr + 1)%BUFSIZE;
359 //check heater every n milliseconds
361 manage_inactivity(1);
368 while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
369 serial_char = MYSERIAL.read();
370 if(serial_char == '\n' ||
371 serial_char == '\r' ||
372 (serial_char == ':' && comment_mode == false) ||
373 serial_count >= (MAX_CMD_SIZE - 1) )
375 if(!serial_count) { //if empty line
376 comment_mode = false; //for new command
379 cmdbuffer[bufindw][serial_count] = 0; //terminate string
381 comment_mode = false; //for new command
382 fromsd[bufindw] = false;
383 if(strstr(cmdbuffer[bufindw], "N") != NULL)
385 strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
386 gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
387 if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
389 SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
390 SERIAL_ERRORLN(gcode_LastN);
391 //Serial.println(gcode_N);
392 FlushSerialRequestResend();
397 if(strstr(cmdbuffer[bufindw], "*") != NULL)
401 while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
402 strchr_pointer = strchr(cmdbuffer[bufindw], '*');
404 if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
406 SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
407 SERIAL_ERRORLN(gcode_LastN);
408 FlushSerialRequestResend();
412 //if no errors, continue parsing
417 SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
418 SERIAL_ERRORLN(gcode_LastN);
419 FlushSerialRequestResend();
424 gcode_LastN = gcode_N;
425 //if no errors, continue parsing
427 else // if we don't receive 'N' but still see '*'
429 if((strstr(cmdbuffer[bufindw], "*") != NULL))
432 SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
433 SERIAL_ERRORLN(gcode_LastN);
438 if((strstr(cmdbuffer[bufindw], "G") != NULL)){
439 strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
440 switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
445 if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
450 SERIAL_PROTOCOLLNPGM(MSG_OK);
453 SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
454 LCD_MESSAGEPGM(MSG_STOPPED);
462 bufindw = (bufindw + 1)%BUFSIZE;
465 serial_count = 0; //clear buffer
469 if(serial_char == ';') comment_mode = true;
470 if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
474 if(!card.sdprinting || serial_count!=0){
477 while( !card.eof() && buflen < BUFSIZE) {
478 int16_t n=card.get();
479 serial_char = (char)n;
480 if(serial_char == '\n' ||
481 serial_char == '\r' ||
482 (serial_char == ':' && comment_mode == false) ||
483 serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
486 SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
489 unsigned long t=(stoptime-starttime)/1000;
493 sprintf(time,"%i min, %i sec",min,sec);
497 card.printingHasFinished();
498 card.checkautostart(true);
503 comment_mode = false; //for new command
504 return; //if empty line
506 cmdbuffer[bufindw][serial_count] = 0; //terminate string
507 // if(!comment_mode){
508 fromsd[bufindw] = true;
510 bufindw = (bufindw + 1)%BUFSIZE;
512 comment_mode = false; //for new command
513 serial_count = 0; //clear buffer
517 if(serial_char == ';') comment_mode = true;
518 if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
529 return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
532 long code_value_long()
534 return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
537 bool code_seen(char code_string[]) //Return True if the string was found
539 return (strstr(cmdbuffer[bufindr], code_string) != NULL);
542 bool code_seen(char code)
544 strchr_pointer = strchr(cmdbuffer[bufindr], code);
545 return (strchr_pointer != NULL); //Return True if a character was found
548 #define DEFINE_PGM_READ_ANY(type, reader) \
549 static inline float pgm_read_any(const type *p) \
550 { return pgm_read_##reader##_near(p); }
552 DEFINE_PGM_READ_ANY(float, float);
554 #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
555 static const PROGMEM type array##_P[3] = \
556 { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
557 static inline type array(int axis) \
558 { return pgm_read_any(&array##_P[axis]); }
560 XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
561 XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
562 XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
564 static void axis_is_at_home(int axis) {
565 current_position[axis] = base_home_pos(axis) + add_homeing[axis];
566 min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
567 max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
570 #define HOMEAXIS(LETTER) \
571 if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
573 current_position[LETTER##_AXIS] = 0; \
574 plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); \
575 destination[LETTER##_AXIS] = 1.5 * LETTER##_MAX_LENGTH * LETTER##_HOME_DIR; \
576 feedrate = homing_feedrate[LETTER##_AXIS]; \
577 plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
580 current_position[LETTER##_AXIS] = 0;\
581 plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
582 destination[LETTER##_AXIS] = -LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
583 plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
586 destination[LETTER##_AXIS] = 2*LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
587 feedrate = homing_feedrate[LETTER##_AXIS]/2 ; \
588 plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
591 axis_is_at_home(LETTER##_AXIS); \
592 destination[LETTER##_AXIS] = current_position[LETTER##_AXIS]; \
594 endstops_hit_on_purpose();\
597 void process_commands()
599 unsigned long codenum; //throw away variable
600 char *starpos = NULL;
604 switch((int)code_value())
608 if(Stopped == false) {
609 get_coordinates(); // For X Y Z E F
615 case 2: // G2 - CW ARC
616 if(Stopped == false) {
617 get_arc_coordinates();
618 prepare_arc_move(true);
621 case 3: // G3 - CCW ARC
622 if(Stopped == false) {
623 get_arc_coordinates();
624 prepare_arc_move(false);
628 LCD_MESSAGEPGM(MSG_DWELL);
630 if(code_seen('P')) codenum = code_value(); // milliseconds to wait
631 if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
634 codenum += millis(); // keep track of when we started waiting
635 previous_millis_cmd = millis();
636 while(millis() < codenum ){
638 manage_inactivity(1);
643 case 10: // G10 retract
646 destination[X_AXIS]=current_position[X_AXIS];
647 destination[Y_AXIS]=current_position[Y_AXIS];
648 destination[Z_AXIS]=current_position[Z_AXIS];
649 current_position[Z_AXIS]+=-retract_zlift;
650 destination[E_AXIS]=current_position[E_AXIS]-retract_length;
651 feedrate=retract_feedrate;
657 case 11: // G10 retract_recover
660 destination[X_AXIS]=current_position[X_AXIS];
661 destination[Y_AXIS]=current_position[Y_AXIS];
662 destination[Z_AXIS]=current_position[Z_AXIS];
664 current_position[Z_AXIS]+=retract_zlift;
665 current_position[E_AXIS]+=-retract_recover_length;
666 feedrate=retract_recover_feedrate;
672 case 28: //G28 Home all Axis one at a time
673 saved_feedrate = feedrate;
674 saved_feedmultiply = feedmultiply;
676 previous_millis_cmd = millis();
678 enable_endstops(true);
680 for(int8_t i=0; i < NUM_AXIS; i++) {
681 destination[i] = current_position[i];
684 home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
686 #if Z_HOME_DIR > 0 // If homing away from BED do Z first
687 if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
693 if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
695 current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
697 plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
698 destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
699 feedrate = homing_feedrate[X_AXIS];
700 if(homing_feedrate[Y_AXIS]<feedrate)
701 feedrate =homing_feedrate[Y_AXIS];
702 plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
705 axis_is_at_home(X_AXIS);
706 axis_is_at_home(Y_AXIS);
707 plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
708 destination[X_AXIS] = current_position[X_AXIS];
709 destination[Y_AXIS] = current_position[Y_AXIS];
710 plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
713 endstops_hit_on_purpose();
717 if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
722 if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
726 #if Z_HOME_DIR < 0 // If homing towards BED do Z last
727 if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
732 if(code_seen(axis_codes[X_AXIS]))
734 if(code_value_long() != 0) {
735 current_position[X_AXIS]=code_value()+add_homeing[0];
739 if(code_seen(axis_codes[Y_AXIS])) {
740 if(code_value_long() != 0) {
741 current_position[Y_AXIS]=code_value()+add_homeing[1];
745 if(code_seen(axis_codes[Z_AXIS])) {
746 if(code_value_long() != 0) {
747 current_position[Z_AXIS]=code_value()+add_homeing[2];
750 plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
752 #ifdef ENDSTOPS_ONLY_FOR_HOMING
753 enable_endstops(false);
756 feedrate = saved_feedrate;
757 feedmultiply = saved_feedmultiply;
758 previous_millis_cmd = millis();
759 endstops_hit_on_purpose();
762 relative_mode = false;
765 relative_mode = true;
768 if(!code_seen(axis_codes[E_AXIS]))
770 for(int8_t i=0; i < NUM_AXIS; i++) {
771 if(code_seen(axis_codes[i])) {
773 current_position[i] = code_value();
774 plan_set_e_position(current_position[E_AXIS]);
777 current_position[i] = code_value()+add_homeing[i];
778 plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
786 else if(code_seen('M'))
788 switch( (int)code_value() )
791 case 0: // M0 - Unconditional stop - Wait for user button press on LCD
792 case 1: // M1 - Conditional stop - Wait for user button press on LCD
794 LCD_MESSAGEPGM(MSG_USERWAIT);
796 if(code_seen('P')) codenum = code_value(); // milliseconds to wait
797 if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
800 previous_millis_cmd = millis();
803 codenum += millis(); // keep track of when we started waiting
804 while(millis() < codenum && !CLICKED){
806 manage_inactivity(1);
812 manage_inactivity(1);
820 LCD_MESSAGEPGM(MSG_NO_MOVE);
830 case 20: // M20 - list SD card
831 SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
833 SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
835 case 21: // M21 - init SD card
840 case 22: //M22 - release SD card
844 case 23: //M23 - Select file
845 starpos = (strchr(strchr_pointer + 4,'*'));
848 card.openFile(strchr_pointer + 4,true);
850 case 24: //M24 - Start SD print
851 card.startFileprint();
854 case 25: //M25 - Pause SD print
857 case 26: //M26 - Set SD index
858 if(card.cardOK && code_seen('S')) {
859 card.setIndex(code_value_long());
862 case 27: //M27 - Get SD status
865 case 28: //M28 - Start SD write
866 starpos = (strchr(strchr_pointer + 4,'*'));
868 char* npos = strchr(cmdbuffer[bufindr], 'N');
869 strchr_pointer = strchr(npos,' ') + 1;
872 card.openFile(strchr_pointer+4,false);
874 case 29: //M29 - Stop SD write
875 //processed in write to file routine above
876 //card,saving = false;
878 case 30: //M30 <filename> Delete File
881 starpos = (strchr(strchr_pointer + 4,'*'));
883 char* npos = strchr(cmdbuffer[bufindr], 'N');
884 strchr_pointer = strchr(npos,' ') + 1;
887 card.removeFile(strchr_pointer + 4);
893 case 31: //M31 take time since the start of the SD print or an M109 command
897 unsigned long t=(stoptime-starttime)/1000;
901 sprintf(time,"%i min, %i sec",min,sec);
908 case 42: //M42 -Change pin status via gcode
911 int pin_status = code_value();
912 if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
914 int pin_number = code_value();
915 for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
917 if (sensitive_pins[i] == pin_number)
926 pinMode(pin_number, OUTPUT);
927 digitalWrite(pin_number, pin_status);
928 analogWrite(pin_number, pin_status);
934 tmp_extruder = active_extruder;
936 tmp_extruder = code_value();
937 if(tmp_extruder >= EXTRUDERS) {
939 SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
940 SERIAL_ECHOLN(tmp_extruder);
944 if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
947 case 140: // M140 set bed temp
948 if (code_seen('S')) setTargetBed(code_value());
951 tmp_extruder = active_extruder;
953 tmp_extruder = code_value();
954 if(tmp_extruder >= EXTRUDERS) {
956 SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
957 SERIAL_ECHOLN(tmp_extruder);
961 #if (TEMP_0_PIN > -1)
962 SERIAL_PROTOCOLPGM("ok T:");
963 SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
964 SERIAL_PROTOCOLPGM(" /");
965 SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
966 #if TEMP_BED_PIN > -1
967 SERIAL_PROTOCOLPGM(" B:");
968 SERIAL_PROTOCOL_F(degBed(),1);
969 SERIAL_PROTOCOLPGM(" /");
970 SERIAL_PROTOCOL_F(degTargetBed(),1);
971 #endif //TEMP_BED_PIN
974 SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
977 SERIAL_PROTOCOLPGM(" @:");
978 SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
980 SERIAL_PROTOCOLLN("");
984 {// M109 - Wait for extruder heater to reach target.
985 tmp_extruder = active_extruder;
987 tmp_extruder = code_value();
988 if(tmp_extruder >= EXTRUDERS) {
990 SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
991 SERIAL_ECHOLN(tmp_extruder);
995 LCD_MESSAGEPGM(MSG_HEATING);
997 autotemp_enabled=false;
999 if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
1001 if (code_seen('S')) autotemp_min=code_value();
1002 if (code_seen('B')) autotemp_max=code_value();
1005 autotemp_factor=code_value();
1006 autotemp_enabled=true;
1013 /* See if we are heating up or cooling down */
1014 bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
1016 #ifdef TEMP_RESIDENCY_TIME
1017 long residencyStart;
1018 residencyStart = -1;
1019 /* continue to loop until we have reached the target temp
1020 _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
1021 while((residencyStart == -1) ||
1022 (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
1024 while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
1025 #endif //TEMP_RESIDENCY_TIME
1026 if( (millis() - codenum) > 1000UL )
1027 { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
1028 SERIAL_PROTOCOLPGM("T:");
1029 SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
1030 SERIAL_PROTOCOLPGM(" E:");
1031 SERIAL_PROTOCOL((int)tmp_extruder);
1032 #ifdef TEMP_RESIDENCY_TIME
1033 SERIAL_PROTOCOLPGM(" W:");
1034 if(residencyStart > -1)
1036 codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
1037 SERIAL_PROTOCOLLN( codenum );
1041 SERIAL_PROTOCOLLN( "?" );
1044 SERIAL_PROTOCOLLN("");
1049 manage_inactivity(1);
1051 #ifdef TEMP_RESIDENCY_TIME
1052 /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
1053 or when current temp falls outside the hysteresis after target temp was reached */
1054 if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
1055 (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
1056 (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
1058 residencyStart = millis();
1060 #endif //TEMP_RESIDENCY_TIME
1062 LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
1064 previous_millis_cmd = millis();
1067 case 190: // M190 - Wait for bed heater to reach target.
1068 #if TEMP_BED_PIN > -1
1069 LCD_MESSAGEPGM(MSG_BED_HEATING);
1070 if (code_seen('S')) setTargetBed(code_value());
1072 while(isHeatingBed())
1074 if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
1076 float tt=degHotend(active_extruder);
1077 SERIAL_PROTOCOLPGM("T:");
1078 SERIAL_PROTOCOL(tt);
1079 SERIAL_PROTOCOLPGM(" E:");
1080 SERIAL_PROTOCOL((int)active_extruder);
1081 SERIAL_PROTOCOLPGM(" B:");
1082 SERIAL_PROTOCOL_F(degBed(),1);
1083 SERIAL_PROTOCOLLN("");
1087 manage_inactivity(1);
1090 LCD_MESSAGEPGM(MSG_BED_DONE);
1091 previous_millis_cmd = millis();
1096 case 106: //M106 Fan On
1097 if (code_seen('S')){
1098 FanSpeed=constrain(code_value(),0,255);
1104 case 107: //M107 Fan Off
1109 #if (PS_ON_PIN > -1)
1110 case 80: // M80 - ATX Power On
1111 SET_OUTPUT(PS_ON_PIN); //GND
1112 WRITE(PS_ON_PIN, LOW);
1116 case 81: // M81 - ATX Power Off
1118 #if defined SUICIDE_PIN && SUICIDE_PIN > -1
1121 #elif (PS_ON_PIN > -1)
1122 SET_INPUT(PS_ON_PIN); //Floating
1127 axis_relative_modes[3] = false;
1130 axis_relative_modes[3] = true;
1132 case 18: //compatibility
1135 stepper_inactive_time = code_value() * 1000;
1139 bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
1146 finishAndDisableSteppers();
1151 if(code_seen('X')) disable_x();
1152 if(code_seen('Y')) disable_y();
1153 if(code_seen('Z')) disable_z();
1154 #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
1155 if(code_seen('E')) {
1161 LCD_MESSAGEPGM(MSG_PART_RELEASE);
1167 max_inactive_time = code_value() * 1000;
1170 for(int8_t i=0; i < NUM_AXIS; i++)
1172 if(code_seen(axis_codes[i]))
1175 float value = code_value();
1177 float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
1178 max_e_jerk *= factor;
1179 max_feedrate[i] *= factor;
1180 axis_steps_per_sqr_second[i] *= factor;
1182 axis_steps_per_unit[i] = value;
1185 axis_steps_per_unit[i] = code_value();
1190 SerialprintPGM(MSG_M115_REPORT);
1192 case 117: // M117 display message
1193 LCD_MESSAGE(cmdbuffer[bufindr]+5);
1196 SERIAL_PROTOCOLPGM("X:");
1197 SERIAL_PROTOCOL(current_position[X_AXIS]);
1198 SERIAL_PROTOCOLPGM("Y:");
1199 SERIAL_PROTOCOL(current_position[Y_AXIS]);
1200 SERIAL_PROTOCOLPGM("Z:");
1201 SERIAL_PROTOCOL(current_position[Z_AXIS]);
1202 SERIAL_PROTOCOLPGM("E:");
1203 SERIAL_PROTOCOL(current_position[E_AXIS]);
1205 SERIAL_PROTOCOLPGM(MSG_COUNT_X);
1206 SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
1207 SERIAL_PROTOCOLPGM("Y:");
1208 SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
1209 SERIAL_PROTOCOLPGM("Z:");
1210 SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
1212 SERIAL_PROTOCOLLN("");
1215 enable_endstops(false) ;
1218 enable_endstops(true) ;
1221 #if (X_MIN_PIN > -1)
1222 SERIAL_PROTOCOLPGM(MSG_X_MIN);
1223 SERIAL_PROTOCOL(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
1225 #if (X_MAX_PIN > -1)
1226 SERIAL_PROTOCOLPGM(MSG_X_MAX);
1227 SERIAL_PROTOCOL(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
1229 #if (Y_MIN_PIN > -1)
1230 SERIAL_PROTOCOLPGM(MSG_Y_MIN);
1231 SERIAL_PROTOCOL(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
1233 #if (Y_MAX_PIN > -1)
1234 SERIAL_PROTOCOLPGM(MSG_Y_MAX);
1235 SERIAL_PROTOCOL(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
1237 #if (Z_MIN_PIN > -1)
1238 SERIAL_PROTOCOLPGM(MSG_Z_MIN);
1239 SERIAL_PROTOCOL(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
1241 #if (Z_MAX_PIN > -1)
1242 SERIAL_PROTOCOLPGM(MSG_Z_MAX);
1243 SERIAL_PROTOCOL(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
1245 SERIAL_PROTOCOLLN("");
1247 //TODO: update for all axis, use for loop
1249 for(int8_t i=0; i < NUM_AXIS; i++)
1251 if(code_seen(axis_codes[i]))
1253 max_acceleration_units_per_sq_second[i] = code_value();
1254 axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
1258 #if 0 // Not used for Sprinter/grbl gen6
1260 for(int8_t i=0; i < NUM_AXIS; i++) {
1261 if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
1265 case 203: // M203 max feedrate mm/sec
1266 for(int8_t i=0; i < NUM_AXIS; i++) {
1267 if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
1270 case 204: // M204 acclereration S normal moves T filmanent only moves
1272 if(code_seen('S')) acceleration = code_value() ;
1273 if(code_seen('T')) retract_acceleration = code_value() ;
1276 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
1278 if(code_seen('S')) minimumfeedrate = code_value();
1279 if(code_seen('T')) mintravelfeedrate = code_value();
1280 if(code_seen('B')) minsegmenttime = code_value() ;
1281 if(code_seen('X')) max_xy_jerk = code_value() ;
1282 if(code_seen('Z')) max_z_jerk = code_value() ;
1283 if(code_seen('E')) max_e_jerk = code_value() ;
1286 case 206: // M206 additional homeing offset
1287 for(int8_t i=0; i < 3; i++)
1289 if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
1293 case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
1297 retract_length = code_value() ;
1301 retract_feedrate = code_value() ;
1305 retract_zlift = code_value() ;
1308 case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
1312 retract_recover_length = code_value() ;
1316 retract_recover_feedrate = code_value() ;
1320 case 209: // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
1324 int t= code_value() ;
1327 case 0: autoretract_enabled=false;retracted=false;break;
1328 case 1: autoretract_enabled=true;retracted=false;break;
1331 SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
1332 SERIAL_ECHO(cmdbuffer[bufindr]);
1333 SERIAL_ECHOLNPGM("\"");
1339 case 220: // M220 S<factor in percent>- set speed factor override percentage
1343 feedmultiply = code_value() ;
1344 feedmultiplychanged=true;
1348 case 221: // M221 S<factor in percent>- set extrude factor override percentage
1352 extrudemultiply = code_value() ;
1360 if(code_seen('P')) Kp = code_value();
1361 if(code_seen('I')) Ki = code_value()*PID_dT;
1362 if(code_seen('D')) Kd = code_value()/PID_dT;
1363 #ifdef PID_ADD_EXTRUSION_RATE
1364 if(code_seen('C')) Kc = code_value();
1367 SERIAL_PROTOCOL(MSG_OK);
1368 SERIAL_PROTOCOL(" p:");
1369 SERIAL_PROTOCOL(Kp);
1370 SERIAL_PROTOCOL(" i:");
1371 SERIAL_PROTOCOL(Ki/PID_dT);
1372 SERIAL_PROTOCOL(" d:");
1373 SERIAL_PROTOCOL(Kd*PID_dT);
1374 #ifdef PID_ADD_EXTRUSION_RATE
1375 SERIAL_PROTOCOL(" c:");
1376 SERIAL_PROTOCOL(Kc*PID_dT);
1378 SERIAL_PROTOCOLLN("");
1382 case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
1384 #ifdef PHOTOGRAPH_PIN
1385 #if (PHOTOGRAPH_PIN > -1)
1386 const uint8_t NUM_PULSES=16;
1387 const float PULSE_LENGTH=0.01524;
1388 for(int i=0; i < NUM_PULSES; i++) {
1389 WRITE(PHOTOGRAPH_PIN, HIGH);
1390 _delay_ms(PULSE_LENGTH);
1391 WRITE(PHOTOGRAPH_PIN, LOW);
1392 _delay_ms(PULSE_LENGTH);
1395 for(int i=0; i < NUM_PULSES; i++) {
1396 WRITE(PHOTOGRAPH_PIN, HIGH);
1397 _delay_ms(PULSE_LENGTH);
1398 WRITE(PHOTOGRAPH_PIN, LOW);
1399 _delay_ms(PULSE_LENGTH);
1406 case 302: // allow cold extrudes
1408 allow_cold_extrudes(true);
1411 case 303: // M303 PID autotune
1414 if (code_seen('S')) temp=code_value();
1418 case 400: // M400 finish all moves
1423 case 500: // Store settings in EEPROM
1425 EEPROM_StoreSettings();
1428 case 501: // Read settings from EEPROM
1430 EEPROM_RetrieveSettings();
1433 case 502: // Revert to default settings
1435 EEPROM_RetrieveSettings(true);
1438 case 503: // print settings currently in memory
1440 EEPROM_printSettings();
1443 case 999: // Restart after being stopped
1445 gcode_LastN = Stopped_gcode_LastN;
1446 FlushSerialRequestResend();
1451 else if(code_seen('T'))
1453 tmp_extruder = code_value();
1454 if(tmp_extruder >= EXTRUDERS) {
1457 SERIAL_ECHO(tmp_extruder);
1458 SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
1461 active_extruder = tmp_extruder;
1463 SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
1464 SERIAL_PROTOCOLLN((int)active_extruder);
1471 SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
1472 SERIAL_ECHO(cmdbuffer[bufindr]);
1473 SERIAL_ECHOLNPGM("\"");
1479 void FlushSerialRequestResend()
1481 //char cmdbuffer[bufindr][100]="Resend:";
1483 SERIAL_PROTOCOLPGM(MSG_RESEND);
1484 SERIAL_PROTOCOLLN(gcode_LastN + 1);
1490 previous_millis_cmd = millis();
1495 SERIAL_PROTOCOLLNPGM(MSG_OK);
1498 void get_coordinates()
1500 bool seen[4]={false,false,false,false};
1501 for(int8_t i=0; i < NUM_AXIS; i++) {
1502 if(code_seen(axis_codes[i]))
1504 destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
1507 else destination[i] = current_position[i]; //Are these else lines really needed?
1509 if(code_seen('F')) {
1510 next_feedrate = code_value();
1511 if(next_feedrate > 0.0) feedrate = next_feedrate;
1514 if(autoretract_enabled)
1515 if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
1517 float echange=destination[E_AXIS]-current_position[E_AXIS];
1518 if(echange<-MIN_RETRACT) //retract
1523 destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
1524 //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
1525 float correctede=-echange-retract_length;
1526 //to generate the additional steps, not the destination is changed, but inversely the current position
1527 current_position[E_AXIS]+=-correctede;
1528 feedrate=retract_feedrate;
1534 if(echange>MIN_RETRACT) //retract_recover
1538 //current_position[Z_AXIS]+=-retract_zlift;
1539 //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
1540 float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
1541 current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
1542 feedrate=retract_recover_feedrate;
1551 void get_arc_coordinates()
1554 if(code_seen('I')) {
1555 offset[0] = code_value();
1560 if(code_seen('J')) {
1561 offset[1] = code_value();
1568 void clamp_to_software_endstops(float target[3])
1570 if (min_software_endstops) {
1571 if (destination[X_AXIS] < min_pos[X_AXIS]) destination[X_AXIS] = min_pos[X_AXIS];
1572 if (destination[Y_AXIS] < min_pos[Y_AXIS]) destination[Y_AXIS] = min_pos[Y_AXIS];
1573 if (destination[Z_AXIS] < min_pos[Z_AXIS]) destination[Z_AXIS] = min_pos[Z_AXIS];
1576 if (max_software_endstops) {
1577 if (destination[X_AXIS] > max_pos[X_AXIS]) destination[X_AXIS] = max_pos[X_AXIS];
1578 if (destination[Y_AXIS] > max_pos[Y_AXIS]) destination[Y_AXIS] = max_pos[Y_AXIS];
1579 if (destination[Z_AXIS] > max_pos[Z_AXIS]) destination[Z_AXIS] = max_pos[Z_AXIS];
1585 clamp_to_software_endstops(destination);
1587 previous_millis_cmd = millis();
1588 plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
1589 for(int8_t i=0; i < NUM_AXIS; i++) {
1590 current_position[i] = destination[i];
1594 void prepare_arc_move(char isclockwise) {
1595 float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
1598 mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
1600 // As far as the parser is concerned, the position is now == target. In reality the
1601 // motion control system might still be processing the action and the real tool position
1602 // in any intermediate location.
1603 for(int8_t i=0; i < NUM_AXIS; i++) {
1604 current_position[i] = destination[i];
1606 previous_millis_cmd = millis();
1609 #ifdef CONTROLLERFAN_PIN
1610 unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
1611 unsigned long lastMotorCheck = 0;
1613 void controllerFan()
1615 if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
1617 lastMotorCheck = millis();
1619 if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
1621 || !READ(E2_ENABLE_PIN)
1624 || !READ(E2_ENABLE_PIN)
1626 || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
1628 lastMotor = millis(); //... set time to NOW so the fan will turn on
1631 if ((millis() - lastMotor) >= (CONTROLLERFAN_SEC*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
1633 WRITE(CONTROLLERFAN_PIN, LOW); //... turn the fan off
1637 WRITE(CONTROLLERFAN_PIN, HIGH); //... turn the fan on
1643 void manage_inactivity(byte debug)
1645 if( (millis() - previous_millis_cmd) > max_inactive_time )
1646 if(max_inactive_time)
1648 if(stepper_inactive_time) {
1649 if( (millis() - previous_millis_cmd) > stepper_inactive_time )
1651 if(blocks_queued() == false) {
1661 #ifdef CONTROLLERFAN_PIN
1662 controllerFan(); //Check if fan should be turned on to cool stepper drivers down
1664 #ifdef EXTRUDER_RUNOUT_PREVENT
1665 if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
1666 if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
1668 bool oldstatus=READ(E0_ENABLE_PIN);
1670 float oldepos=current_position[E_AXIS];
1671 float oldedes=destination[E_AXIS];
1672 plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
1673 current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
1674 EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
1675 current_position[E_AXIS]=oldepos;
1676 destination[E_AXIS]=oldedes;
1677 plan_set_e_position(oldepos);
1678 previous_millis_cmd=millis();
1680 WRITE(E0_ENABLE_PIN,oldstatus);
1683 check_axes_activity();
1688 cli(); // Stop interrupts
1698 if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
1700 SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
1701 LCD_MESSAGEPGM(MSG_KILLED);
1703 while(1); // Wait for reset
1709 if(Stopped == false) {
1711 Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
1713 SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
1714 LCD_MESSAGEPGM(MSG_STOPPED);
1718 bool IsStopped() { return Stopped; };
1721 void setPwmFrequency(uint8_t pin, int val)
1724 switch(digitalPinToTimer(pin))
1730 // TCCR0B &= ~(CS00 | CS01 | CS02);
1738 // TCCR1B &= ~(CS10 | CS11 | CS12);
1746 TCCR2 &= ~(CS10 | CS11 | CS12);
1754 TCCR2B &= ~(CS20 | CS21 | CS22);
1763 TCCR3B &= ~(CS30 | CS31 | CS32);
1772 TCCR4B &= ~(CS40 | CS41 | CS42);
1781 TCCR5B &= ~(CS50 | CS51 | CS52);