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