Heater for threaded program
Dependents: LEX_Threaded_Programming_V3
Heater.cpp@44:e358867319f6, 2019-11-15 (annotated)
- Committer:
- justinbuckland
- Date:
- Fri Nov 15 15:59:32 2019 +0000
- Revision:
- 44:e358867319f6
- Parent:
- 43:d34ac9d8648c
- Child:
- 45:5f588512529b
undo accidentally committed guard heater ratio variation with duty cycle
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
omatthews | 0:4e33cc8171f4 | 1 | /*------------------------------------------------------------------------------ |
omatthews | 0:4e33cc8171f4 | 2 | Library code file for interface to Heater |
omatthews | 0:4e33cc8171f4 | 3 | Date: 16/07/2018 |
omatthews | 0:4e33cc8171f4 | 4 | |
omatthews | 0:4e33cc8171f4 | 5 | |
omatthews | 0:4e33cc8171f4 | 6 | ------------------------------------------------------------------------------*/ |
omatthews | 1:4435d407d827 | 7 | #include "mbed.h" |
omatthews | 1:4435d407d827 | 8 | #include "MODSERIAL.h" |
omatthews | 0:4e33cc8171f4 | 9 | #include "Heater.h" |
omatthews | 0:4e33cc8171f4 | 10 | #include "ADS8568_ADC.h" |
omatthews | 0:4e33cc8171f4 | 11 | |
justinbuckland | 35:5acf01897ed6 | 12 | Heater::Heater(const int i_port, const int v_port, const float cal_a, const float cal_b, FastPWM * drive, FastPWM * guard, ADS8568_ADC * adc, DigitalIn adc_busy, const memspcr_ThermalConfiguration & thermal) |
justinbuckland | 35:5acf01897ed6 | 13 | :thermal(thermal), i_port(i_port), v_port(v_port), cal_a(cal_a), cal_b(cal_b), drive(drive), guard(guard), adc(adc), adc_busy(adc_busy) |
omatthews | 30:055d856f05b5 | 14 | { |
justinbuckland | 42:166d9bc7675e | 15 | //Set up PWM |
omatthews | 30:055d856f05b5 | 16 | drive->prescaler(1); |
omatthews | 30:055d856f05b5 | 17 | guard->prescaler(1); |
omatthews | 30:055d856f05b5 | 18 | drive->period_ticks(1000); |
omatthews | 30:055d856f05b5 | 19 | guard->period_ticks(1000); |
justinbuckland | 42:166d9bc7675e | 20 | |
justinbuckland | 42:166d9bc7675e | 21 | //Initialise values for averaging resistance |
justinbuckland | 42:166d9bc7675e | 22 | n_acc = 0; |
justinbuckland | 42:166d9bc7675e | 23 | R_acc = 0; |
omatthews | 30:055d856f05b5 | 24 | } |
omatthews | 30:055d856f05b5 | 25 | |
omatthews | 2:7f15386fcc90 | 26 | void Heater::read() |
omatthews | 0:4e33cc8171f4 | 27 | { |
omatthews | 1:4435d407d827 | 28 | //Reads R and then resets the drive back to its previous value |
omatthews | 30:055d856f05b5 | 29 | int i = 0; |
omatthews | 17:0bfed0e96927 | 30 | double drive_prev = drive->read(); //Store previous value of drive |
justinbuckland | 35:5acf01897ed6 | 31 | |
justinbuckland | 44:e358867319f6 | 32 | *drive = 1.0f; //Turn the driver on for the measurement |
justinbuckland | 44:e358867319f6 | 33 | wait_us(thermal.settling_time_us); //Wait for ADC to settle |
justinbuckland | 34:294adcc3e4b2 | 34 | adc->start_conversion(ADC_CONV_ALL_CH); |
justinbuckland | 34:294adcc3e4b2 | 35 | |
omatthews | 18:f5d26d3d532f | 36 | //Incremental back off until ADC is free |
omatthews | 30:055d856f05b5 | 37 | while(adc_busy == 1) { |
omatthews | 30:055d856f05b5 | 38 | wait_us(1); |
omatthews | 30:055d856f05b5 | 39 | i++; |
omatthews | 30:055d856f05b5 | 40 | } |
omatthews | 25:09a315a59956 | 41 | |
justinbuckland | 44:e358867319f6 | 42 | drive->write(0); //Reset the duty cycle back to what it was |
omatthews | 18:f5d26d3d532f | 43 | |
omatthews | 18:f5d26d3d532f | 44 | //Get voltage, current and R values from the ADC conversion |
omatthews | 31:7c6f05326c4d | 45 | adc->read_channels(); |
omatthews | 31:7c6f05326c4d | 46 | curr = adc->read_channel_result(i_port); |
omatthews | 31:7c6f05326c4d | 47 | v = adc->read_channel_result(v_port); |
omatthews | 30:055d856f05b5 | 48 | |
justinbuckland | 35:5acf01897ed6 | 49 | if (curr > 0) { //Avoid dividing by 0 |
justinbuckland | 35:5acf01897ed6 | 50 | R = (float)v/curr; |
justinbuckland | 35:5acf01897ed6 | 51 | R = cal_a + cal_b*R; //Convert to Ohms |
omatthews | 30:055d856f05b5 | 52 | } |
justinbuckland | 42:166d9bc7675e | 53 | |
justinbuckland | 42:166d9bc7675e | 54 | // Calcualate accumulated resistance values for R_avg output |
justinbuckland | 42:166d9bc7675e | 55 | R_acc = R_acc + R; |
justinbuckland | 42:166d9bc7675e | 56 | n_acc ++; |
justinbuckland | 35:5acf01897ed6 | 57 | |
omatthews | 0:4e33cc8171f4 | 58 | } |
omatthews | 0:4e33cc8171f4 | 59 | |
omatthews | 25:09a315a59956 | 60 | void Heater::update() |
omatthews | 7:59ece353eea2 | 61 | { |
justinbuckland | 44:e358867319f6 | 62 | |
justinbuckland | 41:b1602c68abcd | 63 | //Get error |
justinbuckland | 41:b1602c68abcd | 64 | error = R_ref - R; |
justinbuckland | 41:b1602c68abcd | 65 | |
omatthews | 25:09a315a59956 | 66 | //Update PWM from setpoint and resistance |
paullj | 39:5dffedda7a0d | 67 | double duty_cycle = thermal.pid_kp_mho * error; |
justinbuckland | 41:b1602c68abcd | 68 | |
justinbuckland | 41:b1602c68abcd | 69 | //Get integrated error |
paullj | 39:5dffedda7a0d | 70 | if (thermal.pid_integral_time_ms > 0) // set integral time to zero to have no integral term |
justinbuckland | 41:b1602c68abcd | 71 | if (abs(error) > thermal.pid_wind_up_limit_ohm) error = error * thermal.pid_wind_up_limit_ohm / abs(error); |
justinbuckland | 41:b1602c68abcd | 72 | error_integrated += error * (float) thermal.thermal_control_loop_interval_ms; |
paullj | 39:5dffedda7a0d | 73 | duty_cycle += thermal.pid_kp_mho * error_integrated/thermal.pid_integral_time_ms; |
justinbuckland | 44:e358867319f6 | 74 | |
paullj | 39:5dffedda7a0d | 75 | if (duty_cycle > thermal.pid_pwm_limit) |
paullj | 39:5dffedda7a0d | 76 | duty_cycle = thermal.pid_pwm_limit; |
paullj | 39:5dffedda7a0d | 77 | else if (duty_cycle < 0) |
paullj | 39:5dffedda7a0d | 78 | duty_cycle = 0; |
justinbuckland | 36:a8130bd29349 | 79 | |
omatthews | 31:7c6f05326c4d | 80 | drive->write(duty_cycle); |
justinbuckland | 44:e358867319f6 | 81 | guard->write(duty_cycle * thermal.guard_drive_ratio); |
omatthews | 7:59ece353eea2 | 82 | } |
omatthews | 18:f5d26d3d532f | 83 | |
omatthews | 30:055d856f05b5 | 84 | void Heater::Set_ref(float R) |
omatthews | 19:fccdd7127f94 | 85 | { |
omatthews | 19:fccdd7127f94 | 86 | R_ref = R; |
omatthews | 19:fccdd7127f94 | 87 | } |
omatthews | 30:055d856f05b5 | 88 | void Heater::Set_D(float D) |
omatthews | 30:055d856f05b5 | 89 | { |
omatthews | 25:09a315a59956 | 90 | drive->write(D); |
omatthews | 26:f6c98b05ee85 | 91 | guard->write(D*thermal.guard_drive_ratio); |
omatthews | 30:055d856f05b5 | 92 | } |
omatthews | 0:4e33cc8171f4 | 93 | |
justinbuckland | 42:166d9bc7675e | 94 | float Heater::Get_D() const |
omatthews | 31:7c6f05326c4d | 95 | { |
omatthews | 31:7c6f05326c4d | 96 | return drive->read(); |
omatthews | 31:7c6f05326c4d | 97 | } |
omatthews | 31:7c6f05326c4d | 98 | |
omatthews | 30:055d856f05b5 | 99 | int Heater::Get_i() const |
omatthews | 30:055d856f05b5 | 100 | { |
omatthews | 30:055d856f05b5 | 101 | return curr; |
omatthews | 30:055d856f05b5 | 102 | } |
omatthews | 30:055d856f05b5 | 103 | int Heater::Get_v() const |
omatthews | 30:055d856f05b5 | 104 | { |
omatthews | 30:055d856f05b5 | 105 | return v; |
omatthews | 30:055d856f05b5 | 106 | } |
omatthews | 2:7f15386fcc90 | 107 | |
omatthews | 30:055d856f05b5 | 108 | float Heater::Get_R() const |
omatthews | 30:055d856f05b5 | 109 | { |
omatthews | 30:055d856f05b5 | 110 | return R; |
omatthews | 30:055d856f05b5 | 111 | } |
omatthews | 0:4e33cc8171f4 | 112 | |
justinbuckland | 42:166d9bc7675e | 113 | float Heater::Get_R_avg() |
justinbuckland | 42:166d9bc7675e | 114 | { |
justinbuckland | 42:166d9bc7675e | 115 | R_avg = R_acc / (float) n_acc; |
justinbuckland | 42:166d9bc7675e | 116 | R_acc = 0; |
justinbuckland | 42:166d9bc7675e | 117 | n_acc = 0; |
justinbuckland | 42:166d9bc7675e | 118 | return R_avg; |
justinbuckland | 42:166d9bc7675e | 119 | } |
justinbuckland | 42:166d9bc7675e | 120 | |
omatthews | 31:7c6f05326c4d | 121 | float Heater::Get_R_ref() const |
omatthews | 31:7c6f05326c4d | 122 | { |
omatthews | 31:7c6f05326c4d | 123 | return R_ref; |
omatthews | 31:7c6f05326c4d | 124 | } |
omatthews | 31:7c6f05326c4d | 125 | |
omatthews | 31:7c6f05326c4d | 126 | float Heater::Get_error() const |
omatthews | 31:7c6f05326c4d | 127 | { |
omatthews | 31:7c6f05326c4d | 128 | return error; |
omatthews | 31:7c6f05326c4d | 129 | } |
omatthews | 31:7c6f05326c4d | 130 | |
omatthews | 31:7c6f05326c4d | 131 | float Heater::Get_error_integrated() const |
omatthews | 31:7c6f05326c4d | 132 | { |
omatthews | 31:7c6f05326c4d | 133 | return error_integrated; |
omatthews | 31:7c6f05326c4d | 134 | } |
omatthews | 31:7c6f05326c4d | 135 | |
omatthews | 30:055d856f05b5 | 136 | void Heater::turn_on () |
omatthews | 19:fccdd7127f94 | 137 | { |
omatthews | 19:fccdd7127f94 | 138 | *drive = 1; |
omatthews | 26:f6c98b05ee85 | 139 | *guard = thermal.guard_drive_ratio; |
omatthews | 19:fccdd7127f94 | 140 | } |
omatthews | 1:4435d407d827 | 141 | |
omatthews | 30:055d856f05b5 | 142 | void Heater::turn_off () |
omatthews | 19:fccdd7127f94 | 143 | { |
omatthews | 19:fccdd7127f94 | 144 | *drive = 0; |
omatthews | 19:fccdd7127f94 | 145 | *guard = 0; |
omatthews | 19:fccdd7127f94 | 146 | } |