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_Threaded_Programming
Revision 25:09a315a59956, committed 2019-08-19
- Comitter:
- omatthews
- Date:
- Mon Aug 19 07:55:38 2019 +0000
- Parent:
- 24:6debc2fb9ff3
- Child:
- 26:f6c98b05ee85
- Commit message:
- 10/08/2019
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 Aug 15 10:34:55 2019 +0000
+++ b/Heater.cpp Mon Aug 19 07:55:38 2019 +0000
@@ -13,8 +13,6 @@
extern Timer timer;
extern DigitalIn adc_busy;
extern MODSERIAL pc;
-extern int log_count;
-extern float R_avg;
extern DigitalOut led_0;
@@ -22,10 +20,6 @@
:R_ref(R_ref),i_port(i_port),v_port(v_port),drive(drive),guard(guard),corr_grad(corr_grad),corr_int(corr_int) {}
-// 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
@@ -35,28 +29,24 @@
void Heater::read()
{
//Reads R and then resets the drive back to its previous value
-
+
int i = 0;
//float error_prev = error;
double drive_prev = drive->read(); //Store previous value of drive
- //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);
+
+ adc.start_conversion(15);
+
//Incremental back off until ADC is free
while(adc_busy == 1)
{
wait_us(1);
i++;
}
+
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
adc.read_channels();
@@ -68,11 +58,10 @@
//Get error values
error = R_ref - R;
- //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
- //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;}
+ //Only allow positive integrated errors and limit change in integrated error
+ //to help avoid integral windup
+
if (abs(error) < WIND_UP_LIMIT) {error_integrated += error;}
if (error_integrated < 0.0) {error_integrated = 0.0;}
@@ -81,75 +70,36 @@
-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
-
- int end_time = timer.read_ms() + hold_time;
- while (timer.read_ms() < end_time)
- {
- 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
- }
-}
-
-
-void Heater::ramp_R(const int ramp_time, const float R_final, const float R_start)
+void Heater::update()
{
- //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;
- float ramp_rate = (R_final - R_start)/ramp_time;
-
- while (time < end_time)
+ //Update PWM from setpoint and resistance
+ read();
+ drive->write((double) (Kp * (error + error_integrated/Ti)));
+ guard->write((double) (Kp * GUARD_PWM_RATIO * (error + error_integrated/Ti)));
+ log_count++;
+ if (log_count >= LOG_LIM)
{
- Set_R_ref(R_start + ramp_rate * (time - start_time));
- hold(1);
- time = timer.read_ms();
+ log_count = 0;
+ output();
}
-
-}
-
-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)
+void Heater::Set_ref(float R)
{
R_ref = R;
- error_integrated = 0;
}
-void Heater::Set_T_ref(float T_ref) {R_ref = T_to_R(T_ref);}
-void Heater::Set_D(float D) {drive->write(D);}
+void Heater::Set_D(float D) {
+ drive->write(D);
+ guard->write(D*GUARD_PWM_RATIO);
+ }
int Heater::Get_i() const {return curr;}
int Heater::Get_v() const {return v;}
float Heater::Get_R() const {return R;}
-float Heater::Get_T() const {return R_to_T(R);}
void Heater::turn_on ()
{
--- a/Heater.h Thu Aug 15 10:34:55 2019 +0000
+++ b/Heater.h Mon Aug 19 07:55:38 2019 +0000
@@ -38,33 +38,28 @@
* @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, FastPWM * guard, const float corr_grad, const float corr_int, float R_ref = 1);
+ Heater(const int i_port, const int v_port, FastPWM * drive, FastPWM * guard, const float corr_grad, const float corr_int, float R_ref = 0);
//Public member functions
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 update(); //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 (T is in degrees celcius)
void output() const; //Prints the current state of the heater
void turn_on(); //Turns the heater on
void turn_off(); //Turns the heater off
- //Linear conversions between temperature and resistance. Note that temperature is always in celcius
- float R_to_T(const float R) const;
- float T_to_R(const float T) const;
+
//Getters and setters
- void Set_T_ref(float T);
- void Set_R_ref(float R);
+ void Set_ref(float R);
void Set_D(float D);
int Get_i() const;
int Get_v() const;
float Get_R() const;
- float Get_T() const;
@@ -90,6 +85,7 @@
//Heater correlations give temperature for a given resistance (assume linear relationship)
const float corr_grad;
const float corr_int;
+ int log_count;
};