Heater for threaded program

Dependents:   LEX_Threaded_Programming

Committer:
omatthews
Date:
Tue Jul 30 21:32:49 2019 +0000
Revision:
19:fccdd7127f94
Parent:
18:f5d26d3d532f
Child:
20:2d34a03ae57e
Child:
21:e2a504521b46
Added guards

Who changed what in which revision?

UserRevisionLine numberNew contents of line
omatthews 0:4e33cc8171f4 1 /*------------------------------------------------------------------------------
omatthews 0:4e33cc8171f4 2 Library header file for heater operations
omatthews 0:4e33cc8171f4 3 Date: 16/07/2018
omatthews 0:4e33cc8171f4 4
omatthews 0:4e33cc8171f4 5
omatthews 0:4e33cc8171f4 6 ------------------------------------------------------------------------------*/
omatthews 0:4e33cc8171f4 7
omatthews 0:4e33cc8171f4 8 #ifndef Heater_H
omatthews 0:4e33cc8171f4 9 #define Heater_H
omatthews 0:4e33cc8171f4 10 #include "mbed.h"
omatthews 0:4e33cc8171f4 11 #include "ADS8568_ADC.h"
omatthews 17:0bfed0e96927 12 #include "FastPWM.h"
omatthews 0:4e33cc8171f4 13
omatthews 19:fccdd7127f94 14 #define MEAS_DELAY 50 // measurement delay for ADC in us
omatthews 19:fccdd7127f94 15 #define WAIT_DELAY 3 // wait delay for ADC in ms
omatthews 12:8a048f111140 16
omatthews 16:cd837b230b09 17 #define N_ROLL_AVG 1 // rolling average for R values
omatthews 1:4435d407d827 18 #define ALL_CH 15 //value of convst bus to read all chanels simultaneosly
omatthews 17:0bfed0e96927 19 #define Kp 30.0f //proportional gain
omatthews 19:fccdd7127f94 20 #define Ti 0.75f //Integration time
omatthews 17:0bfed0e96927 21 #define Kd 0.0f //Differentiator gain
omatthews 19:fccdd7127f94 22 #define WIND_UP_LIMIT 0.01f //The change in error which turns off the integral term
omatthews 19:fccdd7127f94 23 #define PWM_PERIOD 5 //Period of Pwm for the motor in us
omatthews 19:fccdd7127f94 24 #define LOG_LIM 100 //Number of reads before the result is logged
omatthews 19:fccdd7127f94 25 #define GUARD_PWM_RATIO 0.25 //Ratio of guard duty cycle to heater duty cycle
omatthews 18:f5d26d3d532f 26
omatthews 0:4e33cc8171f4 27
omatthews 0:4e33cc8171f4 28 class Heater
omatthews 0:4e33cc8171f4 29 {
omatthews 7:59ece353eea2 30 //This class provides the interface through which each heater can be controlled
omatthews 0:4e33cc8171f4 31 public:
omatthews 0:4e33cc8171f4 32 /** Constructor
omatthews 18:f5d26d3d532f 33 * @param i_port, the current port in the ADC
omatthews 18:f5d26d3d532f 34 * @param v_port, the voltage port in the ADC
omatthews 19:fccdd7127f94 35 * @param * drive, a pointer to the heater drive
omatthews 19:fccdd7127f94 36 * @param * guard, a pointer to the guard
omatthews 18:f5d26d3d532f 37 * @param * corr_grad, the gradient of the linear relationship between resistance and temperature
omatthews 18:f5d26d3d532f 38 * @param * corr_int, the intercept of the linear relationship between resistance and temperature
omatthews 18:f5d26d3d532f 39 * @param R_ref (default value 1), optional parameter sets the target value for R
omatthews 18:f5d26d3d532f 40 **/
omatthews 19:fccdd7127f94 41 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);
omatthews 0:4e33cc8171f4 42
omatthews 0:4e33cc8171f4 43 //Public member functions
omatthews 0:4e33cc8171f4 44
omatthews 18:f5d26d3d532f 45 void read(); //Updates the resistance and error values for the heater
omatthews 18:f5d26d3d532f 46 void hold(const int hold_time); //Holds R_ref for hold_time miliseconds
omatthews 18:f5d26d3d532f 47 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
omatthews 19:fccdd7127f94 48 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)
omatthews 18:f5d26d3d532f 49 void output() const; //Prints the current state of the heater
omatthews 18:f5d26d3d532f 50 void turn_on(); //Turns the heater on
omatthews 18:f5d26d3d532f 51 void turn_off(); //Turns the heater off
omatthews 7:59ece353eea2 52
omatthews 18:f5d26d3d532f 53
omatthews 19:fccdd7127f94 54 //Linear conversions between temperature and resistance. Note that temperature is always in celcius
omatthews 18:f5d26d3d532f 55 float R_to_T(const float R) const;
omatthews 18:f5d26d3d532f 56 float T_to_R(const float T) const;
omatthews 1:4435d407d827 57
omatthews 1:4435d407d827 58
omatthews 2:7f15386fcc90 59
omatthews 2:7f15386fcc90 60 //Getters and setters
omatthews 7:59ece353eea2 61 void Set_T_ref(float T);
omatthews 7:59ece353eea2 62 void Set_R_ref(float R);
omatthews 15:e7838491c104 63 void Set_D(float D);
omatthews 18:f5d26d3d532f 64 int Get_i() const;
omatthews 18:f5d26d3d532f 65 int Get_v() const;
omatthews 18:f5d26d3d532f 66 float Get_R() const;
omatthews 18:f5d26d3d532f 67 float Get_T() const;
omatthews 7:59ece353eea2 68
omatthews 0:4e33cc8171f4 69
omatthews 18:f5d26d3d532f 70
omatthews 0:4e33cc8171f4 71
omatthews 0:4e33cc8171f4 72
omatthews 0:4e33cc8171f4 73
omatthews 0:4e33cc8171f4 74 protected:
omatthews 0:4e33cc8171f4 75
omatthews 0:4e33cc8171f4 76
omatthews 18:f5d26d3d532f 77 int curr; //Latest current reading from ADC
omatthews 18:f5d26d3d532f 78 int v; //Latest voltage reading from ADC
omatthews 18:f5d26d3d532f 79 float R; //Latest resistance calculated from ADC current and voltage
omatthews 18:f5d26d3d532f 80 float R_ref; //Current referance for resistance
omatthews 18:f5d26d3d532f 81 float error; //R_ref - R
omatthews 18:f5d26d3d532f 82 //float error_diff; //Differential error
omatthews 18:f5d26d3d532f 83 float error_integrated; //Integrated error
omatthews 1:4435d407d827 84
omatthews 18:f5d26d3d532f 85 const int i_port; //ADC port which corresponds to current measurements
omatthews 18:f5d26d3d532f 86 const int v_port; //ADC port which corresponds to voltage measurements
omatthews 18:f5d26d3d532f 87 FastPWM * drive; //Pointer to the driver
omatthews 19:fccdd7127f94 88 FastPWM * guard; //Pointer to the guard
omatthews 16:cd837b230b09 89
omatthews 7:59ece353eea2 90 //Heater correlations give temperature for a given resistance (assume linear relationship)
omatthews 18:f5d26d3d532f 91 const float corr_grad;
omatthews 18:f5d26d3d532f 92 const float corr_int;
omatthews 0:4e33cc8171f4 93
omatthews 0:4e33cc8171f4 94 };
omatthews 0:4e33cc8171f4 95
omatthews 19:fccdd7127f94 96 #endif