Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813

Fork of Nucleo-transfer by Momo Medical

Revision:
58:8cfa736d8553
Parent:
57:fac732476810
Child:
60:2f7e82c6f916
Child:
65:7cd5eb750efe
diff -r fac732476810 -r 8cfa736d8553 Sensorplate/main.cpp
--- a/Sensorplate/main.cpp	Wed Feb 28 10:03:09 2018 +0000
+++ b/Sensorplate/main.cpp	Tue Mar 06 15:48:35 2018 +0000
@@ -93,7 +93,7 @@
 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.
+short piezo_electric_array[6] = {0,0,0,0,0,0};                                        // 1 PE sensor 5 times per cycle.
 int angle = 0;                                                                      // Accelerometer Z-axis.
 float accelerometer_sensorplate[3] = {0.0, 0.0, 0.0};                               // Raw accelerometer data.
 float gyroscope_sensorplate[3];                                                     // Raw gyroscope data.
@@ -189,7 +189,8 @@
     }
 }
 
-void serial_log() {                                                                   // Function for serial logging. See link to table with code declarations above in code.
+void serial_log()                                                                     // Function for serial logging. See link to table with code declarations above in code.
+{
 
     if (reposition_flag == 1) {                                                     // If statement to control logging for reposition button.
         pi_serial.printf(">01\n");
@@ -200,7 +201,7 @@
 
         reposition_flag = 0;
     }
-    
+
     if (reposition_lock_flag == 1) {                                                     // If statement to control logging for reposition button.
         pi_serial.printf(">10\n");
 
@@ -220,7 +221,7 @@
 
         new_patient_flag = 0;
     }
-    
+
     if (new_patient_lock_flag == 1) {                                                    // If statement to control logging for new patient button.
         pi_serial.printf(">20\n");
 
@@ -250,7 +251,7 @@
 
         mute_lock_flag = 0;
     }
-    
+
     if (lock_flag == 1 && !lock_is_logged) {
         if (lock_state == 0)                                                     // If statement to control logging for lock button.
             pi_serial.printf(">04\n");
@@ -360,18 +361,18 @@
 //            power_plug_logged = power_plug_state;
 //        }
 //    }
-    
+
     if (connection_test_sensorplate == 1) {                                                     // If statement for sending serial information sensorplate data when connection test is active.
         // Receiving order sensor information: 8 resistive sensors, 5 electric readings. Is splitted in two parts - part 1/2.
-        pi_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[0], piezo_resistive_array[1], piezo_resistive_array[2], piezo_resistive_array[3], piezo_resistive_array[4], piezo_resistive_array[5], piezo_resistive_array[6], piezo_resistive_array[7], piezo_electric_array[0], piezo_electric_array[1], piezo_electric_array[2], piezo_electric_array[3], piezo_electric_array[4]); // print all to serial port
+        pi_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[0], piezo_resistive_array[1], piezo_resistive_array[2], piezo_resistive_array[3], piezo_resistive_array[4], piezo_resistive_array[5], piezo_resistive_array[6], piezo_resistive_array[7], piezo_electric_array[0], piezo_electric_array[1], piezo_electric_array[2], piezo_electric_array[3], piezo_electric_array[4], piezo_electric_array[5]); // print all to serial port
 
         if (test_mode == 1) {
-            usb_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[0], piezo_resistive_array[1], piezo_resistive_array[2], piezo_resistive_array[3], piezo_resistive_array[4], piezo_resistive_array[5], piezo_resistive_array[6], piezo_resistive_array[7], piezo_electric_array[0], piezo_electric_array[1], piezo_electric_array[2], piezo_electric_array[3], piezo_electric_array[4]); // print all to serial port
+            usb_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[0], piezo_resistive_array[1], piezo_resistive_array[2], piezo_resistive_array[3], piezo_resistive_array[4], piezo_resistive_array[5], piezo_resistive_array[6], piezo_resistive_array[7], piezo_electric_array[0], piezo_electric_array[1], piezo_electric_array[2], piezo_electric_array[3], piezo_electric_array[4],piezo_electric_array[5]); // print all to serial port
         }
     } else {
-        pi_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",0,0,0,0,0,0,0,0,0,0,0,0,0);
+        pi_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",0,0,0,0,0,0,0,0,0,0,0,0,0,0);
         if (test_mode == 1) {
-            usb_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",0,0,0,0,0,0,0,0,0,0,0,0,0); // print all to serial port
+            usb_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",0,0,0,0,0,0,0,0,0,0,0,0,0,0); // print all to serial port
         }
     }
 
@@ -445,21 +446,21 @@
 {
     if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) {  // Control statement for lock interface and delay for non using buttons at the same time.
         lock_flash = 10;
-    } 
-    
+    }
+
     delay_between_button_pressed.reset();
     delay_between_button_pressed.start();
     if (test_mode == 1) {                                                       // If statement for test purposal.
         usb_serial.printf("Reposition triggered.\n");
         LED_on_dev_board1 = !LED_on_dev_board1;
     }
-    
-    if (lock_state == 1) reposition_lock_flag = 1; 
-    else { 
+
+    if (lock_state == 1) reposition_lock_flag = 1;
+    else {
         reposition_flag = 1;
         reposition_feedback_LED = control_LED_intensity;
         pi_serial.printf("&05\n");
-        if(test_mode == 1) usb_serial.printf("&05\n");    
+        if(test_mode == 1) usb_serial.printf("&05\n");
     }
 }
 
@@ -468,13 +469,13 @@
     if (test_mode == 1) {                                                           // If statement for test purposal.
         usb_serial.printf("Reposition released.\n");
     }
-    
+
     if (reposition_feedback_LED != 0) {
         pi_serial.printf("&50\n");
-        
+
         if(test_mode == 1) usb_serial.printf("&50\n");
     }
-    
+
     reposition_feedback_LED = 0;
 }
 //TODO rename to calibration
@@ -484,29 +485,29 @@
     if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) {  // Control statement for lock interface and delay for non using buttons at the same time.
         lock_flash = 10;
     }
-    
+
     delay_between_button_pressed.reset();
     delay_between_button_pressed.start();
     button_calibration_hold_timer.reset(); // inline ?
     button_calibration_hold_timer.start();
-    
+
     if (lock_state == 1) {
         mute_lock_flag = 1;
     }
-    
+
     else {
-         mute_feedback_LED = control_LED_intensity;
-         pi_serial.printf("&07\n");
-         if (test_mode == 1) usb_serial.printf("&07\n");
-         mute_flag = 1;   
+        mute_feedback_LED = control_LED_intensity;
+        pi_serial.printf("&07\n");
+        if (test_mode == 1) usb_serial.printf("&07\n");
+        mute_flag = 1;
     }
-    
+
     if (test_mode == 1) {                                                       // If statement for test purposal.
         usb_serial.printf("Calibration triggered\n");
         LED_on_dev_board1 = !LED_on_dev_board1;
     }
 
-    
+
 }
 
 void rise_mute()                                                              // Interrupt for rising edge reposition function (deactivation; active low).
@@ -518,7 +519,7 @@
         pi_serial.printf("&70\n");
         if (test_mode == 1) usb_serial.printf("&70\n");
     }
-        
+
     mute_feedback_LED = 0;
 
     button_calibration_hold_timer.stop();                                           // Timer reset for calibration function of new patient button.
@@ -547,34 +548,34 @@
     if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) {
         lock_flash = 10;
     }
-    
+
     delay_between_button_pressed.reset();
     delay_between_button_pressed.start();
-    
+
     if (lock_state == 1) new_patient_lock_flag = 1;
     else {
         new_patient_feedback_LED = control_LED_intensity;
         pi_serial.printf("&06\n");
         new_patient_flag = 1;
-        
-        if(test_mode == 1){
+
+        if(test_mode == 1) {
             usb_serial.printf("&06\n");
         }
     }
-    
+
     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");
-        
+
     }
-    
+
     if (new_patient_feedback_LED != 0) {
         pi_serial.printf("&60\n");
         if(test_mode) usb_serial.printf("&60\n");
@@ -596,7 +597,7 @@
         if (lock_state == 0) {                                                      // If statement to control lock feedback LED above button.
             lock_feedback_LED = control_LED_intensity;
             pi_serial.printf("&08\n");
-            if(test_mode == 1) usb_serial.printf("&08\n"); 
+            if(test_mode == 1) usb_serial.printf("&08\n");
         } else {
             lock_feedback_LED = 0;
             pi_serial.printf("&80\n");
@@ -612,25 +613,25 @@
     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;
-        
+
         if (test_mode == 1) {                                                    // If statement for test purposal.
             usb_serial.printf("Calibrate triggered.\n");
         }
         pi_serial.printf(">33\n");
-        
+
     }
 
     if (delay_between_button_pressed.read_ms() > delay_lock_interface) {            // If buttons are not pressed for 3 minutes, set lock active.
         lock_state = 1;
         LED_on_dev_board2 = 1;
         lock_feedback_LED = 0;
-        if (!auto_lock_led_logged) { 
+        if (!auto_lock_led_logged) {
             pi_serial.printf("&80\n");
             if(test_mode == 1) usb_serial.printf("&80\n");
             auto_lock_led_logged = 1;
         }
     } else {
-        auto_lock_led_logged = 0;   
+        auto_lock_led_logged = 0;
     }
 }
 
@@ -668,7 +669,9 @@
 
 void sensorplate_detached()
 {
-    NVIC_SystemReset();
+    if(piezo_electric_sample_timer.read_us() > total_readout_cycle_time_us) {
+        NVIC_SystemReset();
+    }
 }
 
 
@@ -759,15 +762,18 @@
         }
 
         if (connection_test_sensorplate == 1) {
-            piezo_electric_array[0] = piezo_electric_adc.readADC_Differential_0_1();            // First PE readout.
+            piezo_electric_array[0] = piezo_electric_adc.readADC_Differential_0_3();            // First PE readout.
+            piezo_electric_array[3] = piezo_electric_adc.readADC_Differential_1_3();            // 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.
             }
             if (test_mode == 1) {
 //                usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
             }
-            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_Differential_0_1();            // Second PE readout.
+            while(piezo_electric_sample_timer.read_us()<(1*(total_readout_cycle_time_us/3))) {} // 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_Differential_0_3();            // Second PE readout.
+            piezo_electric_array[4] = piezo_electric_adc.readADC_Differential_1_3();            // First 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.
@@ -775,9 +781,10 @@
             if (test_mode == 1) {
 //                usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
             }
-            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.
+            while(piezo_electric_sample_timer.read_us()<(2*(total_readout_cycle_time_us/3))) {} // 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_Differential_0_3();            // Third PE readout.
+            piezo_electric_array[5] = piezo_electric_adc.readADC_Differential_1_3();            // First 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) {
@@ -810,10 +817,6 @@
             else {
                 pi_serial.printf("?,%f,%f,%f,%f,%f,%f,0,0,0,0,0,0,\n",0,0,0,0,0,0);
             }
-
-            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_Differential_0_1();        // Fourth PE readout.
         }
         if (test_mode == 1) {
 //            usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
@@ -826,14 +829,12 @@
         colour_select_indicating_LED_wall(LED_colour);                                  // Function to select colour.
         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. 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());
         }
 
         if (connection_test_sensorplate == 1) {
-            piezo_electric_array[4] = piezo_electric_adc.readADC_Differential_0_1();         // Fifth PE readout.
+            piezo_electric_array[4] = piezo_electric_adc.readADC_Differential_0_3();         // 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.