Heater for threaded program

Dependents:   LEX_Threaded_Programming

Committer:
omatthews
Date:
Thu Jul 18 12:17:42 2019 +0000
Revision:
4:29ffcc7b410e
Parent:
3:313711a66929
Child:
5:21442d9d19c5
Child:
6:71d9c10fca4a
Final design before PWM

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 3:313711a66929 20 Heater::Heater(int i_port, int v_port, DigitalOut drive, float corr_grad, float corr_int, float R_ref)
omatthews 3:313711a66929 21 :R_ref(R_ref),i_port(i_port),v_port(v_port),drive(drive),corr_grad(corr_grad),corr_int(corr_int) {}
omatthews 3:313711a66929 22
omatthews 3:313711a66929 23 float Heater::R_to_T(float R) {return R*corr_grad + corr_int;}
omatthews 3:313711a66929 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 4:29ffcc7b410e 30
omatthews 1:4435d407d827 31 int i = 0;
omatthews 3:313711a66929 32 int drive_prev = drive; //Store previous value of drive
omatthews 1:4435d407d827 33 drive = 1;
omatthews 4:29ffcc7b410e 34 wait_us(50); //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 4:29ffcc7b410e 41 drive = drive_prev;
omatthews 3:313711a66929 42 adc.read_channels();
omatthews 1:4435d407d827 43
omatthews 1:4435d407d827 44 //pc.printf("conversion took %d us\n", i );
omatthews 4:29ffcc7b410e 45 //i=0;
omatthews 1:4435d407d827 46
omatthews 3:313711a66929 47 curr = adc.read_channel_result(i_port);
omatthews 3:313711a66929 48 v = adc.read_channel_result(v_port);
omatthews 3:313711a66929 49 if (v<0) {pc.printf("v is %d",v);}
omatthews 3:313711a66929 50 if (curr > 0) {R = (float)v/curr;} //Avoid dividing by 0
omatthews 0:4e33cc8171f4 51 }
omatthews 0:4e33cc8171f4 52
omatthews 2:7f15386fcc90 53
omatthews 2:7f15386fcc90 54
omatthews 2:7f15386fcc90 55
omatthews 0:4e33cc8171f4 56 void Heater::hold(int hold_time)
omatthews 0:4e33cc8171f4 57 {
omatthews 3:313711a66929 58 //Holds the heater at R_ref for the given hold time
omatthews 3:313711a66929 59 // in: int hold_time - is the time in ms to hold the reference
omatthews 3:313711a66929 60
omatthews 0:4e33cc8171f4 61 int end_time = timer.read_ms() + hold_time;
omatthews 1:4435d407d827 62 float R_avg = 0;
omatthews 1:4435d407d827 63 while (timer.read_ms() < end_time)
omatthews 0:4e33cc8171f4 64 {
omatthews 2:7f15386fcc90 65 read();
omatthews 3:313711a66929 66 R_avg = ((N_ROLL_AVG-1)*R_avg + R)/N_ROLL_AVG; //Enable rolling average
omatthews 3:313711a66929 67 if (R_avg > R_ref)
omatthews 0:4e33cc8171f4 68 {
omatthews 0:4e33cc8171f4 69 drive = 0;
omatthews 3:313711a66929 70 wait_us(100); //Minimum duty cycle of 10%
omatthews 0:4e33cc8171f4 71 }
omatthews 3:313711a66929 72 else
omatthews 3:313711a66929 73 {
omatthews 3:313711a66929 74 drive = 1;
omatthews 3:313711a66929 75 //wait_us(20); //Shorter wait time as there is no cost for checking
omatthews 3:313711a66929 76 }
omatthews 3:313711a66929 77
omatthews 0:4e33cc8171f4 78 }
omatthews 1:4435d407d827 79 }
omatthews 1:4435d407d827 80
omatthews 0:4e33cc8171f4 81
omatthews 3:313711a66929 82 void Heater::ramp_R(int ramp_time, float R_final, float R_start)
omatthews 3:313711a66929 83 {
omatthews 3:313711a66929 84 int start_time = timer.read_ms();
omatthews 3:313711a66929 85 int end_time = start_time + ramp_time;
omatthews 3:313711a66929 86 float ramp_rate = (R_final - R_start)/ramp_time;
omatthews 3:313711a66929 87
omatthews 3:313711a66929 88 while (int time = timer.read_ms() < end_time)
omatthews 3:313711a66929 89 {
omatthews 3:313711a66929 90 Set_R_ref(R_start + ramp_rate * (time - start_time)/(end_time - start_time));
omatthews 3:313711a66929 91 hold(1);
omatthews 3:313711a66929 92 }
omatthews 3:313711a66929 93 }
omatthews 3:313711a66929 94
omatthews 4:29ffcc7b410e 95 void Heater::ramp_T(int ramp_time, float T_final, float T_start)
omatthews 4:29ffcc7b410e 96 {
omatthews 4:29ffcc7b410e 97 ramp_R(ramp_time, T_to_R(T_final), T_to_R(T_start));
omatthews 4:29ffcc7b410e 98 }
omatthews 3:313711a66929 99 void Heater::Set_R_ref(float R) {R_ref = R;}
omatthews 3:313711a66929 100 void Heater::Set_T_ref(float T_ref) {R_ref = T_to_R(T_ref);}
omatthews 0:4e33cc8171f4 101
omatthews 2:7f15386fcc90 102 int Heater::Get_i() {return curr;}
omatthews 2:7f15386fcc90 103 int Heater::Get_v() {return v;}
omatthews 2:7f15386fcc90 104
omatthews 0:4e33cc8171f4 105 float Heater::Get_R() {return R;}
omatthews 3:313711a66929 106 float Heater::Get_T() {return R_to_T(R);}
omatthews 0:4e33cc8171f4 107
omatthews 1:4435d407d827 108 void Heater::turn_on () {drive = 1;}
omatthews 1:4435d407d827 109
omatthews 1:4435d407d827 110 void Heater::turn_off () {drive = 0;}