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