Heater for threaded program

Dependents:   LEX_Threaded_Programming_V3

Revision:
31:7c6f05326c4d
Parent:
30:055d856f05b5
Child:
33:52ab0641f2e6
--- a/Heater.cpp	Thu Aug 29 16:07:56 2019 +0000
+++ b/Heater.cpp	Fri Aug 30 14:58:47 2019 +0000
@@ -9,18 +9,10 @@
 #include "Heater.h"
 #include "ADS8568_ADC.h"
 
-extern Timer timer;
-extern ADS8568_ADC adc;
-extern DigitalIn adc_busy;
-extern MODSERIAL pc;
-extern DigitalOut led_0;
-extern FastPWM drive_1;
-extern FastPWM drive_2;
-extern FastPWM guard_1;
-extern FastPWM guard_2;
+
 
-Heater::Heater(const int i_port, const int v_port, FastPWM * drive, FastPWM * guard, const memspcr_ThermalConfiguration & thermal)
-    :thermal(thermal), i_port(i_port), v_port(v_port), drive(drive), guard(guard)
+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)
 {
     drive->prescaler(1);
     guard->prescaler(1);
@@ -29,13 +21,6 @@
 }
 
 
-
-void Heater::log()const
-{
-    //Prints the current state to the terminal
-    pc.printf("%d,%f,%f,%f,%f,%f\n",timer.read_ms(),R_ref,R,error,error_integrated,drive->read());
-}
-
 void Heater::read()
 {
     //Reads R and then resets the drive back to its previous value
@@ -43,7 +28,7 @@
     double drive_prev = drive->read();     //Store previous value of drive
     *drive = 1.0f;              //Turn the driver on for the measurement
     wait_us(thermal.adc_settling_time_us);        //Wait for ADC to settle
-    adc.start_conversion(15);
+    adc->start_conversion(15);
 
     //Incremental back off until ADC is free
     while(adc_busy == 1) {
@@ -54,9 +39,9 @@
     drive->write(0);       //Reset the duty cycle back to what it was
 
     //Get voltage, current and R values from the ADC conversion
-    adc.read_channels();
-    curr = adc.read_channel_result(i_port);
-    v = adc.read_channel_result(v_port);
+    adc->read_channels();
+    curr = adc->read_channel_result(i_port);
+    v = adc->read_channel_result(v_port);
 
     if (curr > 0) {
         R = (float)v/curr;   //Avoid dividing by 0
@@ -84,8 +69,10 @@
 void Heater::update()
 {
     //Update PWM from setpoint and resistance
-    drive->write((double) (thermal.adc_settling_time_us * (error + error_integrated/thermal.pid_integral_time)));
-    guard->write((double) (thermal.adc_settling_time_us * thermal.guard_drive_ratio * (error + error_integrated/thermal.pid_integral_time)));
+    double duty_cycle = (double) thermal.pid_kp * (error + error_integrated/thermal.pid_integral_time);
+    if (duty_cycle > PWM_LIMIT) duty_cycle = PWM_LIMIT;
+    drive->write(duty_cycle);
+    guard->write(duty_cycle * thermal.guard_drive_ratio);
 }
 
 
@@ -99,6 +86,11 @@
     guard->write(D*thermal.guard_drive_ratio);
 }
 
+int Heater::Get_D() const
+{
+    return drive->read();
+}
+
 int Heater::Get_i() const
 {
     return curr;
@@ -113,6 +105,21 @@
     return R;
 }
 
+float Heater::Get_R_ref() const
+{
+    return R_ref;
+}
+
+float Heater::Get_error() const
+{
+    return error;
+}
+
+float Heater::Get_error_integrated() const
+{
+    return error_integrated;
+}
+
 void Heater::turn_on ()
 {
     *drive = 1;