Heater for threaded program

Dependents:   LEX_Threaded_Programming_V3

Revision:
43:d34ac9d8648c
Parent:
42:166d9bc7675e
Child:
44:e358867319f6
--- a/Heater.cpp	Thu Oct 17 10:29:13 2019 +0000
+++ b/Heater.cpp	Mon Nov 11 08:29:51 2019 +0000
@@ -9,6 +9,9 @@
 #include "Heater.h"
 #include "ADS8568_ADC.h"
 
+#define GUARD_DUTY_CYCLE_REF   0.1   // start to reduce guard drive ratio at this duty cycle for main heater
+#define GUARD_DUTY_CYCLE_SLOPE 0.5   // slope for reducing guard drive ratio at increasing duty cycle for main heater
+
 Heater::Heater(const int i_port, const int v_port, const float cal_a, const float cal_b, FastPWM * drive, FastPWM * guard, ADS8568_ADC * adc, DigitalIn adc_busy, const memspcr_ThermalConfiguration & thermal)
     :thermal(thermal), i_port(i_port), v_port(v_port), cal_a(cal_a), cal_b(cal_b), drive(drive), guard(guard), adc(adc), adc_busy(adc_busy)
 {
@@ -27,10 +30,17 @@
 {
     //Reads R and then resets the drive back to its previous value
     int i = 0;
+    int guard_wait_us;
+    int main_wait_us;
+    
     double drive_prev = drive->read();     //Store previous value of drive
         
-    *drive = 1.0f;                         //Turn the driver on for the measurement
-    wait_us(thermal.settling_time_us);     //Wait for ADC to settle
+    guard_wait_us = (float)thermal.settling_time_us * thermal.guard_drive_ratio;
+    main_wait_us = thermal.settling_time_us - guard_wait_us;
+       
+    *drive = 1.0f;                         //Turn the main heater on for the measurement
+   
+    wait_us(main_wait_us);     //Wait for ADC to settle
     adc->start_conversion(ADC_CONV_ALL_CH);     
     
     //Incremental back off until ADC is free
@@ -39,7 +49,7 @@
         i++;
     }
 
-    drive->write(0);       //Reset the duty cycle back to what it was
+    drive->write(0);       //Set duty cycle to zero
 
     //Get voltage, current and R values from the ADC conversion
     adc->read_channels();
@@ -59,7 +69,6 @@
 
 void Heater::update()
 {
-
     //Get error
     error = R_ref - R;
 
@@ -71,14 +80,21 @@
         if (abs(error) > thermal.pid_wind_up_limit_ohm) error = error * thermal.pid_wind_up_limit_ohm / abs(error);    
         error_integrated += error * (float) thermal.thermal_control_loop_interval_ms;
         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;
         
+    //Reduce guard drive ratio to be lower at higher duty cycles: full value up to GUARD_DUTY_CYCLE_REF, then decreases with gradient GUARD_DUTY_CYCLE_SLOPE
+    
+    double guard_drive_ratio_factor = 1 + GUARD_DUTY_CYCLE_SLOPE*(1 - duty_cycle/GUARD_DUTY_CYCLE_REF);
+    
+    if (guard_drive_ratio_factor > 1) guard_drive_ratio_factor = 1;
+    if (guard_drive_ratio_factor < 0) guard_drive_ratio_factor = 0; 
+    
     drive->write(duty_cycle);
-    guard->write(duty_cycle * thermal.guard_drive_ratio);
+    guard->write(duty_cycle * thermal.guard_drive_ratio * guard_drive_ratio_factor);
 }
 
 void Heater::Set_ref(float R)