Nucleo-transfer
Dependencies: ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813
Fork of Nucleo-transfer by
Diff: Sensorplate/main.cpp
- Revision:
- 51:69087c44e8ac
- Parent:
- 50:442d13dff34a
- Child:
- 53:54c882995514
--- a/Sensorplate/main.cpp Tue Jan 16 12:36:52 2018 +0000 +++ b/Sensorplate/main.cpp Thu Feb 01 18:03:52 2018 +0000 @@ -55,13 +55,13 @@ DigitalOut speaker1(PC_12); // relatie aangeven! //neopixel::PixelArray indicator_LEDs(PA_7); PixelArray px(WS2812_BUF); -WS2812 ws(PA_7, WS2812_BUF, 3, 9, 9, 6); - +//WS2812 ws(PA_7, WS2812_BUF, 3, 9, 9, 6); +WS2812 ws(PA_7, WS2812_BUF, 3, 35, 12, 4); -PwmOut lock_feedback_LED(PB_1); // Declaration of pulse with modulation outputs. -PwmOut mute_feedback_LED(PB_15); -PwmOut new_patient_feedback_LED(PB_14); -PwmOut reposition_feedback_LED(PB_13); +PwmOut lock_feedback_LED(PB_13); //(PB_1); // Declaration of pulse with modulation outputs. +PwmOut mute_feedback_LED(PB_14); //(PB_15); +PwmOut new_patient_feedback_LED(PB_15); //(PB_14); +PwmOut reposition_feedback_LED(PB_1); //(PB_13); 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). @@ -99,7 +99,7 @@ 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. -int colourbuf[NUM_COLORS] = {0xff0000,0x00ff00,0x0000ff,0xffa500,0xffffff}; // hex codes for the different colours +int colourbuf[NUM_COLORS] = {0xff0000,0x00ff00,0x0000ff,0xffa500,0xffffff}; // hex codes for the different colours 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'). 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. @@ -116,7 +116,7 @@ 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 = 15000; // Digital value to set the indicating LEDs to wall blue (should be set off later). const in hoofdletters -int intensity_day = 40, intensity_night = 7; // Intensity settings for LED's to wall. +int intensity_day = 20, intensity_night = 5; // Intensity settings for LED's to wall. 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! int colour_code = 0b00; bool pi_active = false; @@ -356,9 +356,9 @@ } calibration_flash--; } - for (int z=WS2812_BUF; z >= 0 ; z--) { + //for (int z=WS2812_BUF; z >= 0 ; z--) { ws.write_offsets(px.getBuf(),0,0,0); - } + //} } void trigger_lock() // If rising edge lock button is detected start locktimer. @@ -417,13 +417,9 @@ } else { delay_between_button_pressed.reset(); delay_between_button_pressed.start(); - mute_state = !mute_state; - - if (mute_state == 1) { // If statement for if mute_state is active, set mute feedback LED active. - mute_feedback_LED = control_LED_intensity; - } else { - mute_feedback_LED = 0; - } + button_calibration_hold_timer.reset(); // inline ? + button_calibration_hold_timer.start(); + mute_feedback_LED = control_LED_intensity; if (test_mode == 1) { // If statement for test purposal. usb_serial.printf("Mute triggered %d.\n",mute_state); @@ -434,32 +430,12 @@ } } -void trigger_new_patient() // Function to trigger hold timer for new patient and calibration function. -{ - - if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) { - lock_flash = 10; - } else { - button_calibration_hold_timer.reset(); // inline ? - button_calibration_hold_timer.start(); - new_patient_feedback_LED = control_LED_intensity;; - - if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("New patient triggered.\n"); - } - } -} - -void activate_new_patient_function() // Timer calibration function. +void rise_mute() // Interrupt for rising edge reposition function (deactivation; active low). { if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("New patient released.\n"); + usb_serial.printf("Mute released.\n"); } - 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. && toevoegen? -. als mogelijk mailtje naar Bart: bart@straightupalgorithms.com - new_patient_flag = 1; - } + mute_feedback_LED = 0; button_calibration_hold_timer.stop(); // Timer reset for calibration function of new patient button. button_calibration_hold_timer.reset(); @@ -478,6 +454,31 @@ calibration_flag = 0; } } + +} + +void trigger_new_patient() // Function to trigger hold timer for new patient and calibration function. +{ + + if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) { + lock_flash = 10; + } else { + delay_between_button_pressed.reset(); + delay_between_button_pressed.start(); + new_patient_feedback_LED = control_LED_intensity;; + new_patient_flag = 1; + if (test_mode == 1) { // If statement for test purposal. + usb_serial.printf("New patient triggered.\n"); + } + } +} + +void activate_new_patient_function() // Timer calibration function. +{ + if (test_mode == 1) { // If statement for test purposal. + usb_serial.printf("New patient released.\n"); + } + new_patient_feedback_LED = 0; } void timer_functions() // Function which contains statements using timers. @@ -498,12 +499,12 @@ } } - if (button_new_patient == 1) { + if (button_mute == 1) { button_calibration_hold_timer.stop(); button_calibration_hold_timer.reset(); } - if ((button_calibration_hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && button_new_patient == 0 && lock_state == 0) { // If statement for calibration algorithm. + if ((button_calibration_hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && button_mute == 0 && lock_state == 0) { // If statement for calibration algorithm. calibration_flag = 1; calibration_flash = 11; @@ -551,6 +552,10 @@ int main() // Main function. inline function "Momo Init" bijvoorbeeld { + ws.useII(WS2812::GLOBAL); // use global intensity scaling + set_intensity_LEDs(); // Initialize intensity for user interface LED's and LED's shines to wall. + colour_select_indicating_LED_wall(LED_colour); + speaker1 = 1; wait_ms(boot_delay_ms); // Wait to boot sensorplate first. speaker1 = 0; @@ -559,7 +564,7 @@ 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 #): piezo_resistive_adc2.setGain(GAIN_TWOTHIRDS); - piezo_electric_adc.setGain(GAIN_TWOTHIRDS); + piezo_electric_adc.setGain(GAIN_TWO); pi_serial.format(8, SerialBase::None, 1); // Set serial communication line with PI. button_lock.mode(PullUp); @@ -572,13 +577,9 @@ button_reposition.fall(&reposition_button_triggered); button_reposition.rise(&rise_reposition); button_mute.fall(&mute_button_triggered); + button_mute.rise(&rise_mute); button_new_patient.fall(&trigger_new_patient); // New patient/calibration button rising event. button_new_patient.rise(&activate_new_patient_function); // Falling edge for calibration algorithm option. - delay_between_button_pressed.reset(); // Delaytimer reset en start. - delay_between_button_pressed.start(); - - ws.useII(WS2812::GLOBAL); // use global intensity scaling - set_intensity_LEDs(); // Initialize intensity for user interface LED's and LED's shines to wall. __disable_irq(); while(!pi_active) { @@ -610,8 +611,16 @@ while(Knight_Rider_Timer.read_ms()<(8*(total_knight_rider_cycle_time_ms/8))) {} } + __enable_irq(); + delay_between_button_pressed.reset(); // Delaytimer reset en start. + delay_between_button_pressed.start(); + + reposition_feedback_LED = 0; + new_patient_feedback_LED = 0; + mute_feedback_LED = 0; + usb_serial.printf("Lock State: %d\n",lock_state); lock_feedback_LED = control_LED_intensity; // Lock LED initialization. - __enable_irq(); + while (1) { piezo_electric_sample_timer.reset(); // Clock gebruiken o.i.d.? piezo_electric_sample_timer.start(); @@ -625,7 +634,7 @@ } if (connection_test_sensorplate == 1) { - piezo_electric_array[0] = piezo_electric_adc.readADC_SingleEnded(0); // First PE readout. + piezo_electric_array[0] = piezo_electric_adc.readADC_Differential_0_1(); // First PE readout. for (uint8_t k = 0; k < 4; ++k) { piezo_resistive_array[k] = piezo_resistive_adc1.readADC_SingleEnded(k); // First 4 PR readout. @@ -635,7 +644,7 @@ } 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. + piezo_electric_array[1] = piezo_electric_adc.readADC_Differential_0_1(); // Second PE readout. for (uint8_t k = 0; k < 4; ++k) { piezo_resistive_array[k+4] = piezo_resistive_adc2.readADC_SingleEnded(k); // Last 4 PR readout. @@ -646,11 +655,11 @@ 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. + piezo_electric_array[2] = piezo_electric_adc.readADC_Differential_0_1(); // Third PE readout. angle_device_sensorplate.getAccelero(accelerometer_sensorplate); // Get accelerometer data. angle = accelerometer_sensorplate[2]*100; if(angle == 0) { - MPU6050 angle_device_sensorplate(PB_9, PB_8); + MPU6050_belt angle_device_sensorplate(PB_9, PB_8); angle_device_sensorplate.getAccelero(accelerometer_sensorplate); angle = accelerometer_sensorplate[2]*100; } @@ -674,7 +683,7 @@ 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. + piezo_electric_array[3] = piezo_electric_adc.readADC_Differential_0_1(); // Fourth PE readout. } if (test_mode == 1) { usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); @@ -694,7 +703,7 @@ } if (connection_test_sensorplate == 1) { - piezo_electric_array[4] = piezo_electric_adc.readADC_SingleEnded(0); // Fifth PE readout. + piezo_electric_array[4] = piezo_electric_adc.readADC_Differential_0_1(); // Fifth PE readout. } 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.