LEX_Threaded_Programming
Dependencies: Heater_V2 MODSERIAL Nanopb FastPWM ADS8568_ADC
main.cpp@36:fef05d541679, 2020-03-30 (annotated)
- Committer:
- justinbuckland
- Date:
- Mon Mar 30 16:56:05 2020 +0000
- Revision:
- 36:fef05d541679
- Parent:
- 35:aa35c6325dac
Removed old code no longer used
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 | 31:09d2f3e4ed99 | 14 | #define PRESSURE_SMOOTH_FACTOR 0.9 |
justinbuckland | 17:d86e749ae9be | 15 | |
justinbuckland | 17:d86e749ae9be | 16 | //UID lookup address and pointer |
justinbuckland | 17:d86e749ae9be | 17 | #define UID_ADDR 0x1FFF7A10 |
justinbuckland | 17:d86e749ae9be | 18 | unsigned long *uid = (unsigned long *) UID_ADDR; |
justinbuckland | 17:d86e749ae9be | 19 | |
justinbuckland | 17:d86e749ae9be | 20 | //UID and drive board calibration table |
justinbuckland | 17:d86e749ae9be | 21 | #define UID_TABLE_LENGTH 4 |
justinbuckland | 17:d86e749ae9be | 22 | |
justinbuckland | 17:d86e749ae9be | 23 | int drive_board_serial_number[UID_TABLE_LENGTH] = |
justinbuckland | 17:d86e749ae9be | 24 | {1, |
justinbuckland | 17:d86e749ae9be | 25 | 2, |
justinbuckland | 17:d86e749ae9be | 26 | 3, |
justinbuckland | 17:d86e749ae9be | 27 | 4}; |
justinbuckland | 17:d86e749ae9be | 28 | |
justinbuckland | 17:d86e749ae9be | 29 | unsigned long drive_board_uid[UID_TABLE_LENGTH][3] = |
justinbuckland | 17:d86e749ae9be | 30 | {{0x005B0060, 0x32375101, 0x32363531}, |
justinbuckland | 28:b2513f36a50d | 31 | {0x00530028, 0x32375116, 0x30333732}, // updated board 2 UID {0x0051003D, 0x32375114, 0x30333732}, |
justinbuckland | 17:d86e749ae9be | 32 | {0x00520060, 0x32375101, 0x32363531}, |
justinbuckland | 17:d86e749ae9be | 33 | {0x00570060, 0x32375101, 0x32363531}}; |
justinbuckland | 17:d86e749ae9be | 34 | |
justinbuckland | 17:d86e749ae9be | 35 | float drive_board_cal[UID_TABLE_LENGTH][2][2] = |
paullj | 35:aa35c6325dac | 36 | {{{0.098846, 10.190057}, {0.060713, 10.208784}}, |
paullj | 35:aa35c6325dac | 37 | {{0.060987, 10.154840}, {0.033058, 10.239591}}, |
paullj | 35:aa35c6325dac | 38 | {{0.014312, 10.405662}, {0.000000, 1.0000000}}, |
paullj | 35:aa35c6325dac | 39 | {{0.055152, 10.062970}, {0.114355, 10.123188}}}; |
justinbuckland | 17:d86e749ae9be | 40 | |
omatthews | 0:54bedd3964e2 | 41 | Heater * heater; |
intrinseca | 4:63d7f2a0dec6 | 42 | float r_gradient; //setpoint setting |
omatthews | 5:702b32ead94e | 43 | |
justinbuckland | 8:58c6d51957df | 44 | MODSERIAL pc(PA_9, PA_10, BUFFER_SIZE); //mcu TX, RX, BUFFER_SIZE byte TX and RX buffers |
omatthews | 0:54bedd3964e2 | 45 | ADS8568_ADC adc(PB_15, PB_14, PB_13, PB_12, PC_15, PC_0, PC_1, PC_2, PC_3); |
omatthews | 0:54bedd3964e2 | 46 | I2C i2c(PB_7, PB_8); //SDA, SCL |
omatthews | 0:54bedd3964e2 | 47 | Timer timer; |
omatthews | 0:54bedd3964e2 | 48 | DigitalIn adc_busy(PA_8); //Busy interrupt sig# |
omatthews | 5:702b32ead94e | 49 | |
justinbuckland | 17:d86e749ae9be | 50 | //Fluidic Control |
justinbuckland | 31:09d2f3e4ed99 | 51 | AnalogIn pressure_0(PA_0); |
justinbuckland | 30:b28088f244c2 | 52 | AnalogIn pressure_1(PA_1); |
justinbuckland | 30:b28088f244c2 | 53 | DigitalOut pump(PA_4); |
justinbuckland | 31:09d2f3e4ed99 | 54 | DigitalOut valve_0(PA_5); |
justinbuckland | 31:09d2f3e4ed99 | 55 | DigitalOut valve_1(PA_6); |
justinbuckland | 13:b2e00297b465 | 56 | |
justinbuckland | 32:c825071486eb | 57 | float pressure_in = 0.1; // 0.1 - 0.9 corresponds to 0 - 15psi |
justinbuckland | 32:c825071486eb | 58 | float pressure_out = 0.1; |
omatthews | 0:54bedd3964e2 | 59 | |
omatthews | 0:54bedd3964e2 | 60 | //Heater Control |
justinbuckland | 18:376350fb7ef9 | 61 | FastPWM drive_1(PC_9); |
justinbuckland | 18:376350fb7ef9 | 62 | FastPWM drive_2(PC_8); |
justinbuckland | 18:376350fb7ef9 | 63 | FastPWM guard_1(PC_7); |
justinbuckland | 18:376350fb7ef9 | 64 | FastPWM guard_2(PC_6); |
justinbuckland | 7:a4fc853feb30 | 65 | |
justinbuckland | 18:376350fb7ef9 | 66 | //ADC channels for heater current and voltage measurements |
justinbuckland | 18:376350fb7ef9 | 67 | int i_port_1 = 0; |
justinbuckland | 18:376350fb7ef9 | 68 | int i_port_2 = 2; |
justinbuckland | 18:376350fb7ef9 | 69 | int v_port_1 = 1; |
justinbuckland | 18:376350fb7ef9 | 70 | int v_port_2 = 3; |
omatthews | 0:54bedd3964e2 | 71 | |
justinbuckland | 18:376350fb7ef9 | 72 | //Heater ID: heater 1 is nearer liquid sense location, heater 2 is other location |
justinbuckland | 18:376350fb7ef9 | 73 | int heater_ID; |
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 | 19:8154688252fd | 95 | Thread pressure_thread(osPriorityNormal); |
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 | //Flags |
omatthews | 5:702b32ead94e | 103 | EventFlags flags; //Flags: |
omatthews | 5:702b32ead94e | 104 | // 0 => update heater |
omatthews | 5:702b32ead94e | 105 | // 1 => log state |
justinbuckland | 13:b2e00297b465 | 106 | // 2 => read pressure |
omatthews | 5:702b32ead94e | 107 | bool triggered_flag; |
omatthews | 0:54bedd3964e2 | 108 | bool status = true; |
omatthews | 0:54bedd3964e2 | 109 | |
omatthews | 0:54bedd3964e2 | 110 | //Configuration data |
omatthews | 0:54bedd3964e2 | 111 | memspcr_ExperimentConfiguration exp_config = memspcr_ExperimentConfiguration_init_zero; |
omatthews | 0:54bedd3964e2 | 112 | int buffer_length; |
omatthews | 0:54bedd3964e2 | 113 | size_t message_length; |
justinbuckland | 8:58c6d51957df | 114 | uint8_t buffer[BUFFER_SIZE]; |
omatthews | 0:54bedd3964e2 | 115 | |
omatthews | 0:54bedd3964e2 | 116 | |
omatthews | 0:54bedd3964e2 | 117 | //Functions for reading and decoding the message__________________________________________________ |
omatthews | 0:54bedd3964e2 | 118 | |
omatthews | 0:54bedd3964e2 | 119 | void read_message() |
omatthews | 0:54bedd3964e2 | 120 | { |
justinbuckland | 7:a4fc853feb30 | 121 | if (pc.scanf("%d",&message_length) < 0){pc.printf("# Error reading message length");} |
omatthews | 5:702b32ead94e | 122 | size_t buffer_length = sizeof(buffer); |
omatthews | 5:702b32ead94e | 123 | if (message_length > buffer_length) |
omatthews | 5:702b32ead94e | 124 | { |
justinbuckland | 7:a4fc853feb30 | 125 | pc.printf("# Message length exceeds buffer. \n Input configuration file\n"); |
omatthews | 5:702b32ead94e | 126 | read_message(); |
omatthews | 5:702b32ead94e | 127 | return; |
omatthews | 5:702b32ead94e | 128 | } |
justinbuckland | 7:a4fc853feb30 | 129 | pc.printf("# Message is %d chars long, buffer length is %d\n",message_length,buffer_length); |
omatthews | 5:702b32ead94e | 130 | unsigned int c; |
omatthews | 5:702b32ead94e | 131 | for (int i = 0; i < message_length; i++) |
omatthews | 0:54bedd3964e2 | 132 | { |
omatthews | 0:54bedd3964e2 | 133 | pc.scanf("%02X",&c); |
omatthews | 0:54bedd3964e2 | 134 | buffer[i] = (char) c; |
omatthews | 0:54bedd3964e2 | 135 | } |
omatthews | 0:54bedd3964e2 | 136 | } |
omatthews | 0:54bedd3964e2 | 137 | |
omatthews | 0:54bedd3964e2 | 138 | void decode_message() |
omatthews | 0:54bedd3964e2 | 139 | { |
omatthews | 5:702b32ead94e | 140 | // Create a stream that reads from the buffer. |
omatthews | 0:54bedd3964e2 | 141 | pb_istream_t istream = pb_istream_from_buffer(buffer, message_length); |
omatthews | 0:54bedd3964e2 | 142 | |
omatthews | 5:702b32ead94e | 143 | //Now we are ready to decode the message. |
omatthews | 0:54bedd3964e2 | 144 | status = pb_decode(&istream, memspcr_ExperimentConfiguration_fields, &exp_config); |
omatthews | 0:54bedd3964e2 | 145 | |
omatthews | 5:702b32ead94e | 146 | // Check for errors... |
omatthews | 5:702b32ead94e | 147 | if (!status) { |
justinbuckland | 7:a4fc853feb30 | 148 | pc.printf("# Decoding failed: %s\n", PB_GET_ERROR(&istream)); |
omatthews | 0:54bedd3964e2 | 149 | } |
omatthews | 0:54bedd3964e2 | 150 | } |
omatthews | 0:54bedd3964e2 | 151 | |
omatthews | 0:54bedd3964e2 | 152 | bool decode_callback(pb_istream_t *stream, const pb_field_t *field, void **arg) |
omatthews | 0:54bedd3964e2 | 153 | { |
omatthews | 0:54bedd3964e2 | 154 | vector <memspcr_ThermalStep> * dest = (vector <memspcr_ThermalStep> *)(*arg); |
omatthews | 0:54bedd3964e2 | 155 | memspcr_ThermalStep result = memspcr_ThermalStep_init_zero; |
omatthews | 0:54bedd3964e2 | 156 | status = pb_decode(stream, memspcr_ThermalStep_fields, & result); |
omatthews | 5:702b32ead94e | 157 | |
omatthews | 5:702b32ead94e | 158 | if (!status) { |
justinbuckland | 7:a4fc853feb30 | 159 | pc.printf("# Decode callback failed\n"); |
omatthews | 0:54bedd3964e2 | 160 | } |
omatthews | 0:54bedd3964e2 | 161 | |
intrinseca | 4:63d7f2a0dec6 | 162 | dest->push_back(result); //CHECK: Does result get copied into the vector? |
omatthews | 0:54bedd3964e2 | 163 | return true; |
omatthews | 0:54bedd3964e2 | 164 | } |
omatthews | 0:54bedd3964e2 | 165 | |
omatthews | 0:54bedd3964e2 | 166 | |
omatthews | 0:54bedd3964e2 | 167 | //Ticking functions_________________________________________________________________ |
omatthews | 0:54bedd3964e2 | 168 | |
omatthews | 6:ef2bcc5fe3af | 169 | |
omatthews | 5:702b32ead94e | 170 | void temp_trigger() |
omatthews | 0:54bedd3964e2 | 171 | { |
omatthews | 5:702b32ead94e | 172 | //This function triggers a temperature update. |
omatthews | 5:702b32ead94e | 173 | //N.B. update cannot be called directly from a ticker as tickers and |
omatthews | 0:54bedd3964e2 | 174 | //reading the ADC both rely on interrupts. |
omatthews | 5:702b32ead94e | 175 | flags.set(0x1); |
omatthews | 0:54bedd3964e2 | 176 | } |
omatthews | 0:54bedd3964e2 | 177 | |
omatthews | 6:ef2bcc5fe3af | 178 | |
omatthews | 2:b27d8f9a6e49 | 179 | void log_trigger() |
omatthews | 2:b27d8f9a6e49 | 180 | { |
omatthews | 5:702b32ead94e | 181 | flags.set(0x2); |
omatthews | 2:b27d8f9a6e49 | 182 | } |
justinbuckland | 7:a4fc853feb30 | 183 | |
omatthews | 0:54bedd3964e2 | 184 | |
omatthews | 5:702b32ead94e | 185 | //Other functions__________________________________________________________________ |
omatthews | 0:54bedd3964e2 | 186 | |
omatthews | 0:54bedd3964e2 | 187 | |
omatthews | 0:54bedd3964e2 | 188 | void temp_control() { |
omatthews | 0:54bedd3964e2 | 189 | while(1){ |
omatthews | 6:ef2bcc5fe3af | 190 | flags.wait_any(0x1,osWaitForever,true); |
omatthews | 0:54bedd3964e2 | 191 | heater->read(); |
omatthews | 0:54bedd3964e2 | 192 | heater->update(); |
justinbuckland | 17:d86e749ae9be | 193 | wait_us(200);//Give other threads time to get selected |
omatthews | 0:54bedd3964e2 | 194 | } |
omatthews | 0:54bedd3964e2 | 195 | } |
omatthews | 5:702b32ead94e | 196 | |
omatthews | 2:b27d8f9a6e49 | 197 | void log_state() |
omatthews | 2:b27d8f9a6e49 | 198 | { |
omatthews | 2:b27d8f9a6e49 | 199 | while(1){ |
omatthews | 6:ef2bcc5fe3af | 200 | flags.wait_any(0x2,osWaitForever,true); |
justinbuckland | 32:c825071486eb | 201 | |
justinbuckland | 32:c825071486eb | 202 | //Read and control pressure |
justinbuckland | 32:c825071486eb | 203 | pressure_in = pressure_in * PRESSURE_SMOOTH_FACTOR + (1 - PRESSURE_SMOOTH_FACTOR) * pressure_1.read(); |
justinbuckland | 32:c825071486eb | 204 | pressure_out = pressure_out * PRESSURE_SMOOTH_FACTOR + (1 - PRESSURE_SMOOTH_FACTOR) * pressure_0.read(); |
justinbuckland | 32:c825071486eb | 205 | if (pressure_in < exp_config.fluidics.pressure_sensor_setpoint_adc - exp_config.fluidics.pressure_sensor_hysteresis_adc) { |
justinbuckland | 32:c825071486eb | 206 | led_1 = 1; |
justinbuckland | 32:c825071486eb | 207 | pump = 1; |
justinbuckland | 32:c825071486eb | 208 | } |
justinbuckland | 32:c825071486eb | 209 | else if (pressure_in > exp_config.fluidics.pressure_sensor_setpoint_adc) { |
justinbuckland | 32:c825071486eb | 210 | led_1 = 0; |
justinbuckland | 32:c825071486eb | 211 | pump = 0; |
justinbuckland | 32:c825071486eb | 212 | } |
justinbuckland | 32:c825071486eb | 213 | |
justinbuckland | 29:818c098c9e5a | 214 | //Output time, R_avg, R_ref, R_var, error, error_integrated, duty cycle, input pressure, output pressure |
justinbuckland | 26:e2e834e16367 | 215 | pc.printf("%10d,%10d,%10.6f,%10.6f,%10.6f,%10.6f,%10.6f,%10.6f,%10.6f,%10.6f\n", |
justinbuckland | 29:818c098c9e5a | 216 | heater_ID, timer.read_ms(), heater->Get_R_avg(), heater->Get_R_ref(), heater->Get_R_var(), heater->Get_error(), heater->Get_error_integrated(), heater->Get_D(), pressure_in, pressure_out); |
justinbuckland | 17:d86e749ae9be | 217 | wait_us(200);//Give other threads time to get selected |
justinbuckland | 19:8154688252fd | 218 | } |
omatthews | 2:b27d8f9a6e49 | 219 | } |
omatthews | 5:702b32ead94e | 220 | |
justinbuckland | 13:b2e00297b465 | 221 | |
omatthews | 5:702b32ead94e | 222 | void set_point_routine(std::vector<memspcr_ThermalStep> profile) { |
omatthews | 5:702b32ead94e | 223 | int curr_time; |
omatthews | 5:702b32ead94e | 224 | vector <memspcr_ThermalStep>::iterator it_prev, it = profile.begin(); |
omatthews | 5:702b32ead94e | 225 | if (it->elapsed_time_ms != 0) |
omatthews | 5:702b32ead94e | 226 | { |
justinbuckland | 7:a4fc853feb30 | 227 | pc.printf("# Error: the first point in the profile should be at time 0.\n"); |
omatthews | 5:702b32ead94e | 228 | return; |
omatthews | 5:702b32ead94e | 229 | } |
omatthews | 2:b27d8f9a6e49 | 230 | it++; |
omatthews | 0:54bedd3964e2 | 231 | |
omatthews | 0:54bedd3964e2 | 232 | for (it_prev = profile.begin(); it < profile.end(); it ++, it_prev++){ |
omatthews | 0:54bedd3964e2 | 233 | triggered_flag = false; |
paullj | 14:39a5eb99fbdb | 234 | r_gradient = (it->resistance_set_point - it_prev->resistance_set_point)/(it->elapsed_time_ms - it_prev->elapsed_time_ms); |
omatthews | 0:54bedd3964e2 | 235 | while ((curr_time = timer.read_ms()) <= it->elapsed_time_ms){ |
paullj | 14:39a5eb99fbdb | 236 | heater->Set_ref(it_prev->resistance_set_point + r_gradient * (curr_time - it_prev->elapsed_time_ms)); |
justinbuckland | 8:58c6d51957df | 237 | |
justinbuckland | 9:9474da78cec3 | 238 | if (!triggered_flag && (it->camera_offset_ms != 0) && (curr_time > it_prev->elapsed_time_ms + it->camera_offset_ms)) |
omatthews | 0:54bedd3964e2 | 239 | { |
justinbuckland | 9:9474da78cec3 | 240 | //Start camera exposure and turn on LED if camera_offset_ms is non-zero |
justinbuckland | 7:a4fc853feb30 | 241 | camTrigger = 0; |
justinbuckland | 7:a4fc853feb30 | 242 | wait_us(10); |
justinbuckland | 7:a4fc853feb30 | 243 | camTrigger = 1; |
justinbuckland | 7:a4fc853feb30 | 244 | led_0 = 1; |
justinbuckland | 12:cadcea541fbf | 245 | ledDrive = 1; |
omatthews | 0:54bedd3964e2 | 246 | triggered_flag = true; |
omatthews | 0:54bedd3964e2 | 247 | } |
justinbuckland | 8:58c6d51957df | 248 | wait_us(200); |
omatthews | 0:54bedd3964e2 | 249 | } |
justinbuckland | 8:58c6d51957df | 250 | //Stop camera exposure and turn off LED at end of time segment |
justinbuckland | 8:58c6d51957df | 251 | camTrigger = 0; |
justinbuckland | 8:58c6d51957df | 252 | led_0 = 0; |
justinbuckland | 12:cadcea541fbf | 253 | ledDrive = 0; |
omatthews | 0:54bedd3964e2 | 254 | } |
omatthews | 0:54bedd3964e2 | 255 | } |
omatthews | 0:54bedd3964e2 | 256 | |
omatthews | 0:54bedd3964e2 | 257 | |
omatthews | 5:702b32ead94e | 258 | int main() |
omatthews | 5:702b32ead94e | 259 | { |
justinbuckland | 18:376350fb7ef9 | 260 | int i_board = -1; |
justinbuckland | 18:376350fb7ef9 | 261 | int i_heater; |
justinbuckland | 18:376350fb7ef9 | 262 | float drive_cal_a, drive_cal_b; |
justinbuckland | 36:fef05d541679 | 263 | |
omatthews | 0:54bedd3964e2 | 264 | pc.baud(115200); |
omatthews | 0:54bedd3964e2 | 265 | adc.init(); |
justinbuckland | 17:d86e749ae9be | 266 | |
justinbuckland | 17:d86e749ae9be | 267 | pc.printf("\r\nUnique ID: %08X %08X %08X \r\n", uid[0], uid[1], uid[2]); |
justinbuckland | 17:d86e749ae9be | 268 | for (int i = 0; i < UID_TABLE_LENGTH; i++) |
justinbuckland | 17:d86e749ae9be | 269 | { |
justinbuckland | 17:d86e749ae9be | 270 | 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 | 271 | { |
justinbuckland | 17:d86e749ae9be | 272 | i_board = i; |
justinbuckland | 17:d86e749ae9be | 273 | i = UID_TABLE_LENGTH; |
justinbuckland | 17:d86e749ae9be | 274 | } |
justinbuckland | 17:d86e749ae9be | 275 | } |
justinbuckland | 17:d86e749ae9be | 276 | |
omatthews | 0:54bedd3964e2 | 277 | buffer_length = sizeof(buffer)/sizeof(uint8_t); |
paullj | 22:f65353f6e935 | 278 | pc.printf("# Input [CONFIGURATION] file\n"); |
justinbuckland | 9:9474da78cec3 | 279 | |
intrinseca | 4:63d7f2a0dec6 | 280 | //set up nanopb |
omatthews | 5:702b32ead94e | 281 | std::vector<memspcr_ThermalStep> profile; |
omatthews | 5:702b32ead94e | 282 | exp_config.profile.funcs.decode = decode_callback; |
omatthews | 5:702b32ead94e | 283 | exp_config.profile.arg = &profile; |
omatthews | 5:702b32ead94e | 284 | |
intrinseca | 4:63d7f2a0dec6 | 285 | //read and decode configuration |
justinbuckland | 18:376350fb7ef9 | 286 | read_message(); |
justinbuckland | 7:a4fc853feb30 | 287 | pc.printf("# Message read\n"); |
omatthews | 0:54bedd3964e2 | 288 | decode_message(); |
justinbuckland | 7:a4fc853feb30 | 289 | pc.printf("# Message decoded\n"); |
omatthews | 5:702b32ead94e | 290 | |
justinbuckland | 18:376350fb7ef9 | 291 | //Select heater |
justinbuckland | 18:376350fb7ef9 | 292 | if (exp_config.selected_heater == memspcr_ExperimentConfiguration_Heater_HEATER_1) |
justinbuckland | 18:376350fb7ef9 | 293 | i_heater = 0; |
justinbuckland | 18:376350fb7ef9 | 294 | else if (exp_config.selected_heater == memspcr_ExperimentConfiguration_Heater_HEATER_2) |
justinbuckland | 18:376350fb7ef9 | 295 | i_heater = 1; |
justinbuckland | 18:376350fb7ef9 | 296 | else { |
justinbuckland | 8:58c6d51957df | 297 | pc.printf("# Error - no heater has been selected\n"); |
omatthews | 5:702b32ead94e | 298 | return 1; |
omatthews | 5:702b32ead94e | 299 | } |
justinbuckland | 18:376350fb7ef9 | 300 | heater_ID = i_heater + 1; |
justinbuckland | 18:376350fb7ef9 | 301 | |
justinbuckland | 18:376350fb7ef9 | 302 | //Set drive ADC->Resistance calibration coefficients (default: no change if board not found) |
justinbuckland | 18:376350fb7ef9 | 303 | drive_cal_a = drive_board_cal[i_board][i_heater][0]; |
justinbuckland | 18:376350fb7ef9 | 304 | drive_cal_b = drive_board_cal[i_board][i_heater][1]; |
justinbuckland | 18:376350fb7ef9 | 305 | |
justinbuckland | 18:376350fb7ef9 | 306 | pc.printf("# Heater: %d\n", heater_ID); |
justinbuckland | 20:ccbe2a03893d | 307 | pc.printf("# Drive board calibration: %10.6f, %10.6f\n", drive_cal_a, drive_cal_b); |
omatthews | 5:702b32ead94e | 308 | |
justinbuckland | 18:376350fb7ef9 | 309 | //Define heaters |
justinbuckland | 18:376350fb7ef9 | 310 | Heater * heater_1 = new Heater(i_port_1, v_port_1, drive_cal_a, drive_cal_b, & drive_1, & guard_1, & adc, adc_busy, exp_config.thermal); |
justinbuckland | 18:376350fb7ef9 | 311 | Heater * heater_2 = new Heater(i_port_2, v_port_2, drive_cal_a, drive_cal_b, & drive_2, & guard_2, & adc, adc_busy, exp_config.thermal); |
justinbuckland | 18:376350fb7ef9 | 312 | if (i_heater == 0) |
justinbuckland | 18:376350fb7ef9 | 313 | heater = heater_1; |
justinbuckland | 18:376350fb7ef9 | 314 | else |
justinbuckland | 18:376350fb7ef9 | 315 | heater = heater_2; |
justinbuckland | 18:376350fb7ef9 | 316 | |
justinbuckland | 20:ccbe2a03893d | 317 | //Start pressure control |
justinbuckland | 20:ccbe2a03893d | 318 | pc.printf("# Pressure setpoint: %10.6f hystersess: %10.6f\n",exp_config.fluidics.pressure_sensor_setpoint_adc, exp_config.fluidics.pressure_sensor_hysteresis_adc); |
paullj | 22:f65353f6e935 | 319 | pc.printf("# Waiting for signal to begin [PRESSURE] control (type p or press button 0)\n"); |
justinbuckland | 18:376350fb7ef9 | 320 | while (pc.getcNb()!='p' && !user_0); |
justinbuckland | 18:376350fb7ef9 | 321 | pc.printf("# Pressure control start signal received\n"); |
justinbuckland | 13:b2e00297b465 | 322 | |
justinbuckland | 36:fef05d541679 | 323 | //Start logging (pressure control takes place within logging thread) |
omatthews | 2:b27d8f9a6e49 | 324 | logging_thread.start(& log_state); |
omatthews | 6:ef2bcc5fe3af | 325 | log_tick.attach_us(& log_trigger,exp_config.logging_interval_ms * 1000); |
omatthews | 0:54bedd3964e2 | 326 | |
paullj | 22:f65353f6e935 | 327 | //Start temperature control |
paullj | 22:f65353f6e935 | 328 | pc.printf("# Waiting for signal to begin [THERMAL] control (type s or press button 0)\n"); |
paullj | 22:f65353f6e935 | 329 | while (pc.getcNb()!='s' && !user_0); |
paullj | 22:f65353f6e935 | 330 | pc.printf("# Thermal control start signal received\n"); |
paullj | 22:f65353f6e935 | 331 | |
paullj | 22:f65353f6e935 | 332 | heater->Set_ref(0.0); |
paullj | 22:f65353f6e935 | 333 | heater_control.start(& temp_control); |
paullj | 22:f65353f6e935 | 334 | heat_tick.attach_us(& temp_trigger,exp_config.thermal.thermal_control_loop_interval_ms * 1000); |
paullj | 22:f65353f6e935 | 335 | |
justinbuckland | 27:0ee855da5ba9 | 336 | pc.printf("# Starting routine on drive board: %d\n",drive_board_serial_number[i_board]); |
justinbuckland | 32:c825071486eb | 337 | pc.printf("heater id, time (ms), R_avg (Ohm), R_set (Ohm), R_var (Ohm), Err (Ohm), Err_int (Ohm.ms), Duty cycle, P0 (ADC), P1 (ADC)\n"); |
paullj | 23:d00849a9aa23 | 338 | timer.start(); |
omatthews | 5:702b32ead94e | 339 | set_point_routine(profile); |
omatthews | 6:ef2bcc5fe3af | 340 | |
omatthews | 6:ef2bcc5fe3af | 341 | //Turn off |
omatthews | 5:702b32ead94e | 342 | heat_tick.detach(); |
omatthews | 5:702b32ead94e | 343 | log_tick.detach(); |
omatthews | 5:702b32ead94e | 344 | wait(1); |
omatthews | 2:b27d8f9a6e49 | 345 | heater->turn_off(); |
justinbuckland | 18:376350fb7ef9 | 346 | |
justinbuckland | 7:a4fc853feb30 | 347 | pc.printf("# Finished\n"); |
justinbuckland | 7:a4fc853feb30 | 348 | |
omatthews | 5:702b32ead94e | 349 | return 0; |
omatthews | 5:702b32ead94e | 350 | } |