Nucleo-transfer
Dependencies: ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813
Fork of Nucleo-transfer by
Diff: Sensorplate/main.cpp
- Revision:
- 7:dba5091c8b7d
- Parent:
- 6:9c1944f3ebe5
- Child:
- 8:bf0f7a6fb1fd
--- a/Sensorplate/main.cpp Thu Sep 14 09:43:21 2017 +0000 +++ b/Sensorplate/main.cpp Wed Sep 27 09:04:08 2017 +0000 @@ -1,7 +1,28 @@ #include "mbed.h" #include "Adafruit_ADS1015.h" -#include "USBSerial.h" #include "MPU6050.h" +#include "neopixel.h" +#define NLED (11) +#define ONE_COLOR + +InterruptIn lock(p16); // Interrupts for buttons. +InterruptIn reposition(p17); +InterruptIn mute(p15); +InterruptIn new_patient(p14); +AnalogIn LDR_val(p18); +AnalogIn supplyvoltage(p20); // Analog input between 0 and 1 (0 and 100 %) for reading supplyvoltage from accupack. + +PwmOut LED_intern1(LED1); +DigitalOut LED_intern2(LED2); +DigitalOut LED_intern3(LED4); +neopixel::PixelArray array(p11); + +Timer hold_timer; +Timer delay; +Timer speaker_timer; + +DigitalOut speaker1(p21); +DigitalOut speaker2(p22); I2C i2c(p28, p27); // I2C MPU6050 agu(p28,p27); // Accelerometer/Gyroscope Unit @@ -9,6 +30,7 @@ Adafruit_ADS1115 pr2(&i2c, 0x49); // second PiëzoResistive ADC Adafruit_ADS1115 pel(&i2c, 0x4B); // PiëzoElectric ADC Serial pc(USBTX, USBRX); // tx, rx // Serial USB connection +Serial pi(p9, p10, 9600); // Setup serial communication for pi. Timer t; // Timer for equally time-spaced samples Ticker sample_cycle; // Polling cycle int cycle_time = 100000; // Cycle time in us @@ -20,6 +42,214 @@ int k = 0; float acce[3]; // Raw accelerometer data float gyro[3]; // Raw gyroscope data +char LED_colour; // Variable to set LED colour. +bool lock_state = 0, lock_flag = 0, mute_state = 0, alarm = 0, calibration_flag = 0, intensity_select = 0; // Boolean variables for states lock, mute and alarm. +bool mute_flag = 0, new_patient_flag = 0, reposition_flag = 0, power_plug_flag = 0, battery_voltage_flag = 0; +bool speaker_flag_current = 0, LED_red_flag_current = 0, LED_yellow_flag_current = 0, LED_green_flag_current = 0; +bool speaker_flag_last = 0, LED_red_flag_current = 0, LED_yellow_flag_current = 0, LED_green_flag_current = 0; +int locktime_ms = 2000; // Waittime in ms. +int calibrationtime_ms = 5000; +int calibration_flash; +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; +double alarm_voltage = 0.2; // Needed voltage for alarm expressed as a percentage (0 - 100 % => 0 - 3.3 V). +int red_var, green_var, blue_var, intensity, current_intensity = 0; // Variables to set LED intensity + +void set_intensity() // Function to set the intensity for the LED's +{ + if (intensity_select == 0) { + intensity = 50; + } else { + intensity = 25; + } + //intensity = (1-LDR_val)*100; // Calculate intensity (use right part of the graphic) + + //if (abs(intensity-current_intensity) > 5) { // If difference is greater then 5, change intensity dependent on range. + // if (intensity <= 20) { + // intensity = 20; + // } + + // if (40 >= intensity > 20) { + // intensity = 40; + // } + + // if (60 >= intensity > 40) { + // intensity = 60; + // } + + // if (80 >= intensity > 60) { + // intensity = 80; + // } + + // if (intensity > 80) { + // intensity = 100; + // } + //} + // current_intensity = intensity; // Save intensisty to compare in first if statement of this set_intensity function. +} + +void colour_select(char LED_colour) // Function to select the colour. +{ + set_intensity(); // Call function set_intensity + + if (LED_colour == 'r') { + red_var = (2.55*intensity); + green_var = 0; + blue_var = 0; + } + + if (LED_colour == 'y') { + red_var = (2.55*intensity); + green_var = (2.55*intensity); + blue_var = 0; + } + + if (LED_colour == 'g') { + red_var = 0; + green_var = (2.55*intensity); + blue_var = 0; + } + + if (calibration_flash >= 1) { + if ((calibration_flash % 2) == 0) { + red_var = 255; + green_var = 255; + blue_var = 255; + } else { + red_var = 0; + green_var = 0; + blue_var = 0; + } + calibration_flash--; + } +} + +void trigger_lock() // If rising edge lock button is detected start locktimer. +{ + hold_timer.start(); + delay.reset(); + delay.start(); +} + +void timer_lock() // End timer lock. +{ + lock_flag = 0; // Set lock_flag off. + hold_timer.stop(); // Stop and reset holdtimer + hold_timer.reset(); +} + +void trigger_reposition() +{ + if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. + } else { + delay.reset(); + delay.start(); + pi.printf("02\n"); // Seriele communicatie met PI. + + if (LED_intern1 == 0) { + LED_intern1 = 1.0; + } else { + LED_intern1 = 0.0; + } + + LED_colour = 'r'; + } +} + +void trigger_mute() +{ + if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. + mute_state = 0; + } else { + delay.reset(); + delay.start(); + pi.printf("01\n"); + mute_state = !mute_state; + + if (LED_intern1 == 0) { + LED_intern1 = 1.0; + } else { + LED_intern1 = 0.0; + } + + LED_colour = 'y'; + } +} + +void trigger_new_patient() // Function to trigger hold timer for new patient calibration function. +{ + if (lock_state == 1) { + } else { + hold_timer.start(); + } +} + +void timer_calibration() // Timer calibration function. +{ + hold_timer.stop(); + hold_timer.reset(); + + if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. + } else { + if (calibration_flag == 0) { + pi.printf("03\n"); + + + if (LED_intern1 == 0) { + LED_intern1 = 1.0; + } else { + LED_intern1 = 0.0; + } + + LED_colour = 'g'; + } else { + calibration_flag = 0; + } + } +} + +void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) // Generate LED colour. +{ + out->red = red_var; + out->green = green_var; + out->blue = blue_var; +} + +void read_voltage() +{ + LED_intern3 = 0; + + if (supplyvoltage.read() > alarm_voltage) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage. + alarm = 0; // Alarm is off. + } else { + alarm = 1; // Else alarm is on. + } + + if (alarm == 1 && mute_state == 0 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms. + speaker1 = 1; // Set speaker. + speaker2 = 1; + speaker_timer.start(); // Set timer for speaker to iterate on and off. + LED_intern3 = !LED_intern3; + } + + if (alarm == 1 && mute_state == 1 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms. + speaker1 = 0; // Set speaker. + speaker2 = 0; + speaker_timer.start(); // Set timer for speaker to iterate on and off. + LED_intern3 = !LED_intern3; + } + + if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) { + speaker1 = 0; // Turn off speaker (use two outputs because of currentlimiting of one). + speaker2 = 0; + } + + if (speaker_timer.read_ms() > (speaker_active_ms*2)) { + speaker_timer.stop(); // Stop speaker timer. + speaker_timer.reset(); + } +} void read_adc() { @@ -38,19 +268,41 @@ for (k = 0; k < 4; k = k + 1) { res[k+4] = pr2.readADC_SingleEnded(k); //Last 4 PR readout } - while(t.read_us()<(2*(cycle_time/5))) {} //Wait untill 40% of cycle + while(t.read_us()<(2*(cycle_time/5))) {} //Wait untill 40% of cycle elec[2] = pel.readADC_SingleEnded(0); //Third PE readout agu.getAccelero(acce); //Get accelerometer data angle = acce[2]*10; - + agu.getGyro(gyro); //Get gyroscope data + while(t.read_us()<(3*(cycle_time/5))) {} //Wait untill 60% of cycle elec[3] = pel.readADC_SingleEnded(0); //Fourth PE readout - agu.getGyro(gyro); //Get gyroscope data + if ((hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && lock == 1) { // If statement for lock function. + lock_flag = 1; + LED_intern2 = !LED_intern2; + lock_state = !lock_state; + } + + if ((hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && new_patient == 1) { // If statement for calibration system. + calibration_flag = 1; + calibration_flash = 11; + pi.printf("Calibration button is pressed."); // Print statement for serial communication to inform algorithm to calibrate. + } + if (delay.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active. + lock_state = 1; + LED_intern2 = 1; + } + + read_voltage(); // Supplyvoltage control for alarm. + + uint32_t val = 0; + colour_select(LED_colour); + array.update(generate, NLED, val); + while(t.read_us()<(4*(cycle_time/5))) {} //Wait untill 80% of cycle elec[4] = pel.readADC_SingleEnded(0); //Fifth PE readout @@ -58,6 +310,52 @@ while(t.read_us()<(4.5*(cycle_time/5))) {} //Wait untill 90% of cycle pc.printf(",%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\r\n", res[4], res[7], res[6], res[5], res[1], res[0], res[2], res[3], elec[0], elec[1], elec[2], elec[3], elec[4], acce[0]*100, acce[1]*100, acce[2]*100, gyro[0]*100, gyro[1]*100, gyro[2]*100); // print all to serial port //receiving order: 8 resistive sensors, 5 electric readings, 3 accelerometer axes, 3 gyroscope axes + + if (mute_flag == 1) { + pi.printf(">01\n"); + mute_flag = 0; + } + + if (new_patient_flag == 1) { + pi.printf(">02\n"); + new_patient_flag = 0; + } + + if (reposition_flag == 1) { + pi.printf(">03\n"); + reposition_flag = 0; + } + + if (power_plug_flag == 1) { + pi.printf("#%d\n", power_plug_flag); + power_plug_flag = 0; + } + + if (battery_voltage_flag == 1) { + pi.printf("%%d\n", battery_voltage); + battery_voltage_flag = 0; + } + + if (speaker_flag_current == speaker_flag_last) { + pi.printf("&07\n"); + } else if (speaker_flag_current != speaker_flag_last) { + pi.printf("&70\n"); + } + + if (LED_red_flag == 1) { + pi.printf("& \n"); + LED_red_flag = 0; + } + + if (LED_yellow_flag == 1) { + pi.printf("& \n"); + LED_yellow_flag = 0; // Een event bij deactivatie + } + + if (LED_green_flag == 1) { + pi.printf("& \n"); + LED_green_flag = 0; + } } int main() @@ -67,8 +365,300 @@ pr1.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V pr2.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V pel.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V + pi.format(8, SerialBase::None, 1); + + lock.rise(&trigger_lock); // Interrupt for rising edge lock button. + lock.fall(&timer_lock); + reposition.rise(&trigger_reposition); + mute.rise(&trigger_mute); + new_patient.rise(&trigger_new_patient); // New patient/calibration button rising event. + new_patient.fall(&timer_calibration); // Falling edge for calibration algorithm option. + delay.reset(); // Delaytimer reset en start. + delay.start(); + sample_cycle.attach_us(&read_adc, cycle_time); + while (1) { wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms } -} \ No newline at end of file +} + +/* +Author : R. Molenaar +Company : Momo Medical +Source : developer.mbed.org +File : main.cpp +Version | -date : 0.1 | 18-9-2017 + + +#include "mbed.h" +#include "pwm_tone.h" +#include "neopixel.h" +#define NLED (11) +#define ONE_COLOR + +Serial pi(p9, p10, 9600); // Setup serial communication. +Serial pc(USBTX, USBRX, 9600); + +InterruptIn lock(p16); // Interrupts for buttons. +InterruptIn reposition(p17); +InterruptIn mute(p15); +InterruptIn new_patient(p14); + +AnalogIn LDR_val(p18); +AnalogIn supplyvoltage(p20); // Analog input between 0 and 1 (0 and 100 %) for reading supplyvoltage from accupack. + +PwmOut LED_intern1(LED1); +DigitalOut LED_intern2(LED2); +DigitalOut LED_intern3(LED4); + +Timer hold_timer; +Timer delay; +Timer speaker_timer; + +DigitalOut speaker1(p21); +DigitalOut speaker2(p22); + +char LED_colour; // Variable to set LED colour. +bool lock_state, lock_flag, mute_state, alarm, calibration_flag; // Boolean variables for states lock, mute and alarm. +int locktime_ms = 2000; // Waittime in ms. +int calibrationtime_ms = 5000; +int calibration_flash; +int buttondelay_ms = 750; // Button delay in ms. +int delay_lock_interface = 3000*60; // Delay for non using interface. +int speaker_active_ms = 750; +double alarm_voltage = 0.2; // Needed voltage for alarm expressed as a percentage (0 - 100 % => 0 - 3.3 V). +int red_var, green_var, blue_var, intensity, current_intensity = 0; // Variables to set LED intensity + +void set_intensity() // Function to set the intensity for the LED's +{ + intensity = (1-LDR_val)*100; // Calculate intensity (use right part of the graphic) + + if (abs(intensity-current_intensity) > 5) { // If difference is greater then 5, change intensity dependent on range. + if (intensity <= 20) { + intensity = 20; + } + + if (40 >= intensity > 20) { + intensity = 40; + } + + if (60 >= intensity > 40) { + intensity = 60; + } + + if (80 >= intensity > 60) { + intensity = 80; + } + + if (intensity > 80) { + intensity = 100; + } + } + current_intensity = intensity; // Save intensisty to compare in first if statement of this set_intensity function. +} + +void colour_select(char LED_colour) // Function to select the colour. +{ + set_intensity(); // Call function set_intensity + + if (LED_colour == 'r') { + red_var = (2.55*intensity); + green_var = 0; + blue_var = 0; + } + + if (LED_colour == 'y') { + red_var = (2.55*intensity); + green_var = (2.55*intensity); + blue_var = 0; + } + + if (LED_colour == 'g') { + red_var = 0; + green_var = (2.55*intensity); + blue_var = 0; + } + + if (calibration_flash >= 1) { + if((calibration_flash % 2) == 0) { + red_var = 255; + green_var = 255; + blue_var = 255; + } else { + red_var = 0; + green_var = 0; + blue_var = 0; + } + calibration_flash--; + } +} + + +void trigger_lock() // If rising edge lock button is detected start locktimer. +{ + hold_timer.start(); + delay.reset(); + delay.start(); +} + +void timer_lock() // End timer lock. +{ + lock_flag = 0; // Set lock_flag off. + hold_timer.stop(); // Stop and reset holdtimer + hold_timer.reset(); +} + +void trigger_reposition() +{ + if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. + } else { + delay.reset(); + delay.start(); + pi.printf("02\n"); // Seriele communicatie met PI. + + if (LED_intern1 == 0) { + LED_intern1 = 1.0; + } else { + LED_intern1 = 0.0; + } + + LED_colour = 'r'; + } +} + +void trigger_mute() +{ + if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. + mute_state = 0; + } else { + delay.reset(); + delay.start(); + pi.printf("01\n"); + mute_state = !mute_state; + + if (LED_intern1 == 0) { + LED_intern1 = 1.0; + } else { + LED_intern1 = 0.0; + } + + LED_colour = 'y'; + } +} + +void trigger_new_patient() // Function to trigger hold timer for new patient calibration function. +{ + if (lock_state == 1) { + } else { + hold_timer.start(); + } +} + +void timer_calibration() // Timer calibration function. +{ + hold_timer.stop(); + hold_timer.reset(); + if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. + } else { + if (calibration_flag == 0) { + pi.printf("03\n"); + + + if (LED_intern1 == 0) { + LED_intern1 = 1.0; + } else { + LED_intern1 = 0.0; + } + + LED_colour = 'g'; + } else { + calibration_flag = 0; + } + } +} + +void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) // Generate LED colour. +{ + out->red = red_var; + out->green = green_var; + out->blue = blue_var; +} + +void read_voltage() +{ + LED_intern3 = 0; + + if (supplyvoltage.read() > alarm_voltage) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage. + alarm = 0; // Alarm is off. + } else { + alarm = 1; // Else alarm is on. + } + + if (alarm == 1 && mute_state == 0 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms. + speaker1 = 1; // Set speaker. + speaker2 = 1; + speaker_timer.start(); // Set timer for speaker to iterate on and off. + LED_intern3 = !LED_intern3; + } + + if (alarm == 1 && mute_state == 1 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms. + speaker1 = 0; // Set speaker. + speaker2 = 0; + speaker_timer.start(); // Set timer for speaker to iterate on and off. + LED_intern3 = !LED_intern3; + } + + if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) { + speaker1 = 0; // Turn off speaker (use two outputs because of currentlimiting of one). + speaker2 = 0; + } + + if (speaker_timer.read_ms() > (speaker_active_ms*2)) { + speaker_timer.stop(); // Stop speaker timer. + speaker_timer.reset(); + } +} + +int main() +{ + pi.format(8, SerialBase::None, 1); + + lock.rise(&trigger_lock); // Interrupt for rising edge lock button. + lock.fall(&timer_lock); + reposition.rise(&trigger_reposition); + mute.rise(&trigger_mute); + new_patient.rise(&trigger_new_patient); // New patient/calibration button rising event. + new_patient.fall(&timer_calibration); // Falling edge for calibration algorithm option. + delay.reset(); // Delaytimer reset en start. + delay.start(); + + neopixel::PixelArray array(p11); + + while(1) { + wait_ms(100); // Simulate 100 ms delay from sensorplate code. + + if ((hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && lock == 1) { // If statement for lock function. + lock_flag = 1; + LED_intern2 = !LED_intern2; + lock_state = !lock_state; + } + + if ((hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && new_patient == 1) { // If statement for calibration system. + calibration_flag = 1; + calibration_flash = 11; + pi.printf("Calibration button is pressed."); // Print statement for serial communication to inform algorithm to calibrate. + + if (delay.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active. + lock_state = 1; + LED_intern2 = 1; + } + + read_voltage(); // Supplyvoltage control for alarm. + + uint32_t val = 0; + colour_select(LED_colour); + array.update(generate, NLED, val); + } + +}*/ \ No newline at end of file