Pilot 1 working code (excluding new patient after calculation). %d changed in %f sensordata serial log.
Dependencies: ADS1015 MPU6050 PixelArray mbed
Fork of Momo_New by
Sensorplate/main.cpp
- Committer:
- ricardo_95
- Date:
- 2017-10-02
- Revision:
- 23:4a09554bdc1a
- Parent:
- 22:a09775c25890
- Child:
- 24:782c4dc4a3ff
File content as of revision 23:4a09554bdc1a:
/********************* CODE INFORMATIE ****************************** Author : Danny Eldering & Ricardo Molenaar Company : Momo Medical Source : developer.mbed.org File : main.cpp Version | -date : 1.0 | 28-9-2017 */ /************************ CONFIG ***********************************/ #include "mbed.h" #include "Adafruit_ADS1015.h" #include "MPU6050.h" #include "neopixel.h" #define NLED (3) #define ONE_COLOR InterruptIn lock(p16); // Interrupts for buttons. InterruptIn reposition(p17); InterruptIn mute(p15); InterruptIn new_patient(p18); DigitalIn supplyvoltage(p20); // Analog input between 0 and 1 for reading supplyvoltage from measuringpoint before power supply. DigitalOut LED_intern1(LED1); DigitalOut LED_intern2(LED2); DigitalOut LED_intern3(LED3); neopixel::PixelArray array(p11); Timer lock_hold_timer; Timer calibration_hold_timer; Timer delay; Timer speaker_timer; //Timer led_timer; DigitalOut speaker1(p21); DigitalOut speaker2(p22); PwmOut lock_LED(p23); PwmOut reposition_LED(p24); PwmOut mute_LED(p25); PwmOut new_patient_LED(p26); I2C i2c(p28, p27); // I2C I2C i2cAccu(p9, p10); // I2C for accupack MPU6050 agu(p28,p27); // Accelerometer/Gyroscope Unit Adafruit_ADS1115 pr1(&i2c, 0x48); // first PiëzoResistive ADC Adafruit_ADS1115 pr2(&i2c, 0x49); // second PiëzoResistive ADC Adafruit_ADS1115 pel(&i2c, 0x4B); // PiëzoElectric ADC Adafruit_ADS1115 adsAccu(&i2cAccu, 0x48); Serial pc(USBTX, USBRX); // tx, rx // Serial USB connection Serial pi(p13, p14); // tx, rx // Setup serial communication for pi. Timer t; // Timer for equally time-spaced samples Ticker sample_cycle; // Polling cycle int boot_delay_ms = 500; int cycle_time = 100000; // Cycle time in us int i2c_freq = 400000; // I2C Frequency int baud = 115200; // Baud rate short res[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle short elec[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle int angle = 0; // Accelerometer Z-axis int k = 0; float acce[3]; // Raw accelerometer data float gyro[3]; // Raw gyroscope data char LED_colour = 'g'; // 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 logging. bool mute_flag = 0, new_patient_flag = 0, reposition_flag = 0, sensorplate_connect = 0; 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; 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 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 red_var, green_var, blue_var; // Variables to set LED intensity. short batteryvoltage_current = 0, batteryvoltage_last = 0, powervoltage_current, powervoltage_last; // Variables to manage batteryvoltage. int intensity_day = 50, intensity_night = 25; // 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. /*************************** CODE ********************************/ void set_intensity() // Function to set the intensity for the LED's. { if (intensity_select == 1) { intensity = intensity_day; } else { intensity = intensity_night; } control_LED_intensity = (intensity/100); pc.printf("control_LED_intensity = %f\n", control_LED_intensity); } void serial_read() // Serial read for select LED intensity and colour. { if (pi.readable()) { char message[10]; if (message[0] == '0') { intensity_select = 0; } if (message[0] == '1') { intensity_select = 1; } if (message[1] == 'g') { LED_colour = 'g'; } if (message[1] == 'y') { LED_colour = 'y'; } if (message[1] == 'r') { LED_colour = 'r'; } } } void serial_log() { if (mute_flag == 1) { pi.printf(">01\n"); pc.printf(">01\n"); mute_flag = 0; } if (new_patient_flag == 1) { pi.printf(">03\n"); pc.printf(">03\n"); new_patient_flag = 0; } if (reposition_flag == 1) { pi.printf(">02\n"); pc.printf(">02\n"); reposition_flag = 0; } if (batteryvoltage_current != batteryvoltage_last) { pi.printf("%%d\n", batteryvoltage_current); batteryvoltage_last = batteryvoltage_current; } if (LED_red_logged != LED_red_state) { if (LED_red_state == 1) { pi.printf("&04\n"); LED_red_logged = LED_red_state; } if (LED_red_state == 0) { pi.printf("&40\n"); LED_red_logged = LED_red_state; } } if (LED_yellow_logged != LED_yellow_state) { if (LED_yellow_state == 1) { pi.printf("&06\n"); LED_yellow_logged = LED_yellow_state; } if (LED_yellow_state == 0) { pi.printf("&60\n"); LED_yellow_logged = LED_yellow_state; } } if (LED_green_logged != LED_green_state) { if (LED_green_state == 1) { pi.printf("&05\n"); LED_green_logged = LED_green_state; } if (LED_green_state == 0) { pi.printf("&50\n"); LED_green_logged = LED_green_state; } } if (speaker_logged != speaker_state) { if (speaker_state == 1) { pi.printf("&07\n"); speaker_logged = speaker_state; } if (speaker_state == 0) { pi.printf("&70\n"); speaker_logged = speaker_state; } } if (power_plug_logged != power_plug_state) { if (power_plug_state == 1) { pi.printf("#08\n"); pc.printf("#08 power on\n"); power_plug_logged = power_plug_state; } if (power_plug_state == 0) { pi.printf("#80\n"); pc.printf("#08 power off\n"); power_plug_logged = power_plug_state; } } } 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; LED_red_state = 1; } else { LED_red_state = 0; } if (LED_colour == 'y') { red_var = (2.55*intensity); green_var = (2.55*intensity); blue_var = 0; LED_yellow_state = 1; } else { LED_green_state = 0; } if (LED_colour == 'g') { red_var = 0; green_var = (2.55*intensity); blue_var = 0; LED_green_state = 1; } else { LED_green_state = 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. { pc.printf("Lock triggered.\n"); lock_hold_timer.reset(); lock_hold_timer.start(); delay.reset(); delay.start(); } void timer_lock() // End timer lock. { lock_flag = 0; // Set lock_flag off. lock_hold_timer.stop(); // Stop and reset holdtimer lock_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(); pc.printf("Reposition triggered.\n"); if (LED_intern1 == 0) { LED_intern1 = 1; } else { LED_intern1 = 0; } reposition_flag = 1; reposition_LED = control_LED_intensity; } } void rise_reposition() { reposition_LED = 0; } 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. } else { delay.reset(); delay.start(); mute_state = !mute_state; if (mute_state == 1) { mute_LED = control_LED_intensity; } else { mute_LED = 0; } pc.printf("Mute triggered %d.\n",mute_state); if (LED_intern1 == 0) { LED_intern1 = 1; } else { LED_intern1 = 0; } mute_flag = 1; } } void trigger_new_patient() // Function to trigger hold timer for new patient calibration function. { if (lock_state == 1) { } else { calibration_hold_timer.reset(); calibration_hold_timer.start(); new_patient_LED = control_LED_intensity;; pc.printf("New patient triggered.\n"); } } void timer_calibration() // Timer calibration function. { new_patient_LED = 0; if (0 < calibration_hold_timer.read_ms() < calibrationtime_ms) { new_patient_flag = 1; } calibration_hold_timer.stop(); calibration_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) { if (LED_intern1 == 0) { LED_intern1 = 1; } else { LED_intern1 = 0; } } else { calibration_flag = 0; } } } void timer_functions() { if ((lock_hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && lock == 0) { // If statement for lock function. lock_flag = 1; LED_intern2 = !LED_intern2; lock_state = !lock_state; if (lock_state == 0) { lock_LED = control_LED_intensity; } else { lock_LED = 0; } } if ((calibration_hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && new_patient == 0 && lock_state == 0) { // If statement for calibration system. calibration_flag = 1; calibration_flash = 11; pc.printf("Calibrate triggered.\n"); pi.printf(">30\n"); // 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; lock_LED = 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 set_ui_LED() // Control functions for LED above buttons (added because of failures). { if (lock_state == 1) { } else { if (reposition == 0) { reposition_LED = control_LED_intensity; } else { reposition_LED = 0; } if (new_patient == 0) { new_patient_LED = control_LED_intensity; } else { new_patient_LED = 0; } } } void read_voltage() { if (power_plug_state == 1) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage. alarm = 0; // Alarm is off. speaker_state = 0; } else { alarm = 1; // Else alarm is on. speaker_state = 1; } if (alarm == 1 && mute_state == 1 && (batteryvoltage_current > alarm_voltage)) { // Set speaker on for 750 ms. speaker1 = 0; // Set speaker. speaker2 = 0; } if ((alarm == 1 && mute_state == 0 && (speaker_timer.read_ms() < speaker_active_ms)) || ((batteryvoltage_current < alarm_voltage) && (speaker_timer.read_ms() < speaker_active_ms))) { // Set speaker on for 750 ms. speaker1 = 1; // Set speaker. speaker2 = 1; speaker_timer.reset(); speaker_timer.start(); // Set timer for speaker to iterate on and off. } 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(); } batteryvoltage_current = adsAccu.readADC_SingleEnded(0); // Read channel 0 from external ADC. powervoltage_current = adsAccu.readADC_SingleEnded(1); // Read channel 1 from external ADC. if (powervoltage_current < 20000) { power_plug_state = 0; } else { power_plug_state = 1; } } void read_adc() { t.reset(); t.start(); if (sensorplate_connect == 1) { elec[0] = pel.readADC_SingleEnded(0); // First PE readout for (k = 0; k < 4; k = k + 1) { res[k] = pr1.readADC_SingleEnded(k); // First 4 PR readout } while(t.read_us()<(1*(cycle_time/5))) {} // Wait untill 20% of cycle elec[1] = pel.readADC_SingleEnded(0); // Second PE readout 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 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 } timer_functions(); batteryvoltage_current = batteryvoltage_last; powervoltage_current = powervoltage_last; read_voltage(); // Supplyvoltage control for alarm. pc.printf("Voltage = %d , %d\n", batteryvoltage_current, powervoltage_current); uint32_t val = 0; colour_select(LED_colour); array.update(generate, NLED, val); set_ui_LED(); while(t.read_us()<(4*(cycle_time/5))) {} // Wait untill 80% of cycle if (sensorplate_connect == 1) { elec[4] = pel.readADC_SingleEnded(0); // Fifth PE readout } while(t.read_us()<(4.25*(cycle_time/5))) {} // Wait untill 85% of cycle pi.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%f,%f,\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 serial_read(); serial_log(); } int main() { wait_ms(boot_delay_ms); // Wait to boot sensorplate first i2c.frequency(i2c_freq); i2cAccu.frequency(i2c_freq); pc.baud(baud); pi.baud(baud); 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 adsAccu.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V pi.format(8, SerialBase::None, 1); lock.fall(&trigger_lock); // Interrupt for rising edge lock button. lock.rise(&timer_lock); reposition.fall(&trigger_reposition); reposition.rise(&rise_reposition); mute.fall(&trigger_mute); new_patient.fall(&trigger_new_patient); // New patient/calibration button rising event. new_patient.rise(&timer_calibration); // Falling edge for calibration algorithm option. delay.reset(); // Delaytimer reset en start. delay.start(); set_intensity(); lock_LED = control_LED_intensity; // Lock LED initialization. sample_cycle.attach_us(&read_adc, cycle_time); while (1) { wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms } }