summaryrefslogtreecommitdiff
path: root/Sprinter
diff options
context:
space:
mode:
Diffstat (limited to 'Sprinter')
-rw-r--r--Sprinter/Configuration.h4
-rw-r--r--Sprinter/Sprinter.pde20
2 files changed, 17 insertions, 7 deletions
diff --git a/Sprinter/Configuration.h b/Sprinter/Configuration.h
index df55c7d..e246e65 100644
--- a/Sprinter/Configuration.h
+++ b/Sprinter/Configuration.h
@@ -118,7 +118,7 @@ char uuid[] = "00000000-0000-0000-0000-000000000000";
//// PID settings:
// Uncomment the following line to enable PID support. This is untested and could be disastrous. Be careful.
-//#define PIDTEMP
+//#define PIDTEMP 1
#ifdef PIDTEMP
#define PID_INTEGRAL_DRIVE_MAX 80 // too big, and heater will lag after changing temperature, too small and it might not compensate enough for long-term errors
#define PID_PGAIN 1280 //256 is 1.0 // value of 5.0 means that error of 20C is changing it almost halfway of the PWM range
@@ -127,7 +127,7 @@ char uuid[] = "00000000-0000-0000-0000-000000000000";
// magic formula 1, to get approximate "zero error" PWM duty. It is most likely linear formula
#define HEATER_DUTY_FOR_SETPOINT(setpoint) (22+1*setpoint)
// magic formula 2, to make led brightness approximately linear
-#define LED_PWM_FOR_BRIGTHNESS(brightness) ((64*brightness-1384)/(300-brightness))
+#define LED_PWM_FOR_BRIGHTNESS(brightness) ((64*brightness-1384)/(300-brightness))
#endif
// Change this value (range 1-255) to limit the current to the nozzle
diff --git a/Sprinter/Sprinter.pde b/Sprinter/Sprinter.pde
index 2f8cac9..7e9b3ba 100644
--- a/Sprinter/Sprinter.pde
+++ b/Sprinter/Sprinter.pde
@@ -1499,12 +1499,22 @@ void manage_heater()
int delta_temp = current_temp - prev_temp;
prev_temp = current_temp;
pTerm = ((long)PID_PGAIN * error) / 256;
- temp_iState += error;
- temp_iState = constrain(temp_iState, temp_iState_min, temp_iState_max);
- iTerm = ((long)PID_IGAIN * temp_iState) / 256;
- dTerm = ((long)PID_DGAIN * delta_temp) / 256;
const int H0 = min(HEATER_DUTY_FOR_SETPOINT(target_temp),HEATER_CURRENT);
- heater_duty = H0 + constrain(pTerm + iTerm - dTerm, -H0, HEATER_CURRENT-H0);
+ heater_duty = H0 + pTerm;
+ if(error < 20){
+ temp_iState += error;
+ temp_iState = constrain(temp_iState, temp_iState_min, temp_iState_max);
+ iTerm = ((long)PID_IGAIN * temp_iState) / 256;
+ heater_duty += iTerm;
+ }
+ int prev_error = abs(target_temp - prev_temp);
+ int log3 = 1; // discrete logarithm base 3, plus 1
+ if(prev_error > 81){ prev_error /= 81; log3 += 4; }
+ if(prev_error > 9){ prev_error /= 9; log3 += 2; }
+ if(prev_error > 3){ prev_error /= 3; log3 ++; }
+ dTerm = ((long)PID_DGAIN * delta_temp) / (256*log3);
+ heater_duty += dTerm;
+ heater_duty = constrain(heater_duty, 0, HEATER_CURRENT);
analogWrite(HEATER_0_PIN, heater_duty);
#if LED_PIN>-1
analogWrite(LED_PIN, constrain(LED_PWM_FOR_BRIGHTNESS(heater_duty),0,255));