Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813

Fork of Nucleo-transfer by Momo Medical

Revision:
71:040674ed2ce1
Parent:
70:204686903e4c
--- a/Sensorplate/main.cpp	Tue Jun 05 16:12:08 2018 +0000
+++ b/Sensorplate/main.cpp	Tue Jul 03 17:23:49 2018 +0000
@@ -31,6 +31,7 @@
 #include "MPU6050_belt.h"
 #include "PixelArray.h"
 #include "WS2812.h"
+#include "Watchdog.h"
 
 #define ALARMBUF 16
 #define NUM_COLORS 8
@@ -43,6 +44,8 @@
 #define COMET_TAIL_END_INTENSITY 90
 #define FADE_STEPS 20
 
+Watchdog woofwoof;
+
 InterruptIn button_lock(PC_0);                                                       // Input on intterupt base decleration.
 InterruptIn button_reposition(PC_1);
 InterruptIn button_mute(PC_2);
@@ -97,7 +100,7 @@
 int boot_delay_ms = 500;
 int total_readout_cycle_time_us = 100000;                                           // Cycle time in us.
 int total_comet_cycle_time_ms = 750/16;
-int i2c__frequency = 400000;                                                              // I2C Frequency.
+int i2c__frequency = 100000;                                                              // I2C Frequency.
 int baud_rate = 115200;                                                             // Baud rate.
 int uart_input_buffer[120];
 int buffer_counter = 0;
@@ -151,7 +154,7 @@
 float percentage_tester=0;
 
 // Variable to set if belt is used to test algorithm:
-bool test_belt = 0;
+bool test_belt = 1;
 
 // Set test mode on (log functions to pc serial: interrupts, LED intensity and serial messages):
 bool test_mode = 0;
@@ -339,26 +342,6 @@
         }
     }
 
-//    if (power_plug_logged != power_plug_state) {                                    // If statement to control the logging for the state of the power plug.
-//        if (power_plug_state == 1) {
-//            pi_serial.printf("#08\n");
-//
-//            if (test_mode == 1) {                                                   // If statement for test purposal.
-//                usb_serial.printf("#08\n");
-//            }
-//            power_plug_logged = power_plug_state;
-//        }
-//
-//        if (power_plug_state == 0) {
-//            pi_serial.printf("#80\n");
-//
-//            if (test_mode == 1) {                                                   // If statement for test purposal.
-//                usb_serial.printf("#80\n");
-//            }
-//            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,%d,\n", piezo_resistive_array[0], piezo_resistive_array[3], piezo_resistive_array[1], piezo_resistive_array[4], piezo_resistive_array[2], 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
@@ -513,7 +496,7 @@
     }
 
     if(!reposition_button_hold_timer.read_ms()&&circle_filling_reposition&&!colour_wheel_drain_reposition) {
-        //usb_serial.printf("Short hold repo\n");
+        usb_serial.printf("Short hold repo\n");
         px.SetAll(colourbuf[6]);
         for(int k =0; k<colour_wheel_filler; k++) {
             px.Set(k,colourbuf[5]);
@@ -527,7 +510,7 @@
     }
 
     if(!new_patient_button_hold_timer.read_ms()&&circle_filling_new_patient&&!colour_wheel_drain_new_patient) {
-        //usb_serial.printf("Short hold patient\n");
+        usb_serial.printf("Short hold patient\n");
         px.SetAll(colourbuf[6]);
         for(int k =0; k<colour_wheel_filler; k++) {
             px.Set((k+8)%16,colourbuf[5]);
@@ -540,8 +523,8 @@
         } else colour_wheel_filler++;
     }
     //usb_serial.printf("pre drain %d\n",colour_wheel_filler);
-    if(colour_wheel_drain_reposition) {
-        //usb_serial.printf("drain_repo\n");
+    if(colour_wheel_drain_reposition&&!circle_filling_new_patient) {
+        usb_serial.printf("drain_repo\n");
         px.SetAll(colourbuf[6]);
         for(int k =0; k<colour_wheel_filler; k++) {
             px.Set(k,colourbuf[5]);
@@ -553,8 +536,8 @@
         if(!colour_wheel_filler)colour_wheel_drain_reposition=false;
     }
     //usb_serial.printf("post repo %d\n",colour_wheel_filler);
-    if(colour_wheel_drain_new_patient) {
-        //usb_serial.printf("drain_patient\n");
+    if(colour_wheel_drain_new_patient&&!circle_filling_reposition) {
+        usb_serial.printf("drain_patient\n");
         px.SetAll(colourbuf[6]);
         for(int k =0; k<colour_wheel_filler; k++) {
             px.Set((k+8)%16,colourbuf[5]);
@@ -585,6 +568,7 @@
         circle_filled_new_patient--;
     }
     if(!connection_test_sensorplate||i2c_error) {
+        //usb_serial.printf("i2c error = %d\n",i2c_error);
         px.Set(5,colourbuf[0]);
         px.Set(6,colourbuf[0]);
         px.Set(7,colourbuf[0]);
@@ -596,46 +580,6 @@
     ring_colour_old=ring_colour;
 }
 
-void colour_select_indicating_LED_wall(char nLED_colour)                             // Function to select the colour for LED's to wall (values comes from algorithm).
-{
-    set_intensity_LEDs();                                                           // Call function set_intensity_LEDs to set the intensity for LED's to wall and above buttons.
-    ws.setII(2.55*intensity);
-    switch(nLED_colour) {
-        case 'r' :
-            px.SetAll(colourbuf[0]);
-            LED_red_state = 1;
-            LED_yellow_state = 0;
-            break;
-        case 'g' :
-            px.SetAll(colourbuf[1]);
-            LED_green_state = 1;
-            LED_red_state = 0;
-            break;
-        case 'b' :
-            px.SetAll(colourbuf[2]);
-            break;
-        case 'y' :
-            px.SetAll(colourbuf[3]);
-            LED_yellow_state = 1;
-            LED_green_state = 0;
-            break;
-        default  :
-            px.SetAll(colourbuf[4]);
-    }
-
-    if (calibration_flash >= 1) {
-        if ((calibration_flash % 2) == 0) {
-            px.SetAll(colourbuf[2]);
-        } else {
-            ws.setII(0);
-        }
-        calibration_flash--;
-    }
-    //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.
 {
     if (test_mode == 1) {
@@ -907,18 +851,19 @@
 
 void sensorplate_detached()
 {
-        //NVIC_SystemReset();
-I2C i2c_sensorplate_adc(PB_9, PB_8);                                                  // I2C for sensorplate.
-MPU6050_belt angle_device_sensorplate(PB_9, PB_8);                                          // i2c pins // i2c address hardcoded 0x68.
-MPU6050 angle_device_reference_belt(PB_9, PB_8);                                  // i2c pins // i2c address hardcoded 0x69.
-Adafruit_ADS1115 piezo_resistive_adc1(&i2c_sensorplate_adc, 0x48);                  // i2c pins, i2c address.
-Adafruit_ADS1115 piezo_resistive_adc2(&i2c_sensorplate_adc, 0x49);                  // i2c pins, i2c address.
-Adafruit_ADS1115 piezo_electric_adc(&i2c_sensorplate_adc, 0x4B);                    // i2c pins, i2c address.        
+    //NVIC_SystemReset();
+    I2C i2c_sensorplate_adc(PB_9, PB_8);                                                  // I2C for sensorplate.
+    MPU6050_belt angle_device_sensorplate(PB_9, PB_8);                                          // i2c pins // i2c address hardcoded 0x68.
+    MPU6050 angle_device_reference_belt(PB_9, PB_8);                                  // i2c pins // i2c address hardcoded 0x69.
+    Adafruit_ADS1115 piezo_resistive_adc1(&i2c_sensorplate_adc, 0x48);                  // i2c pins, i2c address.
+    Adafruit_ADS1115 piezo_resistive_adc2(&i2c_sensorplate_adc, 0x49);                  // i2c pins, i2c address.
+    Adafruit_ADS1115 piezo_electric_adc(&i2c_sensorplate_adc, 0x4B);                    // i2c pins, i2c address.
 }
 
 
 int main()                                                                          // Main function. inline function "Momo Init" bijvoorbeeld
 {
+
     pi_serial.attach(serial_read,Serial::RxIrq);
     ws.useII(WS2812::PER_PIXEL);
     ws.setII(255);
@@ -926,9 +871,6 @@
     set_intensity_LEDs();                                                           // Initialize intensity for user interface LED's and LED's shines to wall.
     ws.write_offsets(px.getBuf(),0,0,0);
 
-    speaker1 = 1;
-    wait_ms(boot_delay_ms);                                                         // Wait to boot sensorplate first.
-    speaker1 = 0;
     i2c_sensorplate_adc.frequency(i2c__frequency);                                        // Set frequency for i2c connection to sensorplate (variable is declared in config part).
     usb_serial.baud(baud_rate);                                                     // Set serial USB connection baud rate (variable is declared in config part).
     pi_serial.baud(baud_rate);                                                      // Same as line 697, but now for serial PI connection.
@@ -944,7 +886,7 @@
     button_new_patient.mode(PullUp);
 
     testpin_sensorplate.rise(&sensorplate_detached);
-    testpin_sensorplate.fall(&sensorplate_detached);    
+    testpin_sensorplate.fall(&sensorplate_detached);
     button_lock.fall(&trigger_lock);                                                // Interrupt for rising edge lock button.
     button_lock.rise(&end_timer_lock_button);
     button_reposition.fall(&reposition_button_triggered);
@@ -959,6 +901,9 @@
     }
 
     if(!pi_active) {
+        speaker1 = 1;
+        wait_ms(boot_delay_ms);                                                         // Wait to boot sensorplate first.
+        speaker1 = 0;
         __disable_irq();
         while(!pi_active) {
             comet_timer.reset();
@@ -979,7 +924,7 @@
             colour_code = (colour_code_1 << 1 | colour_code_0);
             if(colour_code != 0b00 && pi_active == false && (comet%ALARMBUF)==(ALARMBUF-1)) {
                 pi_active = true;
-            }
+            }//else if(comet>160)pi_active=true;
             if(testpin_sensorplate.read()||i2c_error) {
                 px.Set(5,colourbuf[0]);
                 px.Set(6,colourbuf[0]);
@@ -1027,12 +972,13 @@
     mute_feedback_LED = 0;
     usb_serial.printf("Lock State: %d\n",lock_state);
     lock_feedback_LED = control_LED_intensity;                                      // Lock LED initialization.
+    woofwoof.Configure(10);
 
     while (1) {
         piezo_electric_sample_timer.reset();                                            // Clock gebruiken o.i.d.?
         piezo_electric_sample_timer.start();
         connection_test_sensorplate = !testpin_sensorplate && pi_active;
-        usb_serial.printf("Loop %d\n",connection_test_sensorplate);
+        //usb_serial.printf("start %d\n",piezo_electric_sample_timer.read_us());
 
         if (test_mode == 1) {
 //            usb_serial.printf("Connection test sensorplate = %d\n", connection_test_sensorplate);
@@ -1042,9 +988,7 @@
         }
 
         if (!testpin_sensorplate) {
-            usb_serial.printf("%d\n",piezo_electric_sample_timer.read_us());            
             piezo_electric_array[0] = piezo_electric_adc.readADC_Differential_0_3();            // First PE readout.
-            usb_serial.printf("%d\n",piezo_electric_sample_timer.read_us());
             piezo_electric_array[3] = piezo_electric_adc.readADC_Differential_1_3();            // First PE readout.
 
             for (uint8_t k = 0; k < 4; ++k) {
@@ -1053,6 +997,7 @@
             if (test_mode == 1) {
 //                usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
             }
+            //usb_serial.printf("eenderde %d\n",piezo_electric_sample_timer.read_us());
             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.
@@ -1063,6 +1008,7 @@
             if (test_mode == 1) {
 //                usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
             }
+            //usb_serial.printf("tweederde %d\n",piezo_electric_sample_timer.read_us());
             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.
@@ -1113,19 +1059,23 @@
 //            usb_serial.printf("Loop time after timer_functions: %d ms\n",piezo_electric_sample_timer.read_ms());
         }
 
-        if(buffer_counter>4) {
-            for(int i=0; i<4; i++) {
+        if(buffer_counter>4&& buffer_counter%2==0) {
+            for(int i=0; i<buffer_counter-1; i++) {
                 if(uart_input_buffer[i]=='='&&(uart_input_buffer[(i+2)%4]=='{'||uart_input_buffer[(i+2)%4]=='='))patient_present=uart_input_buffer[(i+1)];
                 if(uart_input_buffer[i]=='{'&&(uart_input_buffer[(i+2)%4]=='='||uart_input_buffer[(i+2)%4]=='{'))LED_colour_wheel_percentage=uart_input_buffer[(i+1)];
                 if((patient_present_old==4&&patient_present!=4)||(patient_present_old!=4&&patient_present==4))mixer=0;
                 //usb_serial.putc(uart_input_buffer[i]);
             }
+            //usb_serial.putc(0x20);
             patient_present_old=patient_present;
             buffer_counter=0;
         }
+        if(1==0){
+        percentage_tester+=0.25;
+        patient_present=1;
+        LED_colour_wheel_percentage=percentage_tester;
+        }
         colour_wheel(LED_colour_wheel_percentage);                                  // Function to select colour.
-        percentage_tester+=0.2;
-        //LED_colour_wheel_percentage=percentage_tester;
         set_userinterface_LED();                                                        // Set LED's of user interface (LED's above buttons).
 
         if (test_mode == 1) {                                                           // If statement for test purposal.
@@ -1149,6 +1099,8 @@
             //usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
         }
 
+        woofwoof.Service();
+        //usb_serial.printf("einde %d\n",piezo_electric_sample_timer.read_us());
         while(piezo_electric_sample_timer.read_us()<(total_readout_cycle_time_us)) {}  // Wait untill 100% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
         //if (test_pin == 1) {
 //            test_mode = 1;