Chanel's edits
Dependencies: max32630fthr USBDevice
main.cpp
- Committer:
- saleiferis
- Date:
- 2020-03-09
- Revision:
- 13:a555fd1253e7
- Parent:
- 12:ad628acddbf7
- Child:
- 14:ee2175578993
File content as of revision 13:a555fd1253e7:
#include "mbed.h" #include "math.h" #include "max32630fthr.h" #include "max86150.h" #include "I2C.h" #include "ble/BLE.h" #include "ble/Gap.h" //#include "ble/services/HeartRateService.h" #include "ECGService.h" #include <events/mbed_events.h> #include "bt32630.h" #include "filters.h" //Register definitions #define MAX86150_Addr 0xBC //updated per I2Cscanner, 8 bit version of 7 bit code 0x5E #define maxi2cFreq 1000000 #define recommendedi2cFreq 400000 #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 Serial pc(USBTX,USBRX,NULL,BaudRate); InterruptIn intPin(P5_5); // interrupts currently not used I2C i2c(I2C2_SDA, I2C2_SCL); MAX86150 max86150Sensor; ECGService *hrServicePtr; //Variables modified in inerrupt routine /* volatile uint32_t intCount = 0; volatile bool intFlag = false; volatile uint8_t writePointer; volatile uint8_t readPointer; volatile uint32_t curr_time; volatile uint32_t prev_time; volatile int16_t samplesToRead; */ //Timer timer; static events::EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE); /* void ISR_AFULL() { intPin.disable_irq(); intFlag = true; readPointer = max86150Sensor.getReadPointer(); writePointer = max86150Sensor.getWritePointer(); samplesToRead = (int8_t)writePointer - (int8_t)readPointer; //if (samplesToRead < 0) samplesToRead+=32; intCount++; //intPin.enable_irq(); }*/ /* void ISR_DATA_READY() { intPin.disable_irq(); readPointer = max86150Sensor.getReadPointer(); writePointer = max86150Sensor.getWritePointer(); samplesToRead = (int8_t)writePointer - (int8_t)readPointer; //if (samplesToRead < 0) samplesToRead+=32; intCount++; char data[3]; //char *p = data; max86150Sensor.writeRegister8(MAX86150_Addr,0x07,0x01); i2c.write(MAX86150_Addr,command, 1); i2c.read(MAX86150_Addr, data, 3, false); uint8_t temp[sizeof(int32_t)]; //Array of 4 bytes that we will convert into long int32_t tempLong; temp[1] = data[0]; temp[2] = data[1]; temp[3] = data[2]; temp[0] = 0; //Convert array to long memcpy(&tempLong, temp, sizeof(tempLong)); pc.printf("%d\n",tempLong); intPin.enable_irq(); }*/ ////////// 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 */ } //***** SETUP SENSOR */ max86150Sensor.setup(); //Configure sensor wait_ms(300); pc.printf("SYSCONTOL REG: %x\n", max86150Sensor.readRegister8(MAX86150_Addr,0x0D)); pc.printf("FIFO CONFIG: %X\n",max86150Sensor.readRegister8(MAX86150_Addr,0x08)); pc.printf("INT_EN1: %X\n", max86150Sensor.readRegister8(MAX86150_Addr,0x02)); pc.printf("INT_EN2: %X\n", max86150Sensor.readRegister8(MAX86150_Addr,0x03)); 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; while(1){ if(max86150Sensor.check()>0){ ecgsigned16 = (int16_t) (max86150Sensor.getFIFOECG()>>2); max86150Sensor.nextSample(); ble.updateHeartRate(ecgsigned16); pc.printf("%f\n",(float)ecgsigned16);*/ // Below code is for testing interrupts and sensor polling without involving bluetooth API // 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 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 = 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 for (i = 0; i < 8; i++) { rr1[i] = 0; rr2[i] = 0; } while(1){ if(max86150Sensor.check()>0){ //ecgsigned16 = (int16_t) (max86150Sensor.getFIFOECG()>>2); //max86150Sensor.nextSample(); //pc.printf("%f\n",(float)ecgsigned16); // if buffer is full if (sample_idx >= BUFF_SIZE){ for (k=0; k<BUFF_SIZE-1; k++){ //discard oldest sample, shift samples in buffer signal[k] = signal[k+1]; bp_signal[k] = bp_signal[k+1]; derivative[k] = derivative[k+1]; integral[k] = integral[k+1]; squared[k] = squared[k+1]; } curr = BUFF_SIZE-1; // indicates that buffer is full 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 } 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){ // 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]; } bp_signal[curr] = y; } sq_y = pow(y-prev_y,2); squared[curr] = sq_y; //moving window integration movmean = 0.0; // reset for current sample for(k=0; k<(int)WINDOW_SIZE/2; k++){ //pc.printf("%d\n",(int)curr - (int)(WINDOW_SIZE)); if ((int)curr - (int)(WINDOW_SIZE) > k){ //there are enough samples in squared[] // movmean = movmean + squared[(curr-k-(int)WINDOW_SIZE/2)] + squared[curr+k-(int)WINDOW_SIZE/2]; movmean = movmean + squared[curr-k]; }else{ break; } } movmean = movmean/(float)(k+1); integral[curr] = movmean; 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]; }*/ } } }