Heater for threaded program

Dependents:   LEX_Threaded_Programming

Revision:
3:313711a66929
Parent:
2:7f15386fcc90
Child:
4:29ffcc7b410e
--- a/Heater.cpp	Wed Jul 17 13:55:33 2019 +0000
+++ b/Heater.cpp	Wed Jul 17 20:46:20 2019 +0000
@@ -17,35 +17,39 @@
 
 
     
-Heater::Heater(int i_port, int v_port, DigitalOut drive, float R_set)
-    :R_set(R_set),i_port(i_port),v_port(v_port),drive(drive){}
+Heater::Heater(int i_port, int v_port, DigitalOut drive, float corr_grad, 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) {}
+
+float Heater::R_to_T(float R) {return R*corr_grad + corr_int;}
+float Heater::T_to_R(float T) {return (T - corr_int)/corr_grad;}
 
 
 void Heater::read()
 {
     //Reads R and then resets the drive back to its previous value
     int i = 0;
-    int drive_prev = drive;
+    int drive_prev = drive;     //Store previous value of drive
     drive = 1;
-    wait_us(10);
+    wait_us(MEAS_DELAY);        //Wait for ADC to settle
     adc.start_conversion(ALL_CH);
     while(adc_busy == 1)
         {
             wait_us(1);
             i++;
         }
+    adc.read_channels();
         
     drive = drive_prev;
 
-    adc.read_channels();
             
     //pc.printf("conversion took %d us\n", i );
     i=0;
     
     
-    curr = adc.read_channel_result(i_port)/scale_factors[i_port];
-    v = adc.read_channel_result(v_port)/scale_factors[v_port];
-    if (curr > 0) R = float(v)/float(curr);
+    curr = adc.read_channel_result(i_port);
+    v = adc.read_channel_result(v_port);
+    if (v<0) {pc.printf("v is %d",v);}
+    if (curr > 0) {R = (float)v/curr;}        //Avoid dividing by 0
 }
 
 
@@ -53,31 +57,52 @@
 
 void Heater::hold(int hold_time)
 {
+    //Holds the heater at R_ref for the given hold time
+    //  in: int hold_time - is the time in ms to hold the reference
+    
     int end_time = timer.read_ms() + hold_time;
-    pc.printf("end time is %d \n",end_time);
     float R_avg = 0;
     while (timer.read_ms() < end_time)
     {
-        drive = 1;
-        wait_us(MEAS_DELAY);
         read();
-        R_avg = ((N_ROLL_AVG-1)*R_avg + R)/N_ROLL_AVG;
-        if (R_avg > R_set)
+        R_avg = ((N_ROLL_AVG-1)*R_avg + R)/N_ROLL_AVG;      //Enable rolling average
+        if (R_avg > R_ref)
         {
             drive = 0;
-            wait_us(2*MEAS_DELAY);
+            wait_us(100);  //Minimum duty cycle of 10%
         }
-
+        else
+        {
+            drive = 1;
+            //wait_us(20); //Shorter wait time as there is no cost for checking
+        }
+        
     }
 }
 
 
-void Heater::Set_R_set(float R) {R_set = R;}
+void Heater::ramp_R(int ramp_time, float R_final, float R_start)
+{
+    int start_time = timer.read_ms();
+    int end_time = start_time + ramp_time;
+    float ramp_rate = (R_final - R_start)/ramp_time;
+    
+    while (int time = timer.read_ms() < end_time)
+    {
+        Set_R_ref(R_start + ramp_rate * (time - start_time)/(end_time - start_time));
+        hold(1);
+    }
+}
+    
+
+void Heater::Set_R_ref(float R) {R_ref = R;}
+void Heater::Set_T_ref(float T_ref) {R_ref = T_to_R(T_ref);}
 
 int Heater::Get_i() {return curr;}
 int Heater::Get_v() {return v;}
 
 float Heater::Get_R() {return R;}
+float Heater::Get_T() {return R_to_T(R);}
 
 void Heater::turn_on () {drive = 1;}