Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813

Fork of Nucleo-transfer by Momo Medical

Revision:
34:1614f4f2b841
Parent:
33:df21cb8dc5c7
Child:
35:e9026c40726e
--- a/Sensorplate/main.cpp	Wed Oct 11 12:56:17 2017 +0000
+++ b/Sensorplate/main.cpp	Thu Oct 12 09:29:51 2017 +0000
@@ -1,15 +1,15 @@
 /********************* CODE INFORMATIE ******************************
-Date of creation:           20-12-1908
-Author:                     Elke Salzmann
+Date of creation:           30-09-2017
+Authors:                    Danny Eldering & Ricardo Molenaar
 co-authors:                 Menno Gravemaker
-(c) Copyright by Momo Medical BV. 
- 
+(c) Copyright by Momo Medical BV.
+
 Current version name:                   2.1.2
-Date of creation:                       14-01-2017
-Purpose of this file                    Oneliner of purpose 
-Update ‘what’s new in this version?’:   short meaningful description (no more than 3 lines) 
-Todo:                                   short meaningful description of next actions that could be worked on(less than 3 lines) 
-Source file:                            http://mbed.com 
+Date of modification:                   11-10-2017
+Purpose of this file:                   Oneliner of purpose
+Update ‘what’s new in this version?’:   short meaningful description (no more than 3 lines)
+Todo:
+Source file:                            http://mbed.com
 
 */
 
@@ -20,46 +20,48 @@
 #include "MPU6050.h"
 #include "MPU6050_belt.h"
 #include "neopixel.h"
-#define NLED (3)
+#define NLED (3)                                                            // to do: nled uitschrijven
 #define ONE_COLOR
 
-InterruptIn lock(p15);                                                      // Interrupts for buttons.
+InterruptIn lock(p15);                                                      // Interrupts for buttons. todo: button toevoegen
 InterruptIn reposition(p17);
 InterruptIn mute(p16);
 InterruptIn new_patient(p18);
 
-DigitalOut  LED_intern1(LED1);
+DigitalOut  LED_intern1(LED1);                                              // todo: intern veranderen in on_dev_board oid
 DigitalOut  LED_intern2(LED2);
 DigitalOut  LED_intern3(LED3);
 DigitalOut  LED_intern4(LED4);
-neopixel::PixelArray array(p11);
+neopixel::PixelArray array(p11);                                            // todo: array -> wat ermee gedaan wordt
 
 Timer lock_hold_timer;
 Timer calibration_hold_timer;
-Timer delay;
+Timer delay;                                                                // todo: delay -> delay_between_2_buttons_pressed
 Timer speaker_timer;
-Timer test_timer;
+Timer test_timer;                                                           // todo: naar testsectie
 
 DigitalOut speaker1(p21);
 DigitalOut speaker2(p22);
-PwmOut lock_LED(p23);
+PwmOut lock_LED(p23);                                                       // todo: button toevoegen bijv. feedback_LED_lock
 PwmOut reposition_LED(p25);
 PwmOut mute_LED(p26);
 PwmOut new_patient_LED(p24);
-
-I2C i2c(p28, p27);                  // I2C
-I2C i2cAccu(p9, p10);               // I2C for accupack
+// todo: comments for library hardcoding etc.
+I2C i2c(p28, p27);                  // I2C   todo: i2c_sensorplaat
+I2C i2cAccu(p9, p10);               // I2C for accupack todo: i2c_voltage_measurement
 MPU6050 agu(p28,p27);               // Accelerometer/Gyroscope Unit
 MPU6050_belt agu_belt(p28,p27);     // Accelerometer/Gyroscope Unit Belt
 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 pc(USBTX, USBRX); // tx, rx  // Serial USB connection todo -> test en uart toevoegen ook bij pi
 Serial pi(p13, p14);     // tx, rx  // Setup serial communication for pi.
 Timer t;                            // Timer for equally time-spaced samples
 Ticker sample_cycle;                // Polling cycle
 
+// todo: uitschrijven namen
+
 int boot_delay_ms = 500;
 int cycle_time = 100000;            // Cycle time in us
 int i2c_freq = 400000;              // I2C Frequency
@@ -92,13 +94,15 @@
 
 /*************************** TEST ********************************/
 // Verify algoritm function: for belt activation, set test_belt 1 (connect pin p20 to 3.3V)
-DigitalIn test_pin(p20, PullDown);
+DigitalIn test_pin(p30, PullDown);
 bool test_belt = 1; //test_pin;
 
 // Verify if interrupts are working properly
 
-// Verify if parameters are 
-bool test_mode = 1;
+
+// Verify if parameters are
+bool test_mode = 0;
+
 
 /*************************** CODE ********************************/
 
@@ -110,7 +114,7 @@
         intensity = intensity_night;
     }
     control_LED_intensity = (intensity/100);
-    
+
     if (test_mode == 1) {
         pc.printf("Intensity LED's shines to wall = %f\n", intensity);
         pc.printf("Intensity LED's above buttons = %f\n", control_LED_intensity);
@@ -120,14 +124,22 @@
 void serial_read()                                                          // Serial read for select LED intensity and colour.
 {
     if (pi.readable()) {
-        char message[4];
+        char message[10];
         pi.scanf("%s", message);
+        pc.printf("Message = %s, Intensity_select = %d en LED_colour = %c\n", message, intensity_select, LED_colour);
         
-        intensity_select = message[0];
-        LED_colour = message[1];
-        
+        if (intensity_select != (message[0]-'0')) {
+            intensity_select = (message[0]-'0');
+        }
+
+        if (LED_colour != message[1]) {
+            LED_colour = message[1];
+        }
+
+        pc.printf("Intensity_select = %d en LED_colour = %c\n", intensity_select, LED_colour);
+
         if (test_mode == 1) {
-            pc.printf("%s", message);   
+            pc.printf("message: %s\n", message);
         }
     }
 }
@@ -136,41 +148,41 @@
 {
     if (mute_flag == 1) {
         pi.printf(">01\n");
-        
-        if (test_mode == 1) { 
+
+        if (test_mode == 1) {
             pc.printf(">01\n");
         }
-        
+
         mute_flag = 0;
     }
 
     if (new_patient_flag == 1) {
         pi.printf(">03\n");
-        
-        if (test_mode == 1) { 
+
+        if (test_mode == 1) {
             pc.printf(">03\n");
         }
-        
+
         new_patient_flag = 0;
     }
 
     if (reposition_flag == 1) {
         pi.printf(">02\n");
-        
-        if (test_mode == 1) { 
+
+        if (test_mode == 1) {
             pc.printf(">02\n");
         }
-        
+
         reposition_flag = 0;
     }
 
     if (batteryvoltage_current != batteryvoltage_last) {
-        pi.printf("%%d\n", batteryvoltage_current);
-        
-        if (test_mode == 1) {    
-            pc.printf("%%d\n", batteryvoltage_current);
+        pi.printf("%%" "%d\n", batteryvoltage_current);
+
+        if (test_mode == 1) {
+            pc.printf("%%" "%d\n", batteryvoltage_current);
         }
-        
+
         batteryvoltage_last = batteryvoltage_current;
     }
 
@@ -178,10 +190,16 @@
         if (LED_red_state == 1) {
             pi.printf("&04\n");
             LED_red_logged = LED_red_state;
+            if (test_mode == 1) {
+                pc.printf("&04\n");
+            }
         }
         if (LED_red_state == 0) {
             pi.printf("&40\n");
             LED_red_logged = LED_red_state;
+            if (test_mode == 1) {
+                pc.printf("&40\n");
+            }
         }
     }
 
@@ -189,10 +207,16 @@
         if (LED_yellow_state == 1) {
             pi.printf("&06\n");
             LED_yellow_logged = LED_yellow_state;
+            if (test_mode == 1) {
+                pc.printf("&06\n");
+            }
         }
         if (LED_yellow_state == 0) {
             pi.printf("&60\n");
             LED_yellow_logged = LED_yellow_state;
+            if (test_mode == 1) {
+                pc.printf("&60\n");
+            }
         }
     }
 
@@ -200,10 +224,16 @@
         if (LED_green_state == 1) {
             pi.printf("&05\n");
             LED_green_logged = LED_green_state;
+            if (test_mode == 1) {
+                pc.printf("&05\n");
+            }
         }
         if (LED_green_state == 0) {
             pi.printf("&50\n");
             LED_green_logged = LED_green_state;
+            if (test_mode == 1) {
+                pc.printf("&50\n");
+            }
         }
     }
 
@@ -211,73 +241,88 @@
         if (speaker_state == 1) {
             pi.printf("&07\n");
             speaker_logged = speaker_state;
+            if (test_mode == 1) {
+                pc.printf("&07\n");
+            }
         }
         if (speaker_state == 0) {
             pi.printf("&70\n");
             speaker_logged = speaker_state;
+            if (test_mode == 1) {
+                pc.printf("&70\n");
+            }
         }
     }
 
     if (power_plug_logged != power_plug_state) {
         if (power_plug_state == 1) {
             pi.printf("#08\n");
-            pc.printf("#08 power on\n");
+            if (test_mode == 1) {
+                pc.printf("#08\n");
+            }
             power_plug_logged = power_plug_state;
         }
         if (power_plug_state == 0) {
             pi.printf("#80\n");
-            pc.printf("#08 power off\n");
+            if (test_mode == 1) {
+                pc.printf("#80\n");
+            }
             power_plug_logged = power_plug_state;
         }
     }
-    
     if (a == 1) {
-        pi.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%f,%f,%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], acce[1], acce[2], gyro[0], gyro[1], gyro[2], acce_belt[0], acce_belt[1], acce_belt[2], gyro_belt[0], gyro_belt[1], gyro_belt[2]); // print all to serial port
-        pc.printf("Belt accelerometer values: %f,%f,%f,%f,%f,%f\n", acce_belt[0], acce_belt[1], acce_belt[2], gyro_belt[0], gyro_belt[1], gyro_belt[2]);
+        //receiving order: 8 resistive sensors, 5 electric readings
+        pi.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\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]); // print all to serial port
+        if (test_mode == 1) {
+            pc.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\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]); // print all to serial port
+        }
     }
-    //receiving order: 8 resistive sensors, 5 electric readings, 3 accelerometer axes, 3 gyroscope axes
-    //Belt values print to pc
+
 }
 
 void colour_select(char LED_colour)                                         // Function to select the colour.
 {
     set_intensity();                                                        // Call function set_intensity
-
-    red_var = 0;
-    green_var = 0;
-    blue_var = 0;
-
-    if (LED_colour == 'r') {
-        red_var = (2.55*intensity);
+    
+    if ((LED_colour == 'r') || (LED_colour == 'g') || (LED_colour == 'b') || (LED_colour == 'y')) { 
+        red_var = 0;
         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;
+        
+        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 (LED_colour == 'b') {
+            red_var = 0;
+            green_var = 0;
+            blue_var = (2.55*intensity);
+        }
     } 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 (LED_colour == 'b') {
-        red_var = 0;
-        green_var = 0;
-        blue_var = (2.55*intensity);
+        LED_colour = LED_colour;
     }
 
     if (calibration_flash >= 1) {
@@ -298,7 +343,9 @@
 
 void trigger_lock()                                                         // If rising edge lock button is detected start locktimer.
 {
-    pc.printf("Lock triggered.\n");
+    if (test_mode == 1) {
+        pc.printf("Lock triggered.\n");
+    }
     lock_hold_timer.reset();
     lock_hold_timer.start();
     delay.reset();
@@ -307,6 +354,9 @@
 
 void timer_lock()                                                           // End timer lock.
 {
+    if (test_mode == 1) {
+        pc.printf("Lock released.\n");
+    }
     lock_flag = 0;                                                          // Set lock_flag off.
     lock_hold_timer.stop();                                                 // Stop and reset holdtimer
     lock_hold_timer.reset();
@@ -318,13 +368,10 @@
     } else {
         delay.reset();
         delay.start();
-        pc.printf("Reposition triggered.\n");
-        if (LED_intern1 == 0) {
-            LED_intern1 = 1;
-        } else {
-            LED_intern1 = 0;
+        if (test_mode == 1) {
+            pc.printf("Reposition triggered.\n");
+            LED_intern1 = !LED_intern1;
         }
-
         reposition_flag = 1;
 
         reposition_LED = control_LED_intensity;
@@ -333,7 +380,11 @@
 
 void rise_reposition()
 {
+    if (test_mode == 1) {
+        pc.printf("Reposition released.\n");
+    }
     reposition_LED = 0;
+
 }
 
 void trigger_mute()
@@ -349,11 +400,9 @@
         } else {
             mute_LED = 0;
         }
-        pc.printf("Mute triggered %d.\n",mute_state);
-        if (LED_intern1 == 0) {
-            LED_intern1 = 1;
-        } else {
-            LED_intern1 = 0;
+        if (test_mode == 1) {
+            pc.printf("Mute triggered %d.\n",mute_state);
+            LED_intern1 = !LED_intern1;
         }
 
         mute_flag = 1;
@@ -363,17 +412,22 @@
 void trigger_new_patient()                                                  // Function to trigger hold timer for new patient calibration function.
 {
 
-    if (lock_state == 1) {
+    if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) {
     } else {
         calibration_hold_timer.reset();
         calibration_hold_timer.start();
         new_patient_LED = control_LED_intensity;;
-        pc.printf("New patient triggered.\n");
+        if (test_mode == 1) {
+            pc.printf("New patient triggered.\n");
+        }
     }
 }
 
 void timer_calibration()                                                    // Timer calibration function.
 {
+    if (test_mode == 1) {
+        pc.printf("New patient released.\n");
+    }
     new_patient_LED = 0;
 
     if (0 < calibration_hold_timer.read_ms() < calibrationtime_ms) {
@@ -401,7 +455,6 @@
 
 void timer_functions()
 {
-    pc.printf("Locktime = %d\n",lock_hold_timer.read_ms());
     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;
@@ -416,7 +469,9 @@
     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");
+        if (test_mode == 1) {
+            pc.printf("Calibrate triggered.\n");
+        }
         pi.printf(">30\n");                                                 // Print statement for serial communication to inform algorithm to calibrate.
     }
 
@@ -532,12 +587,13 @@
             angle = acce[2]*100;
         }
         agu.getGyro(gyro);                                                  // Get gyroscope data
-        
+
         if (test_belt == 1) {
             agu_belt.getGyro(gyro_belt);                                        // Get gyroscope data from Belt
             agu_belt.getAccelero(acce_belt);                                    // Get accelerometer data from belt
         }
-        
+        pi.printf("?,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n", acce[0], acce[1], acce[2], gyro[0], gyro[1], gyro[2],acce_belt[0], acce_belt[1], acce_belt[2], gyro_belt[0], gyro_belt[1], gyro_belt[2]);
+
         while(t.read_us()<(3*(cycle_time/5))) {}                            // Wait untill 60% of cycle
 
         elec[3] = pel.readADC_SingleEnded(0);                               // Fourth PE readout
@@ -548,8 +604,9 @@
     batteryvoltage_current = batteryvoltage_last;
     powervoltage_current = powervoltage_last;
     read_voltage();                                                         // Supplyvoltage control for alarm.
-    pc.printf("Voltage = %d   ,   %d\n", batteryvoltage_current, powervoltage_current);
-
+    if (test_mode == 1) {
+        pc.printf("Voltage = %d   ,   %d\n", batteryvoltage_current, powervoltage_current);
+    }
     uint32_t val = 0;
     colour_select(LED_colour);
     array.update(generate, NLED, val);
@@ -566,6 +623,23 @@
 
     serial_read();
     serial_log();
+    if (test_mode ==1) {
+        pc.printf("Loop time: %d ms\n",t.read_ms());
+    }
+    if (test_pin == 1) {
+        test_mode = 1;
+        pc.printf("%d\n",test_mode);
+    }
+    if (test_pin == 0) {
+        test_mode = 0;
+        pc.printf("%d\n",test_mode);
+    }
+    if (test_mode == 1) {
+        //cycle_time = 500000;
+    } else {
+        cycle_time = 100000;
+    }
+    pc.printf("Loop time: %d ms\n",t.read_ms());
 }
 
 int main()
@@ -594,6 +668,7 @@
     set_intensity();
     lock_LED = control_LED_intensity;                                       // Lock LED initialization.
 
+
     sample_cycle.attach_us(&read_adc, cycle_time);
 
     while (1) {