Added 4 point or 8 point averaging function .

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of merged_code2_3rd_nov_2017 by nikita teggi

Committer:
suhasini
Date:
Thu Mar 30 11:34:04 2017 +0000
Revision:
5:a3ea7c82b7e1
Parent:
3:9a06c2bed650
Child:
8:adf2ba7e6412
PID updation possible, Proper screen navigation enabled, Return functionality from BP to main screen-done, date-time format modified, BP data not saved into SD card

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikitateggi 3:9a06c2bed650 1 //BP Optimised Program for Averaging the received value from PPG and ECG & Check for SD card Storage//
nikitateggi 3:9a06c2bed650 2
nikitateggi 3:9a06c2bed650 3
nikitateggi 3:9a06c2bed650 4 /** ECG ADS1291 Test program.
nikitateggi 3:9a06c2bed650 5 ADS1291 is a single channel ECG chip
nikitateggi 3:9a06c2bed650 6 with a 24 bit Sigma-Delta ADC
nikitateggi 3:9a06c2bed650 7 */
nikitateggi 3:9a06c2bed650 8
nikitateggi 3:9a06c2bed650 9 #include "mbed.h"
nikitateggi 3:9a06c2bed650 10 #include "MAX30102.h" // BP ADD
nikitateggi 3:9a06c2bed650 11 #include <string.h>
nikitateggi 3:9a06c2bed650 12 #include <stdio.h>
nikitateggi 3:9a06c2bed650 13 #include "ds3231.h"
nikitateggi 3:9a06c2bed650 14 #include "SDFileSystem.h"
nikitateggi 3:9a06c2bed650 15 #include "rtc.h"
nikitateggi 3:9a06c2bed650 16 #include "sdcard.h"
nikitateggi 3:9a06c2bed650 17 #include "ec_bp.h"
nikitateggi 3:9a06c2bed650 18 #include "bp.h"
nikitateggi 3:9a06c2bed650 19 #include "display_modules.h"
nikitateggi 3:9a06c2bed650 20
nikitateggi 3:9a06c2bed650 21
nikitateggi 3:9a06c2bed650 22 Serial bc(USBTX,USBRX);
nikitateggi 3:9a06c2bed650 23 DigitalIn INTR(PTC7);
nikitateggi 3:9a06c2bed650 24
nikitateggi 3:9a06c2bed650 25
nikitateggi 3:9a06c2bed650 26
suhasini 5:a3ea7c82b7e1 27 void bp() {
nikitateggi 3:9a06c2bed650 28
nikitateggi 3:9a06c2bed650 29 //------------------BP ADD------------------------------//{
suhasini 5:a3ea7c82b7e1 30 //int location; // BP ADD
nikitateggi 3:9a06c2bed650 31 int32_t n_red_buffer_length = 1500; //data length
nikitateggi 3:9a06c2bed650 32 uint32_t aun_red_buffer[1500]; //Red LED sensor data, set to 500
nikitateggi 3:9a06c2bed650 33 uint32_t red_value;
nikitateggi 3:9a06c2bed650 34 uint32_t ecg_value;
nikitateggi 3:9a06c2bed650 35 uint8_t uch_dummy;
nikitateggi 3:9a06c2bed650 36 uint32_t ecg_buf[1000];
nikitateggi 3:9a06c2bed650 37 int32_t ecg_buffer_length = 1000;
nikitateggi 3:9a06c2bed650 38 float SBP, DBP, PWV, delta_t;
nikitateggi 3:9a06c2bed650 39 int d = 210;
nikitateggi 3:9a06c2bed650 40
suhasini 5:a3ea7c82b7e1 41 uint32_t un_min, un_max, ecg_min, ecg_max ; //variables to calculate the on-board LED brightness that reflects the heartbeats
suhasini 5:a3ea7c82b7e1 42 //int i, j;
nikitateggi 3:9a06c2bed650 43 float ecg_location, ppg_location;
suhasini 5:a3ea7c82b7e1 44 //int32_t n_brightness;
nikitateggi 3:9a06c2bed650 45
suhasini 5:a3ea7c82b7e1 46 // n_brightness=0;
nikitateggi 3:9a06c2bed650 47 un_min=0x3FFFF;
nikitateggi 3:9a06c2bed650 48 un_max=0;
nikitateggi 3:9a06c2bed650 49 ecg_min = 0xFFFFFF;
nikitateggi 3:9a06c2bed650 50 ecg_max = 0;
nikitateggi 3:9a06c2bed650 51 //---------------- BP ADD-----------------------------//}
nikitateggi 3:9a06c2bed650 52
nikitateggi 3:9a06c2bed650 53
nikitateggi 3:9a06c2bed650 54 //time_t epoch_time;
nikitateggi 3:9a06c2bed650 55 //int fp;
nikitateggi 3:9a06c2bed650 56 int concatenate_value1 = 0;
nikitateggi 3:9a06c2bed650 57 //uint32_t ecg_buf[1500];
nikitateggi 3:9a06c2bed650 58 //int32_t ecg_der[1500];
nikitateggi 3:9a06c2bed650 59 //int buff1[15]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
suhasini 5:a3ea7c82b7e1 60 //int32_t sample_sum; // static int32_t avg[1500];
suhasini 5:a3ea7c82b7e1 61 //int32_t count = 0; int32_t fs = 500; int32_t w=0,w1=0;
nikitateggi 3:9a06c2bed650 62
nikitateggi 3:9a06c2bed650 63 unsigned char chk = 1;
nikitateggi 3:9a06c2bed650 64 bc.baud(baud_rate);
nikitateggi 3:9a06c2bed650 65 // mySpi.frequency(freq) ;
nikitateggi 3:9a06c2bed650 66 // mySpi.format(bits, mode) ;
nikitateggi 3:9a06c2bed650 67 freqset();
nikitateggi 3:9a06c2bed650 68 // Ds3231 rtc(PTC11, PTC10); //sda,scl2yy
nikitateggi 3:9a06c2bed650 69
nikitateggi 3:9a06c2bed650 70 //------------------BP ADD---------------------------// {
nikitateggi 3:9a06c2bed650 71 while(bc.readable()==0)
nikitateggi 3:9a06c2bed650 72 {
nikitateggi 3:9a06c2bed650 73 bc.printf("\x1B[2J"); //clear terminal program screen
nikitateggi 3:9a06c2bed650 74 bc.printf("Press any key to start conversion\n\r");
nikitateggi 3:9a06c2bed650 75 wait(1);
nikitateggi 3:9a06c2bed650 76 }
nikitateggi 3:9a06c2bed650 77 wait(5);
nikitateggi 3:9a06c2bed650 78 bc.printf("Start of capture, Remain CALM and Donot MOVE\n");
nikitateggi 3:9a06c2bed650 79 wait(2);
nikitateggi 3:9a06c2bed650 80 //-------------------BP ADD---------------------------//}
nikitateggi 3:9a06c2bed650 81
nikitateggi 3:9a06c2bed650 82 setupfunc();
nikitateggi 3:9a06c2bed650 83 ecgsetupfunc();
nikitateggi 3:9a06c2bed650 84 //--------------- BP ADD------------------------------------//{
nikitateggi 3:9a06c2bed650 85 maxim_max30102_reset(); // PPG reset
nikitateggi 3:9a06c2bed650 86 maxim_max30102_init(); // PPG ADC register Initialise
nikitateggi 3:9a06c2bed650 87 //---------------BP ADD ----------------------------------// }
nikitateggi 3:9a06c2bed650 88
nikitateggi 3:9a06c2bed650 89
nikitateggi 3:9a06c2bed650 90 chk = 1;
nikitateggi 3:9a06c2bed650 91 bc.printf("rawecg......................\n");
nikitateggi 3:9a06c2bed650 92 for(int i=0; i<N_ECG; i++)
nikitateggi 3:9a06c2bed650 93 {
nikitateggi 3:9a06c2bed650 94
nikitateggi 3:9a06c2bed650 95 concatenate_value1=readvalue();
nikitateggi 3:9a06c2bed650 96 ecg_buf[i] = concatenate_value1;
nikitateggi 3:9a06c2bed650 97
nikitateggi 3:9a06c2bed650 98 bc.printf( "%d\n", concatenate_value1);
nikitateggi 3:9a06c2bed650 99 maxim_max30102_read_reg(0,&uch_dummy);
nikitateggi 3:9a06c2bed650 100 while(INTR.read()==1);
nikitateggi 3:9a06c2bed650 101 maxim_max30102_read_fifo((aun_red_buffer+i));
nikitateggi 3:9a06c2bed650 102 //bc.printf( "%d\n", concatenate_value1);
nikitateggi 3:9a06c2bed650 103 // bc.printf("%d\n",aun_red_buffer[i]);
nikitateggi 3:9a06c2bed650 104 //sd_write(concatenate_value); - -- BP ADD commented
nikitateggi 3:9a06c2bed650 105 //printf( "%d\n", concatenate_value); -- BP ADD commented
nikitateggi 3:9a06c2bed650 106 // bc.printf("loop2");
nikitateggi 3:9a06c2bed650 107 }
nikitateggi 3:9a06c2bed650 108
nikitateggi 3:9a06c2bed650 109 for (int w=1000; w < n_red_buffer_length; w++) // for reading extra 500 PPG samples.
nikitateggi 3:9a06c2bed650 110 {
nikitateggi 3:9a06c2bed650 111 //bc.printf("loop2");
nikitateggi 3:9a06c2bed650 112 while(INTR.read()==1);
nikitateggi 3:9a06c2bed650 113 maxim_max30102_read_fifo((aun_red_buffer+w));
nikitateggi 3:9a06c2bed650 114 }
nikitateggi 3:9a06c2bed650 115
nikitateggi 3:9a06c2bed650 116
nikitateggi 3:9a06c2bed650 117 //sd_close(); -- BP ADD commented
nikitateggi 3:9a06c2bed650 118
nikitateggi 3:9a06c2bed650 119 int32_t ecg_n_denom;
nikitateggi 3:9a06c2bed650 120
nikitateggi 3:9a06c2bed650 121 // 4 pt Moving Average ECG
nikitateggi 3:9a06c2bed650 122 bc.printf("PRINTING ECG 4 POINT IN DECIMAL........\n");
nikitateggi 3:9a06c2bed650 123 for(int y=0; y<996; y++){
nikitateggi 3:9a06c2bed650 124 ecg_n_denom= ( ecg_buf[y]+ ecg_buf[y+1]+ ecg_buf[y+2]+ ecg_buf[y+3]);
nikitateggi 3:9a06c2bed650 125 ecg_buf[y]= ecg_n_denom/(int32_t)4;
nikitateggi 3:9a06c2bed650 126 }
nikitateggi 3:9a06c2bed650 127 for( int l= 0; l<1000; l++){
nikitateggi 3:9a06c2bed650 128 bc.printf("%d\n",ecg_buf[l]); //PRINTING ecg AFTER AVERAGING
nikitateggi 3:9a06c2bed650 129 }
nikitateggi 3:9a06c2bed650 130 bc.printf("PRINTING PPG IN DECIMAL (after averaging)........\n");
nikitateggi 3:9a06c2bed650 131 int32_t ppg_n_denom;
nikitateggi 3:9a06c2bed650 132 // 4 pt Moving Average PPG
nikitateggi 3:9a06c2bed650 133 for(int f=0; f<1496; f++){
nikitateggi 3:9a06c2bed650 134 ppg_n_denom= ( aun_red_buffer[f]+aun_red_buffer[f+1]+ aun_red_buffer[f+2]+ aun_red_buffer[f+3]);
nikitateggi 3:9a06c2bed650 135 aun_red_buffer[f]= ppg_n_denom/(int32_t)4;
nikitateggi 3:9a06c2bed650 136 }
nikitateggi 3:9a06c2bed650 137 for( int b = 0; b<1500; b++){
nikitateggi 3:9a06c2bed650 138 bc.printf("%d\n",aun_red_buffer[b]);
nikitateggi 3:9a06c2bed650 139 }
nikitateggi 3:9a06c2bed650 140
nikitateggi 3:9a06c2bed650 141
nikitateggi 3:9a06c2bed650 142 for(int f=0; f<ecg_buffer_length; f++)
nikitateggi 3:9a06c2bed650 143 {
nikitateggi 3:9a06c2bed650 144 if(ecg_min > ecg_buf[f])
nikitateggi 3:9a06c2bed650 145 ecg_min = ecg_buf[f]; //update signal min
nikitateggi 3:9a06c2bed650 146 if(ecg_max < ecg_buf[f])
nikitateggi 3:9a06c2bed650 147 {
nikitateggi 3:9a06c2bed650 148 ecg_max=ecg_buf[f]; //update signal max
nikitateggi 3:9a06c2bed650 149 ecg_location = f+1;
nikitateggi 3:9a06c2bed650 150 }
nikitateggi 3:9a06c2bed650 151 }
nikitateggi 3:9a06c2bed650 152 bc.printf( "ecg_location = %f\n", ecg_location);
nikitateggi 3:9a06c2bed650 153
nikitateggi 3:9a06c2bed650 154 for(int g = ecg_location; g < (ecg_location+400); g++) // Peak detect changed to 400 samples from 500 samples
nikitateggi 3:9a06c2bed650 155 {
nikitateggi 3:9a06c2bed650 156 if(un_min>aun_red_buffer[g])
nikitateggi 3:9a06c2bed650 157 un_min=aun_red_buffer[g]; //update signal min
nikitateggi 3:9a06c2bed650 158 if(un_max<aun_red_buffer[g])
nikitateggi 3:9a06c2bed650 159 {
nikitateggi 3:9a06c2bed650 160 un_max=aun_red_buffer[g]; //update signal max
nikitateggi 3:9a06c2bed650 161 ppg_location = g+1;
nikitateggi 3:9a06c2bed650 162 }
nikitateggi 3:9a06c2bed650 163 }
nikitateggi 3:9a06c2bed650 164 bc.printf( "ppg_location = %f\n", ppg_location);
nikitateggi 3:9a06c2bed650 165
nikitateggi 3:9a06c2bed650 166 delta_t = (2*(ppg_location - ecg_location))/1000;
nikitateggi 3:9a06c2bed650 167 bc.printf( "delta_t = %f\n", delta_t);
nikitateggi 3:9a06c2bed650 168
nikitateggi 3:9a06c2bed650 169 PWV = d/delta_t;
nikitateggi 3:9a06c2bed650 170 bc.printf( "PWV = %f\n", PWV);
nikitateggi 3:9a06c2bed650 171
nikitateggi 3:9a06c2bed650 172 SBP = 0.0508955*PWV+62.559;
nikitateggi 3:9a06c2bed650 173 DBP = 0.0494*PWV + 17.480;
nikitateggi 3:9a06c2bed650 174
nikitateggi 3:9a06c2bed650 175
nikitateggi 3:9a06c2bed650 176 bc.printf( "SBP = %f\n", SBP);
nikitateggi 3:9a06c2bed650 177 bc.printf( "DBP = %f\n", DBP);
nikitateggi 3:9a06c2bed650 178
nikitateggi 3:9a06c2bed650 179 screen_bp1(SBP,DBP);
nikitateggi 3:9a06c2bed650 180
nikitateggi 3:9a06c2bed650 181
nikitateggi 3:9a06c2bed650 182 //-----------------WRITE ECG &PPG
suhasini 5:a3ea7c82b7e1 183 /*
nikitateggi 3:9a06c2bed650 184 sd_open_BPfile(1);
nikitateggi 3:9a06c2bed650 185 for (int w=0; w < 1000; w++)
nikitateggi 3:9a06c2bed650 186 {
nikitateggi 3:9a06c2bed650 187 ecg_value = ecg_buf[w];
nikitateggi 3:9a06c2bed650 188 sd_write(ecg_value);
nikitateggi 3:9a06c2bed650 189 }
nikitateggi 3:9a06c2bed650 190
nikitateggi 3:9a06c2bed650 191
nikitateggi 3:9a06c2bed650 192
nikitateggi 3:9a06c2bed650 193 for (int w=0; w < n_red_buffer_length; w++)
nikitateggi 3:9a06c2bed650 194 {
nikitateggi 3:9a06c2bed650 195 red_value = aun_red_buffer[w];
nikitateggi 3:9a06c2bed650 196 sd_write(red_value);
nikitateggi 3:9a06c2bed650 197 }
suhasini 5:a3ea7c82b7e1 198 sd_close();*/
nikitateggi 3:9a06c2bed650 199
nikitateggi 3:9a06c2bed650 200 //----------- FINISHED WRITING ECG & PPG TO SD CARD
nikitateggi 3:9a06c2bed650 201
nikitateggi 3:9a06c2bed650 202
nikitateggi 3:9a06c2bed650 203
nikitateggi 3:9a06c2bed650 204
nikitateggi 3:9a06c2bed650 205 } // End of main function