Heater for threaded program

Dependents:   LEX_Threaded_Programming_V3

Revision:
34:294adcc3e4b2
Parent:
33:52ab0641f2e6
Child:
35:5acf01897ed6
diff -r 52ab0641f2e6 -r 294adcc3e4b2 Heater.cpp
--- a/Heater.cpp	Thu Sep 19 16:13:30 2019 +0000
+++ b/Heater.cpp	Fri Sep 20 14:53:54 2019 +0000
@@ -9,8 +9,6 @@
 #include "Heater.h"
 #include "ADS8568_ADC.h"
 
-
-
 Heater::Heater(const int i_port, const int v_port, FastPWM * drive, FastPWM * guard, ADS8568_ADC * adc, DigitalIn adc_busy, const memspcr_ThermalConfiguration & thermal)
     :thermal(thermal), i_port(i_port), v_port(v_port), drive(drive), guard(guard), adc(adc), adc_busy(adc_busy)
 {
@@ -20,16 +18,16 @@
     guard->period_ticks(1000);
 }
 
-
 void Heater::read()
 {
     //Reads R and then resets the drive back to its previous value
     int i = 0;
+    float drive_cal[2] = {0.0, 10.0};       // replace with drive cal passed via Heater class
     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
-    adc->start_conversion(15);
-
+    *drive = 1.0f;                         //Turn the driver on for the measurement
+    wait_us(thermal.settling_time_us);     //Wait for ADC to settle
+    adc->start_conversion(ADC_CONV_ALL_CH);     
+    
     //Incremental back off until ADC is free
     while(adc_busy == 1) {
         wait_us(1);
@@ -44,17 +42,17 @@
     v = adc->read_channel_result(v_port);
 
     if (curr > 0) {
-        R = (float)v/curr;   //Avoid dividing by 0
+        R = (float)v/curr;                //Avoid dividing by 0
+        R = drive_cal[0] + drive_cal[1]*R;  //Convert to Ohms
     }
 
     //Get error values
-
     error = R_ref - R;
 
     //Only allow positive integrated errors and limit change in integrated error
     //to help avoid integral windup
 
-    if (abs(error) > WIND_UP_LIMIT) {error = error * WIND_UP_LIMIT / abs(error);}
+    if (abs(error) > thermal.pid_wind_up_limit_ohm) {error = error * thermal.pid_wind_up_limit_ohm / abs(error);}
     
     error_integrated += error;
 
@@ -63,19 +61,15 @@
     }
 }
 
-
-
-
 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 > PWM_LIMIT) duty_cycle = PWM_LIMIT;
+    if (duty_cycle > thermal.pid_pwm_limit) duty_cycle = thermal.pid_pwm_limit;
     drive->write(duty_cycle);
     guard->write(duty_cycle * thermal.guard_drive_ratio);
 }
 
-
 void Heater::Set_ref(float R)
 {
     R_ref = R;