Heater for threaded program

Dependents:   LEX_Threaded_Programming

Committer:
omatthews
Date:
Thu Jul 18 16:27:37 2019 +0000
Revision:
6:71d9c10fca4a
Parent:
4:29ffcc7b410e
It keeps crashing I can't go on

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 6:71d9c10fca4a 20 Heater::Heater(int i_port, int v_port, PwmOut drive, float corr_grad, float corr_int, float R_ref)
omatthews 6:71d9c10fca4a 21 :R_ref(R_ref),i_port(i_port),v_port(v_port),drive(drive),corr_grad(corr_grad),corr_int(corr_int)
omatthews 6:71d9c10fca4a 22 {
omatthews 6:71d9c10fca4a 23 drive = 0;
omatthews 6:71d9c10fca4a 24 drive.period_us(500);
omatthews 6:71d9c10fca4a 25 }
omatthews 3:313711a66929 26
omatthews 3:313711a66929 27 float Heater::R_to_T(float R) {return R*corr_grad + corr_int;}
omatthews 3:313711a66929 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 4:29ffcc7b410e 34
omatthews 1:4435d407d827 35 int i = 0;
omatthews 6:71d9c10fca4a 36 float drive_prev = drive.read(); //Store previous value of drive
omatthews 6:71d9c10fca4a 37 if (drive_prev > 0.3) {pc.printf("%f\n",drive_prev);}
omatthews 6:71d9c10fca4a 38 drive.write(1.0);
omatthews 6:71d9c10fca4a 39 wait_us(500); //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 6:71d9c10fca4a 46 drive.write(drive_prev);
omatthews 3:313711a66929 47 adc.read_channels();
omatthews 1:4435d407d827 48
omatthews 4:29ffcc7b410e 49 //i=0;
omatthews 1:4435d407d827 50
omatthews 3:313711a66929 51 curr = adc.read_channel_result(i_port);
omatthews 3:313711a66929 52 v = adc.read_channel_result(v_port);
omatthews 3:313711a66929 53 if (v<0) {pc.printf("v is %d",v);}
omatthews 3:313711a66929 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 3:313711a66929 62 //Holds the heater at R_ref for the given hold time
omatthews 3:313711a66929 63 // in: int hold_time - is the time in ms to hold the reference
omatthews 3:313711a66929 64
omatthews 0:4e33cc8171f4 65 int end_time = timer.read_ms() + hold_time;
omatthews 6:71d9c10fca4a 66 //float R_avg = 0;
omatthews 1:4435d407d827 67 while (timer.read_ms() < end_time)
omatthews 0:4e33cc8171f4 68 {
omatthews 2:7f15386fcc90 69 read();
omatthews 6:71d9c10fca4a 70 //R_avg = ((N_ROLL_AVG-1)*R_avg + R)/N_ROLL_AVG; //Enable rolling average
omatthews 6:71d9c10fca4a 71 if (R > R_ref)
omatthews 0:4e33cc8171f4 72 {
omatthews 6:71d9c10fca4a 73 drive.write(0);
omatthews 6:71d9c10fca4a 74 wait_ms(5); //Minimum duty cycle of 10%
omatthews 0:4e33cc8171f4 75 }
omatthews 3:313711a66929 76 else
omatthews 3:313711a66929 77 {
omatthews 6:71d9c10fca4a 78 drive.write(0.1);
omatthews 6:71d9c10fca4a 79 wait_us(5); //Shorter wait time as there is no cost for checking
omatthews 3:313711a66929 80 }
omatthews 3:313711a66929 81
omatthews 0:4e33cc8171f4 82 }
omatthews 1:4435d407d827 83 }
omatthews 1:4435d407d827 84
omatthews 0:4e33cc8171f4 85
omatthews 3:313711a66929 86 void Heater::ramp_R(int ramp_time, float R_final, float R_start)
omatthews 3:313711a66929 87 {
omatthews 3:313711a66929 88 int start_time = timer.read_ms();
omatthews 3:313711a66929 89 int end_time = start_time + ramp_time;
omatthews 3:313711a66929 90 float ramp_rate = (R_final - R_start)/ramp_time;
omatthews 3:313711a66929 91
omatthews 3:313711a66929 92 while (int time = timer.read_ms() < end_time)
omatthews 3:313711a66929 93 {
omatthews 3:313711a66929 94 Set_R_ref(R_start + ramp_rate * (time - start_time)/(end_time - start_time));
omatthews 3:313711a66929 95 hold(1);
omatthews 3:313711a66929 96 }
omatthews 3:313711a66929 97 }
omatthews 3:313711a66929 98
omatthews 4:29ffcc7b410e 99 void Heater::ramp_T(int ramp_time, float T_final, float T_start)
omatthews 4:29ffcc7b410e 100 {
omatthews 4:29ffcc7b410e 101 ramp_R(ramp_time, T_to_R(T_final), T_to_R(T_start));
omatthews 4:29ffcc7b410e 102 }
omatthews 3:313711a66929 103 void Heater::Set_R_ref(float R) {R_ref = R;}
omatthews 3:313711a66929 104 void Heater::Set_T_ref(float T_ref) {R_ref = T_to_R(T_ref);}
omatthews 0:4e33cc8171f4 105
omatthews 2:7f15386fcc90 106 int Heater::Get_i() {return curr;}
omatthews 2:7f15386fcc90 107 int Heater::Get_v() {return v;}
omatthews 2:7f15386fcc90 108
omatthews 0:4e33cc8171f4 109 float Heater::Get_R() {return R;}
omatthews 3:313711a66929 110 float Heater::Get_T() {return R_to_T(R);}
omatthews 0:4e33cc8171f4 111
omatthews 1:4435d407d827 112 void Heater::turn_on () {drive = 1;}
omatthews 1:4435d407d827 113
omatthews 1:4435d407d827 114 void Heater::turn_off () {drive = 0;}