Heater for threaded program

Dependents:   LEX_Threaded_Programming

Revision:
19:fccdd7127f94
Parent:
18:f5d26d3d532f
Child:
20:2d34a03ae57e
Child:
22:085b53e06065
--- a/Heater.cpp	Sun Jul 28 11:49:57 2019 +0000
+++ b/Heater.cpp	Tue Jul 30 21:32:49 2019 +0000
@@ -18,8 +18,8 @@
 
 
     
-Heater::Heater(const int i_port, const int v_port, FastPWM * drive, const float corr_grad, const float corr_int, float R_ref)
-    :R_ref(R_ref),i_port(i_port),v_port(v_port),drive(drive),corr_grad(corr_grad),corr_int(corr_int) {}
+Heater::Heater(const int i_port, const int v_port, FastPWM * drive, FastPWM * guard, const float corr_grad, const float corr_int, float R_ref)
+    :R_ref(R_ref),i_port(i_port),v_port(v_port),drive(drive),guard(guard),corr_grad(corr_grad),corr_int(corr_int) {}
 
 
 // Convert from R to T using the linear relationship - T = R * corr_grad + corr_int
@@ -66,10 +66,10 @@
     error = R_ref - R;
     //error_diff = (error - error_prev)/WAIT_DELAY;
     
-    //Avoid integral windup by limiting integral error past actuation saturation 
-    if (error*Kp > WIND_UP_LIMIT) {error_integrated += WIND_UP_LIMIT/Kp;}
-    else if (error*Kp < -WIND_UP_LIMIT) {error_integrated -= WIND_UP_LIMIT/Kp;}
-    else {error_integrated += error;}
+    //Avoid integral windup by limiting error past actuation saturation (actuator does saturate for any negative error, but  to ensure integrated error can decrease, the limit has been set to the negative of the positive limit
+    //if (error*Kp > WIND_UP_LIMIT) {error_integrated += WIND_UP_LIMIT/Kp;}
+    //else if (error*Kp < -WIND_UP_LIMIT) {error_integrated -= WIND_UP_LIMIT/Kp;}
+    if (error < WIND_UP_LIMIT && error > -WIND_UP_LIMIT) {error_integrated += error;}
     
     
     //Output the error every LOG_LIM reads
@@ -94,6 +94,7 @@
     {
         read();
         drive->write((double) (Kp * (error + error_integrated/Ti)));
+        guard->write((double) (Kp * GUARD_PWM_RATIO * (error + error_integrated/Ti)));
         wait_ms(WAIT_DELAY);    //Wait before reading again  
     }
 }
@@ -131,7 +132,11 @@
 
 
 
-void Heater::Set_R_ref(float R) {R_ref = R;}
+void Heater::Set_R_ref(float R) 
+{
+    R_ref = R;
+    error_integrated = 0;
+}
 void Heater::Set_T_ref(float T_ref) {R_ref = T_to_R(T_ref);}
 void Heater::Set_D(float D) {drive->write(D);}
 
@@ -141,6 +146,14 @@
 float Heater::Get_R() const {return R;}
 float Heater::Get_T() const {return R_to_T(R);}
 
-void Heater::turn_on () {*drive = 1;}
+void Heater::turn_on () 
+{
+    *drive = 1;
+    *guard = GUARD_PWM_RATIO;
+}
 
-void Heater::turn_off () {*drive = 0;}
+void Heater::turn_off () 
+{
+    *drive = 0;
+    *guard = 0;
+}