LEX_Threaded_Programming

Dependencies:   Heater_V2 MODSERIAL Nanopb FastPWM ADS8568_ADC

Committer:
justinbuckland
Date:
Tue Sep 17 16:57:36 2019 +0000
Revision:
11:7394f281e845
Parent:
10:f8202e71e765
Child:
12:cadcea541fbf
report pIn, pOut (note: AnalogIn not fixed)

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