Heater for threaded program

Dependents:   LEX_Threaded_Programming

Committer:
omatthews
Date:
Tue Aug 27 09:22:36 2019 +0000
Revision:
27:bb97231d1be9
Parent:
26:f6c98b05ee85
For Paul

Who changed what in which revision?

UserRevisionLine numberNew 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 27:bb97231d1be9 40 drive->prescaler(1);
omatthews 27:bb97231d1be9 41 guard->prescaler(1);
omatthews 26:f6c98b05ee85 42 drive->period_ticks(1000);
omatthews 26:f6c98b05ee85 43 guard->period_ticks(1000);
omatthews 26:f6c98b05ee85 44 }
omatthews 26:f6c98b05ee85 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
omatthews 26:f6c98b05ee85 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
omatthews 1:4435d407d827 64 while(adc_busy == 1)
omatthews 1:4435d407d827 65 {
omatthews 1:4435d407d827 66 wait_us(1);
omatthews 1:4435d407d827 67 i++;
omatthews 18:f5d26d3d532f 68 }
omatthews 25:09a315a59956 69
omatthews 20:2d34a03ae57e 70 drive->write(0); //Reset the duty cycle back to what it was
omatthews 18:f5d26d3d532f 71
omatthews 18:f5d26d3d532f 72 //Get voltage, current and R values from the ADC conversion
omatthews 0:4e33cc8171f4 73 adc.read_channels();
omatthews 7:59ece353eea2 74 curr = adc.read_channel_result(i_port);
omatthews 7:59ece353eea2 75 v = adc.read_channel_result(v_port);
omatthews 18:f5d26d3d532f 76
omatthews 17:0bfed0e96927 77 if (curr > 0) {R = (float)v/curr;} //Avoid dividing by 0
omatthews 12:8a048f111140 78
omatthews 18:f5d26d3d532f 79 //Get error values
omatthews 18:f5d26d3d532f 80
omatthews 17:0bfed0e96927 81 error = R_ref - R;
omatthews 14:f266bf960b8d 82
omatthews 25:09a315a59956 83 //Only allow positive integrated errors and limit change in integrated error
omatthews 25:09a315a59956 84 //to help avoid integral windup
omatthews 25:09a315a59956 85
omatthews 20:2d34a03ae57e 86 if (abs(error) < WIND_UP_LIMIT) {error_integrated += error;}
omatthews 23:947850bbf325 87 if (error_integrated < 0.0) {error_integrated = 0.0;}
omatthews 26:f6c98b05ee85 88 pc.printf("%f\n",R);
omatthews 20:2d34a03ae57e 89
omatthews 0:4e33cc8171f4 90 }
omatthews 0:4e33cc8171f4 91
omatthews 2:7f15386fcc90 92
omatthews 2:7f15386fcc90 93
omatthews 2:7f15386fcc90 94
omatthews 25:09a315a59956 95 void Heater::update()
omatthews 7:59ece353eea2 96 {
omatthews 25:09a315a59956 97 //Update PWM from setpoint and resistance
omatthews 26:f6c98b05ee85 98 drive->write((double) (thermal.adc_settling_time_us * (error + error_integrated/thermal.pid_integral_time)));
omatthews 26:f6c98b05ee85 99 guard->write((double) (thermal.adc_settling_time_us * thermal.guard_drive_ratio * (error + error_integrated/thermal.pid_integral_time)));
omatthews 25:09a315a59956 100
omatthews 7:59ece353eea2 101 }
omatthews 18:f5d26d3d532f 102
omatthews 18:f5d26d3d532f 103
omatthews 18:f5d26d3d532f 104
omatthews 25:09a315a59956 105 void Heater::Set_ref(float R)
omatthews 19:fccdd7127f94 106 {
omatthews 19:fccdd7127f94 107 R_ref = R;
omatthews 19:fccdd7127f94 108 }
omatthews 25:09a315a59956 109 void Heater::Set_D(float D) {
omatthews 25:09a315a59956 110 drive->write(D);
omatthews 26:f6c98b05ee85 111 guard->write(D*thermal.guard_drive_ratio);
omatthews 25:09a315a59956 112 }
omatthews 0:4e33cc8171f4 113
omatthews 18:f5d26d3d532f 114 int Heater::Get_i() const {return curr;}
omatthews 18:f5d26d3d532f 115 int Heater::Get_v() const {return v;}
omatthews 2:7f15386fcc90 116
omatthews 18:f5d26d3d532f 117 float Heater::Get_R() const {return R;}
omatthews 0:4e33cc8171f4 118
omatthews 19:fccdd7127f94 119 void Heater::turn_on ()
omatthews 19:fccdd7127f94 120 {
omatthews 19:fccdd7127f94 121 *drive = 1;
omatthews 26:f6c98b05ee85 122 *guard = thermal.guard_drive_ratio;
omatthews 19:fccdd7127f94 123 }
omatthews 1:4435d407d827 124
omatthews 19:fccdd7127f94 125 void Heater::turn_off ()
omatthews 19:fccdd7127f94 126 {
omatthews 19:fccdd7127f94 127 *drive = 0;
omatthews 19:fccdd7127f94 128 *guard = 0;
omatthews 19:fccdd7127f94 129 }