Chanel's edits

Dependencies:   max32630fthr USBDevice

Revision:
14:ee2175578993
Parent:
13:a555fd1253e7
Child:
15:b15b4b6c6da8
--- a/main.cpp	Mon Mar 09 08:17:00 2020 +0000
+++ b/main.cpp	Tue Mar 10 08:22:53 2020 +0000
@@ -9,7 +9,7 @@
 #include "ECGService.h"
 #include <events/mbed_events.h>
 #include "bt32630.h"
-#include "filters.h"
+//#include "filters.h"
 
 //Register definitions
 #define MAX86150_Addr 0xBC //updated per I2Cscanner, 8 bit version of 7 bit code 0x5E
@@ -19,7 +19,8 @@
 #define BaudRate 115200
 
 #define WINDOW_SIZE 25 // number of samples for moving winfow integration for HR algo preprocessing. Effective window_size is half of this value
-#define BUFF_SIZE 136  // FIR filter length
+#define BUFF_SIZE 136  // TODO: decouple BUFF_SIZE and FIR_SIZE. now program crashes if != 136
+#define FIR_SIZE 136
 
 Serial pc(USBTX,USBRX,NULL,BaudRate); 
 InterruptIn intPin(P5_5);   // interrupts currently not used
@@ -28,6 +29,27 @@
 
 ECGService *hrServicePtr;
 
+// Variables for ECG pre-processing
+int k; // loop iteration
+float signal[BUFF_SIZE]={0}; //store signal segment to be filtered
+float bp_signal[BUFF_SIZE]={0};
+float derivative[BUFF_SIZE]={0};
+float squared[BUFF_SIZE]={0};
+float integral[BUFF_SIZE] = {0};
+
+// Variables for preprocessing
+float y = 0.0; //filtered sample to be displayed
+float prev_y = 0.0; // keep track of previous output to calculate derivative
+float sq_y = 0.0;
+float movmean = 0.0; // result of moving window integration
+
+
+// Variables for peak-finding
+int rr1[8], rr2[8], rravg1, rravg2, rrlow = 0, rrhigh = 0, rrmiss = 0;
+long unsigned int i, j, sample_idx = 0, curr = 0, lastQRS = 0, lastSlope = 0, currentSlope = 0;
+float peak_i = 0, peak_f = 0, threshold_i1 = 0, threshold_i2 = 0, threshold_f1 = 0, threshold_f2 = 0, spk_i = 0, spk_f = 0, npk_i = 0, npk_f = 0;
+bool qrs, regular = true, prevRegular;
+
 //Variables modified in inerrupt routine
 /*
 volatile uint32_t intCount = 0;
@@ -85,16 +107,17 @@
     
 //////////
 int main(){  
+    /*
     max86150Sensor.begin(i2c, recommendedi2cFreq, MAX86150_Addr); 
     wait_ms(300);
    
     //unsigned char partID = max86150Sensor.readPartID();
     unsigned char partID = max86150Sensor.readRegister8(MAX86150_Addr,0xFF);
     pc.printf("Part ID is: %X\n",partID);
-    while (partID != 0x1E) {/* Connection to sensor is not established */ }
+    while (partID != 0x1E) { } // Connection to sensor is not established 
     
     
-    //***** SETUP SENSOR */
+    // SETUP SENSOR 
     max86150Sensor.setup(); //Configure sensor
     wait_ms(300);
     pc.printf("SYSCONTOL REG: %x\n", max86150Sensor.readRegister8(MAX86150_Addr,0x0D));
@@ -104,43 +127,43 @@
     pc.printf("INT STATUS1: %X\n",max86150Sensor.readRegister8(MAX86150_Addr,0x00));
     pc.printf("INT STATUS2: %X\n",max86150Sensor.readRegister8(MAX86150_Addr,0x01));
     
-    //*************************************************************//
+    ////
     
     max86150Sensor.clearFIFO();
     max86150Sensor.writeRegister8(MAX86150_Addr,0x0D,0x04); //start FIFO
-    
+    */
     //******* SETUP BLUETOOTH *********   
     eventQueue.call_every(1, periodicCallback); // poll sensor every 1ms. New samples come every 5ms, so polling freq can potentially be decreased
     BLE &ble = BLE::Instance();
     ble.onEventsToProcess(scheduleBleEventsProcessing);
-    //ble.init(bleInitComplete);
-    //eventQueue.dispatch_forever();
-    /*int16_t ecgsigned16;
+    ble.init(bleInitComplete);
+    eventQueue.dispatch_forever();
+
     
+    /*
+    int16_t ecgsigned16;
     while(1){
         if(max86150Sensor.check()>0){
             
             ecgsigned16 = (int16_t) (max86150Sensor.getFIFOECG()>>2);
             max86150Sensor.nextSample();
             ble.updateHeartRate(ecgsigned16);
-            pc.printf("%f\n",(float)ecgsigned16);*/
+            pc.printf("%f\n",(float)ecgsigned16);
+    */
     
     
     // Below code is for testing interrupts and sensor polling without involving bluetooth API
-
+    /* UNCOMMENT HERE
    // max86150Sensor.clearFIFO();
     //intPin.fall(&ISR_DATA_READY);
    // max86150Sensor.writeRegister8(MAX86150_Addr,0x0D,0x04);
     int16_t ecgsigned16;
     int k; // loop iteration
-    int32_t sample_idx = 0; // number of samples read
-    uint8_t curr; // keept track of buffer
     float signal[BUFF_SIZE]={0}; //store signal segment to be filtered
     float bp_signal[BUFF_SIZE]={0};
     float derivative[BUFF_SIZE]={0};
     float squared[BUFF_SIZE]={0};
     float integral[BUFF_SIZE] = {0};
-   // uint8_t curr_movwin = 0; //keep track of squared[] buffer that is used for moving window integration
     
     // Variables for preprocessing
     float y = 0.0; //filtered sample to be displayed
@@ -150,7 +173,7 @@
     
     // Variables for peak-finding
     int rr1[8], rr2[8], rravg1, rravg2, rrlow = 0, rrhigh = 0, rrmiss = 0;
-    long unsigned int i, j, sample = 0, lastQRS = 0, lastSlope = 0, currentSlope = 0;
+    long unsigned int i, j, sample_idx = 0, curr = 0, lastQRS = 0, lastSlope = 0, currentSlope = 0;
     float peak_i = 0, peak_f = 0, threshold_i1 = 0, threshold_i2 = 0, threshold_f1 = 0, threshold_f2 = 0, spk_i = 0, spk_f = 0, npk_i = 0, npk_f = 0;
     bool qrs, regular = true, prevRegular;
     // Initializing the RR averages
@@ -182,18 +205,18 @@
                 prev_y = y;
                 y = 0.0;  // reset filter output for current sample
             }else{ //Buffer is not full yet
-                curr = (uint8_t)sample_idx; //position at buffer is same as sample number
+                curr = sample_idx; //position at buffer is same as sample number
             }
             ecgsigned16 = (int16_t) (max86150Sensor.getFIFOECG()>>2); //read a sample
             max86150Sensor.nextSample();    // advance tail to get sample in next iteration
             signal[curr] = (float)ecgsigned16; //add sample to buffer
             sample_idx++;
             
-            if (curr< BUFF_SIZE-1){
+            if (curr< FIR_SIZE-1){
                 // buffer is not full yet
             }else{ //buffer is full, filter current sample
-                for(k = 0; k < BUFF_SIZE; k++){ //FIR bandpass filter
-                    y = y + FIR_BP[k] * signal[BUFF_SIZE-1-k];
+                for(k = 0; k < FIR_SIZE; k++){ //FIR bandpass filter
+                    y = y + FIR_BP[k] * signal[curr-k];
                 }
                 bp_signal[curr] = y;
             }
@@ -217,18 +240,18 @@
              pc.printf("%f %f\n",movmean/1000, y/50);
             
             // Preprocessing done
-            /*
+            
             // Start peak-finding
             qrs = false;
             if (integral[curr] >= threshold_i1 || highpass[current] >= threshold_f1){ 
                 peak_i = integral[curr];
                 peak_f = bp_signal[curr];
-            }*/
+            }
             
            
             
             
         }  
-    }
+    }  end while(1)*/
       
 }
\ No newline at end of file