Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813

Fork of Nucleo-transfer by Momo Medical

Committer:
deldering95
Date:
Tue Jan 16 12:05:00 2018 +0000
Revision:
48:aca02ef5cd01
Parent:
47:80cfc181f8b3
Child:
50:442d13dff34a
Nucleo with clinical feedback;

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