Heater for threaded program

Dependents:   LEX_Threaded_Programming

Revision:
6:71d9c10fca4a
Parent:
4:29ffcc7b410e
--- a/Heater.cpp	Thu Jul 18 12:17:42 2019 +0000
+++ b/Heater.cpp	Thu Jul 18 16:27:37 2019 +0000
@@ -17,8 +17,12 @@
 
 
     
-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) {}
+Heater::Heater(int i_port, int v_port, PwmOut 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) 
+    {
+        drive = 0;
+        drive.period_us(500);
+    }
 
 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;}
@@ -29,19 +33,19 @@
     //Reads R and then resets the drive back to its previous value
    
     int i = 0;
-    int drive_prev = drive;     //Store previous value of drive
-    drive = 1;
-    wait_us(50);        //Wait for ADC to settle
+    float drive_prev = drive.read();     //Store previous value of drive
+    if (drive_prev > 0.3) {pc.printf("%f\n",drive_prev);}
+    drive.write(1.0);
+    wait_us(500);        //Wait for ADC to settle
     adc.start_conversion(ALL_CH);
     while(adc_busy == 1)
         {
             wait_us(1);
             i++;
         }
-    drive = drive_prev;
+    drive.write(drive_prev);
     adc.read_channels();
             
-    //pc.printf("conversion took %d us\n", i );
     //i=0;
     
     curr = adc.read_channel_result(i_port);
@@ -59,20 +63,20 @@
     //  in: int hold_time - is the time in ms to hold the reference
     
     int end_time = timer.read_ms() + hold_time;
-    float R_avg = 0;
+    //float R_avg = 0;
     while (timer.read_ms() < end_time)
     {
         read();
-        R_avg = ((N_ROLL_AVG-1)*R_avg + R)/N_ROLL_AVG;      //Enable rolling average
-        if (R_avg > R_ref)
+        //R_avg = ((N_ROLL_AVG-1)*R_avg + R)/N_ROLL_AVG;      //Enable rolling average
+        if (R > R_ref)
         {
-            drive = 0;
-            wait_us(100);  //Minimum duty cycle of 10%
+            drive.write(0);
+            wait_ms(5);  //Minimum duty cycle of 10%
         }
         else
         {
-            drive = 1;
-            //wait_us(20); //Shorter wait time as there is no cost for checking
+            drive.write(0.1);
+            wait_us(5); //Shorter wait time as there is no cost for checking
         }
         
     }