Heater for threaded program

Dependents:   LEX_Threaded_Programming_V3

Revision:
36:a8130bd29349
Parent:
35:5acf01897ed6
Child:
37:688dad0e1b76
diff -r 5acf01897ed6 -r a8130bd29349 Heater.cpp
--- a/Heater.cpp	Mon Sep 23 15:41:15 2019 +0000
+++ b/Heater.cpp	Mon Sep 23 17:30:05 2019 +0000
@@ -46,7 +46,6 @@
         R = cal_a + cal_b*R;  //Convert to Ohms
     }
         
-    //R = cal_b;  // for debugging
     //Get error values
     error = R_ref - R;
 
@@ -55,7 +54,7 @@
 
     if (abs(error) > thermal.pid_wind_up_limit_ohm) {error = error * thermal.pid_wind_up_limit_ohm / abs(error);}
     
-    error_integrated += error;
+    error_integrated += error* (float) thermal.control_loop_interval_ms;
 
     if (error_integrated < 0.0) {
         error_integrated = 0.0;
@@ -65,8 +64,13 @@
 void Heater::update()
 {
     //Update PWM from setpoint and resistance
-    double duty_cycle = (double) thermal.pid_kp_mho * (error + error_integrated/thermal.pid_integral_time_ms);
-    if (duty_cycle > thermal.pid_pwm_limit) duty_cycle = thermal.pid_pwm_limit;
+    double duty_cycle = thermal.pid_kp_mho * ( error + 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;
+        
     drive->write(duty_cycle);
     guard->write(duty_cycle * thermal.guard_drive_ratio);
 }