Heater for threaded program

Dependents:   LEX_Threaded_Programming

Committer:
omatthews
Date:
Mon Jul 22 10:46:26 2019 +0000
Revision:
10:0e16d8430d66
Parent:
8:5da71ae16115
Child:
11:785a0329f802
Ensuring ADC read is fast enough

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 0:4e33cc8171f4 12 extern ADS8568_ADC adc;
omatthews 0:4e33cc8171f4 13 extern float scale_factors[8];
omatthews 0:4e33cc8171f4 14 extern Timer timer;
omatthews 1:4435d407d827 15 extern DigitalIn adc_busy;
omatthews 1:4435d407d827 16 extern MODSERIAL pc;
omatthews 1:4435d407d827 17
omatthews 0:4e33cc8171f4 18
omatthews 0:4e33cc8171f4 19
omatthews 8:5da71ae16115 20 Heater::Heater(int i_port, int v_port, DigitalOut drive, float corr_grad, float corr_int, float R_ref)
omatthews 8:5da71ae16115 21 :R_ref(R_ref),i_port(i_port),v_port(v_port),drive(drive),corr_grad(corr_grad),corr_int(corr_int) {}
omatthews 7:59ece353eea2 22
omatthews 7:59ece353eea2 23 float Heater::R_to_T(float R) {return R*corr_grad + corr_int;}
omatthews 7:59ece353eea2 24 float Heater::T_to_R(float T) {return (T - corr_int)/corr_grad;}
omatthews 0:4e33cc8171f4 25
omatthews 0:4e33cc8171f4 26
omatthews 2:7f15386fcc90 27 void Heater::read()
omatthews 0:4e33cc8171f4 28 {
omatthews 1:4435d407d827 29 //Reads R and then resets the drive back to its previous value
omatthews 10:0e16d8430d66 30 timer.reset();
omatthews 1:4435d407d827 31 int i = 0;
omatthews 10:0e16d8430d66 32 int drive_prev = drive; //Store previous value of drive
omatthews 8:5da71ae16115 33 drive = 1;
omatthews 7:59ece353eea2 34 wait_us(MEAS_DELAY); //Wait for ADC to settle
omatthews 1:4435d407d827 35 adc.start_conversion(ALL_CH);
omatthews 1:4435d407d827 36 while(adc_busy == 1)
omatthews 1:4435d407d827 37 {
omatthews 1:4435d407d827 38 wait_us(1);
omatthews 1:4435d407d827 39 i++;
omatthews 1:4435d407d827 40 }
omatthews 10:0e16d8430d66 41 drive = drive_prev;
omatthews 0:4e33cc8171f4 42 adc.read_channels();
omatthews 10:0e16d8430d66 43
omatthews 8:5da71ae16115 44
omatthews 8:5da71ae16115 45 //pc.printf("conversion took %d us\n", i );
omatthews 7:59ece353eea2 46 //i=0;
omatthews 1:4435d407d827 47
omatthews 7:59ece353eea2 48 curr = adc.read_channel_result(i_port);
omatthews 7:59ece353eea2 49 v = adc.read_channel_result(v_port);
omatthews 7:59ece353eea2 50 if (v<0) {pc.printf("v is %d",v);}
omatthews 8:5da71ae16115 51 //if (curr > 0) {R = (float)v/curr;} //Avoid dividing by 0
omatthews 8:5da71ae16115 52 R = (float)v/curr;
omatthews 0:4e33cc8171f4 53 }
omatthews 0:4e33cc8171f4 54
omatthews 2:7f15386fcc90 55
omatthews 2:7f15386fcc90 56
omatthews 2:7f15386fcc90 57
omatthews 0:4e33cc8171f4 58 void Heater::hold(int hold_time)
omatthews 0:4e33cc8171f4 59 {
omatthews 7:59ece353eea2 60 //Holds the heater at R_ref for the given hold time
omatthews 7:59ece353eea2 61 // in: int hold_time - is the time in ms to hold the reference
omatthews 7:59ece353eea2 62
omatthews 0:4e33cc8171f4 63 int end_time = timer.read_ms() + hold_time;
omatthews 1:4435d407d827 64 while (timer.read_ms() < end_time)
omatthews 0:4e33cc8171f4 65 {
omatthews 2:7f15386fcc90 66 read();
omatthews 7:59ece353eea2 67 if (R > R_ref)
omatthews 0:4e33cc8171f4 68 {
omatthews 8:5da71ae16115 69 drive = 0;
omatthews 8:5da71ae16115 70 wait_ms(10); //Minimum duty cycle of 10%
omatthews 0:4e33cc8171f4 71 }
omatthews 10:0e16d8430d66 72 else
omatthews 10:0e16d8430d66 73 {
omatthews 10:0e16d8430d66 74 drive = 1;
omatthews 10:0e16d8430d66 75 }
omatthews 8:5da71ae16115 76
omatthews 0:4e33cc8171f4 77 }
omatthews 1:4435d407d827 78 }
omatthews 1:4435d407d827 79
omatthews 0:4e33cc8171f4 80
omatthews 7:59ece353eea2 81 void Heater::ramp_R(int ramp_time, float R_final, float R_start)
omatthews 7:59ece353eea2 82 {
omatthews 8:5da71ae16115 83 int time = timer.read_ms();
omatthews 8:5da71ae16115 84 int start_time = time;
omatthews 7:59ece353eea2 85 int end_time = start_time + ramp_time;
omatthews 7:59ece353eea2 86 float ramp_rate = (R_final - R_start)/ramp_time;
omatthews 7:59ece353eea2 87
omatthews 8:5da71ae16115 88 while (time < end_time)
omatthews 7:59ece353eea2 89 {
omatthews 8:5da71ae16115 90 Set_R_ref(R_start + ramp_rate * (time - start_time));
omatthews 7:59ece353eea2 91 hold(1);
omatthews 8:5da71ae16115 92 time = timer.read_ms();
omatthews 7:59ece353eea2 93 }
omatthews 8:5da71ae16115 94
omatthews 7:59ece353eea2 95 }
omatthews 7:59ece353eea2 96
omatthews 7:59ece353eea2 97 void Heater::ramp_T(int ramp_time, float T_final, float T_start)
omatthews 7:59ece353eea2 98 {
omatthews 7:59ece353eea2 99 ramp_R(ramp_time, T_to_R(T_final), T_to_R(T_start));
omatthews 7:59ece353eea2 100 }
omatthews 7:59ece353eea2 101 void Heater::Set_R_ref(float R) {R_ref = R;}
omatthews 7:59ece353eea2 102 void Heater::Set_T_ref(float T_ref) {R_ref = T_to_R(T_ref);}
omatthews 0:4e33cc8171f4 103
omatthews 2:7f15386fcc90 104 int Heater::Get_i() {return curr;}
omatthews 2:7f15386fcc90 105 int Heater::Get_v() {return v;}
omatthews 2:7f15386fcc90 106
omatthews 0:4e33cc8171f4 107 float Heater::Get_R() {return R;}
omatthews 7:59ece353eea2 108 float Heater::Get_T() {return R_to_T(R);}
omatthews 0:4e33cc8171f4 109
omatthews 8:5da71ae16115 110 void Heater::turn_on () {drive = 1;}
omatthews 1:4435d407d827 111
omatthews 8:5da71ae16115 112 void Heater::turn_off () {drive = 0;}