LEX_Threaded_Programming

Dependencies:   Heater_V2 MODSERIAL Nanopb FastPWM ADS8568_ADC

Committer:
justinbuckland
Date:
Wed Sep 18 16:44:04 2019 +0000
Revision:
13:b2e00297b465
Parent:
12:cadcea541fbf
Child:
14:39a5eb99fbdb
Added pressure control - need to change PA ports in future

Who changed what in which revision?

UserRevisionLine numberNew contents of line
omatthews 0:54bedd3964e2 1 #include "mbed.h"
omatthews 0:54bedd3964e2 2 #include "pb.h"
omatthews 0:54bedd3964e2 3 #include "pb_decode.h"
omatthews 0:54bedd3964e2 4 #include "pb_encode.h"
omatthews 0:54bedd3964e2 5 #include "MODSERIAL.h"
omatthews 0:54bedd3964e2 6 #include "ADS8568_ADC.h"
omatthews 0:54bedd3964e2 7 #include "Heater.h"
omatthews 0:54bedd3964e2 8 #include "FastPWM.h"
omatthews 0:54bedd3964e2 9 #include "memspcr.pb.h"
omatthews 0:54bedd3964e2 10 #include <vector>
omatthews 0:54bedd3964e2 11 #include <iterator>
omatthews 0:54bedd3964e2 12
justinbuckland 8:58c6d51957df 13 #define BUFFER_SIZE 4096
justinbuckland 12:cadcea541fbf 14 #define LED_PULSE_PERIOD 1000 // ticks, 84kHz when CPU clock is 84MHz
justinbuckland 12:cadcea541fbf 15 #define LED_PULSE_WIDTH 100 // ticks, 10% duty cycle
justinbuckland 8:58c6d51957df 16
omatthews 0:54bedd3964e2 17 Heater * heater;
intrinseca 4:63d7f2a0dec6 18 float r_gradient; //setpoint setting
omatthews 5:702b32ead94e 19
omatthews 0:54bedd3964e2 20
justinbuckland 8:58c6d51957df 21 MODSERIAL pc(PA_9, PA_10, BUFFER_SIZE); //mcu TX, RX, BUFFER_SIZE byte TX and RX buffers
omatthews 0:54bedd3964e2 22 ADS8568_ADC adc(PB_15, PB_14, PB_13, PB_12, PC_15, PC_0, PC_1, PC_2, PC_3);
omatthews 0:54bedd3964e2 23 I2C i2c(PB_7, PB_8); //SDA, SCL
omatthews 0:54bedd3964e2 24 Timer timer;
omatthews 0:54bedd3964e2 25 DigitalIn adc_busy(PA_8); //Busy interrupt sig#
omatthews 5:702b32ead94e 26
justinbuckland 10:f8202e71e765 27 //Pressure Control
justinbuckland 13:b2e00297b465 28 DigitalOut pump(PA_2);
justinbuckland 13:b2e00297b465 29 AnalogIn pressure_1(PA_5);
justinbuckland 13:b2e00297b465 30 //set up pressure read averaging
justinbuckland 13:b2e00297b465 31
justinbuckland 13:b2e00297b465 32 float pressure_fAvg = 0.1; // rolling average
justinbuckland 13:b2e00297b465 33
justinbuckland 11:7394f281e845 34 float pressure_in = 0.0;
justinbuckland 11:7394f281e845 35 float pressure_out = 0.0;
justinbuckland 10:f8202e71e765 36
justinbuckland 13:b2e00297b465 37 float pressure_set = 0.46;
justinbuckland 13:b2e00297b465 38 float pressure_hys = 0.02;
justinbuckland 11:7394f281e845 39 float pressure_set_low = pressure_set - pressure_hys/2;
justinbuckland 11:7394f281e845 40 float pressure_set_high = pressure_set + pressure_hys/2;
omatthews 0:54bedd3964e2 41
omatthews 0:54bedd3964e2 42 //Heater Control
omatthews 5:702b32ead94e 43 FastPWM drive_main(PC_9);
omatthews 5:702b32ead94e 44 FastPWM drive_lysis(PC_8);
omatthews 5:702b32ead94e 45 FastPWM guard_main(PC_7);
omatthews 5:702b32ead94e 46 FastPWM guard_lysis(PC_6);
justinbuckland 7:a4fc853feb30 47
justinbuckland 7:a4fc853feb30 48 int heater_ID_main = 1;
justinbuckland 7:a4fc853feb30 49 int heater_ID_lysis = 2;
omatthews 5:702b32ead94e 50 int i_port_main = 0;
omatthews 5:702b32ead94e 51 int i_port_lysis = 2;
omatthews 5:702b32ead94e 52 int v_port_main = 1;
omatthews 5:702b32ead94e 53 int v_port_lysis = 3;
omatthews 0:54bedd3964e2 54
justinbuckland 7:a4fc853feb30 55 int heater_ID = 0;
omatthews 0:54bedd3964e2 56
justinbuckland 9:9474da78cec3 57 //Illumination LED Control
justinbuckland 9:9474da78cec3 58
justinbuckland 7:a4fc853feb30 59 //Indicator LEDs
justinbuckland 13:b2e00297b465 60 DigitalOut hb_led(PC_13); //Red
justinbuckland 13:b2e00297b465 61 DigitalOut led_0(PC_4); //Green
justinbuckland 13:b2e00297b465 62 DigitalOut led_1(PC_5); //Red
omatthews 0:54bedd3964e2 63
justinbuckland 7:a4fc853feb30 64 //Camera and LED drive
justinbuckland 7:a4fc853feb30 65 DigitalOut camTrigger(PB_2); //Trigger camera
justinbuckland 12:cadcea541fbf 66 DigitalOut ledDrive(PB_4); //Drive LED for fluorescence detection
justinbuckland 7:a4fc853feb30 67
omatthews 0:54bedd3964e2 68 //User buttons
omatthews 0:54bedd3964e2 69 DigitalIn user_0(PB_0);
omatthews 0:54bedd3964e2 70 DigitalIn user_1(PB_1);
omatthews 0:54bedd3964e2 71
omatthews 0:54bedd3964e2 72 BusOut converts(PC_0, PC_1, PC_2, PC_3);
omatthews 0:54bedd3964e2 73
omatthews 0:54bedd3964e2 74 //Threads
omatthews 6:ef2bcc5fe3af 75 Thread heater_control(osPriorityHigh);
omatthews 6:ef2bcc5fe3af 76 Thread logging_thread(osPriorityAboveNormal);
justinbuckland 13:b2e00297b465 77 Thread pressure_thread(osPriorityAboveNormal);
omatthews 0:54bedd3964e2 78
omatthews 0:54bedd3964e2 79 //Tickers
omatthews 0:54bedd3964e2 80 Ticker heat_tick;
omatthews 0:54bedd3964e2 81 Ticker pressure_tick;
omatthews 0:54bedd3964e2 82 Ticker log_tick;
omatthews 0:54bedd3964e2 83
omatthews 0:54bedd3964e2 84
omatthews 0:54bedd3964e2 85 //Flags
omatthews 5:702b32ead94e 86 EventFlags flags; //Flags:
omatthews 5:702b32ead94e 87 // 0 => update heater
omatthews 5:702b32ead94e 88 // 1 => log state
justinbuckland 13:b2e00297b465 89 // 2 => read pressure
omatthews 5:702b32ead94e 90 bool triggered_flag;
omatthews 0:54bedd3964e2 91 bool status = true;
omatthews 0:54bedd3964e2 92
omatthews 0:54bedd3964e2 93 //Configuration data
omatthews 0:54bedd3964e2 94 memspcr_ExperimentConfiguration exp_config = memspcr_ExperimentConfiguration_init_zero;
omatthews 0:54bedd3964e2 95 int buffer_length;
omatthews 0:54bedd3964e2 96 size_t message_length;
justinbuckland 8:58c6d51957df 97 uint8_t buffer[BUFFER_SIZE];
omatthews 0:54bedd3964e2 98
omatthews 0:54bedd3964e2 99
omatthews 0:54bedd3964e2 100 //Functions for reading and decoding the message__________________________________________________
omatthews 0:54bedd3964e2 101
omatthews 0:54bedd3964e2 102 void read_message()
omatthews 0:54bedd3964e2 103 {
justinbuckland 7:a4fc853feb30 104 if (pc.scanf("%d",&message_length) < 0){pc.printf("# Error reading message length");}
omatthews 5:702b32ead94e 105 size_t buffer_length = sizeof(buffer);
omatthews 5:702b32ead94e 106 if (message_length > buffer_length)
omatthews 5:702b32ead94e 107 {
justinbuckland 7:a4fc853feb30 108 pc.printf("# Message length exceeds buffer. \n Input configuration file\n");
omatthews 5:702b32ead94e 109 read_message();
omatthews 5:702b32ead94e 110 return;
omatthews 5:702b32ead94e 111 }
justinbuckland 7:a4fc853feb30 112 pc.printf("# Message is %d chars long, buffer length is %d\n",message_length,buffer_length);
omatthews 5:702b32ead94e 113 unsigned int c;
omatthews 5:702b32ead94e 114 for (int i = 0; i < message_length; i++)
omatthews 0:54bedd3964e2 115 {
omatthews 0:54bedd3964e2 116 pc.scanf("%02X",&c);
omatthews 0:54bedd3964e2 117 buffer[i] = (char) c;
omatthews 0:54bedd3964e2 118 }
omatthews 0:54bedd3964e2 119 }
omatthews 0:54bedd3964e2 120
omatthews 0:54bedd3964e2 121 void decode_message()
omatthews 0:54bedd3964e2 122 {
omatthews 5:702b32ead94e 123 // Create a stream that reads from the buffer.
omatthews 5:702b32ead94e 124
omatthews 0:54bedd3964e2 125 pb_istream_t istream = pb_istream_from_buffer(buffer, message_length);
omatthews 0:54bedd3964e2 126
omatthews 5:702b32ead94e 127 //Now we are ready to decode the message.
omatthews 0:54bedd3964e2 128 status = pb_decode(&istream, memspcr_ExperimentConfiguration_fields, &exp_config);
omatthews 0:54bedd3964e2 129
omatthews 5:702b32ead94e 130 // Check for errors...
omatthews 5:702b32ead94e 131 if (!status) {
justinbuckland 7:a4fc853feb30 132 pc.printf("# Decoding failed: %s\n", PB_GET_ERROR(&istream));
omatthews 0:54bedd3964e2 133 }
omatthews 0:54bedd3964e2 134 }
omatthews 0:54bedd3964e2 135
omatthews 0:54bedd3964e2 136 bool decode_callback(pb_istream_t *stream, const pb_field_t *field, void **arg)
omatthews 0:54bedd3964e2 137 {
omatthews 0:54bedd3964e2 138 vector <memspcr_ThermalStep> * dest = (vector <memspcr_ThermalStep> *)(*arg);
omatthews 0:54bedd3964e2 139 memspcr_ThermalStep result = memspcr_ThermalStep_init_zero;
omatthews 0:54bedd3964e2 140 status = pb_decode(stream, memspcr_ThermalStep_fields, & result);
omatthews 5:702b32ead94e 141
omatthews 5:702b32ead94e 142 if (!status) {
justinbuckland 7:a4fc853feb30 143 pc.printf("# Decode callback failed\n");
omatthews 0:54bedd3964e2 144 }
omatthews 0:54bedd3964e2 145
intrinseca 4:63d7f2a0dec6 146 dest->push_back(result); //CHECK: Does result get copied into the vector?
omatthews 0:54bedd3964e2 147 return true;
omatthews 0:54bedd3964e2 148 }
omatthews 0:54bedd3964e2 149
omatthews 0:54bedd3964e2 150
omatthews 0:54bedd3964e2 151 //Ticking functions_________________________________________________________________
omatthews 0:54bedd3964e2 152
omatthews 6:ef2bcc5fe3af 153
omatthews 5:702b32ead94e 154 void temp_trigger()
omatthews 0:54bedd3964e2 155 {
omatthews 5:702b32ead94e 156 //This function triggers a temperature update.
omatthews 5:702b32ead94e 157 //N.B. update cannot be called directly from a ticker as tickers and
omatthews 0:54bedd3964e2 158 //reading the ADC both rely on interrupts.
omatthews 5:702b32ead94e 159 flags.set(0x1);
omatthews 0:54bedd3964e2 160 }
omatthews 0:54bedd3964e2 161
omatthews 6:ef2bcc5fe3af 162
omatthews 2:b27d8f9a6e49 163 void log_trigger()
omatthews 2:b27d8f9a6e49 164 {
omatthews 5:702b32ead94e 165 flags.set(0x2);
omatthews 2:b27d8f9a6e49 166 }
justinbuckland 7:a4fc853feb30 167
justinbuckland 13:b2e00297b465 168 void pressure_trigger()
omatthews 0:54bedd3964e2 169 {
justinbuckland 13:b2e00297b465 170 flags.set(0x3);
justinbuckland 10:f8202e71e765 171 }
omatthews 0:54bedd3964e2 172
omatthews 0:54bedd3964e2 173
omatthews 5:702b32ead94e 174 //Other functions__________________________________________________________________
omatthews 0:54bedd3964e2 175
omatthews 0:54bedd3964e2 176
omatthews 0:54bedd3964e2 177 void temp_control() {
omatthews 0:54bedd3964e2 178 while(1){
omatthews 6:ef2bcc5fe3af 179 flags.wait_any(0x1,osWaitForever,true);
omatthews 0:54bedd3964e2 180 heater->read();
omatthews 0:54bedd3964e2 181 heater->update();
omatthews 0:54bedd3964e2 182 }
omatthews 0:54bedd3964e2 183 }
omatthews 5:702b32ead94e 184
omatthews 2:b27d8f9a6e49 185 void log_state()
omatthews 2:b27d8f9a6e49 186 {
omatthews 2:b27d8f9a6e49 187 while(1){
omatthews 6:ef2bcc5fe3af 188 flags.wait_any(0x2,osWaitForever,true);
omatthews 6:ef2bcc5fe3af 189 //Output time, R_ref, R, error, error_integrated
justinbuckland 11:7394f281e845 190 pc.printf("%10d,%10d,%10.6f,%10.6f,%10.6f,%10.6f\n", heater_ID, timer.read_ms(), heater->Get_R(), heater->Get_R_ref(), pressure_in, pressure_out);
omatthews 2:b27d8f9a6e49 191 }
omatthews 2:b27d8f9a6e49 192 }
omatthews 5:702b32ead94e 193
justinbuckland 13:b2e00297b465 194 void pressure_control() {
justinbuckland 13:b2e00297b465 195 while(1){
justinbuckland 13:b2e00297b465 196 flags.wait_any(0x3,osWaitForever,true);
justinbuckland 13:b2e00297b465 197 pressure_in = pressure_in*(1.0-pressure_fAvg) + pressure_1.read()*pressure_fAvg;
justinbuckland 13:b2e00297b465 198 //pressure_in = 0;
justinbuckland 13:b2e00297b465 199 //for (int i = 0; i < pressure_nAvg; i++) pressure_in += pressure_1.read(); //read pressure
justinbuckland 13:b2e00297b465 200 //pressure_in = pressure_in / pressure_nAvg;
justinbuckland 13:b2e00297b465 201 // pc.printf("%10.6f %10.6f %10.6f\n", pressure_set_low, pressure_set_high, pressure_in);
justinbuckland 13:b2e00297b465 202 if (pressure_in < pressure_set_low) {
justinbuckland 13:b2e00297b465 203 led_1 = 1;
justinbuckland 13:b2e00297b465 204 pump = 1;
justinbuckland 13:b2e00297b465 205 // pc.printf(" pump on\n");
justinbuckland 13:b2e00297b465 206 }
justinbuckland 13:b2e00297b465 207 else if (pressure_in > pressure_set_high) {
justinbuckland 13:b2e00297b465 208 led_1 = 0;
justinbuckland 13:b2e00297b465 209 pump = 0;
justinbuckland 13:b2e00297b465 210 // pc.printf(" pump off\n");
justinbuckland 13:b2e00297b465 211 }
justinbuckland 13:b2e00297b465 212 }
justinbuckland 13:b2e00297b465 213 }
justinbuckland 13:b2e00297b465 214
justinbuckland 13:b2e00297b465 215
omatthews 5:702b32ead94e 216 void set_point_routine(std::vector<memspcr_ThermalStep> profile) {
omatthews 5:702b32ead94e 217 int curr_time;
omatthews 5:702b32ead94e 218 vector <memspcr_ThermalStep>::iterator it_prev, it = profile.begin();
omatthews 5:702b32ead94e 219 if (it->elapsed_time_ms != 0)
omatthews 5:702b32ead94e 220 {
justinbuckland 7:a4fc853feb30 221 pc.printf("# Error: the first point in the profile should be at time 0.\n");
omatthews 5:702b32ead94e 222 return;
omatthews 5:702b32ead94e 223 }
omatthews 2:b27d8f9a6e49 224 it++;
omatthews 0:54bedd3964e2 225
omatthews 0:54bedd3964e2 226 for (it_prev = profile.begin(); it < profile.end(); it ++, it_prev++){
omatthews 0:54bedd3964e2 227 triggered_flag = false;
omatthews 0:54bedd3964e2 228 r_gradient = (it->resistance - it_prev->resistance)/(it->elapsed_time_ms - it_prev->elapsed_time_ms);
omatthews 0:54bedd3964e2 229 while ((curr_time = timer.read_ms()) <= it->elapsed_time_ms){
omatthews 5:702b32ead94e 230 heater->Set_ref(it_prev->resistance + r_gradient * (curr_time - it_prev->elapsed_time_ms));
justinbuckland 8:58c6d51957df 231
justinbuckland 9:9474da78cec3 232 if (!triggered_flag && (it->camera_offset_ms != 0) && (curr_time > it_prev->elapsed_time_ms + it->camera_offset_ms))
omatthews 0:54bedd3964e2 233 {
justinbuckland 9:9474da78cec3 234 //Start camera exposure and turn on LED if camera_offset_ms is non-zero
justinbuckland 7:a4fc853feb30 235 camTrigger = 0;
justinbuckland 7:a4fc853feb30 236 wait_us(10);
justinbuckland 7:a4fc853feb30 237 camTrigger = 1;
justinbuckland 7:a4fc853feb30 238 led_0 = 1;
justinbuckland 12:cadcea541fbf 239 ledDrive = 1;
omatthews 0:54bedd3964e2 240 triggered_flag = true;
omatthews 0:54bedd3964e2 241 }
justinbuckland 8:58c6d51957df 242 wait_us(200);
omatthews 0:54bedd3964e2 243 }
justinbuckland 8:58c6d51957df 244 //Stop camera exposure and turn off LED at end of time segment
justinbuckland 8:58c6d51957df 245 camTrigger = 0;
justinbuckland 8:58c6d51957df 246 led_0 = 0;
justinbuckland 12:cadcea541fbf 247 ledDrive = 0;
omatthews 0:54bedd3964e2 248 }
omatthews 0:54bedd3964e2 249 }
omatthews 0:54bedd3964e2 250
omatthews 0:54bedd3964e2 251
omatthews 5:702b32ead94e 252 int main()
omatthews 5:702b32ead94e 253 {
omatthews 0:54bedd3964e2 254 pc.baud(115200);
omatthews 0:54bedd3964e2 255 adc.init();
omatthews 5:702b32ead94e 256
omatthews 0:54bedd3964e2 257 buffer_length = sizeof(buffer)/sizeof(uint8_t);
justinbuckland 7:a4fc853feb30 258 pc.printf("# Input configuration file\n");
justinbuckland 9:9474da78cec3 259
intrinseca 4:63d7f2a0dec6 260 //set up nanopb
omatthews 5:702b32ead94e 261 std::vector<memspcr_ThermalStep> profile;
omatthews 5:702b32ead94e 262 exp_config.profile.funcs.decode = decode_callback;
omatthews 5:702b32ead94e 263 exp_config.profile.arg = &profile;
omatthews 5:702b32ead94e 264
intrinseca 4:63d7f2a0dec6 265 //read and decode configuration
intrinseca 4:63d7f2a0dec6 266 read_message();
justinbuckland 7:a4fc853feb30 267 pc.printf("# Message read\n");
omatthews 0:54bedd3964e2 268 decode_message();
justinbuckland 7:a4fc853feb30 269 pc.printf("# Message decoded\n");
omatthews 5:702b32ead94e 270
omatthews 6:ef2bcc5fe3af 271 Heater * heater_main = new Heater(i_port_main, v_port_main, & drive_main, & guard_main, & adc, adc_busy, exp_config.thermal);
omatthews 6:ef2bcc5fe3af 272 Heater * heater_lysis = new Heater(i_port_lysis, v_port_lysis, & drive_lysis, & guard_lysis, & adc, adc_busy, exp_config.thermal);
omatthews 5:702b32ead94e 273
omatthews 5:702b32ead94e 274 //Select heater. Put control times in us for ticker functions
omatthews 5:702b32ead94e 275 if (exp_config.selected_heater == memspcr_ExperimentConfiguration_Heater_MAIN) {
omatthews 5:702b32ead94e 276 heater = heater_main;
justinbuckland 7:a4fc853feb30 277 heater_ID = heater_ID_main;
omatthews 5:702b32ead94e 278 } else if (exp_config.selected_heater == memspcr_ExperimentConfiguration_Heater_LYSIS) {
omatthews 5:702b32ead94e 279 heater = heater_lysis;
justinbuckland 7:a4fc853feb30 280 heater_ID = heater_ID_lysis;
omatthews 5:702b32ead94e 281 } else {
justinbuckland 8:58c6d51957df 282 pc.printf("# Error - no heater has been selected\n");
omatthews 5:702b32ead94e 283 return 1;
omatthews 5:702b32ead94e 284 }
omatthews 5:702b32ead94e 285
justinbuckland 13:b2e00297b465 286 pc.printf("# Starting pressure control\n");
justinbuckland 13:b2e00297b465 287 pressure_thread.start(& pressure_control);
justinbuckland 13:b2e00297b465 288 pressure_tick.attach_us(& pressure_trigger, 500000);
justinbuckland 13:b2e00297b465 289
justinbuckland 7:a4fc853feb30 290 pc.printf("# Waiting for start signal to begin temperature control (type in an s or press button 0)\n");
omatthews 6:ef2bcc5fe3af 291 heater->Set_ref(0.0);
omatthews 6:ef2bcc5fe3af 292 heater_control.start(& temp_control);
omatthews 6:ef2bcc5fe3af 293 heat_tick.attach_us(& temp_trigger,exp_config.thermal.control_loop_interval_ms * 1000);
omatthews 6:ef2bcc5fe3af 294
omatthews 2:b27d8f9a6e49 295 while (pc.getcNb()!='s' && !user_0);
omatthews 5:702b32ead94e 296
justinbuckland 8:58c6d51957df 297 pc.printf("# Start signal received\n");
omatthews 0:54bedd3964e2 298
omatthews 2:b27d8f9a6e49 299 logging_thread.start(& log_state);
omatthews 6:ef2bcc5fe3af 300 log_tick.attach_us(& log_trigger,exp_config.logging_interval_ms * 1000);
omatthews 0:54bedd3964e2 301
justinbuckland 13:b2e00297b465 302 pc.printf("# Starting routine\n");
justinbuckland 11:7394f281e845 303 pc.printf("# heater, time(ms), r, rSet, pIn, pOut\n");
omatthews 2:b27d8f9a6e49 304 timer.start();
omatthews 5:702b32ead94e 305 set_point_routine(profile);
omatthews 6:ef2bcc5fe3af 306
omatthews 6:ef2bcc5fe3af 307 //Turn off
omatthews 5:702b32ead94e 308 heat_tick.detach();
omatthews 5:702b32ead94e 309 log_tick.detach();
omatthews 5:702b32ead94e 310 wait(1);
omatthews 2:b27d8f9a6e49 311 heater->turn_off();
justinbuckland 8:58c6d51957df 312
justinbuckland 8:58c6d51957df 313
justinbuckland 7:a4fc853feb30 314 pc.printf("# Finished\n");
justinbuckland 7:a4fc853feb30 315
omatthews 5:702b32ead94e 316 return 0;
omatthews 5:702b32ead94e 317 }