Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217
Fork of merged_code2_3rd_nov_2017 by
ecgg.cpp
- Committer:
- nikitateggi
- Date:
- 2017-06-17
- Revision:
- 20:7c64e6ecad76
- Parent:
- 19:1650bbc923cc
- Child:
- 22:ffa88619551d
File content as of revision 20:7c64e6ecad76:
/** ECG ADS1291 Test program.
ADS1291 is a single channel ECG chip
with a 24 bit Sigma-Delta ADC
*/
#include "mbed.h"
#include <string.h>
#include <stdio.h>
#include "ds3231.h"
#include "SDFileSystem.h"
#include "ecg_dec.h"
#include "rtc.h"
#include "sdcard.h"
#include "ec_bp.h"
#include "struct.h" // Added on 31/5/17_Nidhin
//#include "test_type.h" //Added on 31/5/17_Nidhin
Serial pc(USBTX,USBRX);
float ecg(int pid)
{
uint8_t lead_reg=0; // added on 14/06 to check lead -off
//----------------------- Structure for Bluetooth Added Nidhin 1/6/2017-------------------//
//BLUETOOTH STRUCTURE
BLEMsg_info *ptr_BLEMsg_info_ecg, BLEMsg_info_ecg; // A copy of master strcuture [ "BLEMsg_info" ] by name "BLEMsg_info_ecg" is created
ptr_BLEMsg_info_ecg = &BLEMsg_info_ecg; // *ptr_BLEMsg_info_bp is the pointer to local copy;
// Declaration of Date Structure
DateTime_info *ptr_DateTime_info_ecg, DateTime_info_ecg; // A copy of Master Structure "DateTime_info" created,
ptr_DateTime_info_ecg = &DateTime_info_ecg; // Structure pointer points to that copy.
// RTC operations
time_t epoch_time_ecg; //A copy of time_t by name epoch_time_bp is created
epoch_time_ecg = rtc_read(); // time is got from get epoch function.
struct tm * ptr_time_info_ecg, time_info_ecg; // Sturucture copy of tm is created
ptr_time_info_ecg = localtime(&epoch_time_ecg); // Structure accepts the time in local format from "time_t" type.
//BELOW LINE IS TO CHECK Date and TIME
//pc.printf("Time is %d: %d: %d\n", (*ptr_time_info_ecg).tm_hour, (*ptr_time_info_ecg).tm_min, (*ptr_time_info_ecg).tm_sec);
//pc.printf("Date is %d:%d:%d\n", (*ptr_time_info_ecg).tm_mday, (*ptr_time_info_ecg).tm_mon+1, (*ptr_time_info_ecg).tm_year-100);
//Copying from one structure to the other using variables
DateTime_info_ecg.hour = (uint8_t)(*ptr_time_info_ecg).tm_hour;
DateTime_info_ecg.mins = (uint8_t)(*ptr_time_info_ecg).tm_min;
DateTime_info_ecg.sec = (uint8_t)(*ptr_time_info_ecg).tm_sec;
DateTime_info_ecg.date = (uint8_t) (*ptr_time_info_ecg).tm_mday;
DateTime_info_ecg.month =(uint8_t)(*ptr_time_info_ecg).tm_mon+1;
DateTime_info_ecg.year = (uint8_t)(*ptr_time_info_ecg).tm_year-100;
// Copying Time to Main structure
BLEMsg_info_ecg.date_time.hour = DateTime_info_ecg.hour;
BLEMsg_info_ecg.date_time.mins = DateTime_info_ecg.mins;
BLEMsg_info_ecg.date_time.sec = DateTime_info_ecg.sec;
BLEMsg_info_ecg.date_time.date = DateTime_info_ecg.date ;
BLEMsg_info_ecg.date_time.month = DateTime_info_ecg.month ;
BLEMsg_info_ecg.date_time.year = DateTime_info_ecg.year ;
//Checking if the structure has these values
//pc.printf("Time 2 is %d:%d:%d\n", DateTime_info_ecg.hour, DateTime_info_ecg.mins, DateTime_info_ecg.sec);
//pc.printf("\t Date is %d:%d:%d\n",DateTime_info_ecg.date, DateTime_info_ecg.month, DateTime_info_ecg.year);
//Loading values to of Test type
test_type_info test_type_info_ecg; // copy of " test_type_info" created
test_type_info_ecg = ECG_Test; // Loaded value 00 to the test type
BLEMsg_info_ecg.test_type = test_type_info_ecg;
//Check if 00 is getting printed
//pc.printf("Test Type is : %d\n", test_type_info_ecg);
// Loading values of Length , PID, DID, sampling frequency, number of samples, calculated data.
BLEMsg_info_ecg.device_id = 01; // Device ID fixed
BLEMsg_info_ecg.patient_id = (uint32_t)pid; // Patient ID
BLEMsg_info_ecg.sampling_freq = 500; // sampling frrquency
BLEMsg_info_ecg.length = 4026; //Total length of data in bytes 22 B+10752 B
/*
//Loading number of samples
NumSamples_info NumSamples_info_bp; //Copy of structure NumSamples_info
NumSamples_info_bp.num_ppg_sample = 1664; // PPG & ECG Sample number loaded in structure copy
NumSamples_info_bp.num_ecg_sample = 1024;
*/
BLEMsg_info_ecg.num_samples.num_sample_ppg_dummy = 0 ;// PPG number of samples copied to master struct
BLEMsg_info_ecg.num_samples.num_sample_ecg_OTtyp = 1000 ; // ECG number of samples copied to master struct
//----------------------------------------END Structure for Bluetooth - Added Nidhin 1/6/2017-------
int32_t concatenate_value2 = 0; // ORG. "int concatenate_value2 = 0;" Nidhin 1/6/17
int32_t *ecg_ptr; // Added 1/6/2017 Nidhin
ecg_ptr = &concatenate_value2; // Pointer to pass for ECG write into SD card Nidhin 1/6/2017
// int32_t sample_sum; // static int32_t avg[1500];
int32_t count = 0; int32_t fs = 500;
uint32_t ecg_buf[N_ECG]; // Moved out of func() Nidhin 9/6/17
Timer t;
//------------------ Declaration for Peak value detection ------------------------------------
uint32_t hi_val;uint32_t pk_val[20];unsigned int pk_pos[20]={0};unsigned int a; //uint32_t pk=0;
int hi_dif = 0;int j=0; // int count1 = N_ECG/fs, a_dif=0, fs1 = fs ,h=0;
// ------------------------- Declaration for Heart Rate calculation --------------------------
int n=0; //int n=10;
float pos_dif, HR[2], HR1,t_pos_dif;int t_sec = 60; float HR_sum = 0,HR_avg;
// -------------------------------------------------------------------------------------------
// unsigned char chk = 1;
pc.baud(baud_rate);
freqset(); // setting the frequency
setupfunc();
lead_reg=ecgsetupfunc(); // added a return function to read leadoff // 14/06
// chk = 1;
if (lead_reg==0) // checking for proper lead contact// 14/06
{
//ORIGINAL sd_open_ECGfile(pid); // opening the ecg file COMMENTED Nidhin 1/6/2017
sd_open_ECGfilee(pid); // REPLACED Nidhin 1/6/2017 Nidhin
pc.printf( "Raw data is = \n");
for(int i=0; i<N_ECG; i++)
{
concatenate_value2= readvalue();
//ORIGINAL sd_write(concatenate_value2); // writing into the sd card COMMENTED Nidhin 1/6/2017
sd_ecgwrite(ecg_ptr); // REPLACED Nidhin 1/6/2017
ecg_buf[i] = concatenate_value2;
//// pc.printf( "%d\n", concatenate_value2); //COMMENTED Nidhin 1/6/2017
}
//sd_close(); // closing the file COMMENTED Nidhin 1/6/2017
sd_close_ecg(); // closing the ECG file REPLACED Nidhin 1/6/2017 Nidhin
/*
// ---------- reading back SD data for processing --------------------
sd_read_file(15);
printf("Reading back SD data\n");
for(int i=10;i<N_ECG;i++)
{
ecg_buf= sd_read();
//printf("%d\n",ecg_buf[i]);
}
sd_close(); */
//----------------------------- PEAK DETECTION AND HEART RATE CALCULATION ---------------------------------------------------
// -------------------------------------- PEAK DETECTION -------------------------------------------------------------
// ------------------ Main loop ---------------------------------
for(int i=0;i<N_ECG-10;i++){
if(ecg_buf[i]>ecg_buf[i+1])
{
hi_val = ecg_buf[i]; //To find the high value
//printf("high value= %d\n",hi_val);
a = i;
//printf("a= %d\n",a);
hi_dif = hi_val-ecg_buf[a+10];
//a_dif = ecg_buf[a+2] - ecg_buf[a+1];
//printf("The difference between high and low value is = %d\n",hi_dif);
// ---------------------------- If hi_val is greater than next ten input values, then compare the hi_val with the tenth input value.
// If the diff is greater than 10000, then it is a valid peak (pls chk the below condition)--------------------------------------------------
// if((hi_dif > 10000) && ((a+10) < N_ECG))
if(hi_dif > 10000)
{
// if(a_dif <= 0) ------------------------ add this condition if needed ----------------------
// {
pk_val[j] = hi_val; //if condition satisfied, put the "pk" value into "pk_val" buffer
//// pc.printf("peak value= %d\n",pk_val[j]);
pk_pos[j]=a; // also save the peak's position
//// pc.printf("peak position is = %d\n",pk_pos[j]);
i = a+120; // once confirmed that this is the necessary peak, skip the next 120 input values
n = j; // where n is the number of peaks detected
j = j+1;
// printf("j after peak detection is= %d\n",j);
// }
}
else
{
//h = a+1;
i = a+1;
// printf("i if peak not found is= %d\n",i);
}
// store the peak value position in "pk_pos"
}
}
// ----------------- HEART RATE LOGIC ---------------------------
for(int i = 0;i < n-1;i++)
{
pos_dif = pk_pos[i+1] - pk_pos[i]; // difference between two consequtive peaks
pc.printf("peak position diff is = %f\n",pos_dif);
//printf("peak position i value is = %d\n",i);
t_pos_dif = pos_dif/fs; // sample difference between peak positions divided by sampling frequency gives the difference value in terms of actual time
pc.printf("time in seconds is = %f\n",t_pos_dif);
HR[i] = t_sec/t_pos_dif; //HR calculation
pc.printf("Heart Rate is = %f\n",HR[i]);
// n = i;
HR1 = HR[0];
}
// ---------------------- To average individual HRs for higher number of samples -----------------------
/* for(int i = 0;i < 5;i++)
{
HR_sum = HR[i]+HR_sum;
}
HR_avg = HR_sum/5; // To find average of all the individual HRs calculated
printf("Heart Rate sum is = %f\n",HR_sum);
printf("Denominator = %d\n",n);
printf("Heart Rate = %f\n",HR_avg);
*/
//ADDED Nidhin 1/6/2017
BLEMsg_info_ecg.cal_data.cal_sbp_dummy = 0;
BLEMsg_info_ecg.cal_data.cal_dbp_OTtyp = (uint16_t)HR1; //To be modified after HR code is added.
structure_file(ptr_BLEMsg_info_ecg, pid); //copy the ECG structure to Main file
ecgfile_mainfile(pid); // copy raw data to the main file and ECG file is cleared.
pc.printf("Closed the main file\n");
return HR1;
// return HR_avg;
}
else
{
pc.printf("no - leadoff");
return 0;
}
} // End of main function
