Heater for threaded program

Dependents:   LEX_Threaded_Programming_V3

Revision:
39:5dffedda7a0d
Parent:
38:3a2778f1f90a
Child:
40:2be211a07d09
Child:
41:b1602c68abcd
--- a/Heater.cpp	Tue Sep 24 15:14:50 2019 +0000
+++ b/Heater.cpp	Mon Sep 30 16:09:43 2019 +0000
@@ -52,17 +52,21 @@
 
     //Only allow positive integrated errors and limit change in integrated error
     //to help avoid integral windup
-    if (error_integrated < 0.0) error_integrated = 0.0;
-    if (abs(error) > thermal.pid_wind_up_limit_ohm) error = error * thermal.pid_wind_up_limit_ohm / abs(error);    
+     if (abs(error) > thermal.pid_wind_up_limit_ohm) error = error * thermal.pid_wind_up_limit_ohm / abs(error);    
 }
 
 void Heater::update()
 {
     //Update PWM from setpoint and resistance
-    double duty_cycle = thermal.pid_kp_mho * (error + error_integrated/thermal.pid_integral_time_ms);
+    double duty_cycle = thermal.pid_kp_mho * error;
+    
+    if (thermal.pid_integral_time_ms > 0)  // set integral time to zero to have no integral term
+        duty_cycle += thermal.pid_kp_mho * error_integrated/thermal.pid_integral_time_ms;
     
-    if (duty_cycle > thermal.pid_pwm_limit) duty_cycle = thermal.pid_pwm_limit;
-    else if (duty_cycle < 0) duty_cycle = 0;
+    if (duty_cycle > thermal.pid_pwm_limit)
+        duty_cycle = thermal.pid_pwm_limit;  
+    else if (duty_cycle < 0)
+        duty_cycle = 0;
         
     drive->write(duty_cycle);
     guard->write(duty_cycle * thermal.guard_drive_ratio);