Demo entry 6778014

생체신호처리기 펌웨어 코드

   

Submitted by anonymous on Dec 12, 2018 at 05:26
Language: C++. Code size: 34.2 kB.

/****************************************************************************************
Sampling Rate = 125;
if you want to change the number ECG Sampling rate, you have to change HPM_count Logic


**************************************************************************************/


#include "mbed.h"
#include "max32630fthr.h"
#include "USBSerial.h"
#include "bmi160.h" //IMU header
//****** ECG DSP PART ******//
#include "My_Queue.h"
#include "IIR_filter.h"
#include "Variable.h"
#include "math.h"


//#include <stdio.h>

//Ticker ECG_Acquisition_Handler;
//Ticker SerialOut_Handler;

Ticker timer1;
Ticker timer2;

Timer ECG_Acquisition_Handler;
Timer SerialOut_Handler;
Timer Respiration_Handler;
Serial pc(UART2_TX, UART2_RX);
//RawSerial pc(UART2_TX, UART2_RX); 

MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);

// Hardware serial port over DAPLink
Serial daplink(P2_1, P2_0);

// Virtual serial port over USB
USBSerial microUSB; 

//DigitalOut rLED(LED1,LED_ON);
DigitalOut gLED(LED2,LED_ON);
//DigitalOut bLED(LED3);

DigitalOut SDN(P5_2);

AnalogIn A_BODY_TEMP(AIN_2);
AnalogIn ECG_LEADOFF(AIN_3);
AnalogIn A_ECG(AIN_4);
AnalogIn A_PPG(AIN_5);



//***************  IMU SETUP  ***************//
I2C i2cBus(P5_7, P6_0);
BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO);
BMI160::GyroConfig gyroConfig;
BMI160::AccConfig accConfig;


void IMU(bool FLAG)
{
    // SETUP //
    if(FLAG)
    {
        i2cBus.frequency(400000);
        
        accConfig.range = BMI160::SENS_4G;
        accConfig.us = BMI160::ACC_US_OFF;
        accConfig.bwp = BMI160::ACC_BWP_2;
        accConfig.odr = BMI160::ACC_ODR_8;
        //daplink.printf("\033[H");  //home
        //daplink.printf("\033[0J");  //erase from cursor to end of screen
        
        
        if(imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
        {
            //daplink.printf("Failed to set gyroscope power mode\n");
            failures++;
        }
        wait_ms(100);
        
        if(imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
        {
            //daplink.printf("Failed to set accelerometer power mode\n");
            failures++;
        }
        wait_ms(100);
        
        
        //example of using getSensorConfig
        if(imu.getSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
        {
            //daplink.printf("ACC Range = %d\n", accConfig.range); 
            //daplink.printf("ACC UnderSampling = %d\n", accConfig.us);
            //daplink.printf("ACC BandWidthParam = %d\n", accConfig.bwp);
            //daplink.printf("ACC OutputDataRate = %d\n\n", accConfig.odr);
        }
        else
        {
            //daplink.printf("Failed to get accelerometer configuration\n");
            failures++;
        }
        
        //example of setting user defined configuration
        if(imu.setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
        {
            //daplink.printf("ACC Range = %d\n", accConfig.range);
            //daplink.printf("ACC UnderSampling = %d\n", accConfig.us);
            //daplink.printf("ACC BandWidthParam = %d\n", accConfig.bwp);
            //daplink.printf("ACC OutputDataRate = %d\n\n", accConfig.odr);
        }
        else
        {
            //daplink.printf("Failed to set accelerometer configuration\n");
            failures++;
        }
        

        if(imu.getSensorConfig(gyroConfig) == BMI160::RTN_NO_ERROR)
        {
            //daplink.printf("GYRO Range = %d\n", gyroConfig.range);
            //daplink.printf("GYRO BandWidthParam = %d\n", gyroConfig.bwp);
            //daplink.printf("GYRO OutputDataRate = %d\n\n", gyroConfig.odr);
        }
        else
        {
            //daplink.printf("Failed to get gyroscope configuration\n");
            failures++;
        }
        
        IMU_SEQUENCE_FLAG = IMU_START;
        
    
    }else
    {
        if(failures == 0)
        {
            BMI160::SensorData accData;
            BMI160::SensorData gyroData;
            BMI160::SensorTime sensorTime;
            
            
            imu.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range);
            //imu.getTemperature(&imuTemperature);
            
            /*    
            IMU_X = (int)(accData.xAxis.scaled * 1000);
            IMU_Y = (int)(accData.yAxis.scaled * 1000);
            IMU_Z = (int)(accData.zAxis.scaled * 1000);
            
        
            IMU_sum = abs(IMU_X) + abs(IMU_Y) + abs(IMU_Z);
            IMU_BUFFER.push(IMU_sum);
            IMU_Integral_Sum += IMU_sum;
            
            if(IMU_BUFFER.count() == IMU_FILTER_SIZE){
                IMU_Integral  = IMU_Integral_Sum/IMU_FILTER_SIZE;
                IMU_Integral_Sum -= IMU_BUFFER.pop();
            }
            
            */
            
            GYRO_X = (int)(gyroData.xAxis.scaled);
            GYRO_Y = (int)(gyroData.yAxis.scaled);
            GYRO_Z = (int)(gyroData.zAxis.scaled);
            /*
            microUSB.printf("ACC xAxis = %s%4.3f\r\n", "\033[K", accData.xAxis.scaled);
            microUSB.printf("ACC yAxis = %s%4.3f\r\n", "\033[K", accData.yAxis.scaled);
            microUSB.printf("ACC zAxis = %s%4.3f\r\n\r\n", "\033[K", accData.zAxis.scaled);
            
            microUSB.printf("GYRO xAxis = %s%5.1f\r\n", "\033[K", gyroData.xAxis.scaled);
            microUSB.printf("GYRO yAxis = %s%5.1f\r\n", "\033[K", gyroData.yAxis.scaled);
            microUSB.printf("GYRO zAxis = %s%5.1f\r\n\r\n", "\033[K", gyroData.zAxis.scaled);
            
            microUSB.printf("Sensor Time = %s%f\r\n", "\033[K", sensorTime.seconds);
            microUSB.printf("Sensor Temperature = %s%5.3f\r\n", "\033[K", imuTemperature);
            */
            
            GYRO_X = GYRO_X>>1;
            GYRO_Y = GYRO_Y<<4;
            GYRO_Z = GYRO_Z>>1;
            
            GYRO_sum = abs(GYRO_X) + abs(GYRO_Y) + abs(GYRO_Z);
            GYRO_BUFFER.push(GYRO_sum);
            GYRO_Integral_Sum += GYRO_sum;
            
            if(GYRO_BUFFER.count() == GYRO_FILTER_SIZE){
                GYRO_Integral  = GYRO_Integral_Sum/GYRO_FILTER_SIZE;
                GYRO_Integral_Sum -= GYRO_BUFFER.pop();
            }
            
            
            if(GYRO_Integral > GYRO_THRESHOLD){ Move = 1; }
            else{ Move = 0; }
            
        } 
    }
   
}

int Simblee_UART()
{
    char UART_BUFF[10];
    int UART_BUFF_IDX = 0;
    int UART_DATA = 0;
    char UART_temp_data = 0;
    
    while(UART_temp_data !='\n')
    { 
        UART_temp_data = pc.getc();
        UART_BUFF[UART_BUFF_IDX] = UART_temp_data;
        UART_BUFF_IDX++;
    }       
    UART_BUFF[UART_BUFF_IDX] = '\0';
    UART_DATA = atoi(UART_BUFF);
                    
    return UART_DATA;
    
} 

void UART_Protocol()
{
    if(pc.readable())
    {
        int UART_DATA_;
        // RESPIRATION 1(1)->2(else)->3(2)
        // ATMOSPHERE  1(3)->4(else)->5(4)
        // RESPIRATION ERROR 1(1) -> 2(2) or 2(3) -> 3(2)
        // ATMOSPHERE ERROR 1(3)
        switch(UART_SQUENCE)
        {
            case 1: //DATA ACQUISITION
                RESP_FLAG = false;
                ATMOSTEMP_FLAG = false;
                UART_DATA_ = Simblee_UART();
                if(UART_DATA_ == 1){
                    UART_SQUENCE = 2;
                    RESP_FLAG = true;
                    break;
                }else if(UART_DATA_ == 2){
                    UART_SQUENCE = 1;
                    break;
                }else if(UART_DATA_ == 3){
                    UART_SQUENCE = 4;
                    ATMOSTEMP_FLAG = true;
                    break;
                }else if(UART_DATA_ == 4){
                    UART_SQUENCE = 1;
                    break;
                }else{
                    UART_SQUENCE = 1;
                    break;
                }
                
            case 2: // RASPIRATION
                UART_DATA_ = Simblee_UART();
                
                if(RESP_FLAG == true){
                    Resp_temp = UART_DATA_;
                    UART_SQUENCE = 3;
                    //Respiration = UART_DATA_;
                }else{ 
                    UART_SQUENCE = 1;
                }
                
                break;
 
                
            case 3:
                UART_DATA_ = Simblee_UART();
                EOT = UART_DATA_;
                if(UART_DATA_ == 1){
                    UART_SQUENCE = 1;
                    break;
                }else if(UART_DATA_ == 2){
                    /*
                    if(RespirationError && RESP_FLAG){ 
                        Respiration = 0;
                        Resp_Count = 0;
                    }
                    else
                    */
                    if(RESP_FLAG == true){ 
                        if( Resp_temp > 100){Respiration = Resp_temp; }
                        else { Respiration = 0;}
                    }
                    
                    RESP_FLAG = false;
                    //RespirationError = false;
                    UART_SQUENCE = 1;
                    break;
                }else if(UART_DATA_ == 3){
                    UART_SQUENCE = 1;
                    break;
                }else if(UART_DATA_ == 4){
                    UART_SQUENCE = 1;
                    break;
                }else{
                    UART_SQUENCE = 1;
                    break;
                }
            case 4: //ATMOSPHERE TEMPERATURE
                UART_DATA_ = Simblee_UART();
                EOT = UART_DATA_;
                if(UART_DATA_ == 1){
                    UART_SQUENCE = 1;
                    break;
                }else if(UART_DATA_ == 2){
                    UART_SQUENCE = 1;
                    break;
                }else if(UART_DATA_ == 3){
                    UART_SQUENCE = 1;
                    break;
                }else if(UART_DATA_ == 4){
                    UART_SQUENCE = 1;
                    break;
                }else{
                    if(ATMOSTEMP_FLAG == true){
                        Atmostemp_Temp = UART_DATA_;
                        UART_SQUENCE = 5;
                        //Respiration = UART_DATA_;
                    }else{ 
                        UART_SQUENCE = 1;
                    }   
                    break;
                }
            case 5:
                UART_DATA_ = Simblee_UART();
                EOT = UART_DATA_;
                if(UART_DATA_ == 1){
                    UART_SQUENCE = 1;
                    break;
                }else if(UART_DATA_ == 2){
                    UART_SQUENCE = 1;
                    break;
                }else if(UART_DATA_ == 3){
                    UART_SQUENCE = 1;
                    break;
                }else if(UART_DATA_ == 4){
                    Atmos_Temp = Atmostemp_Temp;
                    AtmosTempTimeCount = 0;
                    AtmosTempAcquisitFLAG = true;
                    ATMOSTEMP_FLAG = false;
                    UART_SQUENCE = 1;
                    break;
                }else{
                    UART_SQUENCE = 1;
                    break;
                }
        }
        
    }

}


void Debug_print()
{       
    int Length;
    
    Length = snprintf(NULL, 0, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
    SOF,Control_Code,Length,Move,HeartRate,
    Body_Temp,Atmos_Temp,Resp_Count,SpO2,BloodPressure,
    SEND_ECG,Respiration,PPG,Status,checksum,EOT);
    Length += 1;
    
    char MAIN_BUFF[Length+1];//Including NULL char
    
    sprintf(MAIN_BUFF,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
    SOF,Control_Code,Length,Move,HeartRate,
    Body_Temp,Atmos_Temp,Resp_Count,SpO2,BloodPressure,
    SEND_ECG,Respiration,PPG,Status,checksum,EOT);
    microUSB.printf(MAIN_BUFF);
    
    
    //daplink.printf(MAIN_BUFF);
    
    //microUSB.printf("Hello World\r\n");
        
}

//********************************** ECG PART ***********************************//


void ECG_Acquisition()
{
    C_Raw_ECG = (int)(10000 * A_ECG.read());
    C_Raw_ECG = C_Raw_ECG>>2;
    ECG_DATA_BUFFER.push( C_Raw_ECG );
    //ECG_DATA_BUFFER.push( (int)(10000 * A_ECG.read()) );
}

void Init_ECG_Variables(){
    max_= 0;
    for(int i =0;i<5;i++){  max_buff[i] = 0; }
    ECG = 0;
    ECG_Thr = 0;
    Threshold_Setting_Count =0;
    StartECGCount_FLAG = false;
    Count_time = 0;
    HPM_count = 0;
    HPM = 0;
    Threshold_reset_count = 0;
    time_inx = 0;
    ECG_temp = 0;
    ECG_sum = 0;
    
    First_R_peak= false;
    Second_R_peak = false;
    
}

int Find_ECG_Max()
{
    int Find_ECG_Max = 0;
    int Find_ECG_Max_temp = 0;
    int Find_ECG_COUNT = Rpeak_Buff.count();
    for(int i =0;i<Find_ECG_COUNT;i++)
    {
        Find_ECG_Max_temp = Rpeak_Buff.pop();
        if( Find_ECG_Max < Find_ECG_Max_temp)
        {
            Find_ECG_Max = Find_ECG_Max_temp;
        }
    }
    
    return Find_ECG_Max;
}


int GET_HPM(int data) {

  //  ******  ECG Signal Processing  ****** //

  int Filtered_ECG = 0;

  Filtered_ECG = (int)Filter_IIR_SOS(BPFNUM_ECG, BPFDEN_ECG, (float)data, 5, ECG_buff);
      C_Filter = Filtered_ECG;



  ECG_temp = Filtered_ECG;
  if (ECG_temp < 0) {
    ECG_temp = -ECG_temp;
  }
      C_PtoZ = ECG_temp;
      
      ECG_temp = ECG_temp>>4 ;
      ECG_temp = ECG_temp*ECG_temp;
      ECG_temp = ECG_temp>>2;
      C_Square = ECG_temp;      
  
  ECG_DSP_buffer.push(ECG_temp);
  ECG_sum += ECG_temp;
  Integral_FLAG = false;


  //Integral Function
  if (ECG_DSP_buffer.count() == INTEGRAL_FILTER_SIZE_ECG) {
    ECG = ECG_sum / INTEGRAL_FILTER_SIZE_ECG;  //Averaged ECG
        C_Integral = ECG;
    ECG_buffer_temp = ECG_DSP_buffer.pop();
    ECG_sum -= ECG_buffer_temp;

    Integral_FLAG = true;
  }
    
   HPM_COUNT_FLAG = false;
    
   if( ECG>ECG_Thr ){
        if( Last_ECG > ECG){
            Max_Check_Count++;
            
            if(Max_Check_Count == 1){ FirstPeakData = Last_ECG ; }
            else if(Max_Check_Count == 3 && PEAK_CHECK_FLAG == false)
            {
                Max_Check_Count = 0;
                PEAK = FirstPeakData;    
                PEAK_CHECK_FLAG = true;
                        
                if (Second_R_peak) {//  How to Calculation Parameter 60/(x*8*0.001) , x is time_inx
                    //SOF = time_inx;
                    HPM_count = (int)(7500 / (time_inx+12));  // moderate m/d 11-11 from 7500 to 6750
                    HPM_COUNT_FLAG = true;
                    if (50 > HPM_count){
                      HPM_count = last_HPM_count;
                    }else if (HPM_count > 150){
                      HPM_count = last_HPM_count;
                    }
        
                    last_HPM_count = HPM_count;
            
                    First_R_peak = false;
                    Second_R_peak = false;
            
                    //int temp_time_inx = time_inx;
                    time_inx = 0;
                
                }
                /*
                First_R_peak = true;
                if(First_R_peak == true){Second_R_peak = true;}
                */
                First_R_peak = true;
                Second_R_peak = true;
                
                if(PTT_FLAG == false){ 
                    PTT_FLAG = true;  
                }
            }
        }else{
            Max_Check_Count = 0;    
        }
        Last_ECG = ECG;
            
    }else{
        Last_ECG = 0;
        PEAK_CHECK_FLAG = false;    
    }
    
    if (First_R_peak) { time_inx++; } //count R-R interval time 
    if (PTT_FLAG) { PTT_time++; }
    
    //** Check ECG R-R interval **//
    
    // PEAK DETECTION
    /*
    HPM_COUNT_FLAG = false;
     if (ECG > ECG_Thr) {
         if( Last_ECG > ECG )
         {
            Rpeak_Buff.push(ECG);
         }
            Last_ECG = ECG;
         if( PEAK_CHECK_FLAG == false){ PEAK_CHECK_FLAG = true; }
            
     }else{
        
        if(PEAK_CHECK_FLAG == true){
            PEAK_CHECK_FLAG = false;
            PEAK = Find_ECG_Max();
            

            if (Second_R_peak) {//  How to Calculation Parameter 60/(x*8*0.001) , x is time_inx
              HPM_count = (int)(6750 / time_inx);  // moderate m/d 11-11 from 7500 to 6750
              HPM_COUNT_FLAG = true;
                if (50 > HPM_count){
                  HPM_count = last_HPM_count;
                }
                else if (HPM_count > 150){
                  HPM_count = last_HPM_count;
                }
    
            last_HPM_count = HPM_count;
    
            First_R_peak = false;
            Second_R_peak = false;
    
            //int temp_time_inx = time_inx;
            time_inx = 0;
            
            }
            First_R_peak = true;
            if(First_R_peak == true){Second_R_peak = true;}
            
            
        }
        Last_ECG = 0;
     }//else
    
    if (First_R_peak) { time_inx++; } //count R-R interval time 
     
    */
    
    // OLD
    /*
    if (ECG > ECG_Thr) {
      if (Second_R_peak) {//  How to Calculation Parameter 60/(x*8*0.001) , x is time_inx
        HPM_count = (int)(7500 / time_inx);
        if (50 > HPM_count) {
          HPM_count = last_HPM_count;
        }
        else if (HPM_count > 150) {
          HPM_count = last_HPM_count;
        }

        // if((last_HPM_count*0.6 >HPM_count) && (last_HPM_count*1.4<HPM_count)){
       // HPM_count = last_HPM_count;
       // }
        last_HPM_count = HPM_count;

        First_R_peak = false;
        Second_R_peak = false;

        //int temp_time_inx = time_inx;
        time_inx = 0;
        
      }
      First_R_peak = true;

    }
    else {
      if (First_R_peak) { Second_R_peak = true; }
    }

    if (First_R_peak) { time_inx++; } //count R-R interval time 
    
  */

  //*** Set Threshold 
  int temp_max = 0;

  //if (Integral_FLAG && FirstinitSetThreshold) {
    Threshold_ECG.push(ECG);
    if (Threshold_ECG.count() == 175) {  //wait for 1.4 Sec   175*8ms = 1.4s

      for (int i = 0; i < 5; i++) {
        max_ = Threshold_ECG.pop();
        for (int j = 0; j < 35; j++) {
          temp_max = Threshold_ECG.pop();
          if (max_ < temp_max) { max_ = temp_max; }
        }// j end
        max_buff[i] = max_;
      }//i end

      int avr = 0;
      for (int i = 0; i < 5; i++) {
        avr += max_buff[i];
      }
      ECG_Thr = (avr / 5)*0.5;  //50% for Threshold ECG
      StartECGCount_FLAG = true;  // when Threshold set, start count HPM

    }// if queue end 
  //}// initSetThreshold end
   //*** Set Threshold End


   // *****  ECG  Signal Processing end  ****** //

   return HPM_count;

}


void PPG_Protocol(int PPG_data)
{
    PPG = PPG_data;
    if (PPG_data < 30){ 
        SpO2 = 0;
        PPG_LeadOFF = true;
        PPG = 0;
     }else if( PPG_data > 650 ){
        PPG = 0;
     }
    int Filtered_PPG = 0;
    

    Filtered_PPG = (int)Filter_IIR_SOS(BPFNUM_PPG, BPFDEN_PPG, (float)PPG_data, 5, PPG_buff);
    
    //SEND_ECG = Filtered_PPG;

    if(Filtered_PPG >0)
    {
        if(Last_PPG > Filtered_PPG)
        {
            PPG_Peak_Count++;
            if(PPG_Peak_Count ==1){PPG_FirstPeakData = Last_PPG;}
            else if(PPG_Peak_Count ==3 && PPG_PEAK_CHECK_FLAG == false)
            {
                PPG_PEAK_CHECK_FLAG = true; 
                
                if (PPG_Second_peak) {
                    //Control_Code = PPG_time_inx;
                    
                    //HeartRate = (int)(7500 / (PPG_time_inx+12));  // moderate m/d 11-11 from 7500 to 6750
                    
                    if (PPG_ON_COUNT == 14){
                        if( 51 <= PPG_time_inx && PPG_time_inx <= 60 ){
                            SpO2 = 95;    
                        }else if( 61 <= PPG_time_inx && PPG_time_inx <= 84){
                            SpO2 = 96;
                        }else if( 85<= PPG_time_inx && PPG_time_inx <= 104){
                            SpO2 = 97;
                        }else if( 105 <= PPG_time_inx && PPG_time_inx <= 130){
                            SpO2 = 98;
                        }else{
                            //SpO2_Error_Count++;
                        }
                        
                        PPG_ON_COUNT = 0;
                        PPG_LeadOFF = false;
                    }
                    
                    
                    if( 51 <= PPG_time_inx && PPG_time_inx <= 69 ){
                        PPG_ON_COUNT++;
                    }else if( 70 <= PPG_time_inx && PPG_time_inx <= 99){
                        PPG_ON_COUNT++;
                    }else if( 100 <= PPG_time_inx && PPG_time_inx <= 130){
                       PPG_ON_COUNT++;
                    }else{
                        if(PPG_ON_COUNT>0) PPG_ON_COUNT = 0;
                    }
                    
                    //checksum = PPG_time_inx;
                    //EOT = SpO2_Error_Count;
                    
                    if(PPG_LeadOFF == true){ SpO2 = 0;}

                    
                    PPG_First_peak = false;
                    PPG_Second_peak = false;
            
                    //int temp_time_inx = time_inx;
                    //Control_Code = (int)(7500 / (PPG_time_inx+12));  // moderate m/d 11-11 from 7500 to 6750
                    PPG_time_inx = 0;
                
                }
                
                PPG_First_peak = true;
                PPG_Second_peak = true; 
                
                //Blood Pressure
                if(PTT_FLAG == true){
                    PPG_Queue.push(PTT_time);
                    
                    int PPG_Queue_Count = PPG_Queue.count();
                    
                    if(PPG_Queue_Count == 6){
                        int PPG_Sum =0;
                        for(int i = 0;i< PPG_Queue_Count ;i++){
                            PPG_Sum += PPG_Queue.pop();
                        }
                        
                        int Average_PTT_time = (int)(PPG_Sum/PPG_Queue_Count); 
                        //checksum = Average_PTT_time; 
                        
                        PTT = (float)(Average_PTT_time);
                        PTT = PTT*0.008f;  
                        PWV = (BDC/PTT);
                        
                        PWV = (1.73f*PWV + 111.3f);
                        BloodPressure = (int)PWV;
                        //checksum = Average_PTT_time; 
                        
                    }
                    //EOT = PTT_time;
                    PTT_FLAG = false;
                    PTT_time = 0;
                } 
            }
            
        }else{
            PPG_Peak_Count = 0;    
        }
        
        Last_PPG = Filtered_PPG;
        //PPG_Time_Count++;   
         
    }else{
        Last_PPG = 0;
        PPG_PEAK_CHECK_FLAG = false;
    }
    
    if (PPG_First_peak) { PPG_time_inx++; } //count R-R interval time 
    /*
    
    
    if(Filtered_PPG > 0)
    {
        if( PPG_temp> Filtered_PPG)
        {
            if(PPG_tempQueue.count() <100){PPG_tempQueue.push(PPG_temp);}
        }
        PPG_temp = Filtered_PPG;
    }else{
        if(First_PPG_FLAG == false)
            Find_PPG_Max();            
    }
    */
    
}

void Respiration_Count_Check()
{
    if(Last_Respiration > Respiration){//Lower Part
        if(Resp_LowerPeak_Count>=1){ Resp_LowerPeak_Count = 0;}
        if(Resp_UpperPeak_Count==2){ Resp_Peak_Check_FLAG = false; }
        Resp_UpperPeak_Count++;
    }else if( Last_Respiration == Respiration ){
        
    }else{//Upper Part
        if(Resp_UpperPeak_Count>=1){ Resp_UpperPeak_Count = 0; }
        Resp_LowerPeak_Count++;
        if(Resp_LowerPeak_Count ==1){First_Resp_Peak_Data = Last_Respiration;}
        else if(Resp_LowerPeak_Count ==2){
            if(Resp_Peak_FLAG == true && Resp_Peak_Check_FLAG == false){
                //Atmos_Temp = First_Resp_Peak_Data;
                //EOT = Resp_Time_Count;
                Resp_Count = int(7500/(Resp_Time_Count+100));
                
                /*
                if( Repiration_Startup_Sequence == 10 ){
                    
                    if( Resp_Count < (Last_Resp_Count-10) ){ Resp_Count = Last_Resp_Count; }
                    else if( Resp_Count > (Last_Resp_Count+10) ){Resp_Count = Last_Resp_Count; }
                    
                    //if( Resp_Count < 5 ){ Resp_Count = Last_Resp_Count; }

                }else{
                    Repiration_Startup_Sequence++;    
                }
                */
                if( Resp_Count > 120) { Resp_Count = Last_Resp_Count; }
                
                Resp_Peak_FLAG = false;
                Resp_Time_Count = 0;
                Resp_Peak_Check_FLAG = true;
                Last_Resp_Count = Resp_Count;
            }else{
                Resp_Peak_FLAG = true;
            }
            Resp_LowerPeak_Count = 0;
        }
        
    }
    
    Last_Respiration = Respiration;
    
    
    
}


//*******************************  ECG PART END   ******************************//


// main() runs in its own thread in the OS
// (note the calls to Thread::wait below for delays)
int main()
{
    pc.baud(115200);
    //IMU SETUP
    IMU(IMU_SETUP);
    
    //daplink.printf("daplink serial port\r\n");
    //microUSB.printf("micro USB serial port\r\n");
    
    //AD8232 SETUP
    SDN = 1;
    
    //ECG SETUP
    //Init_ECG_Variables();
//    int Body_temperature = 0;
//    int BodyTemp_i = 0;
//    char BTP = 0; // Body Temperature Packet
    //ECG_Acquisition_Handler.attach(&ECG_Acquisition,0.008);    // ECG DATA Acquisition Handler
    //SerialOut_Handler.attach(&Debug_print,0.01);
    
    //** HANDLER ATTACH **/
     //timer1.attach(&timer_test1,1);
     //timer2.attach(&timer_test2,1);
    
     ECG_Acquisition_Handler.start(); // start timer1 counting
     SerialOut_Handler.start(); // start timer2 counting
     Respiration_Handler.start();
     //** HANDLER ATTACH END**/
    while(1) {
         //if(START_CONDITION){  wait_ms(2000);  START_CONDITION = 0;}
         
         //====================== HANDLER ACTIVE FUNCTION LINE ======================//
         
         if (ECG_Acquisition_Handler.read_ms()>=8) // read time
         {
             ECG_ACQUISITION_FLAG = true;
             //ECG_Acquisition(); // call task1 function
             ECG_Acquisition_Handler.reset(); // reset timer
         }
         
         if (SerialOut_Handler.read_ms()>=10) // read time
         {
             DEBUG_PRINT_FLAG = true;
             //TIMER++;
             //IMU(IMU_SEQUENCE_FLAG);
             SerialOut_Handler.reset(); // reset timer
         }
         
         if (Respiration_Handler.read_ms()>=4) // read time
         {
             RESPIRATION_FLAG = true;
             //TIMER++;
             //IMU(IMU_SEQUENCE_FLAG);
             Respiration_Handler.reset(); // reset timer
         }
         
         //====================== HANDLER ACTIVE FUNCTION LINE END ======================//
        
        //====================== DATA Acquisition Part  ======================//
         UART_Protocol();
        
        
         if (ECG_ACQUISITION_FLAG == true)  
         {
            ECG_ACQUISITION_FLAG = false;    
            ECG_Acquisition(); // call task1 function
            
            if(ECG_DATA_BUFFER.count() > 0)
            {
                //HeartRate = GET_HPM(ECG_DATA_BUFFER.pop());
                
                if( HPM_COUNT_FLAG == true ){HPM_BUFFER.push(GET_HPM(ECG_DATA_BUFFER.pop())); }
                else { GET_HPM(ECG_DATA_BUFFER.pop()); }
                
                HPM_AVERAGE_BUFFER_COUNT = HPM_BUFFER.count();
                if( HPM_AVERAGE_BUFFER_COUNT > 1 ){   
                    int temp_HPM = 0;
                    int total = 0;
                    for(int i= 0;i<HPM_AVERAGE_BUFFER_COUNT;i++)
                    {
                            temp_HPM = HPM_BUFFER.pop();
                            total += temp_HPM;
                    }
                    HeartRate = (int)(total/HPM_AVERAGE_BUFFER_COUNT);
                    //checksum = (int)(total/HPM_AVERAGE_BUFFER_COUNT);
                }
                //SEND_ECG = C_Raw_ECG;  
                SEND_ECG = ECG;  
            }
            
            
            PPG_Protocol( (int)(A_PPG.read()*1000) );
            if(Resp_Peak_FLAG){ Resp_Time_Count ++;}

         }
         
         if(DEBUG_PRINT_FLAG == true)
         {
             DEBUG_PRINT_FLAG = false;
             IMU(IMU_SEQUENCE_FLAG);
             
             
             //int body_temp_data = (int)((A_BODY_TEMP.read()+0.050f)*10000);
             double body_temp_data = ((A_BODY_TEMP.read()+0.050f)*10000);
             //EOT = (int)body_temp_data;
             //body_temp_data /= 100;
             Body_Temp = (int)(body_temp_data + 2.0);
             if( Body_Temp >= 5000 ){
                Body_Temp = 0;
             }
             //Body_Temp = int((TEMP_CO_X*body_temp_data + TEMP_CO_Y)*100);
             //HeartRate = (int)(body_temp_data*100);
             if(100 < (int)((ECG_LEADOFF.read())*10000)){
                 SEND_ECG = 0;
                 HeartRate = 0;
                 BloodPressure = 0;
             }
             
             /*
             if(AtmosTempAcquisitFLAG == 1){//First Acquisition Atmosphere Temperature
                 AtmosTempTimeCount++;    //Counting Time , fs = 125hz
             }else if (AtmosTempAcquisitFLAG == 2){
                 AtmosTempAcquisitFLAG = 0;
                 AtmosTempTimeCount = 0;     
             }*/
             
             if( AtmosTempTimeCount > 130){//1.3s
                Atmos_Temp = 0;
                AtmosTempTimeCount = 0;
                AtmosTempAcquisitFLAG = false;
             }else if ( AtmosTempAcquisitFLAG == true ){
                AtmosTempTimeCount++; //Counting Time , fs = 125hz
             }
             //EOT = AtmosTempTimeCount;
             //checksum = AtmosTempAcquisitFLAG;
             
             /*
             EOT = Respiration;
             Error_Last_Respiration = Respiration;
             if( Respiration-2 <= Error_Last_Respiration &&  Error_Last_Respiration <= Respiration+2 ){
                 if(Respiration_Error_Count == 100){
                     Respiration = 0;
                 }else{
                    Respiration_Error_Count++; 
                 }
             }else{ Respiration_Error_Count = 0;}
             
            */
             

             Debug_print(); // call task2 function
         }
         
         
         if(RESPIRATION_FLAG){
            RESPIRATION_FLAG = false;
            Respiration_Count_Check();   
         }
        
        

        
        //====================== DATA Acquisition Part END ======================/
        
        //************************************ DONE CODE ************************************//
        //IMU DATA ACQUISITION
        
        
        //ECG ACQUISITION
        //microUSB.printf("%1.2f \r\n", (6.0f * A_ECG.read()));
        
        //PPG ACQUISITION
        //microUSB.printf("%1.2f \r\n", (6.0f * A_PPG.read()));
        
        //BODY TEMPERATURE ACQUISITION
        
        //microUSB.printf("%.4f ,%1.2f \r\n",(A_BODY_TEMP.read()+0.050),(6.0f * A_ECG.read()));
        
        //microUSB.printf("a,%1.2f, b,%1.2f \r\n", (6.0f * A_ECG.read()),(6.0f * A_PPG.read()));
        
        //microUSB.printf("ECG,%1.4f\r\n", (6.0f * A_ECG.read()));
        
        
        //RESPIRATION DONE
        /*
        
        if(pc.readable())
        {
                char UART_BUFF[10];
                int UART_BUFF_IDX = 0;
                int UART_DATA = 0;
                char UART_temp_data = 0;
                
                
                while(UART_temp_data !='\n')
                { 
                    UART_temp_data = pc.getc();
                    UART_BUFF[UART_BUFF_IDX] = UART_temp_data;
                    UART_BUFF_IDX++;
                }       
                UART_BUFF[UART_BUFF_IDX] = '\0';
                UART_DATA = atoi(UART_BUFF);
                        
                microUSB.printf("%d\n",UART_DATA);

        }
        */
        
        
    
        //************************************ DONE CODE END ************************************//

        
        //microUSB.printf("%s",Resp_print());
        //microUSB.printf("ECG,%2.f,RESP,%2.f, %s \r\n",(10 * A_ECG.read()),(100 * A_PPG.read()),buff);
        
        
        
        gLED = !gLED;
        //Resp_print();

    }
}


        //RESPIRATION DONE
        /*
        
            char UART_BUFF[10];
            int UART_BUFF_IDX = 0;
            int UART_DATA = 0;
            char UART_temp_data = 0;
            
            while(UART_temp_data !='\n')
            { 
                UART_temp_data = pc.getc();
                UART_BUFF[RESP_BUFF_IDX] = UART_temp_data;
                UART_BUFF_IDX++;
            }       
            UART_BUFF[RESP_BUFF_IDX] = '\0';
            UART_DATA = atoi(UART_BUFF);
                    
        */

This snippet took 0.05 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).