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