Heater for threaded program

Dependents:   LEX_Threaded_Programming

Revision:
12:8a048f111140
Parent:
11:785a0329f802
Child:
14:f266bf960b8d
--- a/Heater.cpp	Tue Jul 23 08:04:51 2019 +0000
+++ b/Heater.cpp	Tue Jul 23 09:41:53 2019 +0000
@@ -19,7 +19,7 @@
 
 
     
-Heater::Heater(int i_port, int v_port, DigitalOut drive, float corr_grad, float corr_int, float R_ref)
+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) {}
 
 float Heater::R_to_T(float R) {return R*corr_grad + corr_int;}
@@ -27,7 +27,7 @@
 
 void Heater::output()
 {
-    pc.printf("%d,%f,%f\n",timer.read_ms(),R_to_T(R),R);
+    pc.printf("%d,%f,%f,%f\n",timer.read_ms(),R_ref,R,drive.read());
 }
 
 void Heater::read()
@@ -56,8 +56,13 @@
     if (v<0) {pc.printf("v is %d",v);}
     //if (curr > 0) {R = (float)v/curr;}        //Avoid dividing by 0
     R = (float)v/curr;
+    R_avg = (((N_ROLL_AVG - 1) * R_avg) + R)/N_ROLL_AVG;
+
+    error = R_ref - R;
+    error_integrated += error;
+    
     log_count++;
-    if (log_count > 0)
+    if (log_count > 2000)
     {
     log_count = 0;
     output();
@@ -76,17 +81,11 @@
     while (timer.read_ms() < end_time)
     {
         read();
-        R_avg = (((N_ROLL_AVG - 1) * R_avg) + R)/N_ROLL_AVG;
-        if (R_avg > R_ref)
-        {
-            drive = 0;
-            wait_ms(5);  //Minimum duty cycle of 10%
-        }
-        else
-        {
-            drive = 1;
-            wait_ms(5);
-        }
+        
+        drive.write(Kd * error + Ki * error_integrated);
+        wait_ms(WAIT_DELAY);  //Minimum duty cycle of 1%
+        pc.printf("%f,%f,%f\n",error,error_integrated,drive.read());
+   
   
     }
 }