Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: LEX-Demo-Firmware-Logging LEX-Demo-Firmware-Logging
Revision 18:f5d26d3d532f, committed 2019-07-28
- Comitter:
- omatthews
- Date:
- Sun Jul 28 11:49:57 2019 +0000
- Parent:
- 17:0bfed0e96927
- Child:
- 19:fccdd7127f94
- Commit message:
- Added in consts
Changed in this revision
| Heater.cpp | Show annotated file Show diff for this revision Revisions of this file |
| Heater.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/Heater.cpp Thu Jul 25 16:20:01 2019 +0000
+++ b/Heater.cpp Sun Jul 28 11:49:57 2019 +0000
@@ -10,7 +10,6 @@
#include "ADS8568_ADC.h"
extern ADS8568_ADC adc;
-extern float scale_factors[8];
extern Timer timer;
extern DigitalIn adc_busy;
extern MODSERIAL pc;
@@ -19,14 +18,17 @@
-Heater::Heater(int i_port, int v_port, FastPWM * drive, float corr_grad, float corr_int, float R_ref)
+Heater::Heater(const int i_port, const int v_port, FastPWM * drive, const float corr_grad, const 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::output()
+// Convert from R to T using the linear relationship - T = R * corr_grad + corr_int
+float Heater::R_to_T(const float R) const {return R*corr_grad + corr_int;}
+float Heater::T_to_R(const float T) const {return (T - corr_int)/corr_grad;}
+
+void Heater::output()const
{
+ //Prints the current state to the terminal
pc.printf("%d,%f,%f,%f,%f,%f\n",timer.read_ms(),R_ref,R,error,error_integrated,drive->read());
}
@@ -35,45 +37,44 @@
//Reads R and then resets the drive back to its previous value
int i = 0;
- float error_prev = error;
+ //float error_prev = error;
double drive_prev = drive->read(); //Store previous value of drive
- drive->period_us(1); //Time_on seems to have a precision of us. Larger period => more precise control
- *drive = 1.0f;
+ drive->period_us(1); //Set period to 1us for the measurement
+ *drive = 1.0f; //Turn the driver on for the measurement
wait_us(MEAS_DELAY); //Wait for ADC to settle
adc.start_conversion(ALL_CH);
+ //Incremental back off until ADC is free
while(adc_busy == 1)
{
wait_us(1);
i++;
- }
- drive->write(drive_prev);
- drive->period_us(PWM_PERIOD); //Time_on seems to have a precision of us. Larger period => more precise control
+ }
+ 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
+
+ //Get voltage, current and R values from the ADC conversion
adc.read_channels();
-
-
- //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
- //R_avg = (((N_ROLL_AVG - 1) * R_avg) + R)/N_ROLL_AVG;
+ //Get error values
+
error = R_ref - R;
- error_diff = (error - error_prev)/WAIT_DELAY;
+ //error_diff = (error - error_prev)/WAIT_DELAY;
- //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
+ //Avoid integral windup by limiting integral error past actuation saturation
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;}
else {error_integrated += error;}
-
+ //Output the error every LOG_LIM reads
log_count++;
- if (log_count > 100)
+ if (log_count >= LOG_LIM)
{
log_count = 0;
output();
@@ -83,7 +84,7 @@
-void Heater::hold(int hold_time)
+void Heater::hold(const 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
@@ -92,18 +93,19 @@
while (timer.read_ms() < end_time)
{
read();
-
drive->write((double) (Kp * (error + error_integrated/Ti)));
- wait_ms(WAIT_DELAY); //Minimum duty cycle of 1%
- //pc.printf("%f,%f,%f\n",error,error_integrated,drive.read());
-
-
+ wait_ms(WAIT_DELAY); //Wait before reading again
}
}
-void Heater::ramp_R(int ramp_time, float R_final, float R_start)
+void Heater::ramp_R(const int ramp_time, const float R_final, const float R_start)
{
+ //Ramps the heater from R_start to R_final for the given hold time
+ // in: int hold_time - is the time in ms to hold the reference
+ // float R_final - is the final R_ref value
+ // float R_start - is the initial R_ref value
+
int time = timer.read_ms();
int start_time = time;
int end_time = start_time + ramp_time;
@@ -118,20 +120,26 @@
}
-void Heater::ramp_T(int ramp_time, float T_final, float T_start)
+void Heater::ramp_T(const int ramp_time, const float T_final, const float T_start)
{
+ //Ramps the heater from T_start to T_final for the given hold time
+ // in: int hold_time - is the time in ms to hold the reference
+ // float T_final - is the final T_ref value
+ // float T_start - is the initial T_ref value
ramp_R(ramp_time, T_to_R(T_final), T_to_R(T_start));
}
+
+
+
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);}
-
void Heater::Set_D(float D) {drive->write(D);}
-int Heater::Get_i() {return curr;}
-int Heater::Get_v() {return v;}
+int Heater::Get_i() const {return curr;}
+int Heater::Get_v() const {return v;}
-float Heater::Get_R() {return R;}
-float Heater::Get_T() {return R_to_T(R);}
+float Heater::Get_R() const {return R;}
+float Heater::Get_T() const {return R_to_T(R);}
void Heater::turn_on () {*drive = 1;}
--- a/Heater.h Thu Jul 25 16:20:01 2019 +0000
+++ b/Heater.h Sun Jul 28 11:49:57 2019 +0000
@@ -12,39 +12,46 @@
#include "FastPWM.h"
#define MEAS_DELAY 50 // measurement delay for ADC
-#define WAIT_DELAY 5 // wait delay for ADC
+#define WAIT_DELAY 3 // wait delay for ADC
#define N_ROLL_AVG 1 // rolling average for R values
#define ALL_CH 15 //value of convst bus to read all chanels simultaneosly
#define Kp 30.0f //proportional gain
-#define Ti 2 //Integration time
+#define Ti 1.0f //Integration time
#define Kd 0.0f //Differentiator gain
-#define WIND_UP_LIMIT 0.006f //Avoids integral windup on a sharp drop in T_ref
-#define PWM_PERIOD 50
+#define WIND_UP_LIMIT 0.005f //The change in error which turns off the integral term
+#define PWM_PERIOD 5 //Period of Pwm for the motor
+#define LOG_LIM 100 //Period of Pwm for the motor
+
class Heater
{
//This class provides the interface through which each heater can be controlled
public:
/** Constructor
- * @param i_port, the port for the current in the ADC
- * @param v_port, the port for the voltage in the ADC
- * @param drive, the motor drive
- * @param R_ref, the target value for R
- */
- Heater(int i_port, int v_port, FastPWM * drive, float corr_grad, float corr_int, float R_ref = 1);
+ * @param i_port, the current port in the ADC
+ * @param v_port, the voltage port in the ADC
+ * @param * drive, a pointer to the motor drive
+ * @param * corr_grad, the gradient of the linear relationship between resistance and temperature
+ * @param * corr_int, the intercept of the linear relationship between resistance and temperature
+ * @param R_ref (default value 1), optional parameter sets the target value for R
+ **/
+ Heater(const int i_port, const int v_port, FastPWM * drive, const float corr_grad, const float corr_int, float R_ref = 1);
//Public member functions
- void read();
- void hold(int hold_time);
- void ramp_R(int ramp_time, float R_final, float R_start);
- void ramp_T(int ramp_time, float T_final, float T_start);
- void output();
+ void read(); //Updates the resistance and error values for the heater
+ void hold(const int hold_time); //Holds R_ref for hold_time miliseconds
+ void ramp_R(const int ramp_time, const float R_final, const float R_start); //Ramps for ramp_time miliseconds from R_start to R_final
+ void ramp_T(const int ramp_time, const float T_final, const float T_start); //Same as above but with T
+ void output() const; //Prints the current state of the heater
+ void turn_on(); //Turns the heater on
+ void turn_off(); //Turns the heater off
- //Conversions between temperature and resistance
- float R_to_T(float R);
- float T_to_R(float T);
+
+ //Linear conversions between temperature and resistance
+ float R_to_T(const float R) const;
+ float T_to_R(const float T) const;
@@ -52,35 +59,34 @@
void Set_T_ref(float T);
void Set_R_ref(float R);
void Set_D(float D);
- int Get_i();
- int Get_v();
- float Get_R();
- float Get_T();
+ int Get_i() const;
+ int Get_v() const;
+ float Get_R() const;
+ float Get_T() const;
- void turn_on();
- void turn_off();
+
protected:
- int curr;
- int v;
- float R;
- float R_ref;
- float error;
- float error_diff;
- float error_integrated;
+ int curr; //Latest current reading from ADC
+ int v; //Latest voltage reading from ADC
+ float R; //Latest resistance calculated from ADC current and voltage
+ float R_ref; //Current referance for resistance
+ float error; //R_ref - R
+ //float error_diff; //Differential error
+ float error_integrated; //Integrated error
- int i_port;
- int v_port;
- FastPWM * drive;
+ const int i_port; //ADC port which corresponds to current measurements
+ const int v_port; //ADC port which corresponds to voltage measurements
+ FastPWM * drive; //Pointer to the driver
//Heater correlations give temperature for a given resistance (assume linear relationship)
- float corr_grad;
- float corr_int;
+ const float corr_grad;
+ const float corr_int;
};