Nucleo-transfer
Dependencies: ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813
Fork of Nucleo-transfer by
Diff: Sensorplate/main.cpp
- Revision:
- 37:d8f7b2b5719a
- Parent:
- 36:d10f368d037b
- Child:
- 38:764847892afc
--- a/Sensorplate/main.cpp Wed Oct 18 14:12:11 2017 +0000 +++ b/Sensorplate/main.cpp Thu Oct 19 15:59:42 2017 +0000 @@ -21,6 +21,7 @@ Information files: (1) Flowchart: (2) Table serial communication: https://docs.google.com/spreadsheets/d/1kHlithHxtoMDGvbcdH8vwSw5W5ArxlwDPsyfra1dtQM/edit?usp=drive_web +(3) Technical manual CU-/software: */ /************************ CONFIG ***********************************/ @@ -30,7 +31,8 @@ #include "MPU6050.h" #include "MPU6050_belt.h" #include "neopixel.h" -#define NUMBER_LED_FRONT (3) + +#define NUMBER_LED_FRONT (3) // declaren waarvoor dient #define ONE_COLOR InterruptIn button_lock(p15); // Input on intterupt base decleration. @@ -42,7 +44,7 @@ DigitalOut LED_on_dev_board2(LED2); DigitalOut LED_on_dev_board3(LED3); DigitalOut LED_on_dev_board4(LED4); -DigitalOut speaker1(p21); +DigitalOut speaker1(p21); // relatie aangeven! DigitalOut speaker2(p22); neopixel::PixelArray indicator_LEDs(p11); @@ -51,7 +53,7 @@ PwmOut mute_feedback_LED(p26); PwmOut new_patient_feedback_LED(p24); -Timer button_lock_hold_timer; // +Timer button_lock_hold_timer; // Timer for time lock button should be pressed. Timer button_calibration_hold_timer; // Timer for calibration function (new patient holding 5 seconds). Timer delay_between_button_pressed; // Timer for time between two buttons (to prevent pressing buttons simultaneously). Timer speaker_timer; // Timer for speaker activation. @@ -79,33 +81,32 @@ int boot_delay_ms = 500; int total_readout_cycle_time_us = 100000; // Cycle time in us. -int i2c_freq = 400000; // I2C Frequency. +int i2c_frequency = 400000; // I2C Frequency. int baud_rate = 115200; // Baud rate. short piezo_resistive_array[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle. short piezo_electric_array[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle. int angle = 0; // Accelerometer Z-axis. -int k = 0; -float accelerometer_sensorplate[3]; // Raw accelerometer data. +float accelerometer_sensorplate[3] = {0.0, 0.0, 0.0}; // Raw accelerometer data. float gyroscope_sensorplate[3]; // Raw gyroscope data. float accelerometer_reference_belt[3]; // Raw accelerometer data from belt. float gyroscope_reference_belt[3]; // Raw gyroscope data from belt. -char LED_colour = 'g'; // Variable to set LED colour (standard set to green, untill PI sends other character). -bool lock_state = 0, lock_flag = 0, mute_state = 0, alarm = 0, calibration_flag = 0, intensity_select = 1; // Boolean variables for logging states. +char LED_colour = 'g'; // Variable to set LED colour (standard set to green, untill PI sends other character). Other possible colours: red ('r') & yellow ('y'). +bool lock_state = false, lock_flag = 0, mute_state = 0, alarm = 0, calibration_flag = 0, intensity_select = 1; // Boolean variables for logging states. bool mute_flag = 0, new_patient_flag = 0, reposition_flag = 0; // Flag variables. bool speaker_state = 0, LED_red_state = 0, LED_yellow_state = 0, LED_green_state = 0, power_plug_state = 0; -bool speaker_logged = 0, LED_red_logged = 0, LED_yellow_logged = 0, LED_green_logged = 0, power_plug_logged = 0; +bool speaker_logged = 0, LED_red_logged = 0, LED_yellow_logged = 0, LED_green_logged = 0, power_plug_logged = 0; // is toevoegen int locktime_ms = 2000; // Waittime for lock user interface in ms. int calibrationtime_ms = 5000; // Time to press new_patient button for calibration system. -int calibration_flash; // Variable for flash LED's to indicate calibration. +int calibration_flash = 0; // Variable for flash LED's to indicate calibration. int buttondelay_ms = 750; // Button delay in ms. int delay_lock_interface = 3000*60; // Delay for non using interface locktime. int speaker_active_ms = 750; // Time to iterate speaker on and off when alarm occurs. int alarm_voltage = 5867; // Needed voltage for alarm expressed as a digital 15 bit value (=20% of max battery voltage). -int LED_red_intensity, LED_blue_intensity, LED_green_intensity; // Variables to set LED intensity. -short batteryvoltage_current = 0, batteryvoltage_last = 0, powervoltage_current, powervoltage_last; // Variables to manage batteryvoltage. -int digital_value_ADC_powervoltage_unplugged = 20000; // Digital value to set the indicating LEDs to wall blue (should be set off later). +int LED_red_intensity = 0, LED_blue_intensity = 0, LED_green_intensity = 0; // Variables to set LED intensity. +short batteryvoltage_current = 0, batteryvoltage_last = 0, powervoltage_current, powervoltage_last; // Variables to manage batteryvoltage. Maybe change current to other? +const int digital_value_ADC_powervoltage_unplugged = 20000; // Digital value to set the indicating LEDs to wall blue (should be set off later). const in hoofdletters int intensity_day = 40, intensity_night = 10; // Intensity settings for LED's to wall. -double intensity, control_LED_intensity = 0; // Variable between 0 and 1 to set the intensity of the LED's above the buttons. +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! /*************************** TEST ********************************/ // Verify algoritm function: for belt activation, set test_belt 1 (connect pin p20 to 3.3V). @@ -132,7 +133,7 @@ } control_LED_intensity = (intensity/100); - if (test_mode == 1) { // If statement for test purposal LED_intensity values. + if (test_mode == 1) { // If statement for test purposal LED_intensity values. if def gebruiken voor testmode usb_serial.printf("Intensity LED's shines to wall = %f\n", intensity); usb_serial.printf("Intensity LED's above buttons = %f\n", control_LED_intensity); } @@ -156,10 +157,9 @@ LED_colour = message[1]; } - usb_serial.printf("Intensity_select = %d en LED_colour = %c\n", intensity_select, LED_colour); - if (test_mode == 1) { usb_serial.printf("Message: %s\n", message); + usb_serial.printf("Intensity_select = %d en LED_colour = %c\n", intensity_select, LED_colour); } } } @@ -447,7 +447,7 @@ if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) { } else { - button_calibration_hold_timer.reset(); + button_calibration_hold_timer.reset(); // inline ? button_calibration_hold_timer.start(); new_patient_feedback_LED = control_LED_intensity;; @@ -464,7 +464,7 @@ } new_patient_feedback_LED = 0; - 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. + 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 new_patient_flag = 1; } @@ -555,7 +555,7 @@ } - if (alarm == 1 && mute_state == 1 && (batteryvoltage_current > alarm_voltage)) {// Set speaker on for 750 ms. + if (alarm == 1 && mute_state == 1 && (batteryvoltage_current > alarm_voltage)) {// Set speaker on for 750 ms. Use PWM? => Split in more functions. speaker1 = 0; // Set speaker. speaker2 = 0; } @@ -571,7 +571,7 @@ speaker2 = 0; } - if (speaker_timer.read_ms() > (speaker_active_ms*2)) { + if (speaker_timer.read_ms() > (speaker_active_ms*2)) { // speaker_timer.stop(); // Stop speaker timer. speaker_timer.reset(); } @@ -589,7 +589,7 @@ void read_adc() { - piezo_electric_sample_timer.reset(); + piezo_electric_sample_timer.reset(); // Clock gebruiken o.i.d.? piezo_electric_sample_timer.start(); connection_test_sensorplate = angle_device_sensorplate.testConnection(); @@ -607,19 +607,19 @@ if (connection_test_sensorplate == 1) { piezo_electric_array[0] = piezo_electric_adc.readADC_SingleEnded(0); // First PE readout. - for (k = 0; k < 4; k = k + 1) { + for (uint8_t k = 0; k < 4; ++k) { piezo_resistive_array[k] = piezo_resistive_adc1.readADC_SingleEnded(k); // First 4 PR readout. } - while(piezo_electric_sample_timer.read_us()<(1*(total_readout_cycle_time_us/5))) {} // Wait untill 20% of cycle. + 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. piezo_electric_array[1] = piezo_electric_adc.readADC_SingleEnded(0); // Second PE readout. - for (k = 0; k < 4; k = k + 1) { + for (uint8_t k = 0; k < 4; ++k) { piezo_resistive_array[k+4] = piezo_resistive_adc2.readADC_SingleEnded(k); // Last 4 PR readout. } - while(piezo_electric_sample_timer.read_us()<(2*(total_readout_cycle_time_us/5))) {} // Wait untill 40% of cycle. + 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. piezo_electric_array[2] = piezo_electric_adc.readADC_SingleEnded(0); // Third PE readout. @@ -640,9 +640,9 @@ if (connection_test_sensorplate == 1) { // If statement for sending serial information sensorplate data when connection test is active. // 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. 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]); - } + } // binair print and convert in pi - while(piezo_electric_sample_timer.read_us()<(3*(total_readout_cycle_time_us/5))) {} // Wait untill 60% of cycle. + 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. piezo_electric_array[3] = piezo_electric_adc.readADC_SingleEnded(0); // Fourth PE readout. } @@ -662,7 +662,7 @@ indicator_LEDs.update(generate, NUMBER_LED_FRONT, val); // Function to set the LED's which shines to the wall (indicating change patient position). set_userinterface_LED(); // Set LED's of user interface (LED's above buttons). - while(piezo_electric_sample_timer.read_us()<(4*(total_readout_cycle_time_us/5))) {} // Wait untill 80% of cycle. + 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. if (test_mode == 1){ // If statement for test purposal. usb_serial.printf("Angle device sensorplate = %d\n",angle_device_sensorplate.testConnection()); @@ -672,7 +672,7 @@ piezo_electric_array[4] = piezo_electric_adc.readADC_SingleEnded(0); // Fifth PE readout. } - while(piezo_electric_sample_timer.read_us()<(4.25*(total_readout_cycle_time_us/5))) {} // Wait untill 85% of cycle. + 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. serial_read(); // Call function for reading information from PI by serial connection. serial_log(); // Call function for logging information to PI by serial connection. @@ -695,10 +695,10 @@ // * End of if statements for test purposal. } -int main() { // Main function. +int main() { // Main function. inline function "Momo Init" bijvoorbeeld wait_ms(boot_delay_ms); // Wait to boot sensorplate first. - i2c_sensorplate_adc.frequency(i2c_freq); // Set frequency for i2c connection to sensorplate (variable is declared in config part). - i2c_power_adc.frequency(i2c_freq); // Same as line 695, but now for ADC to read battery- en powervoltage. + i2c_sensorplate_adc.frequency(i2c_frequencyuencyuency); // Set frequency for i2c connection to sensorplate (variable is declared in config part). + i2c_power_adc.frequency(i2c_frequencyuencyuency); // Same as line 695, but now for ADC to read battery- en powervoltage. usb_serial.baud(baud_rate); // Set serial USB connection baud rate (variable is declared in config part). pi_serial.baud(baud_rate); // Same as line 697, but now for serial PI connection. piezo_resistive_adc1.setGain(GAIN_TWOTHIRDS); // Set ranges of ADC to +/-6.144V (end is marked with #):