First commit for threaded programme

Dependencies:   Heater MODSERIAL FastPWM ADS8568_ADC

Committer:
omatthews
Date:
Mon Aug 19 07:58:32 2019 +0000
Revision:
99:43cb163b267a
Parent:
98:6c7c7d36a5fe
Working threaded programme

Who changed what in which revision?

UserRevisionLine numberNew contents of line
omatthews 98:6c7c7d36a5fe 1 #include "mbed.h"
omatthews 98:6c7c7d36a5fe 2 #include "MODSERIAL.h"
omatthews 98:6c7c7d36a5fe 3 #include "ADS8568_ADC.h"
omatthews 98:6c7c7d36a5fe 4 #include "Heater.h"
omatthews 98:6c7c7d36a5fe 5 #include "FastPWM.h"
omatthews 98:6c7c7d36a5fe 6
omatthews 98:6c7c7d36a5fe 7 #define ALL_CH 15 //value of convst bus to read all chanels simultaneosly
omatthews 98:6c7c7d36a5fe 8
omatthews 98:6c7c7d36a5fe 9 float r_gradient;
omatthews 98:6c7c7d36a5fe 10 float r_set_points[5] = {0.0,0.50,0.51,0.52,0.50};
omatthews 98:6c7c7d36a5fe 11 int time_points[5] = {0,5000,10000,15000,20000};
omatthews 98:6c7c7d36a5fe 12 int trig_time[5] = {0,0,50,50,50};
omatthews 98:6c7c7d36a5fe 13 int curr_time;
omatthews 98:6c7c7d36a5fe 14 //Globals
omatthews 98:6c7c7d36a5fe 15 int camera_read_time;
omatthews 98:6c7c7d36a5fe 16
omatthews 98:6c7c7d36a5fe 17 MODSERIAL pc(PA_9, PA_10, 512); //mcu TX, RX, 512 byte TX and RX buffers
omatthews 98:6c7c7d36a5fe 18 ADS8568_ADC adc(PB_15, PB_14, PB_13, PB_12, PC_15, PC_0, PC_1, PC_2, PC_3);
omatthews 98:6c7c7d36a5fe 19 I2C i2c(PB_7, PB_8); //SDA, SCL
omatthews 98:6c7c7d36a5fe 20 Timer timer;
omatthews 98:6c7c7d36a5fe 21 DigitalIn adc_busy(PA_8); //Busy interrupt sig#
omatthews 98:6c7c7d36a5fe 22
omatthews 98:6c7c7d36a5fe 23
omatthews 98:6c7c7d36a5fe 24 //Heater Control
omatthews 98:6c7c7d36a5fe 25 FastPWM drive_1(PC_9);
omatthews 98:6c7c7d36a5fe 26 FastPWM drive_2(PC_8);
omatthews 98:6c7c7d36a5fe 27 FastPWM guard_1(PC_7);
omatthews 98:6c7c7d36a5fe 28 FastPWM guard_2(PC_6);
omatthews 98:6c7c7d36a5fe 29
omatthews 98:6c7c7d36a5fe 30
omatthews 98:6c7c7d36a5fe 31 //Heaters
omatthews 98:6c7c7d36a5fe 32 Heater heater_1(0,1,&drive_1, &guard_1, 525.2, -206);
omatthews 98:6c7c7d36a5fe 33 Heater heater_2(2,3,&drive_2, &guard_2, 525.2, -206);
mbed_official 82:abf1b1785bd7 34
omatthews 98:6c7c7d36a5fe 35 //indicator LEDs
omatthews 98:6c7c7d36a5fe 36 DigitalOut hb_led(PC_13); //Green
omatthews 98:6c7c7d36a5fe 37 DigitalOut led_0(PC_4); //Red
omatthews 98:6c7c7d36a5fe 38 DigitalOut led_1(PC_5); //Green
omatthews 98:6c7c7d36a5fe 39
omatthews 98:6c7c7d36a5fe 40 //User buttons
omatthews 98:6c7c7d36a5fe 41 DigitalIn user_0(PB_0);
omatthews 98:6c7c7d36a5fe 42 DigitalIn user_1(PB_1);
omatthews 98:6c7c7d36a5fe 43
omatthews 98:6c7c7d36a5fe 44 BusOut converts(PC_0, PC_1, PC_2, PC_3);
omatthews 98:6c7c7d36a5fe 45
omatthews 98:6c7c7d36a5fe 46
omatthews 98:6c7c7d36a5fe 47 //Threads
omatthews 98:6c7c7d36a5fe 48 Thread serial_listen;
omatthews 98:6c7c7d36a5fe 49 Thread heater_control;
Jonathan Austin 0:2757d7abb7d9 50
omatthews 98:6c7c7d36a5fe 51 //Semaphores
omatthews 98:6c7c7d36a5fe 52 Semaphore heater_semaphore;
omatthews 98:6c7c7d36a5fe 53
omatthews 98:6c7c7d36a5fe 54 //Tickers
omatthews 98:6c7c7d36a5fe 55 Ticker heat_tick;
omatthews 98:6c7c7d36a5fe 56 Ticker pressure_tick;
omatthews 98:6c7c7d36a5fe 57
Jonathan Austin 0:2757d7abb7d9 58
omatthews 98:6c7c7d36a5fe 59 //Flags
omatthews 98:6c7c7d36a5fe 60 bool start_flag = false;
omatthews 98:6c7c7d36a5fe 61 bool config_flag = false;
omatthews 98:6c7c7d36a5fe 62 volatile bool heater_flag = false;
omatthews 98:6c7c7d36a5fe 63 bool triggered_flag = false;
omatthews 98:6c7c7d36a5fe 64 bool untriggered_flag = false;
omatthews 98:6c7c7d36a5fe 65
omatthews 98:6c7c7d36a5fe 66
omatthews 98:6c7c7d36a5fe 67 //Functions
mbed_official 88:bea4f2daa48c 68
omatthews 98:6c7c7d36a5fe 69 void serial_rx(){
omatthews 98:6c7c7d36a5fe 70 while(pc.getc() != 'c');
omatthews 98:6c7c7d36a5fe 71 config_flag = true;
omatthews 98:6c7c7d36a5fe 72 while(pc.getc() != 's');
omatthews 98:6c7c7d36a5fe 73 start_flag = true;
omatthews 98:6c7c7d36a5fe 74 }
omatthews 98:6c7c7d36a5fe 75
omatthews 98:6c7c7d36a5fe 76
omatthews 98:6c7c7d36a5fe 77 void temp_trigger() {
omatthews 98:6c7c7d36a5fe 78 heater_flag = true;
omatthews 98:6c7c7d36a5fe 79 led_1 = !led_1;
omatthews 98:6c7c7d36a5fe 80 }
omatthews 98:6c7c7d36a5fe 81
omatthews 98:6c7c7d36a5fe 82 void temp_control() {
omatthews 98:6c7c7d36a5fe 83 while(1){
omatthews 98:6c7c7d36a5fe 84 while(!heater_flag);
omatthews 98:6c7c7d36a5fe 85 heater_1.update();
omatthews 98:6c7c7d36a5fe 86 }
omatthews 98:6c7c7d36a5fe 87 }
mbed_official 82:abf1b1785bd7 88
omatthews 98:6c7c7d36a5fe 89
omatthews 98:6c7c7d36a5fe 90 void set_point_routine() {
omatthews 98:6c7c7d36a5fe 91 for (int i_set = 1; i_set < sizeof(r_set_points)/sizeof(float); i_set ++){
omatthews 98:6c7c7d36a5fe 92 r_gradient = (r_set_points[i_set] - r_set_points[i_set-1])/(time_points[i_set] - time_points[i_set-1]);
omatthews 98:6c7c7d36a5fe 93 while ((curr_time = timer.read_ms()) <= time_points[i_set]){
omatthews 98:6c7c7d36a5fe 94 heater_1.Set_ref(r_set_points[i_set-1] + r_gradient * (curr_time - time_points[i_set-1]));
omatthews 98:6c7c7d36a5fe 95 if (!triggered_flag && curr_time > time_points[i_set - 1] + trig_time[i_set]){
omatthews 98:6c7c7d36a5fe 96 //Start camera trigger
omatthews 98:6c7c7d36a5fe 97 triggered_flag = true;
omatthews 98:6c7c7d36a5fe 98 }
omatthews 98:6c7c7d36a5fe 99 if (!untriggered_flag && curr_time > time_points[i_set - 1] + trig_time[i_set] + camera_read_time){
omatthews 98:6c7c7d36a5fe 100 //End camera read
omatthews 98:6c7c7d36a5fe 101 untriggered_flag = true;
omatthews 98:6c7c7d36a5fe 102 }
omatthews 98:6c7c7d36a5fe 103 wait_ms(1);
omatthews 98:6c7c7d36a5fe 104 }
omatthews 98:6c7c7d36a5fe 105
omatthews 98:6c7c7d36a5fe 106 }
omatthews 98:6c7c7d36a5fe 107 }
mbed_official 82:abf1b1785bd7 108
omatthews 98:6c7c7d36a5fe 109 void pressure_control() {
omatthews 98:6c7c7d36a5fe 110 //Input pressure control function here
omatthews 98:6c7c7d36a5fe 111 //i.e.
omatthews 98:6c7c7d36a5fe 112 //read_pressure();
omatthews 98:6c7c7d36a5fe 113 //if (pressure < lower_bound) {
omatthews 98:6c7c7d36a5fe 114 //pump_turn_on();
omatthews 98:6c7c7d36a5fe 115 //}
omatthews 98:6c7c7d36a5fe 116 //else if (pressure > upper_bound) {
omatthews 98:6c7c7d36a5fe 117 //pump_turn_off();
omatthews 98:6c7c7d36a5fe 118 //}
omatthews 98:6c7c7d36a5fe 119 }
omatthews 98:6c7c7d36a5fe 120
omatthews 98:6c7c7d36a5fe 121
omatthews 98:6c7c7d36a5fe 122
omatthews 98:6c7c7d36a5fe 123
omatthews 98:6c7c7d36a5fe 124 int main() {
omatthews 98:6c7c7d36a5fe 125 pc.baud(115200);
omatthews 98:6c7c7d36a5fe 126
omatthews 98:6c7c7d36a5fe 127 serial_listen.start(&serial_rx);
omatthews 98:6c7c7d36a5fe 128 heater_control.start(& temp_control);
omatthews 98:6c7c7d36a5fe 129 pressure_tick.attach(& pressure_control, 1);
omatthews 98:6c7c7d36a5fe 130 heat_tick.attach_us(& temp_trigger,3000);
omatthews 98:6c7c7d36a5fe 131
omatthews 98:6c7c7d36a5fe 132 while (!start_flag){
omatthews 98:6c7c7d36a5fe 133 pc.printf("Waiting for start signal\n");
omatthews 98:6c7c7d36a5fe 134 wait(1);
mbed_official 88:bea4f2daa48c 135 }
omatthews 98:6c7c7d36a5fe 136
omatthews 98:6c7c7d36a5fe 137 pc.printf("Starting routine...\n");
omatthews 98:6c7c7d36a5fe 138 set_point_routine();
omatthews 98:6c7c7d36a5fe 139
omatthews 98:6c7c7d36a5fe 140
omatthews 98:6c7c7d36a5fe 141 }