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