Heater for threaded program

Dependents:   LEX_Threaded_Programming

Committer:
omatthews
Date:
Wed Jul 17 13:00:41 2019 +0000
Revision:
1:4435d407d827
Parent:
0:4e33cc8171f4
Child:
2:7f15386fcc90
Implemented heaters

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 0:4e33cc8171f4 20 Heater::Heater(int i_port, int v_port, DigitalOut drive, float R_set)
omatthews 1:4435d407d827 21 :R_set(R_set),i_port(i_port),v_port(v_port),drive(drive){}
omatthews 0:4e33cc8171f4 22
omatthews 0:4e33cc8171f4 23
omatthews 0:4e33cc8171f4 24 void Heater::read_R()
omatthews 0:4e33cc8171f4 25 {
omatthews 1:4435d407d827 26 //Reads R and then resets the drive back to its previous value
omatthews 1:4435d407d827 27 int i = 0;
omatthews 1:4435d407d827 28 int drive_prev = drive;
omatthews 1:4435d407d827 29 drive = 1;
omatthews 1:4435d407d827 30 wait_us(10);
omatthews 1:4435d407d827 31 adc.start_conversion(ALL_CH);
omatthews 1:4435d407d827 32 while(adc_busy == 1)
omatthews 1:4435d407d827 33 {
omatthews 1:4435d407d827 34 wait_us(1);
omatthews 1:4435d407d827 35 i++;
omatthews 1:4435d407d827 36 }
omatthews 0:4e33cc8171f4 37 adc.read_channels();
omatthews 1:4435d407d827 38
omatthews 1:4435d407d827 39 drive = drive_prev;
omatthews 1:4435d407d827 40 //pc.printf("conversion took %d us\n", i );
omatthews 1:4435d407d827 41 i=0;
omatthews 1:4435d407d827 42
omatthews 1:4435d407d827 43
omatthews 1:4435d407d827 44 curr = adc.read_channel_result(i_port)/scale_factors[i_port];
omatthews 0:4e33cc8171f4 45 v = adc.read_channel_result(v_port)/scale_factors[v_port];
omatthews 1:4435d407d827 46 if (curr != 0) R = v/curr;
omatthews 0:4e33cc8171f4 47 }
omatthews 0:4e33cc8171f4 48
omatthews 0:4e33cc8171f4 49 void Heater::hold(int hold_time)
omatthews 0:4e33cc8171f4 50 {
omatthews 0:4e33cc8171f4 51 int end_time = timer.read_ms() + hold_time;
omatthews 1:4435d407d827 52 pc.printf("end time is %d \n",end_time);
omatthews 1:4435d407d827 53 float R_avg = 0;
omatthews 1:4435d407d827 54 while (timer.read_ms() < end_time)
omatthews 0:4e33cc8171f4 55 {
omatthews 0:4e33cc8171f4 56 drive = 1;
omatthews 0:4e33cc8171f4 57 wait_us(MEAS_DELAY);
omatthews 0:4e33cc8171f4 58 read_R();
omatthews 1:4435d407d827 59 R_avg = ((N_ROLL_AVG-1)*R_avg + R)/N_ROLL_AVG;
omatthews 1:4435d407d827 60 if (R_avg > R_set)
omatthews 0:4e33cc8171f4 61 {
omatthews 0:4e33cc8171f4 62 drive = 0;
omatthews 1:4435d407d827 63 wait_us(2*MEAS_DELAY);
omatthews 0:4e33cc8171f4 64 }
omatthews 1:4435d407d827 65
omatthews 0:4e33cc8171f4 66 }
omatthews 1:4435d407d827 67 }
omatthews 1:4435d407d827 68
omatthews 0:4e33cc8171f4 69
omatthews 0:4e33cc8171f4 70 void Heater::Set_R_set(float R) {R_set = R;}
omatthews 0:4e33cc8171f4 71
omatthews 0:4e33cc8171f4 72 float Heater::Get_R() {return R;}
omatthews 0:4e33cc8171f4 73
omatthews 1:4435d407d827 74 void Heater::turn_on () {drive = 1;}
omatthews 1:4435d407d827 75
omatthews 1:4435d407d827 76 void Heater::turn_off () {drive = 0;}