![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
LEX_Threaded_Programming
Dependencies: Heater_V2 MODSERIAL Nanopb FastPWM ADS8568_ADC
main.cpp@21:1f233639f0ce, 2019-09-23 (annotated)
- Committer:
- paullj
- Date:
- Mon Sep 23 12:39:34 2019 +0000
- Revision:
- 21:1f233639f0ce
- Parent:
- 17:d86e749ae9be
Changed the input messages to work with gui controller;
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 | |
paullj | 14:39a5eb99fbdb | 13 | #define BUFFER_SIZE 8192 |
justinbuckland | 17:d86e749ae9be | 14 | |
justinbuckland | 17:d86e749ae9be | 15 | //UID lookup address and pointer |
justinbuckland | 17:d86e749ae9be | 16 | #define UID_ADDR 0x1FFF7A10 |
justinbuckland | 17:d86e749ae9be | 17 | unsigned long *uid = (unsigned long *) UID_ADDR; |
justinbuckland | 17:d86e749ae9be | 18 | |
justinbuckland | 17:d86e749ae9be | 19 | //UID and drive board calibration table |
justinbuckland | 17:d86e749ae9be | 20 | #define UID_TABLE_LENGTH 4 |
justinbuckland | 17:d86e749ae9be | 21 | |
justinbuckland | 17:d86e749ae9be | 22 | int drive_board_serial_number[UID_TABLE_LENGTH] = |
justinbuckland | 17:d86e749ae9be | 23 | {1, |
justinbuckland | 17:d86e749ae9be | 24 | 2, |
justinbuckland | 17:d86e749ae9be | 25 | 3, |
justinbuckland | 17:d86e749ae9be | 26 | 4}; |
justinbuckland | 17:d86e749ae9be | 27 | |
justinbuckland | 17:d86e749ae9be | 28 | unsigned long drive_board_uid[UID_TABLE_LENGTH][3] = |
justinbuckland | 17:d86e749ae9be | 29 | {{0x005B0060, 0x32375101, 0x32363531}, |
justinbuckland | 17:d86e749ae9be | 30 | {0x0051003D, 0x32375114, 0x30333732}, |
justinbuckland | 17:d86e749ae9be | 31 | {0x00520060, 0x32375101, 0x32363531}, |
justinbuckland | 17:d86e749ae9be | 32 | {0x00570060, 0x32375101, 0x32363531}}; |
justinbuckland | 17:d86e749ae9be | 33 | |
justinbuckland | 17:d86e749ae9be | 34 | float drive_board_cal[UID_TABLE_LENGTH][2][2] = |
justinbuckland | 17:d86e749ae9be | 35 | {{{0.096724353, 10.1817431}, {0.056098807, 10.19962849}}, |
justinbuckland | 17:d86e749ae9be | 36 | {{0.059473025, 10.14814327}, {0.03200058, 10.25073923}}, |
justinbuckland | 17:d86e749ae9be | 37 | {{0.01887149, 10.39360225}, {0.03115874, 10.28199855}}, |
justinbuckland | 17:d86e749ae9be | 38 | {{0.052545339, 10.06008621}, {0.094239471, 10.11983777}}}; |
justinbuckland | 17:d86e749ae9be | 39 | |
justinbuckland | 17:d86e749ae9be | 40 | float drive_cal[2] = {0.0, 1.0}; |
justinbuckland | 8:58c6d51957df | 41 | |
omatthews | 0:54bedd3964e2 | 42 | Heater * heater; |
intrinseca | 4:63d7f2a0dec6 | 43 | float r_gradient; //setpoint setting |
omatthews | 5:702b32ead94e | 44 | |
justinbuckland | 8:58c6d51957df | 45 | MODSERIAL pc(PA_9, PA_10, BUFFER_SIZE); //mcu TX, RX, BUFFER_SIZE byte TX and RX buffers |
omatthews | 0:54bedd3964e2 | 46 | ADS8568_ADC adc(PB_15, PB_14, PB_13, PB_12, PC_15, PC_0, PC_1, PC_2, PC_3); |
omatthews | 0:54bedd3964e2 | 47 | I2C i2c(PB_7, PB_8); //SDA, SCL |
omatthews | 0:54bedd3964e2 | 48 | Timer timer; |
omatthews | 0:54bedd3964e2 | 49 | DigitalIn adc_busy(PA_8); //Busy interrupt sig# |
omatthews | 5:702b32ead94e | 50 | |
justinbuckland | 17:d86e749ae9be | 51 | //Fluidic Control |
justinbuckland | 13:b2e00297b465 | 52 | DigitalOut pump(PA_2); |
justinbuckland | 13:b2e00297b465 | 53 | AnalogIn pressure_1(PA_5); |
justinbuckland | 13:b2e00297b465 | 54 | |
justinbuckland | 17:d86e749ae9be | 55 | float pressure_in; |
justinbuckland | 17:d86e749ae9be | 56 | float pressure_out; |
justinbuckland | 17:d86e749ae9be | 57 | float pressure_set_low; |
justinbuckland | 17:d86e749ae9be | 58 | float pressure_set_high; |
omatthews | 0:54bedd3964e2 | 59 | |
omatthews | 0:54bedd3964e2 | 60 | //Heater Control |
omatthews | 5:702b32ead94e | 61 | FastPWM drive_main(PC_9); |
omatthews | 5:702b32ead94e | 62 | FastPWM drive_lysis(PC_8); |
omatthews | 5:702b32ead94e | 63 | FastPWM guard_main(PC_7); |
omatthews | 5:702b32ead94e | 64 | FastPWM guard_lysis(PC_6); |
justinbuckland | 7:a4fc853feb30 | 65 | |
justinbuckland | 7:a4fc853feb30 | 66 | int heater_ID_main = 1; |
justinbuckland | 7:a4fc853feb30 | 67 | int heater_ID_lysis = 2; |
omatthews | 5:702b32ead94e | 68 | int i_port_main = 0; |
omatthews | 5:702b32ead94e | 69 | int i_port_lysis = 2; |
omatthews | 5:702b32ead94e | 70 | int v_port_main = 1; |
omatthews | 5:702b32ead94e | 71 | int v_port_lysis = 3; |
omatthews | 0:54bedd3964e2 | 72 | |
justinbuckland | 7:a4fc853feb30 | 73 | int heater_ID = 0; |
omatthews | 0:54bedd3964e2 | 74 | |
justinbuckland | 9:9474da78cec3 | 75 | //Illumination LED Control |
justinbuckland | 9:9474da78cec3 | 76 | |
justinbuckland | 7:a4fc853feb30 | 77 | //Indicator LEDs |
justinbuckland | 13:b2e00297b465 | 78 | DigitalOut hb_led(PC_13); //Red |
justinbuckland | 13:b2e00297b465 | 79 | DigitalOut led_0(PC_4); //Green |
justinbuckland | 13:b2e00297b465 | 80 | DigitalOut led_1(PC_5); //Red |
omatthews | 0:54bedd3964e2 | 81 | |
justinbuckland | 7:a4fc853feb30 | 82 | //Camera and LED drive |
justinbuckland | 7:a4fc853feb30 | 83 | DigitalOut camTrigger(PB_2); //Trigger camera |
justinbuckland | 12:cadcea541fbf | 84 | DigitalOut ledDrive(PB_4); //Drive LED for fluorescence detection |
justinbuckland | 7:a4fc853feb30 | 85 | |
omatthews | 0:54bedd3964e2 | 86 | //User buttons |
omatthews | 0:54bedd3964e2 | 87 | DigitalIn user_0(PB_0); |
omatthews | 0:54bedd3964e2 | 88 | DigitalIn user_1(PB_1); |
omatthews | 0:54bedd3964e2 | 89 | |
omatthews | 0:54bedd3964e2 | 90 | BusOut converts(PC_0, PC_1, PC_2, PC_3); |
omatthews | 0:54bedd3964e2 | 91 | |
omatthews | 0:54bedd3964e2 | 92 | //Threads |
omatthews | 6:ef2bcc5fe3af | 93 | Thread heater_control(osPriorityHigh); |
omatthews | 6:ef2bcc5fe3af | 94 | Thread logging_thread(osPriorityAboveNormal); |
justinbuckland | 13:b2e00297b465 | 95 | Thread pressure_thread(osPriorityAboveNormal); |
omatthews | 0:54bedd3964e2 | 96 | |
omatthews | 0:54bedd3964e2 | 97 | //Tickers |
omatthews | 0:54bedd3964e2 | 98 | Ticker heat_tick; |
omatthews | 0:54bedd3964e2 | 99 | Ticker pressure_tick; |
omatthews | 0:54bedd3964e2 | 100 | Ticker log_tick; |
omatthews | 0:54bedd3964e2 | 101 | |
omatthews | 0:54bedd3964e2 | 102 | |
omatthews | 0:54bedd3964e2 | 103 | //Flags |
omatthews | 5:702b32ead94e | 104 | EventFlags flags; //Flags: |
omatthews | 5:702b32ead94e | 105 | // 0 => update heater |
omatthews | 5:702b32ead94e | 106 | // 1 => log state |
justinbuckland | 13:b2e00297b465 | 107 | // 2 => read pressure |
omatthews | 5:702b32ead94e | 108 | bool triggered_flag; |
omatthews | 0:54bedd3964e2 | 109 | bool status = true; |
omatthews | 0:54bedd3964e2 | 110 | |
omatthews | 0:54bedd3964e2 | 111 | //Configuration data |
omatthews | 0:54bedd3964e2 | 112 | memspcr_ExperimentConfiguration exp_config = memspcr_ExperimentConfiguration_init_zero; |
omatthews | 0:54bedd3964e2 | 113 | int buffer_length; |
omatthews | 0:54bedd3964e2 | 114 | size_t message_length; |
justinbuckland | 8:58c6d51957df | 115 | uint8_t buffer[BUFFER_SIZE]; |
omatthews | 0:54bedd3964e2 | 116 | |
omatthews | 0:54bedd3964e2 | 117 | |
omatthews | 0:54bedd3964e2 | 118 | //Functions for reading and decoding the message__________________________________________________ |
omatthews | 0:54bedd3964e2 | 119 | |
omatthews | 0:54bedd3964e2 | 120 | void read_message() |
omatthews | 0:54bedd3964e2 | 121 | { |
paullj | 21:1f233639f0ce | 122 | if (pc.scanf("%d",&message_length) < 0){pc.printf("[ERROR] Error reading message length");} |
omatthews | 5:702b32ead94e | 123 | size_t buffer_length = sizeof(buffer); |
omatthews | 5:702b32ead94e | 124 | if (message_length > buffer_length) |
omatthews | 5:702b32ead94e | 125 | { |
paullj | 21:1f233639f0ce | 126 | pc.printf("[ERROR] Message length exceeds buffer. \n Input configuration file\n"); |
paullj | 21:1f233639f0ce | 127 | pc.printf("[CONFIG] Input configuration file\n"); |
omatthews | 5:702b32ead94e | 128 | read_message(); |
omatthews | 5:702b32ead94e | 129 | return; |
omatthews | 5:702b32ead94e | 130 | } |
paullj | 21:1f233639f0ce | 131 | pc.printf("Message is %d chars long, buffer length is %d\n",message_length,buffer_length); |
omatthews | 5:702b32ead94e | 132 | unsigned int c; |
omatthews | 5:702b32ead94e | 133 | for (int i = 0; i < message_length; i++) |
omatthews | 0:54bedd3964e2 | 134 | { |
omatthews | 0:54bedd3964e2 | 135 | pc.scanf("%02X",&c); |
omatthews | 0:54bedd3964e2 | 136 | buffer[i] = (char) c; |
omatthews | 0:54bedd3964e2 | 137 | } |
omatthews | 0:54bedd3964e2 | 138 | } |
omatthews | 0:54bedd3964e2 | 139 | |
omatthews | 0:54bedd3964e2 | 140 | void decode_message() |
omatthews | 0:54bedd3964e2 | 141 | { |
omatthews | 5:702b32ead94e | 142 | // Create a stream that reads from the buffer. |
omatthews | 0:54bedd3964e2 | 143 | pb_istream_t istream = pb_istream_from_buffer(buffer, message_length); |
omatthews | 0:54bedd3964e2 | 144 | |
omatthews | 5:702b32ead94e | 145 | //Now we are ready to decode the message. |
omatthews | 0:54bedd3964e2 | 146 | status = pb_decode(&istream, memspcr_ExperimentConfiguration_fields, &exp_config); |
omatthews | 0:54bedd3964e2 | 147 | |
omatthews | 5:702b32ead94e | 148 | // Check for errors... |
omatthews | 5:702b32ead94e | 149 | if (!status) { |
paullj | 21:1f233639f0ce | 150 | pc.printf("[ERROR] Decoding failed: %s\n", PB_GET_ERROR(&istream)); |
omatthews | 0:54bedd3964e2 | 151 | } |
omatthews | 0:54bedd3964e2 | 152 | } |
omatthews | 0:54bedd3964e2 | 153 | |
omatthews | 0:54bedd3964e2 | 154 | bool decode_callback(pb_istream_t *stream, const pb_field_t *field, void **arg) |
omatthews | 0:54bedd3964e2 | 155 | { |
omatthews | 0:54bedd3964e2 | 156 | vector <memspcr_ThermalStep> * dest = (vector <memspcr_ThermalStep> *)(*arg); |
omatthews | 0:54bedd3964e2 | 157 | memspcr_ThermalStep result = memspcr_ThermalStep_init_zero; |
omatthews | 0:54bedd3964e2 | 158 | status = pb_decode(stream, memspcr_ThermalStep_fields, & result); |
omatthews | 5:702b32ead94e | 159 | |
omatthews | 5:702b32ead94e | 160 | if (!status) { |
paullj | 21:1f233639f0ce | 161 | pc.printf("[ERROR] Decode callback failed\n"); |
omatthews | 0:54bedd3964e2 | 162 | } |
omatthews | 0:54bedd3964e2 | 163 | |
intrinseca | 4:63d7f2a0dec6 | 164 | dest->push_back(result); //CHECK: Does result get copied into the vector? |
omatthews | 0:54bedd3964e2 | 165 | return true; |
omatthews | 0:54bedd3964e2 | 166 | } |
omatthews | 0:54bedd3964e2 | 167 | |
omatthews | 0:54bedd3964e2 | 168 | |
omatthews | 0:54bedd3964e2 | 169 | //Ticking functions_________________________________________________________________ |
omatthews | 0:54bedd3964e2 | 170 | |
omatthews | 6:ef2bcc5fe3af | 171 | |
omatthews | 5:702b32ead94e | 172 | void temp_trigger() |
omatthews | 0:54bedd3964e2 | 173 | { |
omatthews | 5:702b32ead94e | 174 | //This function triggers a temperature update. |
omatthews | 5:702b32ead94e | 175 | //N.B. update cannot be called directly from a ticker as tickers and |
omatthews | 0:54bedd3964e2 | 176 | //reading the ADC both rely on interrupts. |
omatthews | 5:702b32ead94e | 177 | flags.set(0x1); |
omatthews | 0:54bedd3964e2 | 178 | } |
omatthews | 0:54bedd3964e2 | 179 | |
omatthews | 6:ef2bcc5fe3af | 180 | |
omatthews | 2:b27d8f9a6e49 | 181 | void log_trigger() |
omatthews | 2:b27d8f9a6e49 | 182 | { |
omatthews | 5:702b32ead94e | 183 | flags.set(0x2); |
omatthews | 2:b27d8f9a6e49 | 184 | } |
justinbuckland | 7:a4fc853feb30 | 185 | |
justinbuckland | 13:b2e00297b465 | 186 | void pressure_trigger() |
omatthews | 0:54bedd3964e2 | 187 | { |
justinbuckland | 13:b2e00297b465 | 188 | flags.set(0x3); |
justinbuckland | 10:f8202e71e765 | 189 | } |
omatthews | 0:54bedd3964e2 | 190 | |
omatthews | 0:54bedd3964e2 | 191 | |
omatthews | 5:702b32ead94e | 192 | //Other functions__________________________________________________________________ |
omatthews | 0:54bedd3964e2 | 193 | |
omatthews | 0:54bedd3964e2 | 194 | |
omatthews | 0:54bedd3964e2 | 195 | void temp_control() { |
omatthews | 0:54bedd3964e2 | 196 | while(1){ |
omatthews | 6:ef2bcc5fe3af | 197 | flags.wait_any(0x1,osWaitForever,true); |
omatthews | 0:54bedd3964e2 | 198 | heater->read(); |
omatthews | 0:54bedd3964e2 | 199 | heater->update(); |
justinbuckland | 17:d86e749ae9be | 200 | wait_us(200);//Give other threads time to get selected |
omatthews | 0:54bedd3964e2 | 201 | } |
omatthews | 0:54bedd3964e2 | 202 | } |
omatthews | 5:702b32ead94e | 203 | |
omatthews | 2:b27d8f9a6e49 | 204 | void log_state() |
omatthews | 2:b27d8f9a6e49 | 205 | { |
omatthews | 2:b27d8f9a6e49 | 206 | while(1){ |
omatthews | 6:ef2bcc5fe3af | 207 | flags.wait_any(0x2,osWaitForever,true); |
omatthews | 6:ef2bcc5fe3af | 208 | //Output time, R_ref, R, error, error_integrated |
justinbuckland | 11:7394f281e845 | 209 | 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); |
justinbuckland | 17:d86e749ae9be | 210 | wait_us(200);//Give other threads time to get selected |
omatthews | 2:b27d8f9a6e49 | 211 | } |
omatthews | 2:b27d8f9a6e49 | 212 | } |
omatthews | 5:702b32ead94e | 213 | |
justinbuckland | 13:b2e00297b465 | 214 | void pressure_control() { |
justinbuckland | 13:b2e00297b465 | 215 | while(1){ |
justinbuckland | 13:b2e00297b465 | 216 | flags.wait_any(0x3,osWaitForever,true); |
justinbuckland | 17:d86e749ae9be | 217 | pressure_in = pressure_1.read(); |
justinbuckland | 13:b2e00297b465 | 218 | if (pressure_in < pressure_set_low) { |
justinbuckland | 13:b2e00297b465 | 219 | led_1 = 1; |
justinbuckland | 13:b2e00297b465 | 220 | pump = 1; |
justinbuckland | 13:b2e00297b465 | 221 | } |
justinbuckland | 13:b2e00297b465 | 222 | else if (pressure_in > pressure_set_high) { |
justinbuckland | 13:b2e00297b465 | 223 | led_1 = 0; |
justinbuckland | 13:b2e00297b465 | 224 | pump = 0; |
justinbuckland | 13:b2e00297b465 | 225 | } |
justinbuckland | 17:d86e749ae9be | 226 | wait_us(200);//Give other threads time to get selected |
justinbuckland | 13:b2e00297b465 | 227 | } |
justinbuckland | 13:b2e00297b465 | 228 | } |
justinbuckland | 13:b2e00297b465 | 229 | |
justinbuckland | 13:b2e00297b465 | 230 | |
omatthews | 5:702b32ead94e | 231 | void set_point_routine(std::vector<memspcr_ThermalStep> profile) { |
omatthews | 5:702b32ead94e | 232 | int curr_time; |
omatthews | 5:702b32ead94e | 233 | vector <memspcr_ThermalStep>::iterator it_prev, it = profile.begin(); |
omatthews | 5:702b32ead94e | 234 | if (it->elapsed_time_ms != 0) |
omatthews | 5:702b32ead94e | 235 | { |
paullj | 21:1f233639f0ce | 236 | pc.printf("[ERROR] The first point in the profile should be at time 0.\n"); |
omatthews | 5:702b32ead94e | 237 | return; |
omatthews | 5:702b32ead94e | 238 | } |
omatthews | 2:b27d8f9a6e49 | 239 | it++; |
omatthews | 0:54bedd3964e2 | 240 | |
omatthews | 0:54bedd3964e2 | 241 | for (it_prev = profile.begin(); it < profile.end(); it ++, it_prev++){ |
omatthews | 0:54bedd3964e2 | 242 | triggered_flag = false; |
paullj | 14:39a5eb99fbdb | 243 | r_gradient = (it->resistance_set_point - it_prev->resistance_set_point)/(it->elapsed_time_ms - it_prev->elapsed_time_ms); |
omatthews | 0:54bedd3964e2 | 244 | while ((curr_time = timer.read_ms()) <= it->elapsed_time_ms){ |
paullj | 14:39a5eb99fbdb | 245 | heater->Set_ref(it_prev->resistance_set_point + r_gradient * (curr_time - it_prev->elapsed_time_ms)); |
justinbuckland | 8:58c6d51957df | 246 | |
justinbuckland | 9:9474da78cec3 | 247 | if (!triggered_flag && (it->camera_offset_ms != 0) && (curr_time > it_prev->elapsed_time_ms + it->camera_offset_ms)) |
omatthews | 0:54bedd3964e2 | 248 | { |
justinbuckland | 9:9474da78cec3 | 249 | //Start camera exposure and turn on LED if camera_offset_ms is non-zero |
justinbuckland | 7:a4fc853feb30 | 250 | camTrigger = 0; |
justinbuckland | 7:a4fc853feb30 | 251 | wait_us(10); |
justinbuckland | 7:a4fc853feb30 | 252 | camTrigger = 1; |
justinbuckland | 7:a4fc853feb30 | 253 | led_0 = 1; |
justinbuckland | 12:cadcea541fbf | 254 | ledDrive = 1; |
omatthews | 0:54bedd3964e2 | 255 | triggered_flag = true; |
omatthews | 0:54bedd3964e2 | 256 | } |
justinbuckland | 8:58c6d51957df | 257 | wait_us(200); |
omatthews | 0:54bedd3964e2 | 258 | } |
justinbuckland | 8:58c6d51957df | 259 | //Stop camera exposure and turn off LED at end of time segment |
justinbuckland | 8:58c6d51957df | 260 | camTrigger = 0; |
justinbuckland | 8:58c6d51957df | 261 | led_0 = 0; |
justinbuckland | 12:cadcea541fbf | 262 | ledDrive = 0; |
omatthews | 0:54bedd3964e2 | 263 | } |
omatthews | 0:54bedd3964e2 | 264 | } |
omatthews | 0:54bedd3964e2 | 265 | |
omatthews | 0:54bedd3964e2 | 266 | |
omatthews | 5:702b32ead94e | 267 | int main() |
omatthews | 5:702b32ead94e | 268 | { |
omatthews | 0:54bedd3964e2 | 269 | pc.baud(115200); |
omatthews | 0:54bedd3964e2 | 270 | adc.init(); |
justinbuckland | 17:d86e749ae9be | 271 | |
justinbuckland | 17:d86e749ae9be | 272 | pc.printf("\r\nUnique ID: %08X %08X %08X \r\n", uid[0], uid[1], uid[2]); |
justinbuckland | 17:d86e749ae9be | 273 | int i_board = -1; |
justinbuckland | 17:d86e749ae9be | 274 | for (int i = 0; i < UID_TABLE_LENGTH; i++) |
justinbuckland | 17:d86e749ae9be | 275 | { |
justinbuckland | 17:d86e749ae9be | 276 | if (uid[0]==drive_board_uid[i][0] && uid[1]==drive_board_uid[i][1] && uid[2]==drive_board_uid[i][2]) |
justinbuckland | 17:d86e749ae9be | 277 | { |
justinbuckland | 17:d86e749ae9be | 278 | i_board = i; |
justinbuckland | 17:d86e749ae9be | 279 | i = UID_TABLE_LENGTH; |
justinbuckland | 17:d86e749ae9be | 280 | } |
justinbuckland | 17:d86e749ae9be | 281 | } |
justinbuckland | 17:d86e749ae9be | 282 | |
justinbuckland | 17:d86e749ae9be | 283 | if (i_board != -1) pc.printf("Drive board: Board %d\n",drive_board_serial_number[i_board]); |
paullj | 21:1f233639f0ce | 284 | else pc.printf("[ERROR] Drive board UID match not found\n"); |
omatthews | 5:702b32ead94e | 285 | |
omatthews | 0:54bedd3964e2 | 286 | buffer_length = sizeof(buffer)/sizeof(uint8_t); |
paullj | 21:1f233639f0ce | 287 | pc.printf("[CONFIG] Input configuration file\n"); |
justinbuckland | 9:9474da78cec3 | 288 | |
intrinseca | 4:63d7f2a0dec6 | 289 | //set up nanopb |
omatthews | 5:702b32ead94e | 290 | std::vector<memspcr_ThermalStep> profile; |
omatthews | 5:702b32ead94e | 291 | exp_config.profile.funcs.decode = decode_callback; |
omatthews | 5:702b32ead94e | 292 | exp_config.profile.arg = &profile; |
omatthews | 5:702b32ead94e | 293 | |
intrinseca | 4:63d7f2a0dec6 | 294 | //read and decode configuration |
intrinseca | 4:63d7f2a0dec6 | 295 | read_message(); |
paullj | 16:32b598af6f86 | 296 | |
paullj | 21:1f233639f0ce | 297 | pc.printf("Message read\n"); |
omatthews | 0:54bedd3964e2 | 298 | decode_message(); |
paullj | 21:1f233639f0ce | 299 | pc.printf("Message decoded\n"); |
omatthews | 5:702b32ead94e | 300 | |
omatthews | 6:ef2bcc5fe3af | 301 | Heater * heater_main = new Heater(i_port_main, v_port_main, & drive_main, & guard_main, & adc, adc_busy, exp_config.thermal); |
omatthews | 6:ef2bcc5fe3af | 302 | Heater * heater_lysis = new Heater(i_port_lysis, v_port_lysis, & drive_lysis, & guard_lysis, & adc, adc_busy, exp_config.thermal); |
omatthews | 5:702b32ead94e | 303 | |
omatthews | 5:702b32ead94e | 304 | //Select heater. Put control times in us for ticker functions |
paullj | 14:39a5eb99fbdb | 305 | if (exp_config.selected_heater == memspcr_ExperimentConfiguration_Heater_HEATER_1) { |
omatthews | 5:702b32ead94e | 306 | heater = heater_main; |
justinbuckland | 7:a4fc853feb30 | 307 | heater_ID = heater_ID_main; |
paullj | 14:39a5eb99fbdb | 308 | } else if (exp_config.selected_heater == memspcr_ExperimentConfiguration_Heater_HEATER_2) { |
omatthews | 5:702b32ead94e | 309 | heater = heater_lysis; |
justinbuckland | 7:a4fc853feb30 | 310 | heater_ID = heater_ID_lysis; |
omatthews | 5:702b32ead94e | 311 | } else { |
paullj | 21:1f233639f0ce | 312 | pc.printf("[ERROR] No heater has been selected\n"); |
omatthews | 5:702b32ead94e | 313 | return 1; |
omatthews | 5:702b32ead94e | 314 | } |
omatthews | 5:702b32ead94e | 315 | |
justinbuckland | 17:d86e749ae9be | 316 | //Set drive ADC->Resistance calibration coefficients |
justinbuckland | 17:d86e749ae9be | 317 | if (i_board != -1) |
justinbuckland | 17:d86e749ae9be | 318 | for (int i=0; i<2; i++) drive_cal[i] = drive_board_cal[i_board][heater_ID-1][i]; |
justinbuckland | 17:d86e749ae9be | 319 | else { |
justinbuckland | 17:d86e749ae9be | 320 | drive_cal[0] = 0.0; |
paullj | 21:1f233639f0ce | 321 | drive_cal[1] = 10.0; |
justinbuckland | 17:d86e749ae9be | 322 | } |
justinbuckland | 17:d86e749ae9be | 323 | |
justinbuckland | 17:d86e749ae9be | 324 | pc.printf("Heater: %d drive_cal[0]: %10.6f drive_cal[1]: %10.6f\n", heater_ID, drive_cal[0], drive_cal[1]); |
justinbuckland | 17:d86e749ae9be | 325 | |
justinbuckland | 13:b2e00297b465 | 326 | pc.printf("# Starting pressure control\n"); |
justinbuckland | 13:b2e00297b465 | 327 | pressure_thread.start(& pressure_control); |
justinbuckland | 13:b2e00297b465 | 328 | pressure_tick.attach_us(& pressure_trigger, 500000); |
justinbuckland | 13:b2e00297b465 | 329 | |
paullj | 21:1f233639f0ce | 330 | pc.printf("[START] Waiting for start signal to begin temperature control (type in an s or press button 0)\n"); |
omatthews | 6:ef2bcc5fe3af | 331 | heater->Set_ref(0.0); |
omatthews | 6:ef2bcc5fe3af | 332 | heater_control.start(& temp_control); |
omatthews | 6:ef2bcc5fe3af | 333 | heat_tick.attach_us(& temp_trigger,exp_config.thermal.control_loop_interval_ms * 1000); |
omatthews | 6:ef2bcc5fe3af | 334 | |
omatthews | 2:b27d8f9a6e49 | 335 | while (pc.getcNb()!='s' && !user_0); |
omatthews | 5:702b32ead94e | 336 | |
paullj | 21:1f233639f0ce | 337 | pc.printf("Start signal received\n"); |
omatthews | 0:54bedd3964e2 | 338 | |
omatthews | 2:b27d8f9a6e49 | 339 | logging_thread.start(& log_state); |
omatthews | 6:ef2bcc5fe3af | 340 | log_tick.attach_us(& log_trigger,exp_config.logging_interval_ms * 1000); |
omatthews | 0:54bedd3964e2 | 341 | |
paullj | 21:1f233639f0ce | 342 | pc.printf("Starting routine\n"); |
paullj | 21:1f233639f0ce | 343 | pc.printf("heater, time(ms), r, rSet, pIn, pOut\n"); |
omatthews | 2:b27d8f9a6e49 | 344 | timer.start(); |
omatthews | 5:702b32ead94e | 345 | set_point_routine(profile); |
omatthews | 6:ef2bcc5fe3af | 346 | |
omatthews | 6:ef2bcc5fe3af | 347 | //Turn off |
omatthews | 5:702b32ead94e | 348 | heat_tick.detach(); |
omatthews | 5:702b32ead94e | 349 | log_tick.detach(); |
omatthews | 5:702b32ead94e | 350 | wait(1); |
omatthews | 2:b27d8f9a6e49 | 351 | heater->turn_off(); |
justinbuckland | 8:58c6d51957df | 352 | |
justinbuckland | 8:58c6d51957df | 353 | |
justinbuckland | 7:a4fc853feb30 | 354 | pc.printf("# Finished\n"); |
justinbuckland | 7:a4fc853feb30 | 355 | |
omatthews | 5:702b32ead94e | 356 | return 0; |
omatthews | 5:702b32ead94e | 357 | } |