Pilot 1 working code (excluding new patient after calculation). %d changed in %f sensordata serial log.
Dependencies: ADS1015 MPU6050 PixelArray mbed
Fork of Momo_New by
Sensorplate/main.cpp@14:3e7c3044d48b, 2017-09-28 (annotated)
- Committer:
- ricardo_95
- Date:
- Thu Sep 28 15:23:17 2017 +0000
- Revision:
- 14:3e7c3044d48b
- Parent:
- 13:b85f41d6fe6f
- Child:
- 16:adbbac0c79f9
17:23
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ricardo_95 | 12:7b3a5940f911 | 1 | /* |
ricardo_95 | 12:7b3a5940f911 | 2 | Author : Danny Eldering & Ricardo Molenaar |
ricardo_95 | 12:7b3a5940f911 | 3 | Company : Momo Medical |
ricardo_95 | 12:7b3a5940f911 | 4 | Source : developer.mbed.org |
ricardo_95 | 12:7b3a5940f911 | 5 | File : main.cpp |
ricardo_95 | 12:7b3a5940f911 | 6 | Version | -date : 1.0 | 28-9-2017 |
ricardo_95 | 12:7b3a5940f911 | 7 | */ |
ricardo_95 | 12:7b3a5940f911 | 8 | |
DEldering | 0:c0e44c46c573 | 9 | #include "mbed.h" |
DEldering | 0:c0e44c46c573 | 10 | #include "Adafruit_ADS1015.h" |
DEldering | 0:c0e44c46c573 | 11 | #include "MPU6050.h" |
ricardo_95 | 7:dba5091c8b7d | 12 | #include "neopixel.h" |
ricardo_95 | 12:7b3a5940f911 | 13 | #define NLED (3) |
ricardo_95 | 7:dba5091c8b7d | 14 | #define ONE_COLOR |
ricardo_95 | 7:dba5091c8b7d | 15 | |
ricardo_95 | 7:dba5091c8b7d | 16 | InterruptIn lock(p16); // Interrupts for buttons. |
ricardo_95 | 7:dba5091c8b7d | 17 | InterruptIn reposition(p17); |
ricardo_95 | 7:dba5091c8b7d | 18 | InterruptIn mute(p15); |
ricardo_95 | 7:dba5091c8b7d | 19 | InterruptIn new_patient(p14); |
ricardo_95 | 12:7b3a5940f911 | 20 | AnalogIn batteryvoltage(p18); // Analog input between 0 and 1 (0 and 100 %) for reading batteryvoltage from accupack. |
ricardo_95 | 12:7b3a5940f911 | 21 | DigitalIn supplyvoltage(p20); // Analog input between 0 and 1 for reading supplyvoltage from measuringpoint before power supply. |
ricardo_95 | 7:dba5091c8b7d | 22 | |
ricardo_95 | 12:7b3a5940f911 | 23 | PwmOut LED_intern1(LED1); |
ricardo_95 | 12:7b3a5940f911 | 24 | DigitalOut LED_intern2(LED2); |
ricardo_95 | 12:7b3a5940f911 | 25 | DigitalOut LED_intern3(LED3); |
ricardo_95 | 12:7b3a5940f911 | 26 | DigitalOut LED_intern4(LED4); |
ricardo_95 | 7:dba5091c8b7d | 27 | neopixel::PixelArray array(p11); |
ricardo_95 | 7:dba5091c8b7d | 28 | |
ricardo_95 | 7:dba5091c8b7d | 29 | Timer hold_timer; |
ricardo_95 | 7:dba5091c8b7d | 30 | Timer delay; |
ricardo_95 | 7:dba5091c8b7d | 31 | Timer speaker_timer; |
ricardo_95 | 7:dba5091c8b7d | 32 | |
ricardo_95 | 7:dba5091c8b7d | 33 | DigitalOut speaker1(p21); |
ricardo_95 | 7:dba5091c8b7d | 34 | DigitalOut speaker2(p22); |
DEldering | 0:c0e44c46c573 | 35 | |
DEldering | 1:a8e61f3910ad | 36 | I2C i2c(p28, p27); // I2C |
DEldering | 1:a8e61f3910ad | 37 | MPU6050 agu(p28,p27); // Accelerometer/Gyroscope Unit |
DEldering | 1:a8e61f3910ad | 38 | Adafruit_ADS1115 pr1(&i2c, 0x48); // first PiëzoResistive ADC |
DEldering | 1:a8e61f3910ad | 39 | Adafruit_ADS1115 pr2(&i2c, 0x49); // second PiëzoResistive ADC |
DEldering | 1:a8e61f3910ad | 40 | Adafruit_ADS1115 pel(&i2c, 0x4B); // PiëzoElectric ADC |
DEldering | 1:a8e61f3910ad | 41 | Serial pc(USBTX, USBRX); // tx, rx // Serial USB connection |
ricardo_95 | 12:7b3a5940f911 | 42 | Serial pi(p9, p10); // tx, rx // Setup serial communication for pi. |
DEldering | 1:a8e61f3910ad | 43 | Timer t; // Timer for equally time-spaced samples |
DEldering | 1:a8e61f3910ad | 44 | Ticker sample_cycle; // Polling cycle |
ricardo_95 | 8:bf0f7a6fb1fd | 45 | |
ricardo_95 | 12:7b3a5940f911 | 46 | int boot_delay_ms = 500; |
DEldering | 1:a8e61f3910ad | 47 | int cycle_time = 100000; // Cycle time in us |
DEldering | 1:a8e61f3910ad | 48 | int i2c_freq = 400000; // I2C Frequency |
ricardo_95 | 12:7b3a5940f911 | 49 | int baud = 115200; // Baud rate |
DEldering | 1:a8e61f3910ad | 50 | short res[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle |
DEldering | 1:a8e61f3910ad | 51 | short elec[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle |
DEldering | 1:a8e61f3910ad | 52 | int angle = 0; // Accelerometer Z-axis |
DEldering | 1:a8e61f3910ad | 53 | int k = 0; |
DEldering | 1:a8e61f3910ad | 54 | float acce[3]; // Raw accelerometer data |
DEldering | 1:a8e61f3910ad | 55 | float gyro[3]; // Raw gyroscope data |
ricardo_95 | 7:dba5091c8b7d | 56 | char LED_colour; // Variable to set LED colour. |
ricardo_95 | 12:7b3a5940f911 | 57 | bool lock_state, lock_flag, mute_state, alarm, calibration_flag, intensity_select; // Boolean variables for states logging. |
ricardo_95 | 8:bf0f7a6fb1fd | 58 | bool mute_flag, new_patient_flag, reposition_flag; |
ricardo_95 | 8:bf0f7a6fb1fd | 59 | bool speaker_state, LED_red_state, LED_yellow_state, LED_green_state, power_plug_state; |
ricardo_95 | 12:7b3a5940f911 | 60 | bool speaker_logged, LED_red_logged, LED_yellow_logged, LED_green_logged, power_plug_logged; |
ricardo_95 | 11:73c6def38fbd | 61 | int locktime_ms = 2000; // Waittime for lock user interface in ms. |
ricardo_95 | 12:7b3a5940f911 | 62 | int calibrationtime_ms = 5000; // Time to press new_patient button for calibration system. |
ricardo_95 | 12:7b3a5940f911 | 63 | int calibration_flash; // Variable for flash LED's to indicate calibration. |
ricardo_95 | 7:dba5091c8b7d | 64 | int buttondelay_ms = 750; // Button delay in ms. |
ricardo_95 | 7:dba5091c8b7d | 65 | int delay_lock_interface = 3000*60; // Delay for non using interface locktime. |
ricardo_95 | 12:7b3a5940f911 | 66 | int speaker_active_ms = 750; // Time to iterate speaker on and off when alarm occurs. |
ricardo_95 | 7:dba5091c8b7d | 67 | double alarm_voltage = 0.2; // Needed voltage for alarm expressed as a percentage (0 - 100 % => 0 - 3.3 V). |
ricardo_95 | 12:7b3a5940f911 | 68 | int red_var, green_var, blue_var, intensity; // Variables to set LED intensity. |
ricardo_95 | 8:bf0f7a6fb1fd | 69 | int batteryvoltage_current = 0, batteryvoltage_last = 0; |
ricardo_95 | 12:7b3a5940f911 | 70 | int intensity_day = 50, intensity_night = 25; // Intensity settings for LED's to wall. |
ricardo_95 | 7:dba5091c8b7d | 71 | |
ricardo_95 | 11:73c6def38fbd | 72 | void set_intensity() // Function to set the intensity for the LED's. |
ricardo_95 | 7:dba5091c8b7d | 73 | { |
ricardo_95 | 11:73c6def38fbd | 74 | if (intensity_select == 1) { |
ricardo_95 | 12:7b3a5940f911 | 75 | intensity = intensity_day; |
ricardo_95 | 12:7b3a5940f911 | 76 | } else { |
ricardo_95 | 12:7b3a5940f911 | 77 | intensity = intensity_night; |
ricardo_95 | 7:dba5091c8b7d | 78 | } |
ricardo_95 | 7:dba5091c8b7d | 79 | } |
ricardo_95 | 7:dba5091c8b7d | 80 | |
ricardo_95 | 12:7b3a5940f911 | 81 | void serial_read() // Serial read for select LED intensity and colour. |
ricardo_95 | 12:7b3a5940f911 | 82 | { |
ricardo_95 | 11:73c6def38fbd | 83 | if (pi.readable()) { |
ricardo_95 | 11:73c6def38fbd | 84 | char message[3]; |
ricardo_95 | 11:73c6def38fbd | 85 | pi.scanf("%s",message); |
ricardo_95 | 12:7b3a5940f911 | 86 | pc.printf("%s", message); |
ricardo_95 | 11:73c6def38fbd | 87 | message[strlen(message)] = '\n'; |
ricardo_95 | 12:7b3a5940f911 | 88 | |
ricardo_95 | 11:73c6def38fbd | 89 | if (message[0] == '0') { |
ricardo_95 | 11:73c6def38fbd | 90 | intensity_select = 0; |
ricardo_95 | 12:7b3a5940f911 | 91 | } |
ricardo_95 | 12:7b3a5940f911 | 92 | |
ricardo_95 | 12:7b3a5940f911 | 93 | if (message[0] == '1') { |
ricardo_95 | 12:7b3a5940f911 | 94 | intensity_select = 1; |
ricardo_95 | 12:7b3a5940f911 | 95 | } |
ricardo_95 | 12:7b3a5940f911 | 96 | |
ricardo_95 | 11:73c6def38fbd | 97 | if (message[1] == 'g') { |
ricardo_95 | 12:7b3a5940f911 | 98 | LED_colour = 'g'; |
ricardo_95 | 11:73c6def38fbd | 99 | } |
ricardo_95 | 11:73c6def38fbd | 100 | |
ricardo_95 | 11:73c6def38fbd | 101 | if (message[1] == 'y') { |
ricardo_95 | 12:7b3a5940f911 | 102 | LED_colour = 'y'; |
ricardo_95 | 11:73c6def38fbd | 103 | } |
ricardo_95 | 12:7b3a5940f911 | 104 | |
ricardo_95 | 11:73c6def38fbd | 105 | if (message[1] == 'r') { |
ricardo_95 | 12:7b3a5940f911 | 106 | LED_colour = 'r'; |
ricardo_95 | 11:73c6def38fbd | 107 | } |
ricardo_95 | 11:73c6def38fbd | 108 | } |
ricardo_95 | 11:73c6def38fbd | 109 | } |
ricardo_95 | 11:73c6def38fbd | 110 | |
ricardo_95 | 7:dba5091c8b7d | 111 | void colour_select(char LED_colour) // Function to select the colour. |
ricardo_95 | 7:dba5091c8b7d | 112 | { |
ricardo_95 | 7:dba5091c8b7d | 113 | set_intensity(); // Call function set_intensity |
ricardo_95 | 7:dba5091c8b7d | 114 | |
ricardo_95 | 7:dba5091c8b7d | 115 | if (LED_colour == 'r') { |
ricardo_95 | 7:dba5091c8b7d | 116 | red_var = (2.55*intensity); |
ricardo_95 | 7:dba5091c8b7d | 117 | green_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 118 | blue_var = 0; |
ricardo_95 | 12:7b3a5940f911 | 119 | LED_red_state = 1; |
ricardo_95 | 8:bf0f7a6fb1fd | 120 | } else { |
ricardo_95 | 12:7b3a5940f911 | 121 | LED_red_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 122 | } |
ricardo_95 | 7:dba5091c8b7d | 123 | |
ricardo_95 | 7:dba5091c8b7d | 124 | if (LED_colour == 'y') { |
ricardo_95 | 7:dba5091c8b7d | 125 | red_var = (2.55*intensity); |
ricardo_95 | 7:dba5091c8b7d | 126 | green_var = (2.55*intensity); |
ricardo_95 | 12:7b3a5940f911 | 127 | blue_var = 0; |
ricardo_95 | 8:bf0f7a6fb1fd | 128 | LED_yellow_state = 1; |
ricardo_95 | 8:bf0f7a6fb1fd | 129 | } else { |
ricardo_95 | 8:bf0f7a6fb1fd | 130 | LED_green_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 131 | } |
ricardo_95 | 7:dba5091c8b7d | 132 | |
ricardo_95 | 7:dba5091c8b7d | 133 | if (LED_colour == 'g') { |
ricardo_95 | 7:dba5091c8b7d | 134 | red_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 135 | green_var = (2.55*intensity); |
ricardo_95 | 7:dba5091c8b7d | 136 | blue_var = 0; |
ricardo_95 | 12:7b3a5940f911 | 137 | LED_green_state = 1; |
ricardo_95 | 8:bf0f7a6fb1fd | 138 | } else { |
ricardo_95 | 8:bf0f7a6fb1fd | 139 | LED_green_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 140 | } |
ricardo_95 | 7:dba5091c8b7d | 141 | |
ricardo_95 | 7:dba5091c8b7d | 142 | if (calibration_flash >= 1) { |
ricardo_95 | 7:dba5091c8b7d | 143 | if ((calibration_flash % 2) == 0) { |
ricardo_95 | 7:dba5091c8b7d | 144 | red_var = 255; |
ricardo_95 | 7:dba5091c8b7d | 145 | green_var = 255; |
ricardo_95 | 7:dba5091c8b7d | 146 | blue_var = 255; |
ricardo_95 | 7:dba5091c8b7d | 147 | } else { |
ricardo_95 | 7:dba5091c8b7d | 148 | red_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 149 | green_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 150 | blue_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 151 | } |
ricardo_95 | 7:dba5091c8b7d | 152 | calibration_flash--; |
ricardo_95 | 7:dba5091c8b7d | 153 | } |
ricardo_95 | 7:dba5091c8b7d | 154 | } |
ricardo_95 | 7:dba5091c8b7d | 155 | |
ricardo_95 | 7:dba5091c8b7d | 156 | void trigger_lock() // If rising edge lock button is detected start locktimer. |
ricardo_95 | 7:dba5091c8b7d | 157 | { |
ricardo_95 | 7:dba5091c8b7d | 158 | hold_timer.start(); |
ricardo_95 | 7:dba5091c8b7d | 159 | delay.reset(); |
ricardo_95 | 7:dba5091c8b7d | 160 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 161 | } |
ricardo_95 | 7:dba5091c8b7d | 162 | |
ricardo_95 | 7:dba5091c8b7d | 163 | void timer_lock() // End timer lock. |
ricardo_95 | 7:dba5091c8b7d | 164 | { |
ricardo_95 | 7:dba5091c8b7d | 165 | lock_flag = 0; // Set lock_flag off. |
ricardo_95 | 7:dba5091c8b7d | 166 | hold_timer.stop(); // Stop and reset holdtimer |
ricardo_95 | 7:dba5091c8b7d | 167 | hold_timer.reset(); |
ricardo_95 | 7:dba5091c8b7d | 168 | } |
ricardo_95 | 7:dba5091c8b7d | 169 | |
ricardo_95 | 7:dba5091c8b7d | 170 | void trigger_reposition() |
ricardo_95 | 7:dba5091c8b7d | 171 | { |
ricardo_95 | 7:dba5091c8b7d | 172 | if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. |
ricardo_95 | 7:dba5091c8b7d | 173 | } else { |
ricardo_95 | 7:dba5091c8b7d | 174 | delay.reset(); |
ricardo_95 | 7:dba5091c8b7d | 175 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 176 | |
ricardo_95 | 7:dba5091c8b7d | 177 | if (LED_intern1 == 0) { |
ricardo_95 | 7:dba5091c8b7d | 178 | LED_intern1 = 1.0; |
ricardo_95 | 7:dba5091c8b7d | 179 | } else { |
ricardo_95 | 7:dba5091c8b7d | 180 | LED_intern1 = 0.0; |
ricardo_95 | 7:dba5091c8b7d | 181 | } |
ricardo_95 | 7:dba5091c8b7d | 182 | |
ricardo_95 | 8:bf0f7a6fb1fd | 183 | reposition_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 184 | } |
ricardo_95 | 7:dba5091c8b7d | 185 | } |
ricardo_95 | 7:dba5091c8b7d | 186 | |
ricardo_95 | 7:dba5091c8b7d | 187 | void trigger_mute() |
ricardo_95 | 7:dba5091c8b7d | 188 | { |
ricardo_95 | 7:dba5091c8b7d | 189 | if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. |
ricardo_95 | 7:dba5091c8b7d | 190 | mute_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 191 | } else { |
ricardo_95 | 7:dba5091c8b7d | 192 | delay.reset(); |
ricardo_95 | 7:dba5091c8b7d | 193 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 194 | pi.printf("01\n"); |
ricardo_95 | 7:dba5091c8b7d | 195 | mute_state = !mute_state; |
ricardo_95 | 7:dba5091c8b7d | 196 | |
ricardo_95 | 7:dba5091c8b7d | 197 | if (LED_intern1 == 0) { |
ricardo_95 | 7:dba5091c8b7d | 198 | LED_intern1 = 1.0; |
ricardo_95 | 7:dba5091c8b7d | 199 | } else { |
ricardo_95 | 7:dba5091c8b7d | 200 | LED_intern1 = 0.0; |
ricardo_95 | 7:dba5091c8b7d | 201 | } |
ricardo_95 | 12:7b3a5940f911 | 202 | |
ricardo_95 | 8:bf0f7a6fb1fd | 203 | mute_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 204 | } |
ricardo_95 | 7:dba5091c8b7d | 205 | } |
ricardo_95 | 7:dba5091c8b7d | 206 | |
ricardo_95 | 7:dba5091c8b7d | 207 | void trigger_new_patient() // Function to trigger hold timer for new patient calibration function. |
ricardo_95 | 7:dba5091c8b7d | 208 | { |
ricardo_95 | 7:dba5091c8b7d | 209 | if (lock_state == 1) { |
ricardo_95 | 7:dba5091c8b7d | 210 | } else { |
ricardo_95 | 7:dba5091c8b7d | 211 | hold_timer.start(); |
ricardo_95 | 8:bf0f7a6fb1fd | 212 | new_patient_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 213 | } |
ricardo_95 | 7:dba5091c8b7d | 214 | } |
ricardo_95 | 7:dba5091c8b7d | 215 | |
ricardo_95 | 7:dba5091c8b7d | 216 | void timer_calibration() // Timer calibration function. |
ricardo_95 | 7:dba5091c8b7d | 217 | { |
ricardo_95 | 7:dba5091c8b7d | 218 | hold_timer.stop(); |
ricardo_95 | 7:dba5091c8b7d | 219 | hold_timer.reset(); |
ricardo_95 | 12:7b3a5940f911 | 220 | |
ricardo_95 | 7:dba5091c8b7d | 221 | if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. |
ricardo_95 | 7:dba5091c8b7d | 222 | } else { |
ricardo_95 | 7:dba5091c8b7d | 223 | if (calibration_flag == 0) { |
ricardo_95 | 12:7b3a5940f911 | 224 | |
ricardo_95 | 7:dba5091c8b7d | 225 | if (LED_intern1 == 0) { |
ricardo_95 | 7:dba5091c8b7d | 226 | LED_intern1 = 1.0; |
ricardo_95 | 7:dba5091c8b7d | 227 | } else { |
ricardo_95 | 7:dba5091c8b7d | 228 | LED_intern1 = 0.0; |
ricardo_95 | 7:dba5091c8b7d | 229 | } |
ricardo_95 | 12:7b3a5940f911 | 230 | |
ricardo_95 | 7:dba5091c8b7d | 231 | } else { |
ricardo_95 | 7:dba5091c8b7d | 232 | calibration_flag = 0; |
ricardo_95 | 7:dba5091c8b7d | 233 | } |
ricardo_95 | 7:dba5091c8b7d | 234 | } |
ricardo_95 | 7:dba5091c8b7d | 235 | } |
ricardo_95 | 7:dba5091c8b7d | 236 | |
ricardo_95 | 7:dba5091c8b7d | 237 | void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) // Generate LED colour. |
ricardo_95 | 7:dba5091c8b7d | 238 | { |
ricardo_95 | 7:dba5091c8b7d | 239 | out->red = red_var; |
ricardo_95 | 7:dba5091c8b7d | 240 | out->green = green_var; |
ricardo_95 | 7:dba5091c8b7d | 241 | out->blue = blue_var; |
ricardo_95 | 7:dba5091c8b7d | 242 | } |
ricardo_95 | 7:dba5091c8b7d | 243 | |
ricardo_95 | 7:dba5091c8b7d | 244 | void read_voltage() |
ricardo_95 | 7:dba5091c8b7d | 245 | { |
ricardo_95 | 7:dba5091c8b7d | 246 | LED_intern3 = 0; |
ricardo_95 | 12:7b3a5940f911 | 247 | |
ricardo_95 | 8:bf0f7a6fb1fd | 248 | if (batteryvoltage.read() > alarm_voltage) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage. |
ricardo_95 | 7:dba5091c8b7d | 249 | alarm = 0; // Alarm is off. |
ricardo_95 | 12:7b3a5940f911 | 250 | speaker_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 251 | } else { |
ricardo_95 | 7:dba5091c8b7d | 252 | alarm = 1; // Else alarm is on. |
ricardo_95 | 12:7b3a5940f911 | 253 | speaker_state = 1; |
ricardo_95 | 7:dba5091c8b7d | 254 | } |
ricardo_95 | 12:7b3a5940f911 | 255 | |
ricardo_95 | 7:dba5091c8b7d | 256 | if (alarm == 1 && mute_state == 0 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms. |
ricardo_95 | 7:dba5091c8b7d | 257 | speaker1 = 1; // Set speaker. |
ricardo_95 | 7:dba5091c8b7d | 258 | speaker2 = 1; |
ricardo_95 | 7:dba5091c8b7d | 259 | speaker_timer.start(); // Set timer for speaker to iterate on and off. |
ricardo_95 | 7:dba5091c8b7d | 260 | LED_intern3 = !LED_intern3; |
ricardo_95 | 7:dba5091c8b7d | 261 | } |
ricardo_95 | 7:dba5091c8b7d | 262 | |
ricardo_95 | 7:dba5091c8b7d | 263 | if (alarm == 1 && mute_state == 1 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms. |
ricardo_95 | 7:dba5091c8b7d | 264 | speaker1 = 0; // Set speaker. |
ricardo_95 | 7:dba5091c8b7d | 265 | speaker2 = 0; |
ricardo_95 | 7:dba5091c8b7d | 266 | speaker_timer.start(); // Set timer for speaker to iterate on and off. |
ricardo_95 | 7:dba5091c8b7d | 267 | LED_intern3 = !LED_intern3; |
ricardo_95 | 7:dba5091c8b7d | 268 | } |
ricardo_95 | 7:dba5091c8b7d | 269 | |
ricardo_95 | 7:dba5091c8b7d | 270 | if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) { |
ricardo_95 | 7:dba5091c8b7d | 271 | speaker1 = 0; // Turn off speaker (use two outputs because of currentlimiting of one). |
ricardo_95 | 7:dba5091c8b7d | 272 | speaker2 = 0; |
ricardo_95 | 7:dba5091c8b7d | 273 | } |
ricardo_95 | 7:dba5091c8b7d | 274 | |
ricardo_95 | 7:dba5091c8b7d | 275 | if (speaker_timer.read_ms() > (speaker_active_ms*2)) { |
ricardo_95 | 7:dba5091c8b7d | 276 | speaker_timer.stop(); // Stop speaker timer. |
ricardo_95 | 7:dba5091c8b7d | 277 | speaker_timer.reset(); |
ricardo_95 | 7:dba5091c8b7d | 278 | } |
ricardo_95 | 12:7b3a5940f911 | 279 | |
ricardo_95 | 12:7b3a5940f911 | 280 | batteryvoltage_current = batteryvoltage.read(); |
ricardo_95 | 12:7b3a5940f911 | 281 | |
ricardo_95 | 8:bf0f7a6fb1fd | 282 | if (supplyvoltage.read() == 0) { |
ricardo_95 | 12:7b3a5940f911 | 283 | power_plug_state = 1; |
ricardo_95 | 8:bf0f7a6fb1fd | 284 | } else { |
ricardo_95 | 12:7b3a5940f911 | 285 | power_plug_state = 0; |
ricardo_95 | 8:bf0f7a6fb1fd | 286 | } |
ricardo_95 | 7:dba5091c8b7d | 287 | } |
DEldering | 0:c0e44c46c573 | 288 | |
DEldering | 0:c0e44c46c573 | 289 | void read_adc() |
DEldering | 0:c0e44c46c573 | 290 | { |
DEldering | 1:a8e61f3910ad | 291 | t.reset(); |
DEldering | 1:a8e61f3910ad | 292 | t.start(); |
DEldering | 1:a8e61f3910ad | 293 | |
DEldering | 4:367af005956b | 294 | elec[0] = pel.readADC_SingleEnded(0); //First PE readout |
DEldering | 1:a8e61f3910ad | 295 | |
DEldering | 0:c0e44c46c573 | 296 | for (k = 0; k < 4; k = k + 1) { |
DEldering | 4:367af005956b | 297 | res[k] = pr1.readADC_SingleEnded(k); //First 4 PR readout |
DEldering | 1:a8e61f3910ad | 298 | } |
DEldering | 4:367af005956b | 299 | while(t.read_us()<(1*(cycle_time/5))) {} //Wait untill 20% of cycle |
DEldering | 1:a8e61f3910ad | 300 | |
DEldering | 4:367af005956b | 301 | elec[1] = pel.readADC_SingleEnded(0); //Second PE readout |
DEldering | 1:a8e61f3910ad | 302 | |
DEldering | 1:a8e61f3910ad | 303 | for (k = 0; k < 4; k = k + 1) { |
DEldering | 4:367af005956b | 304 | res[k+4] = pr2.readADC_SingleEnded(k); //Last 4 PR readout |
DEldering | 0:c0e44c46c573 | 305 | } |
ricardo_95 | 7:dba5091c8b7d | 306 | while(t.read_us()<(2*(cycle_time/5))) {} //Wait untill 40% of cycle |
DEldering | 1:a8e61f3910ad | 307 | |
DEldering | 4:367af005956b | 308 | elec[2] = pel.readADC_SingleEnded(0); //Third PE readout |
DEldering | 1:a8e61f3910ad | 309 | |
DEldering | 4:367af005956b | 310 | agu.getAccelero(acce); //Get accelerometer data |
DEldering | 1:a8e61f3910ad | 311 | angle = acce[2]*10; |
ricardo_95 | 7:dba5091c8b7d | 312 | agu.getGyro(gyro); //Get gyroscope data |
ricardo_95 | 7:dba5091c8b7d | 313 | |
DEldering | 4:367af005956b | 314 | while(t.read_us()<(3*(cycle_time/5))) {} //Wait untill 60% of cycle |
DEldering | 1:a8e61f3910ad | 315 | |
DEldering | 4:367af005956b | 316 | elec[3] = pel.readADC_SingleEnded(0); //Fourth PE readout |
DEldering | 1:a8e61f3910ad | 317 | |
ricardo_95 | 7:dba5091c8b7d | 318 | if ((hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && lock == 1) { // If statement for lock function. |
ricardo_95 | 7:dba5091c8b7d | 319 | lock_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 320 | LED_intern2 = !LED_intern2; |
ricardo_95 | 7:dba5091c8b7d | 321 | lock_state = !lock_state; |
ricardo_95 | 7:dba5091c8b7d | 322 | } |
ricardo_95 | 7:dba5091c8b7d | 323 | |
ricardo_95 | 7:dba5091c8b7d | 324 | if ((hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && new_patient == 1) { // If statement for calibration system. |
ricardo_95 | 7:dba5091c8b7d | 325 | calibration_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 326 | calibration_flash = 11; |
ricardo_95 | 13:b85f41d6fe6f | 327 | pi.printf(">30\n"); // Print statement for serial communication to inform algorithm to calibrate. |
ricardo_95 | 7:dba5091c8b7d | 328 | } |
ricardo_95 | 12:7b3a5940f911 | 329 | |
ricardo_95 | 7:dba5091c8b7d | 330 | if (delay.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active. |
ricardo_95 | 7:dba5091c8b7d | 331 | lock_state = 1; |
ricardo_95 | 7:dba5091c8b7d | 332 | LED_intern2 = 1; |
ricardo_95 | 7:dba5091c8b7d | 333 | } |
ricardo_95 | 7:dba5091c8b7d | 334 | |
ricardo_95 | 8:bf0f7a6fb1fd | 335 | batteryvoltage_current = batteryvoltage_current*100; |
ricardo_95 | 8:bf0f7a6fb1fd | 336 | batteryvoltage_current = batteryvoltage_last; |
ricardo_95 | 7:dba5091c8b7d | 337 | read_voltage(); // Supplyvoltage control for alarm. |
ricardo_95 | 8:bf0f7a6fb1fd | 338 | |
ricardo_95 | 7:dba5091c8b7d | 339 | uint32_t val = 0; |
ricardo_95 | 7:dba5091c8b7d | 340 | colour_select(LED_colour); |
ricardo_95 | 7:dba5091c8b7d | 341 | array.update(generate, NLED, val); |
ricardo_95 | 7:dba5091c8b7d | 342 | |
DEldering | 4:367af005956b | 343 | while(t.read_us()<(4*(cycle_time/5))) {} //Wait untill 80% of cycle |
DEldering | 1:a8e61f3910ad | 344 | |
DEldering | 4:367af005956b | 345 | elec[4] = pel.readADC_SingleEnded(0); //Fifth PE readout |
DEldering | 1:a8e61f3910ad | 346 | |
DEldering | 4:367af005956b | 347 | while(t.read_us()<(4.5*(cycle_time/5))) {} //Wait untill 90% of cycle |
ricardo_95 | 13:b85f41d6fe6f | 348 | pi.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%f,%f,\n", res[4], res[7], res[6], res[5], res[1], res[0], res[2], res[3], elec[0], elec[1], elec[2], elec[3], elec[4], acce[0]*100, acce[1]*100, acce[2]*100, gyro[0]*100, gyro[1]*100, gyro[2]*100); // print all to serial port |
DEldering | 6:9c1944f3ebe5 | 349 | //receiving order: 8 resistive sensors, 5 electric readings, 3 accelerometer axes, 3 gyroscope axes |
ricardo_95 | 7:dba5091c8b7d | 350 | |
ricardo_95 | 12:7b3a5940f911 | 351 | serial_read(); |
ricardo_95 | 12:7b3a5940f911 | 352 | |
ricardo_95 | 7:dba5091c8b7d | 353 | if (mute_flag == 1) { |
ricardo_95 | 7:dba5091c8b7d | 354 | pi.printf(">01\n"); |
ricardo_95 | 7:dba5091c8b7d | 355 | mute_flag = 0; |
ricardo_95 | 12:7b3a5940f911 | 356 | LED_intern4 = 1; |
ricardo_95 | 11:73c6def38fbd | 357 | } else { |
ricardo_95 | 11:73c6def38fbd | 358 | LED_intern4 = 0; |
ricardo_95 | 7:dba5091c8b7d | 359 | } |
ricardo_95 | 12:7b3a5940f911 | 360 | |
ricardo_95 | 7:dba5091c8b7d | 361 | if (new_patient_flag == 1) { |
ricardo_95 | 7:dba5091c8b7d | 362 | pi.printf(">02\n"); |
ricardo_95 | 7:dba5091c8b7d | 363 | new_patient_flag = 0; |
ricardo_95 | 7:dba5091c8b7d | 364 | } |
ricardo_95 | 12:7b3a5940f911 | 365 | |
ricardo_95 | 7:dba5091c8b7d | 366 | if (reposition_flag == 1) { |
ricardo_95 | 7:dba5091c8b7d | 367 | pi.printf(">03\n"); |
ricardo_95 | 7:dba5091c8b7d | 368 | reposition_flag = 0; |
ricardo_95 | 7:dba5091c8b7d | 369 | } |
ricardo_95 | 12:7b3a5940f911 | 370 | |
ricardo_95 | 8:bf0f7a6fb1fd | 371 | if (batteryvoltage_current != batteryvoltage_last) { |
ricardo_95 | 8:bf0f7a6fb1fd | 372 | pi.printf("%%d\n", batteryvoltage_current); |
ricardo_95 | 12:7b3a5940f911 | 373 | } |
ricardo_95 | 12:7b3a5940f911 | 374 | |
ricardo_95 | 9:514a44bf510f | 375 | if (LED_red_logged != LED_red_state) { |
ricardo_95 | 12:7b3a5940f911 | 376 | if (LED_red_state == 1) { |
ricardo_95 | 9:514a44bf510f | 377 | pi.printf("&04\n"); |
ricardo_95 | 9:514a44bf510f | 378 | LED_red_logged = LED_red_state; |
ricardo_95 | 12:7b3a5940f911 | 379 | } |
ricardo_95 | 9:514a44bf510f | 380 | if (LED_red_state == 0) { |
ricardo_95 | 9:514a44bf510f | 381 | pi.printf("&40\n"); |
ricardo_95 | 12:7b3a5940f911 | 382 | LED_red_logged = LED_red_state; |
ricardo_95 | 9:514a44bf510f | 383 | } |
ricardo_95 | 8:bf0f7a6fb1fd | 384 | } |
ricardo_95 | 12:7b3a5940f911 | 385 | |
ricardo_95 | 9:514a44bf510f | 386 | if (LED_yellow_logged != LED_yellow_state) { |
ricardo_95 | 12:7b3a5940f911 | 387 | if (LED_yellow_state == 1) { |
ricardo_95 | 9:514a44bf510f | 388 | pi.printf("&06\n"); |
ricardo_95 | 9:514a44bf510f | 389 | LED_yellow_logged = LED_yellow_state; |
ricardo_95 | 12:7b3a5940f911 | 390 | } |
ricardo_95 | 9:514a44bf510f | 391 | if (LED_yellow_state == 0) { |
ricardo_95 | 9:514a44bf510f | 392 | pi.printf("&60\n"); |
ricardo_95 | 12:7b3a5940f911 | 393 | LED_yellow_logged = LED_yellow_state; |
ricardo_95 | 9:514a44bf510f | 394 | } |
ricardo_95 | 7:dba5091c8b7d | 395 | } |
ricardo_95 | 12:7b3a5940f911 | 396 | |
ricardo_95 | 9:514a44bf510f | 397 | if (LED_green_logged != LED_green_state) { |
ricardo_95 | 12:7b3a5940f911 | 398 | if (LED_green_state == 1) { |
ricardo_95 | 9:514a44bf510f | 399 | pi.printf("&05\n"); |
ricardo_95 | 9:514a44bf510f | 400 | LED_green_logged = LED_green_state; |
ricardo_95 | 12:7b3a5940f911 | 401 | } |
ricardo_95 | 9:514a44bf510f | 402 | if (LED_green_state == 0) { |
ricardo_95 | 9:514a44bf510f | 403 | pi.printf("&50\n"); |
ricardo_95 | 12:7b3a5940f911 | 404 | LED_green_logged = LED_green_state; |
ricardo_95 | 9:514a44bf510f | 405 | } |
ricardo_95 | 7:dba5091c8b7d | 406 | } |
ricardo_95 | 12:7b3a5940f911 | 407 | |
ricardo_95 | 9:514a44bf510f | 408 | if (speaker_logged != speaker_state) { |
ricardo_95 | 12:7b3a5940f911 | 409 | if (speaker_state == 1) { |
ricardo_95 | 9:514a44bf510f | 410 | pi.printf("&07\n"); |
ricardo_95 | 9:514a44bf510f | 411 | speaker_logged = speaker_state; |
ricardo_95 | 12:7b3a5940f911 | 412 | } |
ricardo_95 | 9:514a44bf510f | 413 | if (speaker_state == 0) { |
ricardo_95 | 9:514a44bf510f | 414 | pi.printf("&70\n"); |
ricardo_95 | 12:7b3a5940f911 | 415 | speaker_logged = speaker_state; |
ricardo_95 | 9:514a44bf510f | 416 | } |
ricardo_95 | 8:bf0f7a6fb1fd | 417 | } |
ricardo_95 | 9:514a44bf510f | 418 | |
ricardo_95 | 9:514a44bf510f | 419 | if (power_plug_logged != power_plug_state) { |
ricardo_95 | 12:7b3a5940f911 | 420 | if (power_plug_state == 1) { |
ricardo_95 | 10:6b3034ec3c47 | 421 | pi.printf("#08\n"); |
ricardo_95 | 9:514a44bf510f | 422 | power_plug_logged = power_plug_state; |
ricardo_95 | 12:7b3a5940f911 | 423 | } |
ricardo_95 | 9:514a44bf510f | 424 | if (power_plug_state == 0) { |
ricardo_95 | 10:6b3034ec3c47 | 425 | pi.printf("#80\n"); |
ricardo_95 | 12:7b3a5940f911 | 426 | power_plug_logged = power_plug_state; |
ricardo_95 | 9:514a44bf510f | 427 | } |
ricardo_95 | 9:514a44bf510f | 428 | } |
DEldering | 0:c0e44c46c573 | 429 | } |
DEldering | 0:c0e44c46c573 | 430 | |
DEldering | 0:c0e44c46c573 | 431 | int main() |
DEldering | 0:c0e44c46c573 | 432 | { |
ricardo_95 | 12:7b3a5940f911 | 433 | wait_ms(boot_delay_ms); // Wait to boot sensorplate first |
DEldering | 1:a8e61f3910ad | 434 | i2c.frequency(i2c_freq); |
ricardo_95 | 12:7b3a5940f911 | 435 | pc.baud(baud); |
ricardo_95 | 12:7b3a5940f911 | 436 | pi.baud(baud); |
DEldering | 1:a8e61f3910ad | 437 | pr1.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 438 | pr2.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 439 | pel.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
ricardo_95 | 7:dba5091c8b7d | 440 | pi.format(8, SerialBase::None, 1); |
ricardo_95 | 7:dba5091c8b7d | 441 | |
ricardo_95 | 7:dba5091c8b7d | 442 | lock.rise(&trigger_lock); // Interrupt for rising edge lock button. |
ricardo_95 | 7:dba5091c8b7d | 443 | lock.fall(&timer_lock); |
ricardo_95 | 7:dba5091c8b7d | 444 | reposition.rise(&trigger_reposition); |
ricardo_95 | 7:dba5091c8b7d | 445 | mute.rise(&trigger_mute); |
ricardo_95 | 7:dba5091c8b7d | 446 | new_patient.rise(&trigger_new_patient); // New patient/calibration button rising event. |
ricardo_95 | 7:dba5091c8b7d | 447 | new_patient.fall(&timer_calibration); // Falling edge for calibration algorithm option. |
ricardo_95 | 7:dba5091c8b7d | 448 | delay.reset(); // Delaytimer reset en start. |
ricardo_95 | 7:dba5091c8b7d | 449 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 450 | |
ricardo_95 | 13:b85f41d6fe6f | 451 | sample_cycle.attach_us(&read_adc, cycle_time); |
ricardo_95 | 13:b85f41d6fe6f | 452 | |
DEldering | 0:c0e44c46c573 | 453 | while (1) { |
DEldering | 1:a8e61f3910ad | 454 | wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms |
DEldering | 0:c0e44c46c573 | 455 | } |
ricardo_95 | 12:7b3a5940f911 | 456 | } |