Heater for threaded program

Dependents:   LEX_Threaded_Programming

Revision:
20:2d34a03ae57e
Parent:
19:fccdd7127f94
Child:
23:947850bbf325
--- a/Heater.cpp	Tue Jul 30 21:32:49 2019 +0000
+++ b/Heater.cpp	Wed Aug 07 16:03:27 2019 +0000
@@ -15,7 +15,7 @@
 extern MODSERIAL pc;  
 extern int log_count;
 extern float R_avg;
-
+extern DigitalOut led_0;
 
     
 Heater::Heater(const int i_port, const int v_port, FastPWM * drive, FastPWM * guard, const float corr_grad, const float corr_int, float R_ref)
@@ -40,9 +40,12 @@
     //float error_prev = error;
     
     double drive_prev = drive->read();     //Store previous value of drive
-    drive->period_us(1);         //Set period to 1us for the measurement
+    //drive->period_ticks(15);         //Set period to 1us for the measurement
+    //guard->period_ticks(15);
     *drive = 1.0f;              //Turn the driver on for the measurement
+    //led_0 = 1;
     wait_us(MEAS_DELAY);        //Wait for ADC to settle
+    //led_0 = 0;
     adc.start_conversion(ALL_CH);
     //Incremental back off until ADC is free
     while(adc_busy == 1)
@@ -50,8 +53,9 @@
             wait_us(1);
             i++;
         }        
-    drive->write(drive_prev);       //Reset the duty cycle back to what it was
-    drive->period_us(PWM_PERIOD);         //Reset the period to what it was
+    drive->write(0);       //Reset the duty cycle back to what it was
+    //drive->period_ticks(1000);         //Reset the period to what it was
+    //guard->period_ticks(1000);
 
 
     //Get voltage, current and R values from the ADC conversion
@@ -69,16 +73,9 @@
     //Avoid integral windup by limiting error past actuation saturation (actuator does saturate for any negative error, but  to ensure integrated error can decrease, the limit has been set to the negative of the positive limit
     //if (error*Kp > WIND_UP_LIMIT) {error_integrated += WIND_UP_LIMIT/Kp;}
     //else if (error*Kp < -WIND_UP_LIMIT) {error_integrated -= WIND_UP_LIMIT/Kp;}
-    if (error < WIND_UP_LIMIT && error > -WIND_UP_LIMIT) {error_integrated += error;}
-    
-    
-    //Output the error every LOG_LIM reads
-    log_count++;
-    if (log_count >= LOG_LIM)
-    {
-    log_count = 0;
-    output();
-    }
+    if (abs(error) < WIND_UP_LIMIT) {error_integrated += error;}
+    if (error_integrated < -0.0) {error_integrated = -0.0;}
+
 }
 
 
@@ -95,6 +92,14 @@
         read();
         drive->write((double) (Kp * (error + error_integrated/Ti)));
         guard->write((double) (Kp * GUARD_PWM_RATIO * (error + error_integrated/Ti)));
+        //Output the error every LOG_LIM reads
+
+        log_count++;
+        if (log_count >= LOG_LIM)
+        {
+        log_count = 0;
+        output();
+        }
         wait_ms(WAIT_DELAY);    //Wait before reading again  
     }
 }