Heater for threaded program
Dependents: LEX_Threaded_Programming
Heater.cpp@25:09a315a59956, 2019-08-19 (annotated)
- Committer:
- omatthews
- Date:
- Mon Aug 19 07:55:38 2019 +0000
- Revision:
- 25:09a315a59956
- Parent:
- 23:947850bbf325
- Child:
- 26:f6c98b05ee85
10/08/2019
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 | |
omatthews | 0:4e33cc8171f4 | 12 | extern ADS8568_ADC adc; |
omatthews | 0:4e33cc8171f4 | 13 | extern Timer timer; |
omatthews | 1:4435d407d827 | 14 | extern DigitalIn adc_busy; |
omatthews | 1:4435d407d827 | 15 | extern MODSERIAL pc; |
omatthews | 20:2d34a03ae57e | 16 | extern DigitalOut led_0; |
omatthews | 0:4e33cc8171f4 | 17 | |
omatthews | 0:4e33cc8171f4 | 18 | |
omatthews | 19:fccdd7127f94 | 19 | Heater::Heater(const int i_port, const int v_port, FastPWM * drive, FastPWM * guard, const float corr_grad, const float corr_int, float R_ref) |
omatthews | 19:fccdd7127f94 | 20 | :R_ref(R_ref),i_port(i_port),v_port(v_port),drive(drive),guard(guard),corr_grad(corr_grad),corr_int(corr_int) {} |
omatthews | 7:59ece353eea2 | 21 | |
omatthews | 0:4e33cc8171f4 | 22 | |
omatthews | 18:f5d26d3d532f | 23 | void Heater::output()const |
omatthews | 11:785a0329f802 | 24 | { |
omatthews | 18:f5d26d3d532f | 25 | //Prints the current state to the terminal |
omatthews | 17:0bfed0e96927 | 26 | pc.printf("%d,%f,%f,%f,%f,%f\n",timer.read_ms(),R_ref,R,error,error_integrated,drive->read()); |
omatthews | 11:785a0329f802 | 27 | } |
omatthews | 0:4e33cc8171f4 | 28 | |
omatthews | 2:7f15386fcc90 | 29 | void Heater::read() |
omatthews | 0:4e33cc8171f4 | 30 | { |
omatthews | 1:4435d407d827 | 31 | //Reads R and then resets the drive back to its previous value |
omatthews | 25:09a315a59956 | 32 | |
omatthews | 1:4435d407d827 | 33 | int i = 0; |
omatthews | 18:f5d26d3d532f | 34 | //float error_prev = error; |
omatthews | 17:0bfed0e96927 | 35 | |
omatthews | 17:0bfed0e96927 | 36 | double drive_prev = drive->read(); //Store previous value of drive |
omatthews | 18:f5d26d3d532f | 37 | *drive = 1.0f; //Turn the driver on for the measurement |
omatthews | 7:59ece353eea2 | 38 | wait_us(MEAS_DELAY); //Wait for ADC to settle |
omatthews | 25:09a315a59956 | 39 | |
omatthews | 25:09a315a59956 | 40 | adc.start_conversion(15); |
omatthews | 25:09a315a59956 | 41 | |
omatthews | 18:f5d26d3d532f | 42 | //Incremental back off until ADC is free |
omatthews | 1:4435d407d827 | 43 | while(adc_busy == 1) |
omatthews | 1:4435d407d827 | 44 | { |
omatthews | 1:4435d407d827 | 45 | wait_us(1); |
omatthews | 1:4435d407d827 | 46 | i++; |
omatthews | 18:f5d26d3d532f | 47 | } |
omatthews | 25:09a315a59956 | 48 | |
omatthews | 20:2d34a03ae57e | 49 | drive->write(0); //Reset the duty cycle back to what it was |
omatthews | 18:f5d26d3d532f | 50 | |
omatthews | 18:f5d26d3d532f | 51 | //Get voltage, current and R values from the ADC conversion |
omatthews | 0:4e33cc8171f4 | 52 | adc.read_channels(); |
omatthews | 7:59ece353eea2 | 53 | curr = adc.read_channel_result(i_port); |
omatthews | 7:59ece353eea2 | 54 | v = adc.read_channel_result(v_port); |
omatthews | 18:f5d26d3d532f | 55 | |
omatthews | 17:0bfed0e96927 | 56 | if (curr > 0) {R = (float)v/curr;} //Avoid dividing by 0 |
omatthews | 12:8a048f111140 | 57 | |
omatthews | 18:f5d26d3d532f | 58 | //Get error values |
omatthews | 18:f5d26d3d532f | 59 | |
omatthews | 17:0bfed0e96927 | 60 | error = R_ref - R; |
omatthews | 14:f266bf960b8d | 61 | |
omatthews | 25:09a315a59956 | 62 | //Only allow positive integrated errors and limit change in integrated error |
omatthews | 25:09a315a59956 | 63 | //to help avoid integral windup |
omatthews | 25:09a315a59956 | 64 | |
omatthews | 20:2d34a03ae57e | 65 | if (abs(error) < WIND_UP_LIMIT) {error_integrated += error;} |
omatthews | 23:947850bbf325 | 66 | if (error_integrated < 0.0) {error_integrated = 0.0;} |
omatthews | 20:2d34a03ae57e | 67 | |
omatthews | 0:4e33cc8171f4 | 68 | } |
omatthews | 0:4e33cc8171f4 | 69 | |
omatthews | 2:7f15386fcc90 | 70 | |
omatthews | 2:7f15386fcc90 | 71 | |
omatthews | 2:7f15386fcc90 | 72 | |
omatthews | 25:09a315a59956 | 73 | void Heater::update() |
omatthews | 7:59ece353eea2 | 74 | { |
omatthews | 25:09a315a59956 | 75 | //Update PWM from setpoint and resistance |
omatthews | 25:09a315a59956 | 76 | read(); |
omatthews | 25:09a315a59956 | 77 | drive->write((double) (Kp * (error + error_integrated/Ti))); |
omatthews | 25:09a315a59956 | 78 | guard->write((double) (Kp * GUARD_PWM_RATIO * (error + error_integrated/Ti))); |
omatthews | 25:09a315a59956 | 79 | log_count++; |
omatthews | 25:09a315a59956 | 80 | if (log_count >= LOG_LIM) |
omatthews | 7:59ece353eea2 | 81 | { |
omatthews | 25:09a315a59956 | 82 | log_count = 0; |
omatthews | 25:09a315a59956 | 83 | output(); |
omatthews | 7:59ece353eea2 | 84 | } |
omatthews | 25:09a315a59956 | 85 | |
omatthews | 7:59ece353eea2 | 86 | } |
omatthews | 18:f5d26d3d532f | 87 | |
omatthews | 18:f5d26d3d532f | 88 | |
omatthews | 18:f5d26d3d532f | 89 | |
omatthews | 25:09a315a59956 | 90 | void Heater::Set_ref(float R) |
omatthews | 19:fccdd7127f94 | 91 | { |
omatthews | 19:fccdd7127f94 | 92 | R_ref = R; |
omatthews | 19:fccdd7127f94 | 93 | } |
omatthews | 25:09a315a59956 | 94 | void Heater::Set_D(float D) { |
omatthews | 25:09a315a59956 | 95 | drive->write(D); |
omatthews | 25:09a315a59956 | 96 | guard->write(D*GUARD_PWM_RATIO); |
omatthews | 25:09a315a59956 | 97 | } |
omatthews | 0:4e33cc8171f4 | 98 | |
omatthews | 18:f5d26d3d532f | 99 | int Heater::Get_i() const {return curr;} |
omatthews | 18:f5d26d3d532f | 100 | int Heater::Get_v() const {return v;} |
omatthews | 2:7f15386fcc90 | 101 | |
omatthews | 18:f5d26d3d532f | 102 | float Heater::Get_R() const {return R;} |
omatthews | 0:4e33cc8171f4 | 103 | |
omatthews | 19:fccdd7127f94 | 104 | void Heater::turn_on () |
omatthews | 19:fccdd7127f94 | 105 | { |
omatthews | 19:fccdd7127f94 | 106 | *drive = 1; |
omatthews | 19:fccdd7127f94 | 107 | *guard = GUARD_PWM_RATIO; |
omatthews | 19:fccdd7127f94 | 108 | } |
omatthews | 1:4435d407d827 | 109 | |
omatthews | 19:fccdd7127f94 | 110 | void Heater::turn_off () |
omatthews | 19:fccdd7127f94 | 111 | { |
omatthews | 19:fccdd7127f94 | 112 | *drive = 0; |
omatthews | 19:fccdd7127f94 | 113 | *guard = 0; |
omatthews | 19:fccdd7127f94 | 114 | } |