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 18 14:12:11 2017 +0000
Revision:
36:d10f368d037b
Parent:
35:e9026c40726e
Child:
37:d8f7b2b5719a
Child:
41:ebc527b78b96
Comments added and functions/variables clearer defined.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ricardo_95 36:d10f368d037b 1 /********************* CODE INFORMATON ******************************
ricardo_95 34:1614f4f2b841 2 Date of creation: 30-09-2017
ricardo_95 34:1614f4f2b841 3 Authors: Danny Eldering & Ricardo Molenaar
ricardo_95 33:df21cb8dc5c7 4 co-authors: Menno Gravemaker
ricardo_95 34:1614f4f2b841 5 (c) Copyright by Momo Medical BV.
ricardo_95 34:1614f4f2b841 6
ricardo_95 36:d10f368d037b 7 Current version name: 2.1.3
ricardo_95 36:d10f368d037b 8 Date of modification: 18-10-2017
ricardo_95 36:d10f368d037b 9 Purpose of this file: Code for LPC1768 microcontroller for controlling buttons, LED's and communicate to PI
ricardo_95 36:d10f368d037b 10 Update ‘what’s new in this version?’: New structure added.
ricardo_95 36:d10f368d037b 11 Readability improved.
ricardo_95 36:d10f368d037b 12 Code optimized (variables and functions).
ricardo_95 36:d10f368d037b 13 Todo: -> Fix LED issue (yellow and red flashes at random moments);
ricardo_95 36:d10f368d037b 14 -> Optimize functions / improve readability;
ricardo_95 36:d10f368d037b 15 -> Split functions in seperate files?;
ricardo_95 36:d10f368d037b 16 -> Fix when sensorplate is not connected;
ricardo_95 36:d10f368d037b 17 -> Rule 570: if statement change to turn off LED's when power is plugged out (also related to rule 106).
ricardo_95 36:d10f368d037b 18 -> For the speaker two outputs of the uC are used. Add MOSFET with external supply and control these by uC?
ricardo_95 36:d10f368d037b 19 Source file: http://mbed.com/
ricardo_95 33:df21cb8dc5c7 20
ricardo_95 36:d10f368d037b 21 Information files:
ricardo_95 36:d10f368d037b 22 (1) Flowchart:
ricardo_95 36:d10f368d037b 23 (2) Table serial communication: https://docs.google.com/spreadsheets/d/1kHlithHxtoMDGvbcdH8vwSw5W5ArxlwDPsyfra1dtQM/edit?usp=drive_web
ricardo_95 12:7b3a5940f911 24 */
ricardo_95 12:7b3a5940f911 25
ricardo_95 21:13e4824bc364 26 /************************ CONFIG ***********************************/
ricardo_95 21:13e4824bc364 27
ricardo_95 36:d10f368d037b 28 #include "mbed.h" // Include files and define parameters.
DEldering 0:c0e44c46c573 29 #include "Adafruit_ADS1015.h"
DEldering 0:c0e44c46c573 30 #include "MPU6050.h"
ricardo_95 33:df21cb8dc5c7 31 #include "MPU6050_belt.h"
ricardo_95 7:dba5091c8b7d 32 #include "neopixel.h"
ricardo_95 36:d10f368d037b 33 #define NUMBER_LED_FRONT (3)
ricardo_95 7:dba5091c8b7d 34 #define ONE_COLOR
ricardo_95 7:dba5091c8b7d 35
ricardo_95 36:d10f368d037b 36 InterruptIn button_lock(p15); // Input on intterupt base decleration.
ricardo_95 36:d10f368d037b 37 InterruptIn button_reposition(p17);
ricardo_95 36:d10f368d037b 38 InterruptIn button_mute(p16);
ricardo_95 36:d10f368d037b 39 InterruptIn button_new_patient(p18);
ricardo_95 7:dba5091c8b7d 40
ricardo_95 36:d10f368d037b 41 DigitalOut LED_on_dev_board1(LED1); // Decleration of digital outputs.
ricardo_95 36:d10f368d037b 42 DigitalOut LED_on_dev_board2(LED2);
ricardo_95 36:d10f368d037b 43 DigitalOut LED_on_dev_board3(LED3);
ricardo_95 36:d10f368d037b 44 DigitalOut LED_on_dev_board4(LED4);
ricardo_95 7:dba5091c8b7d 45 DigitalOut speaker1(p21);
ricardo_95 7:dba5091c8b7d 46 DigitalOut speaker2(p22);
ricardo_95 36:d10f368d037b 47 neopixel::PixelArray indicator_LEDs(p11);
ricardo_95 36:d10f368d037b 48
ricardo_95 36:d10f368d037b 49 PwmOut lock_feedback_LED(p23); // Declaration of pulse with modulation outputs.
ricardo_95 36:d10f368d037b 50 PwmOut reposition_feedback_LED(p25);
ricardo_95 36:d10f368d037b 51 PwmOut mute_feedback_LED(p26);
ricardo_95 36:d10f368d037b 52 PwmOut new_patient_feedback_LED(p24);
ricardo_95 36:d10f368d037b 53
ricardo_95 36:d10f368d037b 54 Timer button_lock_hold_timer; //
ricardo_95 36:d10f368d037b 55 Timer button_calibration_hold_timer; // Timer for calibration function (new patient holding 5 seconds).
ricardo_95 36:d10f368d037b 56 Timer delay_between_button_pressed; // Timer for time between two buttons (to prevent pressing buttons simultaneously).
ricardo_95 36:d10f368d037b 57 Timer speaker_timer; // Timer for speaker activation.
ricardo_95 36:d10f368d037b 58 Timer piezo_electric_sample_timer; // Timer for equally time-spaced samples.
ricardo_95 8:bf0f7a6fb1fd 59
ricardo_95 36:d10f368d037b 60 /*
ricardo_95 36:d10f368d037b 61 The code underneath this commentbox has some fixed parameters for serial/ADC reading:
ricardo_95 36:d10f368d037b 62 -> The address for the angle_device_reference_belt is set to 0x68 in the file MPU6050_belt (rule number: 19);
ricardo_95 36:d10f368d037b 63 -> The adress for the angle_device_sensorplate is set to 0x69 in the file MPU6050.h (rule number: 19);
ricardo_95 36:d10f368d037b 64 -> This is because of using the same I2C line;
ricardo_95 36:d10f368d037b 65 -> For detailed information/questions about this item, please read the technical manual or contact: Ricardo Molenaar | ricardo.molenaar@gmail.com
ricardo_95 36:d10f368d037b 66 */
ricardo_95 36:d10f368d037b 67 I2C i2c_sensorplate_adc(p28, p27); // I2C for sensorplate.
ricardo_95 36:d10f368d037b 68 I2C i2c_power_adc(p9, p10); // I2C for accupack.
ricardo_95 36:d10f368d037b 69 MPU6050 angle_device_sensorplate(p28,p27); // i2c pins // i2c address hardcoded 0x68.
ricardo_95 36:d10f368d037b 70 MPU6050_belt angle_device_reference_belt(p28,p27); // i2c pins // i2c address hardcoded 0x69.
ricardo_95 36:d10f368d037b 71 Adafruit_ADS1115 piezo_resistive_adc1(&i2c_sensorplate_adc, 0x48); // i2c pins, i2c address.
ricardo_95 36:d10f368d037b 72 Adafruit_ADS1115 piezo_resistive_adc2(&i2c_sensorplate_adc, 0x49); // i2c pins, i2c address.
ricardo_95 36:d10f368d037b 73 Adafruit_ADS1115 piezo_electric_adc(&i2c_sensorplate_adc, 0x4B); // i2c pins, i2c address.
ricardo_95 36:d10f368d037b 74 Adafruit_ADS1115 adsAccu(&i2c_power_adc, 0x48); // i2c pins, i2c address.
ricardo_95 36:d10f368d037b 75 Serial usb_serial(USBTX, USBRX); // tx, rx
ricardo_95 36:d10f368d037b 76 Serial pi_serial(p13, p14); // tx, rx
ricardo_95 36:d10f368d037b 77 Ticker total_readout_cycle; // Polling cycle.
ricardo_95 36:d10f368d037b 78 // End of commentbox related to the serial configuration/ADC reading components.
ricardo_95 34:1614f4f2b841 79
ricardo_95 12:7b3a5940f911 80 int boot_delay_ms = 500;
ricardo_95 36:d10f368d037b 81 int total_readout_cycle_time_us = 100000; // Cycle time in us.
ricardo_95 36:d10f368d037b 82 int i2c_freq = 400000; // I2C Frequency.
ricardo_95 36:d10f368d037b 83 int baud_rate = 115200; // Baud rate.
ricardo_95 36:d10f368d037b 84 short piezo_resistive_array[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle.
ricardo_95 36:d10f368d037b 85 short piezo_electric_array[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle.
ricardo_95 36:d10f368d037b 86 int angle = 0; // Accelerometer Z-axis.
DEldering 1:a8e61f3910ad 87 int k = 0;
ricardo_95 36:d10f368d037b 88 float accelerometer_sensorplate[3]; // Raw accelerometer data.
ricardo_95 36:d10f368d037b 89 float gyroscope_sensorplate[3]; // Raw gyroscope data.
ricardo_95 36:d10f368d037b 90 float accelerometer_reference_belt[3]; // Raw accelerometer data from belt.
ricardo_95 36:d10f368d037b 91 float gyroscope_reference_belt[3]; // Raw gyroscope data from belt.
ricardo_95 36:d10f368d037b 92 char LED_colour = 'g'; // Variable to set LED colour (standard set to green, untill PI sends other character).
ricardo_95 36:d10f368d037b 93 bool lock_state = 0, lock_flag = 0, mute_state = 0, alarm = 0, calibration_flag = 0, intensity_select = 1; // Boolean variables for logging states.
ricardo_95 36:d10f368d037b 94 bool mute_flag = 0, new_patient_flag = 0, reposition_flag = 0; // Flag variables.
ricardo_95 21:13e4824bc364 95 bool speaker_state = 0, LED_red_state = 0, LED_yellow_state = 0, LED_green_state = 0, power_plug_state = 0;
ricardo_95 21:13e4824bc364 96 bool speaker_logged = 0, LED_red_logged = 0, LED_yellow_logged = 0, LED_green_logged = 0, power_plug_logged = 0;
ricardo_95 36:d10f368d037b 97 int locktime_ms = 2000; // Waittime for lock user interface in ms.
ricardo_95 36:d10f368d037b 98 int calibrationtime_ms = 5000; // Time to press new_patient button for calibration system.
ricardo_95 36:d10f368d037b 99 int calibration_flash; // Variable for flash LED's to indicate calibration.
ricardo_95 36:d10f368d037b 100 int buttondelay_ms = 750; // Button delay in ms.
ricardo_95 36:d10f368d037b 101 int delay_lock_interface = 3000*60; // Delay for non using interface locktime.
ricardo_95 36:d10f368d037b 102 int speaker_active_ms = 750; // Time to iterate speaker on and off when alarm occurs.
ricardo_95 36:d10f368d037b 103 int alarm_voltage = 5867; // Needed voltage for alarm expressed as a digital 15 bit value (=20% of max battery voltage).
ricardo_95 36:d10f368d037b 104 int LED_red_intensity, LED_blue_intensity, LED_green_intensity; // Variables to set LED intensity.
ricardo_95 21:13e4824bc364 105 short batteryvoltage_current = 0, batteryvoltage_last = 0, powervoltage_current, powervoltage_last; // Variables to manage batteryvoltage.
ricardo_95 36:d10f368d037b 106 int digital_value_ADC_powervoltage_unplugged = 20000; // Digital value to set the indicating LEDs to wall blue (should be set off later).
ricardo_95 36:d10f368d037b 107 int intensity_day = 40, intensity_night = 10; // Intensity settings for LED's to wall.
ricardo_95 36:d10f368d037b 108 double intensity, control_LED_intensity = 0; // Variable between 0 and 1 to set the intensity of the LED's above the buttons.
ricardo_95 21:13e4824bc364 109
ricardo_95 33:df21cb8dc5c7 110 /*************************** TEST ********************************/
ricardo_95 36:d10f368d037b 111 // Verify algoritm function: for belt activation, set test_belt 1 (connect pin p20 to 3.3V).
ricardo_95 36:d10f368d037b 112 Timer test_timer;
ricardo_95 34:1614f4f2b841 113 DigitalIn test_pin(p30, PullDown);
ricardo_95 33:df21cb8dc5c7 114
ricardo_95 36:d10f368d037b 115 // Variable to set if belt is used to test algorithm:
ricardo_95 36:d10f368d037b 116 bool test_belt = 0;
ricardo_95 33:df21cb8dc5c7 117
ricardo_95 36:d10f368d037b 118 // Set test mode on (log functions to pc serial: interrupts, LED intensity and serial messages):
ricardo_95 34:1614f4f2b841 119 bool test_mode = 0;
ricardo_95 34:1614f4f2b841 120
ricardo_95 36:d10f368d037b 121 // Variable for connection test (should be changed):
ricardo_95 36:d10f368d037b 122 int connection_test_sensorplate;
ricardo_95 33:df21cb8dc5c7 123
ricardo_95 21:13e4824bc364 124 /*************************** CODE ********************************/
ricardo_95 7:dba5091c8b7d 125
ricardo_95 36:d10f368d037b 126 void set_intensity_LEDs() // Function to set the intensity for the LED's.
ricardo_95 7:dba5091c8b7d 127 {
ricardo_95 11:73c6def38fbd 128 if (intensity_select == 1) {
ricardo_95 12:7b3a5940f911 129 intensity = intensity_day;
ricardo_95 12:7b3a5940f911 130 } else {
ricardo_95 12:7b3a5940f911 131 intensity = intensity_night;
ricardo_95 7:dba5091c8b7d 132 }
ricardo_95 21:13e4824bc364 133 control_LED_intensity = (intensity/100);
ricardo_95 34:1614f4f2b841 134
ricardo_95 36:d10f368d037b 135 if (test_mode == 1) { // If statement for test purposal LED_intensity values.
ricardo_95 36:d10f368d037b 136 usb_serial.printf("Intensity LED's shines to wall = %f\n", intensity);
ricardo_95 36:d10f368d037b 137 usb_serial.printf("Intensity LED's above buttons = %f\n", control_LED_intensity);
ricardo_95 33:df21cb8dc5c7 138 }
ricardo_95 7:dba5091c8b7d 139 }
ricardo_95 7:dba5091c8b7d 140
ricardo_95 36:d10f368d037b 141 void serial_read() // Function for serial read for select LED intensity and colour.
ricardo_95 12:7b3a5940f911 142 {
ricardo_95 36:d10f368d037b 143 if (pi_serial.readable()) { // Function to check if pi is readable.
ricardo_95 34:1614f4f2b841 144 char message[10];
ricardo_95 36:d10f368d037b 145 pi_serial.scanf("%s", message);
ricardo_95 33:df21cb8dc5c7 146
ricardo_95 36:d10f368d037b 147 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 148 usb_serial.printf("Message = %s, Intensity_select = %d en LED_colour = %c\n", message, intensity_select, LED_colour);
ricardo_95 36:d10f368d037b 149 }
ricardo_95 36:d10f368d037b 150
ricardo_95 36:d10f368d037b 151 if (intensity_select != (message[0]-'0')) { // Read intensity for LED's variable from PI.
ricardo_95 34:1614f4f2b841 152 intensity_select = (message[0]-'0');
ricardo_95 34:1614f4f2b841 153 }
ricardo_95 34:1614f4f2b841 154
ricardo_95 36:d10f368d037b 155 if (LED_colour != message[1]) { // Read character from PI to set LED_colour.
ricardo_95 34:1614f4f2b841 156 LED_colour = message[1];
ricardo_95 34:1614f4f2b841 157 }
ricardo_95 34:1614f4f2b841 158
ricardo_95 36:d10f368d037b 159 usb_serial.printf("Intensity_select = %d en LED_colour = %c\n", intensity_select, LED_colour);
ricardo_95 34:1614f4f2b841 160
ricardo_95 33:df21cb8dc5c7 161 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 162 usb_serial.printf("Message: %s\n", message);
ricardo_95 11:73c6def38fbd 163 }
ricardo_95 11:73c6def38fbd 164 }
ricardo_95 11:73c6def38fbd 165 }
ricardo_95 11:73c6def38fbd 166
ricardo_95 36:d10f368d037b 167 void serial_log() // Function for serial logging. See link to table with code declarations above in code.
ricardo_95 7:dba5091c8b7d 168 {
ricardo_95 36:d10f368d037b 169 if (mute_flag == 1) { // If statement to control logging for mute button.
ricardo_95 36:d10f368d037b 170 pi_serial.printf(">01\n");
ricardo_95 34:1614f4f2b841 171
ricardo_95 36:d10f368d037b 172 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 173 usb_serial.printf(">01\n");
ricardo_95 33:df21cb8dc5c7 174 }
ricardo_95 34:1614f4f2b841 175
ricardo_95 7:dba5091c8b7d 176 mute_flag = 0;
ricardo_95 19:3b5999fa7b7e 177 }
ricardo_95 12:7b3a5940f911 178
ricardo_95 36:d10f368d037b 179 if (new_patient_flag == 1) { // If statement to control logging for new patient button.
ricardo_95 36:d10f368d037b 180 pi_serial.printf(">03\n");
ricardo_95 34:1614f4f2b841 181
ricardo_95 36:d10f368d037b 182 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 183 usb_serial.printf(">03\n");
ricardo_95 33:df21cb8dc5c7 184 }
ricardo_95 34:1614f4f2b841 185
ricardo_95 7:dba5091c8b7d 186 new_patient_flag = 0;
ricardo_95 7:dba5091c8b7d 187 }
ricardo_95 12:7b3a5940f911 188
ricardo_95 36:d10f368d037b 189 if (reposition_flag == 1) { // If statement to control logging for reposition button.
ricardo_95 36:d10f368d037b 190 pi_serial.printf(">02\n");
ricardo_95 34:1614f4f2b841 191
ricardo_95 36:d10f368d037b 192 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 193 usb_serial.printf(">02\n");
ricardo_95 33:df21cb8dc5c7 194 }
ricardo_95 34:1614f4f2b841 195
ricardo_95 7:dba5091c8b7d 196 reposition_flag = 0;
ricardo_95 7:dba5091c8b7d 197 }
ricardo_95 12:7b3a5940f911 198
ricardo_95 36:d10f368d037b 199 if (batteryvoltage_current != batteryvoltage_last) { // If statement to control logging for batteryvoltage.
ricardo_95 36:d10f368d037b 200 pi_serial.printf("%%" "%d\n", batteryvoltage_current);
ricardo_95 34:1614f4f2b841 201
ricardo_95 36:d10f368d037b 202 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 203 usb_serial.printf("%%" "%d\n", batteryvoltage_current);
ricardo_95 33:df21cb8dc5c7 204 }
ricardo_95 34:1614f4f2b841 205
ricardo_95 16:adbbac0c79f9 206 batteryvoltage_last = batteryvoltage_current;
ricardo_95 12:7b3a5940f911 207 }
ricardo_95 12:7b3a5940f911 208
ricardo_95 36:d10f368d037b 209 if (LED_red_logged != LED_red_state) { // If statement to control logging for LED_red.
ricardo_95 12:7b3a5940f911 210 if (LED_red_state == 1) {
ricardo_95 36:d10f368d037b 211 pi_serial.printf("&04\n");
ricardo_95 9:514a44bf510f 212 LED_red_logged = LED_red_state;
ricardo_95 34:1614f4f2b841 213 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 214 usb_serial.printf("&04\n");
ricardo_95 34:1614f4f2b841 215 }
ricardo_95 12:7b3a5940f911 216 }
ricardo_95 36:d10f368d037b 217
ricardo_95 9:514a44bf510f 218 if (LED_red_state == 0) {
ricardo_95 36:d10f368d037b 219 pi_serial.printf("&40\n");
ricardo_95 12:7b3a5940f911 220 LED_red_logged = LED_red_state;
ricardo_95 34:1614f4f2b841 221 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 222 usb_serial.printf("&40\n");
ricardo_95 34:1614f4f2b841 223 }
ricardo_95 9:514a44bf510f 224 }
ricardo_95 8:bf0f7a6fb1fd 225 }
ricardo_95 12:7b3a5940f911 226
ricardo_95 36:d10f368d037b 227 if (LED_yellow_logged != LED_yellow_state) { // If statement to control logging for LED_yellow.
ricardo_95 36:d10f368d037b 228 if (LED_yellow_state == 1) {
ricardo_95 36:d10f368d037b 229 pi_serial.printf("&06\n");
ricardo_95 9:514a44bf510f 230 LED_yellow_logged = LED_yellow_state;
ricardo_95 34:1614f4f2b841 231 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 232 usb_serial.printf("&06\n");
ricardo_95 34:1614f4f2b841 233 }
ricardo_95 12:7b3a5940f911 234 }
ricardo_95 9:514a44bf510f 235 if (LED_yellow_state == 0) {
ricardo_95 36:d10f368d037b 236 pi_serial.printf("&60\n");
ricardo_95 12:7b3a5940f911 237 LED_yellow_logged = LED_yellow_state;
ricardo_95 34:1614f4f2b841 238 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 239 usb_serial.printf("&60\n");
ricardo_95 34:1614f4f2b841 240 }
ricardo_95 9:514a44bf510f 241 }
ricardo_95 7:dba5091c8b7d 242 }
ricardo_95 12:7b3a5940f911 243
ricardo_95 36:d10f368d037b 244 if (LED_green_logged != LED_green_state) { // If statement to control logging for LED_green.
ricardo_95 12:7b3a5940f911 245 if (LED_green_state == 1) {
ricardo_95 36:d10f368d037b 246 pi_serial.printf("&05\n");
ricardo_95 9:514a44bf510f 247 LED_green_logged = LED_green_state;
ricardo_95 36:d10f368d037b 248
ricardo_95 34:1614f4f2b841 249 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 250 usb_serial.printf("&05\n");
ricardo_95 34:1614f4f2b841 251 }
ricardo_95 12:7b3a5940f911 252 }
ricardo_95 36:d10f368d037b 253
ricardo_95 9:514a44bf510f 254 if (LED_green_state == 0) {
ricardo_95 36:d10f368d037b 255 pi_serial.printf("&50\n");
ricardo_95 12:7b3a5940f911 256 LED_green_logged = LED_green_state;
ricardo_95 36:d10f368d037b 257
ricardo_95 34:1614f4f2b841 258 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 259 usb_serial.printf("&50\n");
ricardo_95 34:1614f4f2b841 260 }
ricardo_95 9:514a44bf510f 261 }
ricardo_95 7:dba5091c8b7d 262 }
ricardo_95 12:7b3a5940f911 263
ricardo_95 36:d10f368d037b 264 if (speaker_logged != speaker_state) { // If statement to control logging for speaker.
ricardo_95 12:7b3a5940f911 265 if (speaker_state == 1) {
ricardo_95 36:d10f368d037b 266 pi_serial.printf("&07\n");
ricardo_95 9:514a44bf510f 267 speaker_logged = speaker_state;
ricardo_95 36:d10f368d037b 268
ricardo_95 36:d10f368d037b 269 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 270 usb_serial.printf("&07\n");
ricardo_95 34:1614f4f2b841 271 }
ricardo_95 12:7b3a5940f911 272 }
ricardo_95 36:d10f368d037b 273
ricardo_95 9:514a44bf510f 274 if (speaker_state == 0) {
ricardo_95 36:d10f368d037b 275 pi_serial.printf("&70\n");
ricardo_95 12:7b3a5940f911 276 speaker_logged = speaker_state;
ricardo_95 36:d10f368d037b 277
ricardo_95 36:d10f368d037b 278 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 279 usb_serial.printf("&70\n");
ricardo_95 34:1614f4f2b841 280 }
ricardo_95 9:514a44bf510f 281 }
ricardo_95 8:bf0f7a6fb1fd 282 }
ricardo_95 9:514a44bf510f 283
ricardo_95 36:d10f368d037b 284 if (power_plug_logged != power_plug_state) { // If statement to control the logging for the state of the power plug.
ricardo_95 12:7b3a5940f911 285 if (power_plug_state == 1) {
ricardo_95 36:d10f368d037b 286 pi_serial.printf("#08\n");
ricardo_95 36:d10f368d037b 287
ricardo_95 36:d10f368d037b 288 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 289 usb_serial.printf("#08\n");
ricardo_95 34:1614f4f2b841 290 }
ricardo_95 9:514a44bf510f 291 power_plug_logged = power_plug_state;
ricardo_95 12:7b3a5940f911 292 }
ricardo_95 36:d10f368d037b 293
ricardo_95 9:514a44bf510f 294 if (power_plug_state == 0) {
ricardo_95 36:d10f368d037b 295 pi_serial.printf("#80\n");
ricardo_95 36:d10f368d037b 296
ricardo_95 36:d10f368d037b 297 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 298 usb_serial.printf("#80\n");
ricardo_95 34:1614f4f2b841 299 }
ricardo_95 12:7b3a5940f911 300 power_plug_logged = power_plug_state;
ricardo_95 9:514a44bf510f 301 }
ricardo_95 9:514a44bf510f 302 }
ricardo_95 36:d10f368d037b 303
ricardo_95 36:d10f368d037b 304 if (connection_test_sensorplate == 1) { // If statement for sending serial information sensorplate data when connection test is active.
ricardo_95 36:d10f368d037b 305 // Receiving order sensor information: 8 resistive sensors, 5 electric readings. Is splitted in two parts - part 1/2.
ricardo_95 36:d10f368d037b 306 pi_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[4], piezo_resistive_array[7], piezo_resistive_array[6], piezo_resistive_array[5], piezo_resistive_array[1], piezo_resistive_array[0], piezo_resistive_array[2], piezo_resistive_array[3], piezo_electric_array[0], piezo_electric_array[1], piezo_electric_array[2], piezo_electric_array[3], piezo_electric_array[4]); // print all to serial port
ricardo_95 36:d10f368d037b 307
ricardo_95 34:1614f4f2b841 308 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 309 usb_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[4], piezo_resistive_array[7], piezo_resistive_array[6], piezo_resistive_array[5], piezo_resistive_array[1], piezo_resistive_array[0], piezo_resistive_array[2], piezo_resistive_array[3], piezo_electric_array[0], piezo_electric_array[1], piezo_electric_array[2], piezo_electric_array[3], piezo_electric_array[4]); // print all to serial port
ricardo_95 34:1614f4f2b841 310 }
ricardo_95 28:b4bee068780d 311 }
ricardo_95 34:1614f4f2b841 312
DEldering 0:c0e44c46c573 313 }
DEldering 0:c0e44c46c573 314
ricardo_95 36:d10f368d037b 315 void colour_select_indicating_LED_wall(char LED_colour) // Function to select the colour for LED's to wall (values comes from algorithm).
ricardo_95 21:13e4824bc364 316 {
ricardo_95 36:d10f368d037b 317 set_intensity_LEDs(); // Call function set_intensity_LEDs to set the intensity for LED's to wall and above buttons.
ricardo_95 34:1614f4f2b841 318
ricardo_95 36:d10f368d037b 319 if ((LED_colour == 'r') || (LED_colour == 'g') || (LED_colour == 'b') || (LED_colour == 'y')) { // If statement to prevent potential errors in communication.
ricardo_95 36:d10f368d037b 320 LED_red_intensity = 0; // Reset
ricardo_95 36:d10f368d037b 321 LED_green_intensity = 0;
ricardo_95 36:d10f368d037b 322 LED_blue_intensity = 0;
ricardo_95 34:1614f4f2b841 323
ricardo_95 36:d10f368d037b 324 if (LED_colour == 'r') { // Set LED_colour to red.
ricardo_95 36:d10f368d037b 325 LED_red_intensity = (2.55*intensity); // 255 / 100 = 2.55 (8 - bit digital value; 0-255 = 256 steps); intensity is a value between 0 and 100.
ricardo_95 36:d10f368d037b 326 LED_green_intensity = 0;
ricardo_95 36:d10f368d037b 327 LED_blue_intensity = 0;
ricardo_95 34:1614f4f2b841 328 LED_red_state = 1;
ricardo_95 34:1614f4f2b841 329 } else {
ricardo_95 34:1614f4f2b841 330 LED_red_state = 0;
ricardo_95 34:1614f4f2b841 331 }
ricardo_95 34:1614f4f2b841 332
ricardo_95 36:d10f368d037b 333 if (LED_colour == 'y') { // Set LED_colour to yellow.
ricardo_95 36:d10f368d037b 334 LED_red_intensity = (2.55*intensity);
ricardo_95 36:d10f368d037b 335 LED_green_intensity = (2.55*intensity);
ricardo_95 36:d10f368d037b 336 LED_blue_intensity = 0;
ricardo_95 34:1614f4f2b841 337 LED_yellow_state = 1;
ricardo_95 34:1614f4f2b841 338 } else {
ricardo_95 34:1614f4f2b841 339 LED_green_state = 0;
ricardo_95 34:1614f4f2b841 340 }
ricardo_95 34:1614f4f2b841 341
ricardo_95 36:d10f368d037b 342 if (LED_colour == 'g') { // Set LED_colour to green.
ricardo_95 36:d10f368d037b 343 LED_red_intensity = 0;
ricardo_95 36:d10f368d037b 344 LED_green_intensity = (2.55*intensity);
ricardo_95 36:d10f368d037b 345 LED_blue_intensity = 0;
ricardo_95 34:1614f4f2b841 346 LED_green_state = 1;
ricardo_95 34:1614f4f2b841 347 } else {
ricardo_95 34:1614f4f2b841 348 LED_green_state = 0;
ricardo_95 34:1614f4f2b841 349 }
ricardo_95 34:1614f4f2b841 350
ricardo_95 36:d10f368d037b 351 if (LED_colour == 'b') { // Set LED_colour to blue.
ricardo_95 36:d10f368d037b 352 LED_red_intensity = 0;
ricardo_95 36:d10f368d037b 353 LED_green_intensity = 0;
ricardo_95 36:d10f368d037b 354 LED_blue_intensity = (2.55*intensity);
ricardo_95 34:1614f4f2b841 355 }
ricardo_95 28:b4bee068780d 356 }
ricardo_95 28:b4bee068780d 357
ricardo_95 36:d10f368d037b 358 if (calibration_flash >= 1) { // If statement for flashing LED's (colour = white) when calibration is active.
ricardo_95 36:d10f368d037b 359 if ((calibration_flash % 2) == 0) { // If value can not be devided by two, set LED's on.
ricardo_95 36:d10f368d037b 360 LED_red_intensity = 255;
ricardo_95 36:d10f368d037b 361 LED_green_intensity = 255;
ricardo_95 36:d10f368d037b 362 LED_blue_intensity = 255;
ricardo_95 36:d10f368d037b 363 LED_on_dev_board4 = 1;
ricardo_95 36:d10f368d037b 364 } else { // Else set LED's off.
ricardo_95 36:d10f368d037b 365 LED_red_intensity = 0;
ricardo_95 36:d10f368d037b 366 LED_green_intensity = 0;
ricardo_95 36:d10f368d037b 367 LED_blue_intensity = 0;
ricardo_95 36:d10f368d037b 368 LED_on_dev_board4 = 0;
ricardo_95 21:13e4824bc364 369 }
ricardo_95 21:13e4824bc364 370 calibration_flash--;
ricardo_95 21:13e4824bc364 371 }
ricardo_95 21:13e4824bc364 372 }
ricardo_95 21:13e4824bc364 373
ricardo_95 36:d10f368d037b 374 void trigger_lock() // If rising edge lock button is detected start locktimer.
ricardo_95 21:13e4824bc364 375 {
ricardo_95 34:1614f4f2b841 376 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 377 usb_serial.printf("Lock triggered.\n");
ricardo_95 34:1614f4f2b841 378 }
ricardo_95 36:d10f368d037b 379
ricardo_95 36:d10f368d037b 380 button_lock_hold_timer.reset();
ricardo_95 36:d10f368d037b 381 button_lock_hold_timer.start();
ricardo_95 36:d10f368d037b 382 delay_between_button_pressed.reset();
ricardo_95 36:d10f368d037b 383 delay_between_button_pressed.start();
ricardo_95 21:13e4824bc364 384 }
ricardo_95 21:13e4824bc364 385
ricardo_95 36:d10f368d037b 386 void end_timer_lock_button() // End timer lock.
ricardo_95 21:13e4824bc364 387 {
ricardo_95 36:d10f368d037b 388 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 389 usb_serial.printf("Lock released.\n");
ricardo_95 36:d10f368d037b 390 }
ricardo_95 36:d10f368d037b 391 lock_flag = 0; // Set lock_flag off.
ricardo_95 36:d10f368d037b 392 button_lock_hold_timer.stop(); // Stop and reset holdtimer
ricardo_95 36:d10f368d037b 393 button_lock_hold_timer.reset();
ricardo_95 36:d10f368d037b 394 }
ricardo_95 36:d10f368d037b 395
ricardo_95 36:d10f368d037b 396 void reposition_button_triggered()
ricardo_95 36:d10f368d037b 397 {
ricardo_95 36:d10f368d037b 398 if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time.
ricardo_95 21:13e4824bc364 399 } else {
ricardo_95 36:d10f368d037b 400 delay_between_button_pressed.reset();
ricardo_95 36:d10f368d037b 401 delay_between_button_pressed.start();
ricardo_95 36:d10f368d037b 402 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 403 usb_serial.printf("Reposition triggered.\n");
ricardo_95 36:d10f368d037b 404 LED_on_dev_board1 = !LED_on_dev_board1;
ricardo_95 21:13e4824bc364 405 }
ricardo_95 21:13e4824bc364 406 reposition_flag = 1;
ricardo_95 21:13e4824bc364 407
ricardo_95 36:d10f368d037b 408 reposition_feedback_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 409 }
ricardo_95 21:13e4824bc364 410 }
ricardo_95 21:13e4824bc364 411
ricardo_95 36:d10f368d037b 412 void rise_reposition() // Interrupt for rising edge reposition function (deactivation; active low).
ricardo_95 21:13e4824bc364 413 {
ricardo_95 36:d10f368d037b 414 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 415 usb_serial.printf("Reposition released.\n");
ricardo_95 34:1614f4f2b841 416 }
ricardo_95 36:d10f368d037b 417 reposition_feedback_LED = 0;
ricardo_95 34:1614f4f2b841 418
ricardo_95 21:13e4824bc364 419 }
ricardo_95 21:13e4824bc364 420
ricardo_95 36:d10f368d037b 421 void mute_button_triggered()
ricardo_95 21:13e4824bc364 422 {
ricardo_95 21:13e4824bc364 423
ricardo_95 36:d10f368d037b 424 if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time.
ricardo_95 21:13e4824bc364 425 } else {
ricardo_95 36:d10f368d037b 426 delay_between_button_pressed.reset();
ricardo_95 36:d10f368d037b 427 delay_between_button_pressed.start();
ricardo_95 21:13e4824bc364 428 mute_state = !mute_state;
ricardo_95 36:d10f368d037b 429
ricardo_95 36:d10f368d037b 430 if (mute_state == 1) { // If statement for if mute_state is active, set mute feedback LED active.
ricardo_95 36:d10f368d037b 431 mute_feedback_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 432 } else {
ricardo_95 36:d10f368d037b 433 mute_feedback_LED = 0;
ricardo_95 21:13e4824bc364 434 }
ricardo_95 36:d10f368d037b 435
ricardo_95 36:d10f368d037b 436 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 437 usb_serial.printf("Mute triggered %d.\n",mute_state);
ricardo_95 36:d10f368d037b 438 LED_on_dev_board1 = !LED_on_dev_board1;
ricardo_95 21:13e4824bc364 439 }
ricardo_95 21:13e4824bc364 440
ricardo_95 21:13e4824bc364 441 mute_flag = 1;
ricardo_95 21:13e4824bc364 442 }
ricardo_95 21:13e4824bc364 443 }
ricardo_95 21:13e4824bc364 444
ricardo_95 36:d10f368d037b 445 void trigger_new_patient() // Function to trigger hold timer for new patient and calibration function.
ricardo_95 21:13e4824bc364 446 {
ricardo_95 21:13e4824bc364 447
ricardo_95 36:d10f368d037b 448 if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) {
ricardo_95 21:13e4824bc364 449 } else {
ricardo_95 36:d10f368d037b 450 button_calibration_hold_timer.reset();
ricardo_95 36:d10f368d037b 451 button_calibration_hold_timer.start();
ricardo_95 36:d10f368d037b 452 new_patient_feedback_LED = control_LED_intensity;;
ricardo_95 36:d10f368d037b 453
ricardo_95 36:d10f368d037b 454 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 455 usb_serial.printf("New patient triggered.\n");
ricardo_95 34:1614f4f2b841 456 }
ricardo_95 21:13e4824bc364 457 }
ricardo_95 21:13e4824bc364 458 }
ricardo_95 21:13e4824bc364 459
ricardo_95 36:d10f368d037b 460 void activate_new_patient_function() // Timer calibration function.
ricardo_95 21:13e4824bc364 461 {
ricardo_95 36:d10f368d037b 462 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 463 usb_serial.printf("New patient released.\n");
ricardo_95 34:1614f4f2b841 464 }
ricardo_95 36:d10f368d037b 465 new_patient_feedback_LED = 0;
ricardo_95 21:13e4824bc364 466
ricardo_95 36:d10f368d037b 467 if (0 < button_calibration_hold_timer.read_ms() < calibrationtime_ms) { // If statement for new_patient function: holdtime for calibration is les then set time to calibrate algorithm.
ricardo_95 21:13e4824bc364 468 new_patient_flag = 1;
ricardo_95 21:13e4824bc364 469 }
ricardo_95 21:13e4824bc364 470
ricardo_95 36:d10f368d037b 471 button_calibration_hold_timer.stop(); // Timer reset for calibration function of new patient button.
ricardo_95 36:d10f368d037b 472 button_calibration_hold_timer.reset();
ricardo_95 21:13e4824bc364 473
ricardo_95 36:d10f368d037b 474 if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time.
ricardo_95 21:13e4824bc364 475 } else {
ricardo_95 21:13e4824bc364 476 if (calibration_flag == 0) {
ricardo_95 21:13e4824bc364 477
ricardo_95 36:d10f368d037b 478 if (LED_on_dev_board1 == 0) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 479 LED_on_dev_board1 = 1;
ricardo_95 21:13e4824bc364 480 } else {
ricardo_95 36:d10f368d037b 481 LED_on_dev_board1 = 0;
ricardo_95 21:13e4824bc364 482 }
ricardo_95 21:13e4824bc364 483
ricardo_95 21:13e4824bc364 484 } else {
ricardo_95 21:13e4824bc364 485 calibration_flag = 0;
ricardo_95 21:13e4824bc364 486 }
ricardo_95 21:13e4824bc364 487 }
ricardo_95 21:13e4824bc364 488 }
ricardo_95 21:13e4824bc364 489
ricardo_95 36:d10f368d037b 490 void timer_functions() // Function which contains statements using timers.
ricardo_95 21:13e4824bc364 491 {
ricardo_95 36:d10f368d037b 492 if ((button_lock_hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && button_lock == 0) { // If statement for lock function.
ricardo_95 21:13e4824bc364 493 lock_flag = 1;
ricardo_95 36:d10f368d037b 494 LED_on_dev_board2 = !LED_on_dev_board2;
ricardo_95 21:13e4824bc364 495 lock_state = !lock_state;
ricardo_95 36:d10f368d037b 496
ricardo_95 36:d10f368d037b 497 if (lock_state == 0) { // If statement to control lock feedback LED above button.
ricardo_95 36:d10f368d037b 498 lock_feedback_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 499 } else {
ricardo_95 36:d10f368d037b 500 lock_feedback_LED = 0;
ricardo_95 21:13e4824bc364 501 }
ricardo_95 21:13e4824bc364 502 }
ricardo_95 21:13e4824bc364 503
ricardo_95 36:d10f368d037b 504 if ((button_calibration_hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && button_new_patient == 0 && lock_state == 0) { // If statement for calibration algorithm.
ricardo_95 21:13e4824bc364 505 calibration_flag = 1;
ricardo_95 21:13e4824bc364 506 calibration_flash = 11;
ricardo_95 36:d10f368d037b 507
ricardo_95 36:d10f368d037b 508 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 509 usb_serial.printf("Calibrate triggered.\n");
ricardo_95 34:1614f4f2b841 510 }
ricardo_95 36:d10f368d037b 511
ricardo_95 36:d10f368d037b 512 pi_serial.printf(">30\n"); // Print statement for serial communication to inform algorithm to calibrate.
ricardo_95 21:13e4824bc364 513 }
ricardo_95 21:13e4824bc364 514
ricardo_95 36:d10f368d037b 515 if (delay_between_button_pressed.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active.
ricardo_95 21:13e4824bc364 516 lock_state = 1;
ricardo_95 36:d10f368d037b 517 LED_on_dev_board2 = 1;
ricardo_95 36:d10f368d037b 518 lock_feedback_LED = 0;
ricardo_95 21:13e4824bc364 519 }
ricardo_95 21:13e4824bc364 520 }
ricardo_95 21:13e4824bc364 521
ricardo_95 36:d10f368d037b 522 void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) // Generate LED colour function (library function PixelArray is used for this item).
ricardo_95 21:13e4824bc364 523 {
ricardo_95 36:d10f368d037b 524 out->red = LED_red_intensity;
ricardo_95 36:d10f368d037b 525 out->green = LED_green_intensity;
ricardo_95 36:d10f368d037b 526 out->blue = LED_blue_intensity;
ricardo_95 21:13e4824bc364 527 }
ricardo_95 21:13e4824bc364 528
ricardo_95 36:d10f368d037b 529 void set_userinterface_LED() // Control functions for LED above buttons (added because of failures).
ricardo_95 21:13e4824bc364 530 {
ricardo_95 21:13e4824bc364 531 if (lock_state == 1) {
ricardo_95 21:13e4824bc364 532 } else {
ricardo_95 36:d10f368d037b 533 if (button_reposition == 0) {
ricardo_95 36:d10f368d037b 534 reposition_feedback_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 535 } else {
ricardo_95 36:d10f368d037b 536 reposition_feedback_LED = 0;
ricardo_95 21:13e4824bc364 537 }
ricardo_95 21:13e4824bc364 538
ricardo_95 36:d10f368d037b 539 if (button_new_patient == 0) {
ricardo_95 36:d10f368d037b 540 new_patient_feedback_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 541 } else {
ricardo_95 36:d10f368d037b 542 new_patient_feedback_LED = 0;
ricardo_95 21:13e4824bc364 543 }
ricardo_95 21:13e4824bc364 544 }
ricardo_95 21:13e4824bc364 545 }
ricardo_95 21:13e4824bc364 546
ricardo_95 36:d10f368d037b 547 void read_voltage() // Function for reading voltages from power and battery.
ricardo_95 21:13e4824bc364 548 {
ricardo_95 36:d10f368d037b 549 if (power_plug_state == 1) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage.
ricardo_95 36:d10f368d037b 550 alarm = 0; // Alarm is off.
ricardo_95 21:13e4824bc364 551 speaker_state = 0;
ricardo_95 21:13e4824bc364 552 } else {
ricardo_95 36:d10f368d037b 553 alarm = 1; // Else alarm is on.
ricardo_95 21:13e4824bc364 554 speaker_state = 1;
ricardo_95 21:13e4824bc364 555 }
ricardo_95 26:9e130f7ee829 556
ricardo_95 26:9e130f7ee829 557
ricardo_95 36:d10f368d037b 558 if (alarm == 1 && mute_state == 1 && (batteryvoltage_current > alarm_voltage)) {// Set speaker on for 750 ms.
ricardo_95 36:d10f368d037b 559 speaker1 = 0; // Set speaker.
ricardo_95 21:13e4824bc364 560 speaker2 = 0;
ricardo_95 21:13e4824bc364 561 }
ricardo_95 21:13e4824bc364 562
ricardo_95 24:782c4dc4a3ff 563 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 36:d10f368d037b 564 speaker1 = 1; // Set speaker.
ricardo_95 21:13e4824bc364 565 speaker2 = 1;
ricardo_95 36:d10f368d037b 566 speaker_timer.start(); // Set timer for speaker to iterate on and off.
ricardo_95 21:13e4824bc364 567 }
ricardo_95 21:13e4824bc364 568
ricardo_95 21:13e4824bc364 569 if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) {
ricardo_95 36:d10f368d037b 570 speaker1 = 0; // Turn off speaker (use two outputs because of currentlimiting of one).
ricardo_95 21:13e4824bc364 571 speaker2 = 0;
ricardo_95 21:13e4824bc364 572 }
ricardo_95 21:13e4824bc364 573
ricardo_95 21:13e4824bc364 574 if (speaker_timer.read_ms() > (speaker_active_ms*2)) {
ricardo_95 36:d10f368d037b 575 speaker_timer.stop(); // Stop speaker timer.
ricardo_95 21:13e4824bc364 576 speaker_timer.reset();
ricardo_95 21:13e4824bc364 577 }
ricardo_95 36:d10f368d037b 578 // Read channel 0 from external ADC (batteryvoltage); voltagedeviders are used,
ricardo_95 36:d10f368d037b 579 batteryvoltage_current = adsAccu.readADC_SingleEnded(0); // because of higher voltage then Vcc of ADC (5.3 V (= Vcc + 0.3 V) max possible at each analog input).
ricardo_95 36:d10f368d037b 580 powervoltage_current = adsAccu.readADC_SingleEnded(1); // Read channel 1 from external ADC (powervoltage).
ricardo_95 21:13e4824bc364 581
ricardo_95 36:d10f368d037b 582 if (powervoltage_current < digital_value_ADC_powervoltage_unplugged) { // If statement to set LED's to blue.
ricardo_95 21:13e4824bc364 583 power_plug_state = 0;
ricardo_95 28:b4bee068780d 584 LED_colour = 'b';
ricardo_95 21:13e4824bc364 585 } else {
ricardo_95 21:13e4824bc364 586 power_plug_state = 1;
ricardo_95 21:13e4824bc364 587 }
ricardo_95 21:13e4824bc364 588 }
ricardo_95 21:13e4824bc364 589
ricardo_95 21:13e4824bc364 590 void read_adc()
ricardo_95 21:13e4824bc364 591 {
ricardo_95 36:d10f368d037b 592 piezo_electric_sample_timer.reset();
ricardo_95 36:d10f368d037b 593 piezo_electric_sample_timer.start();
ricardo_95 36:d10f368d037b 594 connection_test_sensorplate = angle_device_sensorplate.testConnection();
ricardo_95 36:d10f368d037b 595
ricardo_95 36:d10f368d037b 596 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 597 usb_serial.printf("Connection test sensorplate = %d\n", connection_test_sensorplate);
ricardo_95 36:d10f368d037b 598 }
ricardo_95 36:d10f368d037b 599
ricardo_95 36:d10f368d037b 600 /*
ricardo_95 36:d10f368d037b 601 if (connection_test_sensorplate == 0) {
ricardo_95 28:b4bee068780d 602 lock_state = 1;
ricardo_95 36:d10f368d037b 603 LED_on_dev_board2 = 1;
ricardo_95 36:d10f368d037b 604 lock_feedback_LED = 0;
ricardo_95 36:d10f368d037b 605 }*/
ricardo_95 36:d10f368d037b 606
ricardo_95 36:d10f368d037b 607 if (connection_test_sensorplate == 1) {
ricardo_95 36:d10f368d037b 608 piezo_electric_array[0] = piezo_electric_adc.readADC_SingleEnded(0); // First PE readout.
ricardo_95 21:13e4824bc364 609
ricardo_95 21:13e4824bc364 610 for (k = 0; k < 4; k = k + 1) {
ricardo_95 36:d10f368d037b 611 piezo_resistive_array[k] = piezo_resistive_adc1.readADC_SingleEnded(k); // First 4 PR readout.
ricardo_95 21:13e4824bc364 612 }
ricardo_95 21:13e4824bc364 613
ricardo_95 36:d10f368d037b 614 while(piezo_electric_sample_timer.read_us()<(1*(total_readout_cycle_time_us/5))) {} // Wait untill 20% of cycle.
ricardo_95 36:d10f368d037b 615
ricardo_95 36:d10f368d037b 616 piezo_electric_array[1] = piezo_electric_adc.readADC_SingleEnded(0); // Second PE readout.
ricardo_95 21:13e4824bc364 617
ricardo_95 21:13e4824bc364 618 for (k = 0; k < 4; k = k + 1) {
ricardo_95 36:d10f368d037b 619 piezo_resistive_array[k+4] = piezo_resistive_adc2.readADC_SingleEnded(k); // Last 4 PR readout.
ricardo_95 21:13e4824bc364 620 }
ricardo_95 21:13e4824bc364 621
ricardo_95 36:d10f368d037b 622 while(piezo_electric_sample_timer.read_us()<(2*(total_readout_cycle_time_us/5))) {} // Wait untill 40% of cycle.
ricardo_95 36:d10f368d037b 623
ricardo_95 36:d10f368d037b 624 piezo_electric_array[2] = piezo_electric_adc.readADC_SingleEnded(0); // Third PE readout.
ricardo_95 21:13e4824bc364 625
ricardo_95 36:d10f368d037b 626 angle_device_sensorplate.getAccelero(accelerometer_sensorplate); // Get accelerometer data.
ricardo_95 36:d10f368d037b 627 angle = accelerometer_sensorplate[2]*100;
ricardo_95 26:9e130f7ee829 628 if(angle == 0) {
ricardo_95 36:d10f368d037b 629 MPU6050 angle_device_sensorplate(p28,p27);
ricardo_95 36:d10f368d037b 630 angle_device_sensorplate.getAccelero(accelerometer_sensorplate);
ricardo_95 36:d10f368d037b 631 angle = accelerometer_sensorplate[2]*100;
ricardo_95 26:9e130f7ee829 632 }
ricardo_95 36:d10f368d037b 633 angle_device_sensorplate.getGyro(gyroscope_sensorplate); // Get gyroscope data.
ricardo_95 34:1614f4f2b841 634
ricardo_95 33:df21cb8dc5c7 635 if (test_belt == 1) {
ricardo_95 36:d10f368d037b 636 angle_device_reference_belt.getGyro(gyroscope_reference_belt); // Get gyroscope data from Belt.
ricardo_95 36:d10f368d037b 637 angle_device_reference_belt.getAccelero(accelerometer_reference_belt); // Get accelerometer data from belt.
ricardo_95 33:df21cb8dc5c7 638 }
ricardo_95 36:d10f368d037b 639
ricardo_95 36:d10f368d037b 640 if (connection_test_sensorplate == 1) { // If statement for sending serial information sensorplate data when connection test is active.
ricardo_95 36:d10f368d037b 641 // Receiving order sensor information: 3 accelero sensors & 3 gyroscope sensors from sensorplate; 3 accelero sensors & 3 gyroscope sensors from belt. Is splitted in two parts - part 2/2.
ricardo_95 36:d10f368d037b 642 pi_serial.printf("?,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n", accelerometer_sensorplate[0], accelerometer_sensorplate[1], accelerometer_sensorplate[2], gyroscope_sensorplate[0], gyroscope_sensorplate[1], gyroscope_sensorplate[2], accelerometer_reference_belt[0], accelerometer_reference_belt[1], accelerometer_reference_belt[2], gyroscope_reference_belt[0], gyroscope_reference_belt[1], gyroscope_reference_belt[2]);
ricardo_95 36:d10f368d037b 643 }
ricardo_95 36:d10f368d037b 644
ricardo_95 36:d10f368d037b 645 while(piezo_electric_sample_timer.read_us()<(3*(total_readout_cycle_time_us/5))) {} // Wait untill 60% of cycle.
ricardo_95 34:1614f4f2b841 646
ricardo_95 36:d10f368d037b 647 piezo_electric_array[3] = piezo_electric_adc.readADC_SingleEnded(0); // Fourth PE readout.
ricardo_95 21:13e4824bc364 648 }
ricardo_95 21:13e4824bc364 649
ricardo_95 21:13e4824bc364 650 timer_functions();
ricardo_95 21:13e4824bc364 651
ricardo_95 21:13e4824bc364 652 batteryvoltage_current = batteryvoltage_last;
ricardo_95 21:13e4824bc364 653 powervoltage_current = powervoltage_last;
ricardo_95 36:d10f368d037b 654 read_voltage(); // Read_voltage function to control alarm.
ricardo_95 36:d10f368d037b 655
ricardo_95 34:1614f4f2b841 656 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 657 usb_serial.printf("Voltage = %d , %d\n", batteryvoltage_current, powervoltage_current);
ricardo_95 34:1614f4f2b841 658 }
ricardo_95 36:d10f368d037b 659
ricardo_95 21:13e4824bc364 660 uint32_t val = 0;
ricardo_95 36:d10f368d037b 661 colour_select_indicating_LED_wall(LED_colour); // Function to select colour.
ricardo_95 36:d10f368d037b 662 indicator_LEDs.update(generate, NUMBER_LED_FRONT, val); // Function to set the LED's which shines to the wall (indicating change patient position).
ricardo_95 36:d10f368d037b 663 set_userinterface_LED(); // Set LED's of user interface (LED's above buttons).
ricardo_95 36:d10f368d037b 664
ricardo_95 36:d10f368d037b 665 while(piezo_electric_sample_timer.read_us()<(4*(total_readout_cycle_time_us/5))) {} // Wait untill 80% of cycle.
ricardo_95 21:13e4824bc364 666
ricardo_95 36:d10f368d037b 667 if (test_mode == 1){ // If statement for test purposal.
ricardo_95 36:d10f368d037b 668 usb_serial.printf("Angle device sensorplate = %d\n",angle_device_sensorplate.testConnection());
ricardo_95 36:d10f368d037b 669 }
ricardo_95 36:d10f368d037b 670
ricardo_95 36:d10f368d037b 671 if (connection_test_sensorplate == 1) {
ricardo_95 36:d10f368d037b 672 piezo_electric_array[4] = piezo_electric_adc.readADC_SingleEnded(0); // Fifth PE readout.
ricardo_95 21:13e4824bc364 673 }
ricardo_95 21:13e4824bc364 674
ricardo_95 36:d10f368d037b 675 while(piezo_electric_sample_timer.read_us()<(4.25*(total_readout_cycle_time_us/5))) {} // Wait untill 85% of cycle.
ricardo_95 28:b4bee068780d 676
ricardo_95 36:d10f368d037b 677 serial_read(); // Call function for reading information from PI by serial connection.
ricardo_95 36:d10f368d037b 678 serial_log(); // Call function for logging information to PI by serial connection.
ricardo_95 36:d10f368d037b 679
ricardo_95 36:d10f368d037b 680 if (test_mode == 1) { // If statements for test purposal (untill * mark).
ricardo_95 36:d10f368d037b 681 usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
ricardo_95 34:1614f4f2b841 682 }
ricardo_95 34:1614f4f2b841 683 if (test_pin == 1) {
ricardo_95 34:1614f4f2b841 684 test_mode = 1;
ricardo_95 36:d10f368d037b 685 usb_serial.printf("%d\n",test_mode);
ricardo_95 34:1614f4f2b841 686 }
ricardo_95 34:1614f4f2b841 687 if (test_pin == 0) {
ricardo_95 34:1614f4f2b841 688 test_mode = 0;
ricardo_95 36:d10f368d037b 689 usb_serial.printf("%d\n",test_mode);
ricardo_95 34:1614f4f2b841 690 }
ricardo_95 36:d10f368d037b 691
ricardo_95 34:1614f4f2b841 692 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 693 usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
ricardo_95 36:d10f368d037b 694 }
ricardo_95 36:d10f368d037b 695 // * End of if statements for test purposal.
ricardo_95 21:13e4824bc364 696 }
ricardo_95 21:13e4824bc364 697
ricardo_95 36:d10f368d037b 698 int main() { // Main function.
ricardo_95 36:d10f368d037b 699 wait_ms(boot_delay_ms); // Wait to boot sensorplate first.
ricardo_95 36:d10f368d037b 700 i2c_sensorplate_adc.frequency(i2c_freq); // Set frequency for i2c connection to sensorplate (variable is declared in config part).
ricardo_95 36:d10f368d037b 701 i2c_power_adc.frequency(i2c_freq); // Same as line 695, but now for ADC to read battery- en powervoltage.
ricardo_95 36:d10f368d037b 702 usb_serial.baud(baud_rate); // Set serial USB connection baud rate (variable is declared in config part).
ricardo_95 36:d10f368d037b 703 pi_serial.baud(baud_rate); // Same as line 697, but now for serial PI connection.
ricardo_95 36:d10f368d037b 704 piezo_resistive_adc1.setGain(GAIN_TWOTHIRDS); // Set ranges of ADC to +/-6.144V (end is marked with #):
ricardo_95 36:d10f368d037b 705 piezo_resistive_adc2.setGain(GAIN_TWOTHIRDS);
ricardo_95 36:d10f368d037b 706 piezo_electric_adc.setGain(GAIN_TWOTHIRDS);
ricardo_95 36:d10f368d037b 707 adsAccu.setGain(GAIN_TWOTHIRDS); // #) End of configuration ADC ranges.
ricardo_95 36:d10f368d037b 708 pi_serial.format(8, SerialBase::None, 1); // Set serial communication line with PI.
ricardo_95 7:dba5091c8b7d 709
ricardo_95 36:d10f368d037b 710 button_lock.fall(&trigger_lock); // Interrupt for rising edge lock button.
ricardo_95 36:d10f368d037b 711 button_lock.rise(&end_timer_lock_button);
ricardo_95 36:d10f368d037b 712 button_reposition.fall(&reposition_button_triggered);
ricardo_95 36:d10f368d037b 713 button_reposition.rise(&rise_reposition);
ricardo_95 36:d10f368d037b 714 button_mute.fall(&mute_button_triggered);
ricardo_95 36:d10f368d037b 715 button_new_patient.fall(&trigger_new_patient); // New patient/calibration button rising event.
ricardo_95 36:d10f368d037b 716 button_new_patient.rise(&activate_new_patient_function); // Falling edge for calibration algorithm option.
ricardo_95 36:d10f368d037b 717 delay_between_button_pressed.reset(); // Delaytimer reset en start.
ricardo_95 36:d10f368d037b 718 delay_between_button_pressed.start();
ricardo_95 26:9e130f7ee829 719
ricardo_95 36:d10f368d037b 720 set_intensity_LEDs(); // Initialize intensity for user interface LED's and LED's shines to wall.
ricardo_95 36:d10f368d037b 721 lock_feedback_LED = control_LED_intensity; // Lock LED initialization.
ricardo_95 21:13e4824bc364 722
ricardo_95 34:1614f4f2b841 723
ricardo_95 36:d10f368d037b 724 total_readout_cycle.attach_us(&read_adc, total_readout_cycle_time_us); // Call function to start reading sensorplate and other functionalities.
ricardo_95 19:3b5999fa7b7e 725
DEldering 0:c0e44c46c573 726 while (1) {
ricardo_95 36:d10f368d037b 727 wait_us(total_readout_cycle_time_us+1); // Wait indefinitely.
DEldering 0:c0e44c46c573 728 }
ricardo_95 12:7b3a5940f911 729 }