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 Momo Medical

Committer:
ricardo_95
Date:
Wed Oct 11 12:56:17 2017 +0000
Revision:
33:df21cb8dc5c7
Parent:
32:0944efc47e46
Child:
34:1614f4f2b841
Belt implemented for testing algorithm.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ricardo_95 21:13e4824bc364 1 /********************* CODE INFORMATIE ******************************
ricardo_95 33:df21cb8dc5c7 2 Date of creation: 20-12-1908
ricardo_95 33:df21cb8dc5c7 3 Author: Elke Salzmann
ricardo_95 33:df21cb8dc5c7 4 co-authors: Menno Gravemaker
ricardo_95 33:df21cb8dc5c7 5 (c) Copyright by Momo Medical BV.
ricardo_95 33:df21cb8dc5c7 6
ricardo_95 33:df21cb8dc5c7 7 Current version name: 2.1.2
ricardo_95 33:df21cb8dc5c7 8 Date of creation: 14-01-2017
ricardo_95 33:df21cb8dc5c7 9 Purpose of this file Oneliner of purpose
ricardo_95 33:df21cb8dc5c7 10 Update ‘what’s new in this version?’: short meaningful description (no more than 3 lines)
ricardo_95 33:df21cb8dc5c7 11 Todo: short meaningful description of next actions that could be worked on(less than 3 lines)
ricardo_95 33:df21cb8dc5c7 12 Source file: http://mbed.com
ricardo_95 33:df21cb8dc5c7 13
ricardo_95 12:7b3a5940f911 14 */
ricardo_95 12:7b3a5940f911 15
ricardo_95 21:13e4824bc364 16 /************************ CONFIG ***********************************/
ricardo_95 21:13e4824bc364 17
DEldering 0:c0e44c46c573 18 #include "mbed.h"
DEldering 0:c0e44c46c573 19 #include "Adafruit_ADS1015.h"
DEldering 0:c0e44c46c573 20 #include "MPU6050.h"
ricardo_95 33:df21cb8dc5c7 21 #include "MPU6050_belt.h"
ricardo_95 7:dba5091c8b7d 22 #include "neopixel.h"
ricardo_95 12:7b3a5940f911 23 #define NLED (3)
ricardo_95 7:dba5091c8b7d 24 #define ONE_COLOR
ricardo_95 7:dba5091c8b7d 25
ricardo_95 26:9e130f7ee829 26 InterruptIn lock(p15); // Interrupts for buttons.
ricardo_95 7:dba5091c8b7d 27 InterruptIn reposition(p17);
ricardo_95 26:9e130f7ee829 28 InterruptIn mute(p16);
ricardo_95 21:13e4824bc364 29 InterruptIn new_patient(p18);
ricardo_95 7:dba5091c8b7d 30
ricardo_95 21:13e4824bc364 31 DigitalOut LED_intern1(LED1);
ricardo_95 12:7b3a5940f911 32 DigitalOut LED_intern2(LED2);
ricardo_95 12:7b3a5940f911 33 DigitalOut LED_intern3(LED3);
ricardo_95 26:9e130f7ee829 34 DigitalOut LED_intern4(LED4);
ricardo_95 7:dba5091c8b7d 35 neopixel::PixelArray array(p11);
ricardo_95 7:dba5091c8b7d 36
ricardo_95 19:3b5999fa7b7e 37 Timer lock_hold_timer;
ricardo_95 19:3b5999fa7b7e 38 Timer calibration_hold_timer;
ricardo_95 7:dba5091c8b7d 39 Timer delay;
ricardo_95 7:dba5091c8b7d 40 Timer speaker_timer;
ricardo_95 28:b4bee068780d 41 Timer test_timer;
ricardo_95 7:dba5091c8b7d 42
ricardo_95 7:dba5091c8b7d 43 DigitalOut speaker1(p21);
ricardo_95 7:dba5091c8b7d 44 DigitalOut speaker2(p22);
ricardo_95 21:13e4824bc364 45 PwmOut lock_LED(p23);
ricardo_95 26:9e130f7ee829 46 PwmOut reposition_LED(p25);
ricardo_95 26:9e130f7ee829 47 PwmOut mute_LED(p26);
ricardo_95 26:9e130f7ee829 48 PwmOut new_patient_LED(p24);
DEldering 0:c0e44c46c573 49
DEldering 1:a8e61f3910ad 50 I2C i2c(p28, p27); // I2C
ricardo_95 21:13e4824bc364 51 I2C i2cAccu(p9, p10); // I2C for accupack
DEldering 1:a8e61f3910ad 52 MPU6050 agu(p28,p27); // Accelerometer/Gyroscope Unit
ricardo_95 33:df21cb8dc5c7 53 MPU6050_belt agu_belt(p28,p27); // Accelerometer/Gyroscope Unit Belt
DEldering 1:a8e61f3910ad 54 Adafruit_ADS1115 pr1(&i2c, 0x48); // first PiëzoResistive ADC
DEldering 1:a8e61f3910ad 55 Adafruit_ADS1115 pr2(&i2c, 0x49); // second PiëzoResistive ADC
DEldering 1:a8e61f3910ad 56 Adafruit_ADS1115 pel(&i2c, 0x4B); // PiëzoElectric ADC
ricardo_95 21:13e4824bc364 57 Adafruit_ADS1115 adsAccu(&i2cAccu, 0x48);
DEldering 1:a8e61f3910ad 58 Serial pc(USBTX, USBRX); // tx, rx // Serial USB connection
ricardo_95 21:13e4824bc364 59 Serial pi(p13, p14); // tx, rx // Setup serial communication for pi.
DEldering 1:a8e61f3910ad 60 Timer t; // Timer for equally time-spaced samples
DEldering 1:a8e61f3910ad 61 Ticker sample_cycle; // Polling cycle
ricardo_95 8:bf0f7a6fb1fd 62
ricardo_95 12:7b3a5940f911 63 int boot_delay_ms = 500;
DEldering 1:a8e61f3910ad 64 int cycle_time = 100000; // Cycle time in us
DEldering 1:a8e61f3910ad 65 int i2c_freq = 400000; // I2C Frequency
ricardo_95 12:7b3a5940f911 66 int baud = 115200; // Baud rate
DEldering 1:a8e61f3910ad 67 short res[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle
DEldering 1:a8e61f3910ad 68 short elec[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle
DEldering 1:a8e61f3910ad 69 int angle = 0; // Accelerometer Z-axis
DEldering 1:a8e61f3910ad 70 int k = 0;
DEldering 1:a8e61f3910ad 71 float acce[3]; // Raw accelerometer data
DEldering 1:a8e61f3910ad 72 float gyro[3]; // Raw gyroscope data
ricardo_95 33:df21cb8dc5c7 73 float acce_belt[3]; // Raw accelerometer data from belt
ricardo_95 33:df21cb8dc5c7 74 float gyro_belt[3]; // Raw gyroscope data from belt
ricardo_95 22:a09775c25890 75 char LED_colour = 'g'; // Variable to set LED colour.
ricardo_95 28:b4bee068780d 76 bool lock_state = 0, lock_flag = 0, mute_state = 0, alarm = 0, calibration_flag = 0, intensity_select = 1; // Boolean variables for states logging.
ricardo_95 25:96c34634abda 77 bool mute_flag = 0, new_patient_flag = 0, reposition_flag = 0;
ricardo_95 21:13e4824bc364 78 bool speaker_state = 0, LED_red_state = 0, LED_yellow_state = 0, LED_green_state = 0, power_plug_state = 0;
ricardo_95 21:13e4824bc364 79 bool speaker_logged = 0, LED_red_logged = 0, LED_yellow_logged = 0, LED_green_logged = 0, power_plug_logged = 0;
ricardo_95 11:73c6def38fbd 80 int locktime_ms = 2000; // Waittime for lock user interface in ms.
ricardo_95 12:7b3a5940f911 81 int calibrationtime_ms = 5000; // Time to press new_patient button for calibration system.
ricardo_95 12:7b3a5940f911 82 int calibration_flash; // Variable for flash LED's to indicate calibration.
ricardo_95 7:dba5091c8b7d 83 int buttondelay_ms = 750; // Button delay in ms.
ricardo_95 7:dba5091c8b7d 84 int delay_lock_interface = 3000*60; // Delay for non using interface locktime.
ricardo_95 12:7b3a5940f911 85 int speaker_active_ms = 750; // Time to iterate speaker on and off when alarm occurs.
ricardo_95 17:6ec7d594c1f1 86 int alarm_voltage = 5867; // Needed voltage for alarm expressed as a digital 15 bit value (=20% of max battery voltage)
ricardo_95 25:96c34634abda 87 int red_var, green_var, blue_var; // Variables to set LED intensity.
ricardo_95 21:13e4824bc364 88 short batteryvoltage_current = 0, batteryvoltage_last = 0, powervoltage_current, powervoltage_last; // Variables to manage batteryvoltage.
ricardo_95 31:24770fa8114b 89 int intensity_day = 40, intensity_night = 10; // Intensity settings for LED's to wall.
ricardo_95 31:24770fa8114b 90 double intensity, control_LED_intensity = 0; // Variable between 0 and 1 to set the intensity of the LED's above the buttons.
ricardo_95 30:bc0bddc6f8d1 91 int a; // Test
ricardo_95 21:13e4824bc364 92
ricardo_95 33:df21cb8dc5c7 93 /*************************** TEST ********************************/
ricardo_95 33:df21cb8dc5c7 94 // Verify algoritm function: for belt activation, set test_belt 1 (connect pin p20 to 3.3V)
ricardo_95 33:df21cb8dc5c7 95 DigitalIn test_pin(p20, PullDown);
ricardo_95 33:df21cb8dc5c7 96 bool test_belt = 1; //test_pin;
ricardo_95 33:df21cb8dc5c7 97
ricardo_95 33:df21cb8dc5c7 98 // Verify if interrupts are working properly
ricardo_95 33:df21cb8dc5c7 99
ricardo_95 33:df21cb8dc5c7 100 // Verify if parameters are
ricardo_95 33:df21cb8dc5c7 101 bool test_mode = 1;
ricardo_95 33:df21cb8dc5c7 102
ricardo_95 21:13e4824bc364 103 /*************************** CODE ********************************/
ricardo_95 7:dba5091c8b7d 104
ricardo_95 11:73c6def38fbd 105 void set_intensity() // Function to set the intensity for the LED's.
ricardo_95 7:dba5091c8b7d 106 {
ricardo_95 11:73c6def38fbd 107 if (intensity_select == 1) {
ricardo_95 12:7b3a5940f911 108 intensity = intensity_day;
ricardo_95 12:7b3a5940f911 109 } else {
ricardo_95 12:7b3a5940f911 110 intensity = intensity_night;
ricardo_95 7:dba5091c8b7d 111 }
ricardo_95 21:13e4824bc364 112 control_LED_intensity = (intensity/100);
ricardo_95 33:df21cb8dc5c7 113
ricardo_95 33:df21cb8dc5c7 114 if (test_mode == 1) {
ricardo_95 33:df21cb8dc5c7 115 pc.printf("Intensity LED's shines to wall = %f\n", intensity);
ricardo_95 33:df21cb8dc5c7 116 pc.printf("Intensity LED's above buttons = %f\n", control_LED_intensity);
ricardo_95 33:df21cb8dc5c7 117 }
ricardo_95 7:dba5091c8b7d 118 }
ricardo_95 7:dba5091c8b7d 119
ricardo_95 12:7b3a5940f911 120 void serial_read() // Serial read for select LED intensity and colour.
ricardo_95 12:7b3a5940f911 121 {
ricardo_95 11:73c6def38fbd 122 if (pi.readable()) {
ricardo_95 33:df21cb8dc5c7 123 char message[4];
ricardo_95 27:77065263c0ea 124 pi.scanf("%s", message);
ricardo_95 33:df21cb8dc5c7 125
ricardo_95 33:df21cb8dc5c7 126 intensity_select = message[0];
ricardo_95 33:df21cb8dc5c7 127 LED_colour = message[1];
ricardo_95 33:df21cb8dc5c7 128
ricardo_95 33:df21cb8dc5c7 129 if (test_mode == 1) {
ricardo_95 33:df21cb8dc5c7 130 pc.printf("%s", message);
ricardo_95 11:73c6def38fbd 131 }
ricardo_95 11:73c6def38fbd 132 }
ricardo_95 11:73c6def38fbd 133 }
ricardo_95 11:73c6def38fbd 134
ricardo_95 21:13e4824bc364 135 void serial_log()
ricardo_95 7:dba5091c8b7d 136 {
ricardo_95 7:dba5091c8b7d 137 if (mute_flag == 1) {
ricardo_95 19:3b5999fa7b7e 138 pi.printf(">01\n");
ricardo_95 33:df21cb8dc5c7 139
ricardo_95 33:df21cb8dc5c7 140 if (test_mode == 1) {
ricardo_95 33:df21cb8dc5c7 141 pc.printf(">01\n");
ricardo_95 33:df21cb8dc5c7 142 }
ricardo_95 33:df21cb8dc5c7 143
ricardo_95 7:dba5091c8b7d 144 mute_flag = 0;
ricardo_95 19:3b5999fa7b7e 145 }
ricardo_95 12:7b3a5940f911 146
ricardo_95 19:3b5999fa7b7e 147 if (new_patient_flag == 1) {
ricardo_95 19:3b5999fa7b7e 148 pi.printf(">03\n");
ricardo_95 33:df21cb8dc5c7 149
ricardo_95 33:df21cb8dc5c7 150 if (test_mode == 1) {
ricardo_95 33:df21cb8dc5c7 151 pc.printf(">03\n");
ricardo_95 33:df21cb8dc5c7 152 }
ricardo_95 33:df21cb8dc5c7 153
ricardo_95 7:dba5091c8b7d 154 new_patient_flag = 0;
ricardo_95 7:dba5091c8b7d 155 }
ricardo_95 12:7b3a5940f911 156
ricardo_95 19:3b5999fa7b7e 157 if (reposition_flag == 1) {
ricardo_95 19:3b5999fa7b7e 158 pi.printf(">02\n");
ricardo_95 33:df21cb8dc5c7 159
ricardo_95 33:df21cb8dc5c7 160 if (test_mode == 1) {
ricardo_95 33:df21cb8dc5c7 161 pc.printf(">02\n");
ricardo_95 33:df21cb8dc5c7 162 }
ricardo_95 33:df21cb8dc5c7 163
ricardo_95 7:dba5091c8b7d 164 reposition_flag = 0;
ricardo_95 7:dba5091c8b7d 165 }
ricardo_95 12:7b3a5940f911 166
ricardo_95 8:bf0f7a6fb1fd 167 if (batteryvoltage_current != batteryvoltage_last) {
ricardo_95 8:bf0f7a6fb1fd 168 pi.printf("%%d\n", batteryvoltage_current);
ricardo_95 33:df21cb8dc5c7 169
ricardo_95 33:df21cb8dc5c7 170 if (test_mode == 1) {
ricardo_95 33:df21cb8dc5c7 171 pc.printf("%%d\n", batteryvoltage_current);
ricardo_95 33:df21cb8dc5c7 172 }
ricardo_95 33:df21cb8dc5c7 173
ricardo_95 16:adbbac0c79f9 174 batteryvoltage_last = batteryvoltage_current;
ricardo_95 12:7b3a5940f911 175 }
ricardo_95 12:7b3a5940f911 176
ricardo_95 9:514a44bf510f 177 if (LED_red_logged != LED_red_state) {
ricardo_95 12:7b3a5940f911 178 if (LED_red_state == 1) {
ricardo_95 9:514a44bf510f 179 pi.printf("&04\n");
ricardo_95 9:514a44bf510f 180 LED_red_logged = LED_red_state;
ricardo_95 12:7b3a5940f911 181 }
ricardo_95 9:514a44bf510f 182 if (LED_red_state == 0) {
ricardo_95 9:514a44bf510f 183 pi.printf("&40\n");
ricardo_95 12:7b3a5940f911 184 LED_red_logged = LED_red_state;
ricardo_95 9:514a44bf510f 185 }
ricardo_95 8:bf0f7a6fb1fd 186 }
ricardo_95 12:7b3a5940f911 187
ricardo_95 9:514a44bf510f 188 if (LED_yellow_logged != LED_yellow_state) {
ricardo_95 12:7b3a5940f911 189 if (LED_yellow_state == 1) {
ricardo_95 9:514a44bf510f 190 pi.printf("&06\n");
ricardo_95 9:514a44bf510f 191 LED_yellow_logged = LED_yellow_state;
ricardo_95 12:7b3a5940f911 192 }
ricardo_95 9:514a44bf510f 193 if (LED_yellow_state == 0) {
ricardo_95 9:514a44bf510f 194 pi.printf("&60\n");
ricardo_95 12:7b3a5940f911 195 LED_yellow_logged = LED_yellow_state;
ricardo_95 9:514a44bf510f 196 }
ricardo_95 7:dba5091c8b7d 197 }
ricardo_95 12:7b3a5940f911 198
ricardo_95 9:514a44bf510f 199 if (LED_green_logged != LED_green_state) {
ricardo_95 12:7b3a5940f911 200 if (LED_green_state == 1) {
ricardo_95 9:514a44bf510f 201 pi.printf("&05\n");
ricardo_95 9:514a44bf510f 202 LED_green_logged = LED_green_state;
ricardo_95 12:7b3a5940f911 203 }
ricardo_95 9:514a44bf510f 204 if (LED_green_state == 0) {
ricardo_95 9:514a44bf510f 205 pi.printf("&50\n");
ricardo_95 12:7b3a5940f911 206 LED_green_logged = LED_green_state;
ricardo_95 9:514a44bf510f 207 }
ricardo_95 7:dba5091c8b7d 208 }
ricardo_95 12:7b3a5940f911 209
ricardo_95 9:514a44bf510f 210 if (speaker_logged != speaker_state) {
ricardo_95 12:7b3a5940f911 211 if (speaker_state == 1) {
ricardo_95 9:514a44bf510f 212 pi.printf("&07\n");
ricardo_95 9:514a44bf510f 213 speaker_logged = speaker_state;
ricardo_95 12:7b3a5940f911 214 }
ricardo_95 9:514a44bf510f 215 if (speaker_state == 0) {
ricardo_95 9:514a44bf510f 216 pi.printf("&70\n");
ricardo_95 12:7b3a5940f911 217 speaker_logged = speaker_state;
ricardo_95 9:514a44bf510f 218 }
ricardo_95 8:bf0f7a6fb1fd 219 }
ricardo_95 9:514a44bf510f 220
ricardo_95 9:514a44bf510f 221 if (power_plug_logged != power_plug_state) {
ricardo_95 12:7b3a5940f911 222 if (power_plug_state == 1) {
ricardo_95 10:6b3034ec3c47 223 pi.printf("#08\n");
ricardo_95 21:13e4824bc364 224 pc.printf("#08 power on\n");
ricardo_95 9:514a44bf510f 225 power_plug_logged = power_plug_state;
ricardo_95 12:7b3a5940f911 226 }
ricardo_95 9:514a44bf510f 227 if (power_plug_state == 0) {
ricardo_95 10:6b3034ec3c47 228 pi.printf("#80\n");
ricardo_95 21:13e4824bc364 229 pc.printf("#08 power off\n");
ricardo_95 12:7b3a5940f911 230 power_plug_logged = power_plug_state;
ricardo_95 9:514a44bf510f 231 }
ricardo_95 9:514a44bf510f 232 }
ricardo_95 33:df21cb8dc5c7 233
ricardo_95 33:df21cb8dc5c7 234 if (a == 1) {
ricardo_95 33:df21cb8dc5c7 235 pi.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%f,%f,%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], acce[1], acce[2], gyro[0], gyro[1], gyro[2], acce_belt[0], acce_belt[1], acce_belt[2], gyro_belt[0], gyro_belt[1], gyro_belt[2]); // print all to serial port
ricardo_95 33:df21cb8dc5c7 236 pc.printf("Belt accelerometer values: %f,%f,%f,%f,%f,%f\n", acce_belt[0], acce_belt[1], acce_belt[2], gyro_belt[0], gyro_belt[1], gyro_belt[2]);
ricardo_95 28:b4bee068780d 237 }
ricardo_95 27:77065263c0ea 238 //receiving order: 8 resistive sensors, 5 electric readings, 3 accelerometer axes, 3 gyroscope axes
ricardo_95 33:df21cb8dc5c7 239 //Belt values print to pc
DEldering 0:c0e44c46c573 240 }
DEldering 0:c0e44c46c573 241
ricardo_95 21:13e4824bc364 242 void colour_select(char LED_colour) // Function to select the colour.
ricardo_95 21:13e4824bc364 243 {
ricardo_95 21:13e4824bc364 244 set_intensity(); // Call function set_intensity
ricardo_95 21:13e4824bc364 245
ricardo_95 28:b4bee068780d 246 red_var = 0;
ricardo_95 28:b4bee068780d 247 green_var = 0;
ricardo_95 28:b4bee068780d 248 blue_var = 0;
ricardo_95 28:b4bee068780d 249
ricardo_95 21:13e4824bc364 250 if (LED_colour == 'r') {
ricardo_95 21:13e4824bc364 251 red_var = (2.55*intensity);
ricardo_95 21:13e4824bc364 252 green_var = 0;
ricardo_95 21:13e4824bc364 253 blue_var = 0;
ricardo_95 21:13e4824bc364 254 LED_red_state = 1;
ricardo_95 21:13e4824bc364 255 } else {
ricardo_95 21:13e4824bc364 256 LED_red_state = 0;
ricardo_95 21:13e4824bc364 257 }
ricardo_95 21:13e4824bc364 258
ricardo_95 21:13e4824bc364 259 if (LED_colour == 'y') {
ricardo_95 21:13e4824bc364 260 red_var = (2.55*intensity);
ricardo_95 21:13e4824bc364 261 green_var = (2.55*intensity);
ricardo_95 21:13e4824bc364 262 blue_var = 0;
ricardo_95 21:13e4824bc364 263 LED_yellow_state = 1;
ricardo_95 21:13e4824bc364 264 } else {
ricardo_95 21:13e4824bc364 265 LED_green_state = 0;
ricardo_95 21:13e4824bc364 266 }
ricardo_95 21:13e4824bc364 267
ricardo_95 21:13e4824bc364 268 if (LED_colour == 'g') {
ricardo_95 21:13e4824bc364 269 red_var = 0;
ricardo_95 21:13e4824bc364 270 green_var = (2.55*intensity);
ricardo_95 21:13e4824bc364 271 blue_var = 0;
ricardo_95 21:13e4824bc364 272 LED_green_state = 1;
ricardo_95 21:13e4824bc364 273 } else {
ricardo_95 21:13e4824bc364 274 LED_green_state = 0;
ricardo_95 21:13e4824bc364 275 }
ricardo_95 21:13e4824bc364 276
ricardo_95 28:b4bee068780d 277 if (LED_colour == 'b') {
ricardo_95 28:b4bee068780d 278 red_var = 0;
ricardo_95 28:b4bee068780d 279 green_var = 0;
ricardo_95 28:b4bee068780d 280 blue_var = (2.55*intensity);
ricardo_95 28:b4bee068780d 281 }
ricardo_95 28:b4bee068780d 282
ricardo_95 21:13e4824bc364 283 if (calibration_flash >= 1) {
ricardo_95 21:13e4824bc364 284 if ((calibration_flash % 2) == 0) {
ricardo_95 21:13e4824bc364 285 red_var = 255;
ricardo_95 21:13e4824bc364 286 green_var = 255;
ricardo_95 21:13e4824bc364 287 blue_var = 255;
ricardo_95 26:9e130f7ee829 288 LED_intern4 = 1;
ricardo_95 21:13e4824bc364 289 } else {
ricardo_95 21:13e4824bc364 290 red_var = 0;
ricardo_95 21:13e4824bc364 291 green_var = 0;
ricardo_95 21:13e4824bc364 292 blue_var = 0;
ricardo_95 26:9e130f7ee829 293 LED_intern4 = 0;
ricardo_95 21:13e4824bc364 294 }
ricardo_95 21:13e4824bc364 295 calibration_flash--;
ricardo_95 21:13e4824bc364 296 }
ricardo_95 21:13e4824bc364 297 }
ricardo_95 21:13e4824bc364 298
ricardo_95 21:13e4824bc364 299 void trigger_lock() // If rising edge lock button is detected start locktimer.
ricardo_95 21:13e4824bc364 300 {
ricardo_95 21:13e4824bc364 301 pc.printf("Lock triggered.\n");
ricardo_95 21:13e4824bc364 302 lock_hold_timer.reset();
ricardo_95 21:13e4824bc364 303 lock_hold_timer.start();
ricardo_95 21:13e4824bc364 304 delay.reset();
ricardo_95 21:13e4824bc364 305 delay.start();
ricardo_95 21:13e4824bc364 306 }
ricardo_95 21:13e4824bc364 307
ricardo_95 21:13e4824bc364 308 void timer_lock() // End timer lock.
ricardo_95 21:13e4824bc364 309 {
ricardo_95 21:13e4824bc364 310 lock_flag = 0; // Set lock_flag off.
ricardo_95 21:13e4824bc364 311 lock_hold_timer.stop(); // Stop and reset holdtimer
ricardo_95 21:13e4824bc364 312 lock_hold_timer.reset();
ricardo_95 21:13e4824bc364 313 }
ricardo_95 21:13e4824bc364 314
ricardo_95 21:13e4824bc364 315 void trigger_reposition()
ricardo_95 21:13e4824bc364 316 {
ricardo_95 21:13e4824bc364 317 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 21:13e4824bc364 318 } else {
ricardo_95 21:13e4824bc364 319 delay.reset();
ricardo_95 21:13e4824bc364 320 delay.start();
ricardo_95 21:13e4824bc364 321 pc.printf("Reposition triggered.\n");
ricardo_95 21:13e4824bc364 322 if (LED_intern1 == 0) {
ricardo_95 21:13e4824bc364 323 LED_intern1 = 1;
ricardo_95 21:13e4824bc364 324 } else {
ricardo_95 21:13e4824bc364 325 LED_intern1 = 0;
ricardo_95 21:13e4824bc364 326 }
ricardo_95 21:13e4824bc364 327
ricardo_95 21:13e4824bc364 328 reposition_flag = 1;
ricardo_95 21:13e4824bc364 329
ricardo_95 21:13e4824bc364 330 reposition_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 331 }
ricardo_95 21:13e4824bc364 332 }
ricardo_95 21:13e4824bc364 333
ricardo_95 21:13e4824bc364 334 void rise_reposition()
ricardo_95 21:13e4824bc364 335 {
ricardo_95 21:13e4824bc364 336 reposition_LED = 0;
ricardo_95 21:13e4824bc364 337 }
ricardo_95 21:13e4824bc364 338
ricardo_95 21:13e4824bc364 339 void trigger_mute()
ricardo_95 21:13e4824bc364 340 {
ricardo_95 21:13e4824bc364 341
ricardo_95 21:13e4824bc364 342 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 21:13e4824bc364 343 } else {
ricardo_95 21:13e4824bc364 344 delay.reset();
ricardo_95 21:13e4824bc364 345 delay.start();
ricardo_95 21:13e4824bc364 346 mute_state = !mute_state;
ricardo_95 21:13e4824bc364 347 if (mute_state == 1) {
ricardo_95 21:13e4824bc364 348 mute_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 349 } else {
ricardo_95 21:13e4824bc364 350 mute_LED = 0;
ricardo_95 21:13e4824bc364 351 }
ricardo_95 21:13e4824bc364 352 pc.printf("Mute triggered %d.\n",mute_state);
ricardo_95 21:13e4824bc364 353 if (LED_intern1 == 0) {
ricardo_95 21:13e4824bc364 354 LED_intern1 = 1;
ricardo_95 21:13e4824bc364 355 } else {
ricardo_95 21:13e4824bc364 356 LED_intern1 = 0;
ricardo_95 21:13e4824bc364 357 }
ricardo_95 21:13e4824bc364 358
ricardo_95 21:13e4824bc364 359 mute_flag = 1;
ricardo_95 21:13e4824bc364 360 }
ricardo_95 21:13e4824bc364 361 }
ricardo_95 21:13e4824bc364 362
ricardo_95 21:13e4824bc364 363 void trigger_new_patient() // Function to trigger hold timer for new patient calibration function.
ricardo_95 21:13e4824bc364 364 {
ricardo_95 21:13e4824bc364 365
ricardo_95 21:13e4824bc364 366 if (lock_state == 1) {
ricardo_95 21:13e4824bc364 367 } else {
ricardo_95 21:13e4824bc364 368 calibration_hold_timer.reset();
ricardo_95 21:13e4824bc364 369 calibration_hold_timer.start();
ricardo_95 21:13e4824bc364 370 new_patient_LED = control_LED_intensity;;
ricardo_95 21:13e4824bc364 371 pc.printf("New patient triggered.\n");
ricardo_95 21:13e4824bc364 372 }
ricardo_95 21:13e4824bc364 373 }
ricardo_95 21:13e4824bc364 374
ricardo_95 21:13e4824bc364 375 void timer_calibration() // Timer calibration function.
ricardo_95 21:13e4824bc364 376 {
ricardo_95 21:13e4824bc364 377 new_patient_LED = 0;
ricardo_95 21:13e4824bc364 378
ricardo_95 21:13e4824bc364 379 if (0 < calibration_hold_timer.read_ms() < calibrationtime_ms) {
ricardo_95 21:13e4824bc364 380 new_patient_flag = 1;
ricardo_95 21:13e4824bc364 381 }
ricardo_95 21:13e4824bc364 382
ricardo_95 21:13e4824bc364 383 calibration_hold_timer.stop();
ricardo_95 21:13e4824bc364 384 calibration_hold_timer.reset();
ricardo_95 21:13e4824bc364 385
ricardo_95 21:13e4824bc364 386 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 21:13e4824bc364 387 } else {
ricardo_95 21:13e4824bc364 388 if (calibration_flag == 0) {
ricardo_95 21:13e4824bc364 389
ricardo_95 21:13e4824bc364 390 if (LED_intern1 == 0) {
ricardo_95 21:13e4824bc364 391 LED_intern1 = 1;
ricardo_95 21:13e4824bc364 392 } else {
ricardo_95 21:13e4824bc364 393 LED_intern1 = 0;
ricardo_95 21:13e4824bc364 394 }
ricardo_95 21:13e4824bc364 395
ricardo_95 21:13e4824bc364 396 } else {
ricardo_95 21:13e4824bc364 397 calibration_flag = 0;
ricardo_95 21:13e4824bc364 398 }
ricardo_95 21:13e4824bc364 399 }
ricardo_95 21:13e4824bc364 400 }
ricardo_95 21:13e4824bc364 401
ricardo_95 21:13e4824bc364 402 void timer_functions()
ricardo_95 21:13e4824bc364 403 {
ricardo_95 33:df21cb8dc5c7 404 pc.printf("Locktime = %d\n",lock_hold_timer.read_ms());
ricardo_95 21:13e4824bc364 405 if ((lock_hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && lock == 0) { // If statement for lock function.
ricardo_95 21:13e4824bc364 406 lock_flag = 1;
ricardo_95 21:13e4824bc364 407 LED_intern2 = !LED_intern2;
ricardo_95 21:13e4824bc364 408 lock_state = !lock_state;
ricardo_95 21:13e4824bc364 409 if (lock_state == 0) {
ricardo_95 21:13e4824bc364 410 lock_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 411 } else {
ricardo_95 21:13e4824bc364 412 lock_LED = 0;
ricardo_95 21:13e4824bc364 413 }
ricardo_95 21:13e4824bc364 414 }
ricardo_95 21:13e4824bc364 415
ricardo_95 23:4a09554bdc1a 416 if ((calibration_hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && new_patient == 0 && lock_state == 0) { // If statement for calibration system.
ricardo_95 21:13e4824bc364 417 calibration_flag = 1;
ricardo_95 21:13e4824bc364 418 calibration_flash = 11;
ricardo_95 21:13e4824bc364 419 pc.printf("Calibrate triggered.\n");
ricardo_95 21:13e4824bc364 420 pi.printf(">30\n"); // Print statement for serial communication to inform algorithm to calibrate.
ricardo_95 21:13e4824bc364 421 }
ricardo_95 21:13e4824bc364 422
ricardo_95 21:13e4824bc364 423 if (delay.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active.
ricardo_95 21:13e4824bc364 424 lock_state = 1;
ricardo_95 21:13e4824bc364 425 LED_intern2 = 1;
ricardo_95 21:13e4824bc364 426 lock_LED = 0;
ricardo_95 21:13e4824bc364 427 }
ricardo_95 21:13e4824bc364 428 }
ricardo_95 21:13e4824bc364 429
ricardo_95 21:13e4824bc364 430 void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) // Generate LED colour.
ricardo_95 21:13e4824bc364 431 {
ricardo_95 21:13e4824bc364 432 out->red = red_var;
ricardo_95 21:13e4824bc364 433 out->green = green_var;
ricardo_95 21:13e4824bc364 434 out->blue = blue_var;
ricardo_95 21:13e4824bc364 435 }
ricardo_95 21:13e4824bc364 436
ricardo_95 21:13e4824bc364 437 void set_ui_LED() // Control functions for LED above buttons (added because of failures).
ricardo_95 21:13e4824bc364 438 {
ricardo_95 21:13e4824bc364 439 if (lock_state == 1) {
ricardo_95 21:13e4824bc364 440 } else {
ricardo_95 21:13e4824bc364 441 if (reposition == 0) {
ricardo_95 22:a09775c25890 442 reposition_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 443 } else {
ricardo_95 21:13e4824bc364 444 reposition_LED = 0;
ricardo_95 21:13e4824bc364 445 }
ricardo_95 21:13e4824bc364 446
ricardo_95 21:13e4824bc364 447 if (new_patient == 0) {
ricardo_95 22:a09775c25890 448 new_patient_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 449 } else {
ricardo_95 21:13e4824bc364 450 new_patient_LED = 0;
ricardo_95 21:13e4824bc364 451 }
ricardo_95 21:13e4824bc364 452 }
ricardo_95 21:13e4824bc364 453 }
ricardo_95 21:13e4824bc364 454
ricardo_95 21:13e4824bc364 455 void read_voltage()
ricardo_95 21:13e4824bc364 456 {
ricardo_95 25:96c34634abda 457 if (power_plug_state == 1) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage.
ricardo_95 21:13e4824bc364 458 alarm = 0; // Alarm is off.
ricardo_95 21:13e4824bc364 459 speaker_state = 0;
ricardo_95 21:13e4824bc364 460 } else {
ricardo_95 21:13e4824bc364 461 alarm = 1; // Else alarm is on.
ricardo_95 21:13e4824bc364 462 speaker_state = 1;
ricardo_95 21:13e4824bc364 463 }
ricardo_95 26:9e130f7ee829 464
ricardo_95 26:9e130f7ee829 465
ricardo_95 21:13e4824bc364 466 if (alarm == 1 && mute_state == 1 && (batteryvoltage_current > alarm_voltage)) { // Set speaker on for 750 ms.
ricardo_95 21:13e4824bc364 467 speaker1 = 0; // Set speaker.
ricardo_95 21:13e4824bc364 468 speaker2 = 0;
ricardo_95 21:13e4824bc364 469 }
ricardo_95 21:13e4824bc364 470
ricardo_95 24:782c4dc4a3ff 471 if ((alarm == 1 && mute_state == 0 && (speaker_timer.read_ms() < speaker_active_ms)) || ((batteryvoltage_current < alarm_voltage) && (speaker_timer.read_ms() < speaker_active_ms) && power_plug_state == 0)) { // Set speaker on for 750 ms.
ricardo_95 21:13e4824bc364 472 speaker1 = 1; // Set speaker.
ricardo_95 21:13e4824bc364 473 speaker2 = 1;
ricardo_95 21:13e4824bc364 474 speaker_timer.start(); // Set timer for speaker to iterate on and off.
ricardo_95 21:13e4824bc364 475 }
ricardo_95 21:13e4824bc364 476
ricardo_95 21:13e4824bc364 477 if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) {
ricardo_95 21:13e4824bc364 478 speaker1 = 0; // Turn off speaker (use two outputs because of currentlimiting of one).
ricardo_95 21:13e4824bc364 479 speaker2 = 0;
ricardo_95 21:13e4824bc364 480 }
ricardo_95 21:13e4824bc364 481
ricardo_95 21:13e4824bc364 482 if (speaker_timer.read_ms() > (speaker_active_ms*2)) {
ricardo_95 21:13e4824bc364 483 speaker_timer.stop(); // Stop speaker timer.
ricardo_95 21:13e4824bc364 484 speaker_timer.reset();
ricardo_95 21:13e4824bc364 485 }
ricardo_95 21:13e4824bc364 486
ricardo_95 21:13e4824bc364 487 batteryvoltage_current = adsAccu.readADC_SingleEnded(0); // Read channel 0 from external ADC.
ricardo_95 25:96c34634abda 488 powervoltage_current = adsAccu.readADC_SingleEnded(1); // Read channel 1 from external ADC.
ricardo_95 26:9e130f7ee829 489
ricardo_95 21:13e4824bc364 490 if (powervoltage_current < 20000) {
ricardo_95 21:13e4824bc364 491 power_plug_state = 0;
ricardo_95 28:b4bee068780d 492 LED_colour = 'b';
ricardo_95 21:13e4824bc364 493 } else {
ricardo_95 21:13e4824bc364 494 power_plug_state = 1;
ricardo_95 21:13e4824bc364 495 }
ricardo_95 21:13e4824bc364 496 }
ricardo_95 21:13e4824bc364 497
ricardo_95 21:13e4824bc364 498 void read_adc()
ricardo_95 21:13e4824bc364 499 {
ricardo_95 21:13e4824bc364 500 t.reset();
ricardo_95 21:13e4824bc364 501 t.start();
ricardo_95 28:b4bee068780d 502 a = agu.testConnection();/*
ricardo_95 28:b4bee068780d 503 pc.printf("a= %d\n",a);
ricardo_95 28:b4bee068780d 504 if( a==0)
ricardo_95 28:b4bee068780d 505 {
ricardo_95 28:b4bee068780d 506 lock_state = 1;
ricardo_95 28:b4bee068780d 507 LED_intern2 = 1;
ricardo_95 28:b4bee068780d 508 lock_LED = 0;
ricardo_95 28:b4bee068780d 509 }*/
ricardo_95 28:b4bee068780d 510 if (a == 1) {
ricardo_95 21:13e4824bc364 511 elec[0] = pel.readADC_SingleEnded(0); // First PE readout
ricardo_95 21:13e4824bc364 512
ricardo_95 21:13e4824bc364 513 for (k = 0; k < 4; k = k + 1) {
ricardo_95 21:13e4824bc364 514 res[k] = pr1.readADC_SingleEnded(k); // First 4 PR readout
ricardo_95 21:13e4824bc364 515 }
ricardo_95 21:13e4824bc364 516 while(t.read_us()<(1*(cycle_time/5))) {} // Wait untill 20% of cycle
ricardo_95 21:13e4824bc364 517
ricardo_95 21:13e4824bc364 518 elec[1] = pel.readADC_SingleEnded(0); // Second PE readout
ricardo_95 21:13e4824bc364 519
ricardo_95 21:13e4824bc364 520 for (k = 0; k < 4; k = k + 1) {
ricardo_95 21:13e4824bc364 521 res[k+4] = pr2.readADC_SingleEnded(k); // Last 4 PR readout
ricardo_95 21:13e4824bc364 522 }
ricardo_95 21:13e4824bc364 523 while(t.read_us()<(2*(cycle_time/5))) {} // Wait untill 40% of cycle
ricardo_95 21:13e4824bc364 524
ricardo_95 21:13e4824bc364 525 elec[2] = pel.readADC_SingleEnded(0); // Third PE readout
ricardo_95 21:13e4824bc364 526
ricardo_95 21:13e4824bc364 527 agu.getAccelero(acce); // Get accelerometer data
ricardo_95 26:9e130f7ee829 528 angle = acce[2]*100;
ricardo_95 26:9e130f7ee829 529 if(angle == 0) {
ricardo_95 26:9e130f7ee829 530 MPU6050 agu(p28,p27);
ricardo_95 26:9e130f7ee829 531 agu.getAccelero(acce);
ricardo_95 26:9e130f7ee829 532 angle = acce[2]*100;
ricardo_95 26:9e130f7ee829 533 }
ricardo_95 21:13e4824bc364 534 agu.getGyro(gyro); // Get gyroscope data
ricardo_95 33:df21cb8dc5c7 535
ricardo_95 33:df21cb8dc5c7 536 if (test_belt == 1) {
ricardo_95 33:df21cb8dc5c7 537 agu_belt.getGyro(gyro_belt); // Get gyroscope data from Belt
ricardo_95 33:df21cb8dc5c7 538 agu_belt.getAccelero(acce_belt); // Get accelerometer data from belt
ricardo_95 33:df21cb8dc5c7 539 }
ricardo_95 33:df21cb8dc5c7 540
ricardo_95 21:13e4824bc364 541 while(t.read_us()<(3*(cycle_time/5))) {} // Wait untill 60% of cycle
ricardo_95 21:13e4824bc364 542
ricardo_95 21:13e4824bc364 543 elec[3] = pel.readADC_SingleEnded(0); // Fourth PE readout
ricardo_95 21:13e4824bc364 544 }
ricardo_95 21:13e4824bc364 545
ricardo_95 21:13e4824bc364 546 timer_functions();
ricardo_95 21:13e4824bc364 547
ricardo_95 21:13e4824bc364 548 batteryvoltage_current = batteryvoltage_last;
ricardo_95 21:13e4824bc364 549 powervoltage_current = powervoltage_last;
ricardo_95 21:13e4824bc364 550 read_voltage(); // Supplyvoltage control for alarm.
ricardo_95 21:13e4824bc364 551 pc.printf("Voltage = %d , %d\n", batteryvoltage_current, powervoltage_current);
ricardo_95 21:13e4824bc364 552
ricardo_95 21:13e4824bc364 553 uint32_t val = 0;
ricardo_95 21:13e4824bc364 554 colour_select(LED_colour);
ricardo_95 21:13e4824bc364 555 array.update(generate, NLED, val);
ricardo_95 21:13e4824bc364 556 set_ui_LED();
ricardo_95 21:13e4824bc364 557
ricardo_95 21:13e4824bc364 558 while(t.read_us()<(4*(cycle_time/5))) {} // Wait untill 80% of cycle
ricardo_95 21:13e4824bc364 559
ricardo_95 28:b4bee068780d 560 // pc.printf("2e = %d\n",agu.testConnection());
ricardo_95 28:b4bee068780d 561 if (a == 1) {
ricardo_95 21:13e4824bc364 562 elec[4] = pel.readADC_SingleEnded(0); // Fifth PE readout
ricardo_95 21:13e4824bc364 563 }
ricardo_95 21:13e4824bc364 564
ricardo_95 21:13e4824bc364 565 while(t.read_us()<(4.25*(cycle_time/5))) {} // Wait untill 85% of cycle
ricardo_95 28:b4bee068780d 566
ricardo_95 21:13e4824bc364 567 serial_read();
ricardo_95 21:13e4824bc364 568 serial_log();
ricardo_95 21:13e4824bc364 569 }
ricardo_95 21:13e4824bc364 570
DEldering 0:c0e44c46c573 571 int main()
DEldering 0:c0e44c46c573 572 {
ricardo_95 25:96c34634abda 573 wait_ms(boot_delay_ms); // Wait to boot sensorplate first
DEldering 1:a8e61f3910ad 574 i2c.frequency(i2c_freq);
ricardo_95 21:13e4824bc364 575 i2cAccu.frequency(i2c_freq);
ricardo_95 12:7b3a5940f911 576 pc.baud(baud);
ricardo_95 12:7b3a5940f911 577 pi.baud(baud);
DEldering 1:a8e61f3910ad 578 pr1.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
ricardo_95 17:6ec7d594c1f1 579 pr2.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
DEldering 1:a8e61f3910ad 580 pel.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
ricardo_95 17:6ec7d594c1f1 581 adsAccu.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
ricardo_95 32:0944efc47e46 582 pi.format(8, SerialBase::None, 1);
ricardo_95 7:dba5091c8b7d 583
ricardo_95 21:13e4824bc364 584 lock.fall(&trigger_lock); // Interrupt for rising edge lock button.
ricardo_95 21:13e4824bc364 585 lock.rise(&timer_lock);
ricardo_95 21:13e4824bc364 586 reposition.fall(&trigger_reposition);
ricardo_95 21:13e4824bc364 587 reposition.rise(&rise_reposition);
ricardo_95 21:13e4824bc364 588 mute.fall(&trigger_mute);
ricardo_95 21:13e4824bc364 589 new_patient.fall(&trigger_new_patient); // New patient/calibration button rising event.
ricardo_95 21:13e4824bc364 590 new_patient.rise(&timer_calibration); // Falling edge for calibration algorithm option.
ricardo_95 7:dba5091c8b7d 591 delay.reset(); // Delaytimer reset en start.
ricardo_95 7:dba5091c8b7d 592 delay.start();
ricardo_95 26:9e130f7ee829 593
ricardo_95 22:a09775c25890 594 set_intensity();
ricardo_95 25:96c34634abda 595 lock_LED = control_LED_intensity; // Lock LED initialization.
ricardo_95 21:13e4824bc364 596
ricardo_95 19:3b5999fa7b7e 597 sample_cycle.attach_us(&read_adc, cycle_time);
ricardo_95 19:3b5999fa7b7e 598
DEldering 0:c0e44c46c573 599 while (1) {
DEldering 1:a8e61f3910ad 600 wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms
DEldering 0:c0e44c46c573 601 }
ricardo_95 12:7b3a5940f911 602 }