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