diff options
Diffstat (limited to 'Sprinter/Sprinter.pde')
-rw-r--r-- | Sprinter/Sprinter.pde | 136 |
1 files changed, 98 insertions, 38 deletions
diff --git a/Sprinter/Sprinter.pde b/Sprinter/Sprinter.pde index a941531..5daee2f 100644 --- a/Sprinter/Sprinter.pde +++ b/Sprinter/Sprinter.pde @@ -43,6 +43,7 @@ // M27 - Report SD print status // M28 - Start SD write (M28 filename.g) // M29 - Stop SD write +// M42 - Set output on free pins, on a non pwm pin (over pin 13 on an arduino mega) use S255 to turn it on and S0 to turn it off. Use P to decide the pin (M42 P23 S255) would turn pin 23 on // M81 - Turn off Power Supply // M82 - Set E codes absolute (default) // M83 - Set E codes relative while in Absolute Coordinates (G90) mode @@ -126,6 +127,9 @@ int tt = 0, bt = 0; int temp_iState_min = 100 * -PID_INTEGRAL_DRIVE_MAX / PID_IGAIN; int temp_iState_max = 100 * PID_INTEGRAL_DRIVE_MAX / PID_IGAIN; #endif +#ifndef HEATER_CURRENT + #define HEATER_CURRENT 255 +#endif #ifdef SMOOTHING uint32_t nma = 0; #endif @@ -312,11 +316,7 @@ void setup() SET_OUTPUT(E_STEP_PIN); #endif #ifdef RAMP_ACCELERATION - for(int i=0; i < NUM_AXIS; i++){ - axis_max_interval[i] = 100000000.0 / (max_start_speed_units_per_second[i] * axis_steps_per_unit[i]); - axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i]; - axis_travel_steps_per_sqr_second[i] = max_travel_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i]; - } + setup_acceleration(); #endif #ifdef HEATER_USES_MAX6675 @@ -726,6 +726,31 @@ inline void process_commands() //savetosd = false; break; #endif + case 42: //M42 -Change pin status via gcode + if (code_seen('S')) + { + int pin_status = code_value(); + if (code_seen('P') && pin_status >= 0 && pin_status <= 255) + { + int pin_number = code_value(); + for(int i = 0; i < sizeof(sensitive_pins); i++) + { + if (sensitive_pins[i] == pin_number) + { + pin_number = -1; + break; + } + } + + if (pin_number > -1) + { + pinMode(pin_number, OUTPUT); + digitalWrite(pin_number, pin_status); + analogWrite(pin_number, pin_status); + } + } + } + break; case 104: // M104 if (code_seen('S')) target_raw = temp2analogh(code_value()); #ifdef WATCHPERIOD @@ -763,7 +788,7 @@ inline void process_commands() #endif return; //break; - case 109: // M109 - Wait for extruder heater to reach target. + case 109: { // M109 - Wait for extruder heater to reach target. if (code_seen('S')) target_raw = temp2analogh(code_value()); #ifdef WATCHPERIOD if(target_raw>current_raw){ @@ -774,16 +799,39 @@ inline void process_commands() } #endif codenum = millis(); - while(current_raw < target_raw) { - if( (millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up. + + /* See if we are heating up or cooling down */ + bool target_direction = (current_raw < target_raw); // true if heating, false if cooling + + #ifdef TEMP_RESIDENCY_TIME + long residencyStart; + residencyStart = -1; + /* continue to loop until we have reached the target temp + _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */ + while( (target_direction ? (current_raw < target_raw) : (current_raw > target_raw)) + || (residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) { + #else + while ( target_direction ? (current_raw < target_raw) : (current_raw > target_raw) ) { + #endif + if( (millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up/cooling down { Serial.print("T:"); - Serial.println( analog2temp(current_raw) ); - codenum = millis(); + Serial.println( analog2temp(current_raw) ); + codenum = millis(); } manage_heater(); - } - break; + #ifdef TEMP_RESIDENCY_TIME + /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time + or when current temp falls outside the hysteresis after target temp was reached */ + if ( (residencyStart == -1 && target_direction && current_raw >= target_raw) + || (residencyStart == -1 && !target_direction && current_raw <= target_raw) + || (residencyStart > -1 && labs(analog2temp(current_raw) - analog2temp(target_raw)) > TEMP_HYSTERESIS) ) { + residencyStart = millis(); + } + #endif + } + } + break; case 190: // M190 - Wait bed for heater to reach target. #if TEMP_1_PIN > -1 if (code_seen('S')) target_bed_raw = temp2analogh(code_value()); @@ -845,15 +893,10 @@ inline void process_commands() if(code_seen(axis_codes[i])) axis_steps_per_unit[i] = code_value(); } - //Update start speed intervals and axis order. TODO: refactor axis_max_interval[] calculation into a function, as it - // should also be used in setup() as well #ifdef RAMP_ACCELERATION - long temp_max_intervals[NUM_AXIS]; - for(int i=0; i < NUM_AXIS; i++) { - axis_max_interval[i] = 100000000.0 / (max_start_speed_units_per_second[i] * axis_steps_per_unit[i]);//TODO: do this for - // all steps_per_unit related variables - } + setup_acceleration(); #endif + break; case 115: // M115 Serial.print("FIRMWARE_NAME:Sprinter FIRMWARE_URL:http%%3A/github.com/kliment/Sprinter/ PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1 UUID:"); @@ -872,27 +915,27 @@ inline void process_commands() case 119: // M119 #if (X_MIN_PIN > -1) Serial.print("x_min:"); - Serial.print((READ(X_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L "); + Serial.print((READ(X_MIN_PIN)^X_ENDSTOP_INVERT)?"H ":"L "); #endif #if (X_MAX_PIN > -1) Serial.print("x_max:"); - Serial.print((READ(X_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L "); + Serial.print((READ(X_MAX_PIN)^X_ENDSTOP_INVERT)?"H ":"L "); #endif #if (Y_MIN_PIN > -1) Serial.print("y_min:"); - Serial.print((READ(Y_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L "); + Serial.print((READ(Y_MIN_PIN)^Y_ENDSTOP_INVERT)?"H ":"L "); #endif #if (Y_MAX_PIN > -1) Serial.print("y_max:"); - Serial.print((READ(Y_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L "); + Serial.print((READ(Y_MAX_PIN)^Y_ENDSTOP_INVERT)?"H ":"L "); #endif #if (Z_MIN_PIN > -1) Serial.print("z_min:"); - Serial.print((READ(Z_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L "); + Serial.print((READ(Z_MIN_PIN)^Z_ENDSTOP_INVERT)?"H ":"L "); #endif #if (Z_MAX_PIN > -1) Serial.print("z_max:"); - Serial.print((READ(Z_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L "); + Serial.print((READ(Z_MAX_PIN)^Z_ENDSTOP_INVERT)?"H ":"L "); #endif Serial.println(""); break; @@ -1047,22 +1090,22 @@ inline void linear_move(unsigned long axis_steps_remaining[]) // make linear mov else WRITE(E_DIR_PIN,INVERT_E_DIR); movereset: #if (X_MIN_PIN > -1) - if(!move_direction[0]) if(READ(X_MIN_PIN) != ENDSTOPS_INVERTING) axis_steps_remaining[0]=0; + if(!move_direction[0]) if(READ(X_MIN_PIN) != X_ENDSTOP_INVERT) axis_steps_remaining[0]=0; #endif #if (Y_MIN_PIN > -1) - if(!move_direction[1]) if(READ(Y_MIN_PIN) != ENDSTOPS_INVERTING) axis_steps_remaining[1]=0; + if(!move_direction[1]) if(READ(Y_MIN_PIN) != Y_ENDSTOP_INVERT) axis_steps_remaining[1]=0; #endif #if (Z_MIN_PIN > -1) - if(!move_direction[2]) if(READ(Z_MIN_PIN) != ENDSTOPS_INVERTING) axis_steps_remaining[2]=0; + if(!move_direction[2]) if(READ(Z_MIN_PIN) != Z_ENDSTOP_INVERT) axis_steps_remaining[2]=0; #endif #if (X_MAX_PIN > -1) - if(move_direction[0]) if(READ(X_MAX_PIN) != ENDSTOPS_INVERTING) axis_steps_remaining[0]=0; + if(move_direction[0]) if(READ(X_MAX_PIN) != X_ENDSTOP_INVERT) axis_steps_remaining[0]=0; #endif #if (Y_MAX_PIN > -1) - if(move_direction[1]) if(READ(Y_MAX_PIN) != ENDSTOPS_INVERTING) axis_steps_remaining[1]=0; + if(move_direction[1]) if(READ(Y_MAX_PIN) != Y_ENDSTOP_INVERT) axis_steps_remaining[1]=0; #endif # if(Z_MAX_PIN > -1) - if(move_direction[2]) if(READ(Z_MAX_PIN) != ENDSTOPS_INVERTING) axis_steps_remaining[2]=0; + if(move_direction[2]) if(READ(Z_MAX_PIN) != Z_ENDSTOP_INVERT) axis_steps_remaining[2]=0; #endif @@ -1238,22 +1281,22 @@ inline void linear_move(unsigned long axis_steps_remaining[]) // make linear mov //If there are x or y steps remaining, perform Bresenham algorithm if(axis_steps_remaining[primary_axis]) { #if (X_MIN_PIN > -1) - if(!move_direction[0]) if(READ(X_MIN_PIN) != ENDSTOPS_INVERTING) if(primary_axis==0) break; else if(axis_steps_remaining[0]) axis_steps_remaining[0]=0; + if(!move_direction[0]) if(READ(X_MIN_PIN) != X_ENDSTOP_INVERT) if(primary_axis==0) break; else if(axis_steps_remaining[0]) axis_steps_remaining[0]=0; #endif #if (Y_MIN_PIN > -1) - if(!move_direction[1]) if(READ(Y_MIN_PIN) != ENDSTOPS_INVERTING) if(primary_axis==1) break; else if(axis_steps_remaining[1]) axis_steps_remaining[1]=0; + if(!move_direction[1]) if(READ(Y_MIN_PIN) != Y_ENDSTOP_INVERT) if(primary_axis==1) break; else if(axis_steps_remaining[1]) axis_steps_remaining[1]=0; #endif #if (X_MAX_PIN > -1) - if(move_direction[0]) if(READ(X_MAX_PIN) != ENDSTOPS_INVERTING) if(primary_axis==0) break; else if(axis_steps_remaining[0]) axis_steps_remaining[0]=0; + if(move_direction[0]) if(READ(X_MAX_PIN) != X_ENDSTOP_INVERT) if(primary_axis==0) break; else if(axis_steps_remaining[0]) axis_steps_remaining[0]=0; #endif #if (Y_MAX_PIN > -1) - if(move_direction[1]) if(READ(Y_MAX_PIN) != ENDSTOPS_INVERTING) if(primary_axis==1) break; else if(axis_steps_remaining[1]) axis_steps_remaining[1]=0; + if(move_direction[1]) if(READ(Y_MAX_PIN) != Y_ENDSTOP_INVERT) if(primary_axis==1) break; else if(axis_steps_remaining[1]) axis_steps_remaining[1]=0; #endif #if (Z_MIN_PIN > -1) - if(!move_direction[2]) if(READ(Z_MIN_PIN) != ENDSTOPS_INVERTING) if(primary_axis==2) break; else if(axis_steps_remaining[2]) axis_steps_remaining[2]=0; + if(!move_direction[2]) if(READ(Z_MIN_PIN) != Z_ENDSTOP_INVERT) if(primary_axis==2) break; else if(axis_steps_remaining[2]) axis_steps_remaining[2]=0; #endif #if (Z_MAX_PIN > -1) - if(move_direction[2]) if(READ(Z_MAX_PIN) != ENDSTOPS_INVERTING) if(primary_axis==2) break; else if(axis_steps_remaining[2]) axis_steps_remaining[2]=0; + if(move_direction[2]) if(READ(Z_MAX_PIN) != Z_ENDSTOP_INVERT) if(primary_axis==2) break; else if(axis_steps_remaining[2]) axis_steps_remaining[2]=0; #endif timediff = micros() * 100 - axis_previous_micros[primary_axis]; if(timediff<0){//check for overflow @@ -1406,6 +1449,7 @@ void manage_heater() if(watch_raw + 1 >= current_raw){ target_raw = 0; WRITE(HEATER_0_PIN,LOW); + analogWrite(HEATER_0_PIN, 0); #if LED_PIN>-1 WRITE(LED_PIN,LOW); #endif @@ -1432,11 +1476,12 @@ void manage_heater() iTerm = (PID_IGAIN * temp_iState) / 100; dTerm = (PID_DGAIN * (current_raw - temp_dState)) / 100; temp_dState = current_raw; - analogWrite(HEATER_0_PIN, constrain(pTerm + iTerm - dTerm, 0, PID_MAX)); + analogWrite(HEATER_0_PIN, constrain(pTerm + iTerm - dTerm, 0, HEATER_CURRENT)); #else if(current_raw >= target_raw) { WRITE(HEATER_0_PIN,LOW); + analogWrite(HEATER_0_PIN, 0); #if LED_PIN>-1 WRITE(LED_PIN,LOW); #endif @@ -1444,6 +1489,7 @@ void manage_heater() else { WRITE(HEATER_0_PIN,HIGH); + analogWrite(HEATER_0_PIN, HEATER_CURRENT); #if LED_PIN > -1 WRITE(LED_PIN,HIGH); #endif @@ -1478,7 +1524,11 @@ void manage_heater() #endif + #ifdef MINTEMP + if(current_bed_raw >= target_bed_raw || current_bed_raw < minttemp) + #else if(current_bed_raw >= target_bed_raw) + #endif { WRITE(HEATER_1_PIN,LOW); } @@ -1585,6 +1635,16 @@ if( (millis()-previous_millis_cmd) > max_inactive_time ) if(max_inactive_time) if( (millis()-previous_millis_cmd) > stepper_inactive_time ) if(stepper_inactive_time) { disable_x(); disable_y(); disable_z(); disable_e(); } } +#ifdef RAMP_ACCELERATION +void setup_acceleration() { + for (int i=0; i < NUM_AXIS; i++) { + axis_max_interval[i] = 100000000.0 / (max_start_speed_units_per_second[i] * axis_steps_per_unit[i]); + axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i]; + axis_travel_steps_per_sqr_second[i] = max_travel_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i]; + } +} +#endif + #ifdef DEBUG void log_message(char* message) { Serial.print("DEBUG"); Serial.println(message); |