Heater for threaded program

Dependents:   LEX_Threaded_Programming

Revision:
8:5da71ae16115
Parent:
7:59ece353eea2
Child:
9:405e86b02d63
Child:
10:0e16d8430d66
--- a/Heater.cpp	Fri Jul 19 09:22:47 2019 +0000
+++ b/Heater.cpp	Fri Jul 19 10:31:38 2019 +0000
@@ -17,12 +17,8 @@
 
 
     
-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.write(0.1);
-        drive.period_us(100);
-    }
+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;}
@@ -33,9 +29,8 @@
     //Reads R and then resets the drive back to its previous value
    
     int i = 0;
-    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);
+    //int drive_prev = drive;     //Store previous value of drive
+    drive = 1;
     wait_us(MEAS_DELAY);        //Wait for ADC to settle
     adc.start_conversion(ALL_CH);
     while(adc_busy == 1)
@@ -43,15 +38,17 @@
             wait_us(1);
             i++;
         }
-    drive.write(drive_prev);
+    //drive = drive_prev;
     adc.read_channels();
-    if (i>0) {pc.printf("wait time is %d us",i);}        
+            
+    //pc.printf("conversion took %d us\n", i );
     //i=0;
     
     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
+    //if (curr > 0) {R = (float)v/curr;}        //Avoid dividing by 0
+    R = (float)v/curr;
 }
 
 
@@ -63,39 +60,33 @@
     //  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;
-    int  j = 0;
-    if (j == 0) {pc.printf("D if before wait is %f\n",drive.read());}
     while (timer.read_ms() < end_time)
     {
         read();
-        //R_avg = ((N_ROLL_AVG-1)*R_avg + R)/N_ROLL_AVG;      //Enable rolling average
         if (R > R_ref)
         {
-            drive.write(0);
-            wait_ms(1000);  //Minimum duty cycle of 10%
+            drive = 0;
+            wait_ms(10);  //Minimum duty cycle of 10%
         }
-        else
-        {
-            drive.write(0.1);
-            wait_ms(1000);   //Shorter wait time as there is no cost for checking
-        }
-        j++;
+  
     }
 }
 
 
 void Heater::ramp_R(int ramp_time, float R_final, float R_start)
 {
-    int start_time = timer.read_ms();
+    int time = timer.read_ms();
+    int start_time = time;
     int end_time = start_time + ramp_time;
     float ramp_rate = (R_final - R_start)/ramp_time;
     
-    while (int time = timer.read_ms() < end_time)
+    while (time < end_time)
     {
-        Set_R_ref(R_start + ramp_rate * (time - start_time)/(end_time - start_time));
+        Set_R_ref(R_start + ramp_rate * (time - start_time));
         hold(1);
+        time = timer.read_ms();
     }
+    
 }
     
 void Heater::ramp_T(int ramp_time, float T_final, float T_start) 
@@ -111,10 +102,6 @@
 float Heater::Get_R() {return R;}
 float Heater::Get_T() {return R_to_T(R);}
 
-void Heater::turn_on () {drive.write(0.1);}
+void Heater::turn_on () {drive = 1;}
 
-void Heater::turn_off () {drive = 0;}
-float Heater::check_D () {return drive.read();}
-
-
-
+void Heater::turn_off () {drive = 0;}
\ No newline at end of file