Heater for threaded program

Dependents:   LEX_Threaded_Programming

Committer:
omatthews
Date:
Fri Jul 19 09:22:47 2019 +0000
Revision:
7:59ece353eea2
Parent:
2:7f15386fcc90
Child:
8:5da71ae16115
Trouble with PWM. Come back later

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 7:59ece353eea2 20 Heater::Heater(int i_port, int v_port, PwmOut drive, float corr_grad, float corr_int, float R_ref)
omatthews 7:59ece353eea2 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 drive.write(0.1);
omatthews 7:59ece353eea2 24 drive.period_us(100);
omatthews 7:59ece353eea2 25 }
omatthews 7:59ece353eea2 26
omatthews 7:59ece353eea2 27 float Heater::R_to_T(float R) {return R*corr_grad + corr_int;}
omatthews 7:59ece353eea2 28 float Heater::T_to_R(float T) {return (T - corr_int)/corr_grad;}
omatthews 0:4e33cc8171f4 29
omatthews 0:4e33cc8171f4 30
omatthews 2:7f15386fcc90 31 void Heater::read()
omatthews 0:4e33cc8171f4 32 {
omatthews 1:4435d407d827 33 //Reads R and then resets the drive back to its previous value
omatthews 7:59ece353eea2 34
omatthews 1:4435d407d827 35 int i = 0;
omatthews 7:59ece353eea2 36 float drive_prev = drive.read(); //Store previous value of drive
omatthews 7:59ece353eea2 37 if (drive_prev > 0.3) {pc.printf("%f\n",drive_prev);}
omatthews 7:59ece353eea2 38 drive.write(1.0);
omatthews 7:59ece353eea2 39 wait_us(MEAS_DELAY); //Wait for ADC to settle
omatthews 1:4435d407d827 40 adc.start_conversion(ALL_CH);
omatthews 1:4435d407d827 41 while(adc_busy == 1)
omatthews 1:4435d407d827 42 {
omatthews 1:4435d407d827 43 wait_us(1);
omatthews 1:4435d407d827 44 i++;
omatthews 1:4435d407d827 45 }
omatthews 7:59ece353eea2 46 drive.write(drive_prev);
omatthews 0:4e33cc8171f4 47 adc.read_channels();
omatthews 7:59ece353eea2 48 if (i>0) {pc.printf("wait time is %d us",i);}
omatthews 7:59ece353eea2 49 //i=0;
omatthews 1:4435d407d827 50
omatthews 7:59ece353eea2 51 curr = adc.read_channel_result(i_port);
omatthews 7:59ece353eea2 52 v = adc.read_channel_result(v_port);
omatthews 7:59ece353eea2 53 if (v<0) {pc.printf("v is %d",v);}
omatthews 7:59ece353eea2 54 if (curr > 0) {R = (float)v/curr;} //Avoid dividing by 0
omatthews 0:4e33cc8171f4 55 }
omatthews 0:4e33cc8171f4 56
omatthews 2:7f15386fcc90 57
omatthews 2:7f15386fcc90 58
omatthews 2:7f15386fcc90 59
omatthews 0:4e33cc8171f4 60 void Heater::hold(int hold_time)
omatthews 0:4e33cc8171f4 61 {
omatthews 7:59ece353eea2 62 //Holds the heater at R_ref for the given hold time
omatthews 7:59ece353eea2 63 // in: int hold_time - is the time in ms to hold the reference
omatthews 7:59ece353eea2 64
omatthews 0:4e33cc8171f4 65 int end_time = timer.read_ms() + hold_time;
omatthews 7:59ece353eea2 66 //float R_avg = 0;
omatthews 7:59ece353eea2 67 int j = 0;
omatthews 7:59ece353eea2 68 if (j == 0) {pc.printf("D if before wait is %f\n",drive.read());}
omatthews 1:4435d407d827 69 while (timer.read_ms() < end_time)
omatthews 0:4e33cc8171f4 70 {
omatthews 2:7f15386fcc90 71 read();
omatthews 7:59ece353eea2 72 //R_avg = ((N_ROLL_AVG-1)*R_avg + R)/N_ROLL_AVG; //Enable rolling average
omatthews 7:59ece353eea2 73 if (R > R_ref)
omatthews 0:4e33cc8171f4 74 {
omatthews 7:59ece353eea2 75 drive.write(0);
omatthews 7:59ece353eea2 76 wait_ms(1000); //Minimum duty cycle of 10%
omatthews 0:4e33cc8171f4 77 }
omatthews 7:59ece353eea2 78 else
omatthews 7:59ece353eea2 79 {
omatthews 7:59ece353eea2 80 drive.write(0.1);
omatthews 7:59ece353eea2 81 wait_ms(1000); //Shorter wait time as there is no cost for checking
omatthews 7:59ece353eea2 82 }
omatthews 7:59ece353eea2 83 j++;
omatthews 0:4e33cc8171f4 84 }
omatthews 1:4435d407d827 85 }
omatthews 1:4435d407d827 86
omatthews 0:4e33cc8171f4 87
omatthews 7:59ece353eea2 88 void Heater::ramp_R(int ramp_time, float R_final, float R_start)
omatthews 7:59ece353eea2 89 {
omatthews 7:59ece353eea2 90 int start_time = timer.read_ms();
omatthews 7:59ece353eea2 91 int end_time = start_time + ramp_time;
omatthews 7:59ece353eea2 92 float ramp_rate = (R_final - R_start)/ramp_time;
omatthews 7:59ece353eea2 93
omatthews 7:59ece353eea2 94 while (int time = timer.read_ms() < end_time)
omatthews 7:59ece353eea2 95 {
omatthews 7:59ece353eea2 96 Set_R_ref(R_start + ramp_rate * (time - start_time)/(end_time - start_time));
omatthews 7:59ece353eea2 97 hold(1);
omatthews 7:59ece353eea2 98 }
omatthews 7:59ece353eea2 99 }
omatthews 7:59ece353eea2 100
omatthews 7:59ece353eea2 101 void Heater::ramp_T(int ramp_time, float T_final, float T_start)
omatthews 7:59ece353eea2 102 {
omatthews 7:59ece353eea2 103 ramp_R(ramp_time, T_to_R(T_final), T_to_R(T_start));
omatthews 7:59ece353eea2 104 }
omatthews 7:59ece353eea2 105 void Heater::Set_R_ref(float R) {R_ref = R;}
omatthews 7:59ece353eea2 106 void Heater::Set_T_ref(float T_ref) {R_ref = T_to_R(T_ref);}
omatthews 0:4e33cc8171f4 107
omatthews 2:7f15386fcc90 108 int Heater::Get_i() {return curr;}
omatthews 2:7f15386fcc90 109 int Heater::Get_v() {return v;}
omatthews 2:7f15386fcc90 110
omatthews 0:4e33cc8171f4 111 float Heater::Get_R() {return R;}
omatthews 7:59ece353eea2 112 float Heater::Get_T() {return R_to_T(R);}
omatthews 0:4e33cc8171f4 113
omatthews 7:59ece353eea2 114 void Heater::turn_on () {drive.write(0.1);}
omatthews 1:4435d407d827 115
omatthews 7:59ece353eea2 116 void Heater::turn_off () {drive = 0;}
omatthews 7:59ece353eea2 117 float Heater::check_D () {return drive.read();}
omatthews 7:59ece353eea2 118
omatthews 7:59ece353eea2 119
omatthews 7:59ece353eea2 120