Heater for threaded program
Dependents: LEX_Threaded_Programming_V3
Heater.cpp@35:5acf01897ed6, 2019-09-23 (annotated)
- Committer:
- justinbuckland
- Date:
- Mon Sep 23 15:41:15 2019 +0000
- Revision:
- 35:5acf01897ed6
- Parent:
- 34:294adcc3e4b2
- Child:
- 36:a8130bd29349
Pass calibration coefficients to heater; heater converts ADC->Ohms and returns R in Ohms
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 | { |
omatthews | 30:055d856f05b5 | 15 | drive->prescaler(1); |
omatthews | 30:055d856f05b5 | 16 | guard->prescaler(1); |
omatthews | 30:055d856f05b5 | 17 | drive->period_ticks(1000); |
omatthews | 30:055d856f05b5 | 18 | guard->period_ticks(1000); |
omatthews | 30:055d856f05b5 | 19 | } |
omatthews | 30:055d856f05b5 | 20 | |
omatthews | 2:7f15386fcc90 | 21 | void Heater::read() |
omatthews | 0:4e33cc8171f4 | 22 | { |
omatthews | 1:4435d407d827 | 23 | //Reads R and then resets the drive back to its previous value |
omatthews | 30:055d856f05b5 | 24 | int i = 0; |
omatthews | 17:0bfed0e96927 | 25 | double drive_prev = drive->read(); //Store previous value of drive |
justinbuckland | 35:5acf01897ed6 | 26 | |
justinbuckland | 34:294adcc3e4b2 | 27 | *drive = 1.0f; //Turn the driver on for the measurement |
justinbuckland | 34:294adcc3e4b2 | 28 | wait_us(thermal.settling_time_us); //Wait for ADC to settle |
justinbuckland | 34:294adcc3e4b2 | 29 | adc->start_conversion(ADC_CONV_ALL_CH); |
justinbuckland | 34:294adcc3e4b2 | 30 | |
omatthews | 18:f5d26d3d532f | 31 | //Incremental back off until ADC is free |
omatthews | 30:055d856f05b5 | 32 | while(adc_busy == 1) { |
omatthews | 30:055d856f05b5 | 33 | wait_us(1); |
omatthews | 30:055d856f05b5 | 34 | i++; |
omatthews | 30:055d856f05b5 | 35 | } |
omatthews | 25:09a315a59956 | 36 | |
omatthews | 20:2d34a03ae57e | 37 | drive->write(0); //Reset the duty cycle back to what it was |
omatthews | 18:f5d26d3d532f | 38 | |
omatthews | 18:f5d26d3d532f | 39 | //Get voltage, current and R values from the ADC conversion |
omatthews | 31:7c6f05326c4d | 40 | adc->read_channels(); |
omatthews | 31:7c6f05326c4d | 41 | curr = adc->read_channel_result(i_port); |
omatthews | 31:7c6f05326c4d | 42 | v = adc->read_channel_result(v_port); |
omatthews | 30:055d856f05b5 | 43 | |
justinbuckland | 35:5acf01897ed6 | 44 | if (curr > 0) { //Avoid dividing by 0 |
justinbuckland | 35:5acf01897ed6 | 45 | R = (float)v/curr; |
justinbuckland | 35:5acf01897ed6 | 46 | R = cal_a + cal_b*R; //Convert to Ohms |
omatthews | 30:055d856f05b5 | 47 | } |
justinbuckland | 35:5acf01897ed6 | 48 | |
justinbuckland | 35:5acf01897ed6 | 49 | //R = cal_b; // for debugging |
omatthews | 18:f5d26d3d532f | 50 | //Get error values |
omatthews | 17:0bfed0e96927 | 51 | error = R_ref - R; |
omatthews | 30:055d856f05b5 | 52 | |
omatthews | 25:09a315a59956 | 53 | //Only allow positive integrated errors and limit change in integrated error |
omatthews | 30:055d856f05b5 | 54 | //to help avoid integral windup |
omatthews | 30:055d856f05b5 | 55 | |
justinbuckland | 34:294adcc3e4b2 | 56 | if (abs(error) > thermal.pid_wind_up_limit_ohm) {error = error * thermal.pid_wind_up_limit_ohm / abs(error);} |
omatthews | 25:09a315a59956 | 57 | |
omatthews | 30:055d856f05b5 | 58 | error_integrated += error; |
omatthews | 30:055d856f05b5 | 59 | |
omatthews | 30:055d856f05b5 | 60 | if (error_integrated < 0.0) { |
omatthews | 30:055d856f05b5 | 61 | error_integrated = 0.0; |
omatthews | 30:055d856f05b5 | 62 | } |
omatthews | 0:4e33cc8171f4 | 63 | } |
omatthews | 0:4e33cc8171f4 | 64 | |
omatthews | 25:09a315a59956 | 65 | void Heater::update() |
omatthews | 7:59ece353eea2 | 66 | { |
omatthews | 25:09a315a59956 | 67 | //Update PWM from setpoint and resistance |
paullj | 33:52ab0641f2e6 | 68 | double duty_cycle = (double) thermal.pid_kp_mho * (error + error_integrated/thermal.pid_integral_time_ms); |
justinbuckland | 34:294adcc3e4b2 | 69 | if (duty_cycle > thermal.pid_pwm_limit) duty_cycle = thermal.pid_pwm_limit; |
omatthews | 31:7c6f05326c4d | 70 | drive->write(duty_cycle); |
omatthews | 31:7c6f05326c4d | 71 | guard->write(duty_cycle * thermal.guard_drive_ratio); |
omatthews | 7:59ece353eea2 | 72 | } |
omatthews | 18:f5d26d3d532f | 73 | |
omatthews | 30:055d856f05b5 | 74 | void Heater::Set_ref(float R) |
omatthews | 19:fccdd7127f94 | 75 | { |
omatthews | 19:fccdd7127f94 | 76 | R_ref = R; |
omatthews | 19:fccdd7127f94 | 77 | } |
omatthews | 30:055d856f05b5 | 78 | void Heater::Set_D(float D) |
omatthews | 30:055d856f05b5 | 79 | { |
omatthews | 25:09a315a59956 | 80 | drive->write(D); |
omatthews | 26:f6c98b05ee85 | 81 | guard->write(D*thermal.guard_drive_ratio); |
omatthews | 30:055d856f05b5 | 82 | } |
omatthews | 0:4e33cc8171f4 | 83 | |
omatthews | 31:7c6f05326c4d | 84 | int Heater::Get_D() const |
omatthews | 31:7c6f05326c4d | 85 | { |
omatthews | 31:7c6f05326c4d | 86 | return drive->read(); |
omatthews | 31:7c6f05326c4d | 87 | } |
omatthews | 31:7c6f05326c4d | 88 | |
omatthews | 30:055d856f05b5 | 89 | int Heater::Get_i() const |
omatthews | 30:055d856f05b5 | 90 | { |
omatthews | 30:055d856f05b5 | 91 | return curr; |
omatthews | 30:055d856f05b5 | 92 | } |
omatthews | 30:055d856f05b5 | 93 | int Heater::Get_v() const |
omatthews | 30:055d856f05b5 | 94 | { |
omatthews | 30:055d856f05b5 | 95 | return v; |
omatthews | 30:055d856f05b5 | 96 | } |
omatthews | 2:7f15386fcc90 | 97 | |
omatthews | 30:055d856f05b5 | 98 | float Heater::Get_R() const |
omatthews | 30:055d856f05b5 | 99 | { |
omatthews | 30:055d856f05b5 | 100 | return R; |
omatthews | 30:055d856f05b5 | 101 | } |
omatthews | 0:4e33cc8171f4 | 102 | |
omatthews | 31:7c6f05326c4d | 103 | float Heater::Get_R_ref() const |
omatthews | 31:7c6f05326c4d | 104 | { |
omatthews | 31:7c6f05326c4d | 105 | return R_ref; |
omatthews | 31:7c6f05326c4d | 106 | } |
omatthews | 31:7c6f05326c4d | 107 | |
omatthews | 31:7c6f05326c4d | 108 | float Heater::Get_error() const |
omatthews | 31:7c6f05326c4d | 109 | { |
omatthews | 31:7c6f05326c4d | 110 | return error; |
omatthews | 31:7c6f05326c4d | 111 | } |
omatthews | 31:7c6f05326c4d | 112 | |
omatthews | 31:7c6f05326c4d | 113 | float Heater::Get_error_integrated() const |
omatthews | 31:7c6f05326c4d | 114 | { |
omatthews | 31:7c6f05326c4d | 115 | return error_integrated; |
omatthews | 31:7c6f05326c4d | 116 | } |
omatthews | 31:7c6f05326c4d | 117 | |
omatthews | 30:055d856f05b5 | 118 | void Heater::turn_on () |
omatthews | 19:fccdd7127f94 | 119 | { |
omatthews | 19:fccdd7127f94 | 120 | *drive = 1; |
omatthews | 26:f6c98b05ee85 | 121 | *guard = thermal.guard_drive_ratio; |
omatthews | 19:fccdd7127f94 | 122 | } |
omatthews | 1:4435d407d827 | 123 | |
omatthews | 30:055d856f05b5 | 124 | void Heater::turn_off () |
omatthews | 19:fccdd7127f94 | 125 | { |
omatthews | 19:fccdd7127f94 | 126 | *drive = 0; |
omatthews | 19:fccdd7127f94 | 127 | *guard = 0; |
omatthews | 19:fccdd7127f94 | 128 | } |