The Technology Partnership / Heater

Dependents:   LEX-Demo-Firmware-Logging LEX-Demo-Firmware-Logging

Files at this revision

API Documentation at this revision

Comitter:
omatthews
Date:
Tue Jul 30 21:32:49 2019 +0000
Parent:
18:f5d26d3d532f
Child:
20:e2a504521b46
Child:
22:2d34a03ae57e
Commit message:
Added guards

Changed in this revision

Heater.cpp Show annotated file Show diff for this revision Revisions of this file
Heater.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
+}
--- a/Heater.h	Sun Jul 28 11:49:57 2019 +0000
+++ b/Heater.h	Tue Jul 30 21:32:49 2019 +0000
@@ -11,17 +11,18 @@
 #include "ADS8568_ADC.h"
 #include "FastPWM.h"
 
-#define MEAS_DELAY          50     // measurement delay for ADC
-#define WAIT_DELAY          3      // wait delay for ADC
+#define MEAS_DELAY          50     // measurement delay for ADC in us
+#define WAIT_DELAY          3      // wait delay for ADC in ms
 
 #define N_ROLL_AVG          1      // rolling average for R values
 #define ALL_CH              15     //value of convst bus to read all chanels simultaneosly
 #define Kp                  30.0f   //proportional gain
-#define Ti                  1.0f       //Integration time
+#define Ti                  0.75f       //Integration time
 #define Kd                  0.0f   //Differentiator gain
-#define WIND_UP_LIMIT       0.005f //The change in error which turns off the integral term
-#define PWM_PERIOD          5     //Period of Pwm for the motor
-#define LOG_LIM          100     //Period of Pwm for the motor
+#define WIND_UP_LIMIT       0.01f //The change in error which turns off the integral term
+#define PWM_PERIOD          5     //Period of Pwm for the motor in us
+#define LOG_LIM             100       //Number of reads before the result is logged
+#define GUARD_PWM_RATIO     0.25    //Ratio of guard duty cycle to heater duty cycle
 
 
 class Heater
@@ -31,25 +32,26 @@
         /** Constructor
                  * @param i_port, the current port in the ADC
                  * @param v_port, the voltage port in the ADC
-                 * @param * drive, a pointer to the motor drive
+                 * @param * drive, a pointer to the heater drive
+         * @param * guard, a pointer to the guard
                  * @param * corr_grad, the gradient of the linear relationship between resistance and temperature
                  * @param * corr_int, the intercept of the linear relationship between resistance and temperature
                  * @param R_ref (default value 1), optional parameter sets the target value for R                 
         **/         
-        Heater(const int i_port, const int v_port, FastPWM * drive, const float corr_grad, const float corr_int, float R_ref = 1);
+        Heater(const int i_port, const int v_port, FastPWM * drive, FastPWM * guard, const float corr_grad, const float corr_int, float R_ref = 1);
         
         //Public member functions
 
         void read();            //Updates the resistance and error values for the heater
         void hold(const int hold_time);  //Holds R_ref for hold_time miliseconds
         void ramp_R(const int ramp_time, const float R_final, const float R_start);   //Ramps for ramp_time miliseconds from R_start to R_final
-        void ramp_T(const int ramp_time, const float T_final, const float T_start);   //Same as above but with T
+        void ramp_T(const int ramp_time, const float T_final, const float T_start);   //Same as above but with T (T is in degrees celcius)
         void output() const;  //Prints the current state of the heater
         void turn_on();     //Turns the heater on
         void turn_off();    //Turns the heater off
         
                 
-        //Linear conversions between temperature and resistance
+        //Linear conversions between temperature and resistance. Note that temperature is always in celcius
         float R_to_T(const float R) const;
         float T_to_R(const float T) const;
 
@@ -83,6 +85,7 @@
         const int i_port;     //ADC port which corresponds to current measurements
         const int v_port;     //ADC port which corresponds to voltage measurements
         FastPWM * drive;    //Pointer to the driver
+    FastPWM * guard;    //Pointer to the guard
 
         //Heater correlations give temperature for a given resistance (assume linear relationship)
         const float corr_grad;
@@ -90,5 +93,4 @@
       
 };
     
-#endif
-    
\ No newline at end of file
+#endif
\ No newline at end of file