Fix all the following issues which we faced during 01st April internal demo 1) Display hang and jump issue 2) Sequencing issue 3) Issue with BP code

Dependencies:   SDFileSystem ds3231 program mbed testUniGraphic_150217

Fork of Int_Demo_01Apr2017_newdisplaycode_bp_nikita by nikita teggi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bp.cpp Source File

bp.cpp

00001 
00002 //BP Optimised Program for Averaging the received value from PPG and ECG & Check for SD card Storage//
00003 
00004 
00005 /** ECG ADS1291 Test program.
00006 ADS1291 is a single channel ECG chip
00007 with a 24 bit Sigma-Delta ADC
00008  */
00009 
00010 /* 
00011 #include "mbed.h"
00012 #include "MAX30102.h" // BP ADD
00013 #include <string.h>
00014 #include <stdio.h>
00015 #include "ds3231.h"
00016 #include "SDFileSystem.h"
00017 #include "rtc.h"
00018 #include "sdcard.h"
00019 #include "ec_bp.h"
00020 #include "bp.h"
00021 #include "display_modules.h"
00022 
00023 
00024 Serial bc(USBTX,USBRX);
00025 DigitalIn INTR(PTC7);
00026 
00027  
00028     
00029 void bp() {
00030  
00031 //------------------BP ADD------------------------------//{ 
00032 //int location; // BP ADD
00033 int32_t n_red_buffer_length = 1500;    //data length
00034 uint32_t aun_red_buffer[1500];    //Red LED sensor data, set to 500
00035 uint32_t red_value;
00036 uint32_t ecg_value;
00037 uint8_t uch_dummy;
00038 uint32_t ecg_buf[1000];
00039 int32_t ecg_buffer_length = 1000;
00040 float SBP, DBP, PWV, delta_t;
00041 int d = 210;
00042 
00043 uint32_t un_min, un_max, ecg_min, ecg_max ;  //variables to calculate the on-board LED brightness that reflects the heartbeats
00044     //int i, j;
00045     float ecg_location, ppg_location; 
00046     //int32_t n_brightness;
00047     
00048    // n_brightness=0;
00049     un_min=0x3FFFF;
00050     un_max=0;
00051     ecg_min = 0xFFFFFF;
00052     ecg_max = 0;
00053  //---------------- BP ADD-----------------------------//}
00054  
00055  
00056  //time_t epoch_time;
00057   //int fp;
00058  int concatenate_value1 = 0;
00059 //uint32_t ecg_buf[1500];
00060 //int32_t ecg_der[1500];
00061 //int buff1[15]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
00062 //int32_t sample_sum; // static int32_t avg[1500];
00063  //int32_t count = 0; int32_t fs = 500; int32_t w=0,w1=0;
00064 
00065    unsigned char chk = 1;
00066     bc.baud(baud_rate);
00067    // mySpi.frequency(freq) ;
00068    // mySpi.format(bits, mode) ; 
00069  freqset();
00070   //  Ds3231 rtc(PTC11, PTC10); //sda,scl2yy
00071    
00072    //------------------BP ADD---------------------------// {
00073     while(bc.readable()==0)
00074     {
00075         bc.printf("\x1B[2J");  //clear terminal program screen
00076         bc.printf("Press any key to start conversion\n\r");
00077         wait(1);
00078     }
00079   wait(5);
00080   bc.printf("Start of capture, Remain CALM and Donot MOVE\n");  
00081   wait(2);
00082   //-------------------BP ADD---------------------------//}
00083     
00084   setupfunc();
00085   ecgsetupfunc(); 
00086   //--------------- BP ADD------------------------------------//{
00087   maxim_max30102_reset();  // PPG reset
00088   maxim_max30102_init(); // PPG ADC register Initialise 
00089   //---------------BP ADD ----------------------------------// }  
00090   
00091  
00092    chk = 1;
00093  bc.printf("rawecg......................\n");
00094  for(int i=0; i<N_ECG; i++)
00095  {
00096     
00097      concatenate_value1=readvalue();
00098     ecg_buf[i] = concatenate_value1;
00099    
00100     bc.printf( "%d\n", concatenate_value1);
00101     maxim_max30102_read_reg(0,&uch_dummy);
00102     while(INTR.read()==1); 
00103     maxim_max30102_read_fifo((aun_red_buffer+i)); 
00104      //bc.printf( "%d\n", concatenate_value1);
00105       // bc.printf("%d\n",aun_red_buffer[i]);
00106     //sd_write(concatenate_value); - -- BP ADD commented
00107     //printf( "%d\n", concatenate_value); -- BP ADD commented
00108     // bc.printf("loop2");
00109     }
00110     
00111   for (int w=1000; w < n_red_buffer_length; w++)  // for reading extra 500 PPG samples.
00112   {
00113  //bc.printf("loop2");
00114   while(INTR.read()==1);
00115   maxim_max30102_read_fifo((aun_red_buffer+w));
00116   } 
00117 
00118 
00119    //sd_close();  -- BP ADD commented
00120    
00121    int32_t ecg_n_denom;
00122 
00123  // 4 pt Moving Average ECG
00124     bc.printf("PRINTING ECG 4 POINT IN DECIMAL........\n");
00125     for(int y=0; y<996; y++){
00126         ecg_n_denom= ( ecg_buf[y]+ ecg_buf[y+1]+ ecg_buf[y+2]+ ecg_buf[y+3]);
00127         ecg_buf[y]=  ecg_n_denom/(int32_t)4;
00128     }
00129     for( int l= 0; l<1000; l++){
00130     bc.printf("%d\n",ecg_buf[l]); //PRINTING ecg AFTER AVERAGING
00131     }
00132 bc.printf("PRINTING PPG IN DECIMAL (after averaging)........\n");
00133 int32_t ppg_n_denom;
00134   // 4 pt Moving Average PPG
00135     for(int f=0; f<1496; f++){
00136         ppg_n_denom= ( aun_red_buffer[f]+aun_red_buffer[f+1]+ aun_red_buffer[f+2]+ aun_red_buffer[f+3]);
00137         aun_red_buffer[f]= ppg_n_denom/(int32_t)4;
00138     }
00139     for( int b = 0; b<1500; b++){
00140        bc.printf("%d\n",aun_red_buffer[b]);
00141     }
00142     
00143 
00144 for(int f=0; f<ecg_buffer_length; f++)
00145    {
00146        if(ecg_min > ecg_buf[f])
00147            ecg_min = ecg_buf[f];    //update signal min
00148         if(ecg_max < ecg_buf[f])
00149         {
00150             ecg_max=ecg_buf[f];    //update signal max
00151             ecg_location = f+1;
00152     }
00153     }
00154     bc.printf( "ecg_location = %f\n", ecg_location);
00155 
00156 for(int g = ecg_location;  g < (ecg_location+400); g++) // Peak detect changed to 400 samples from 500 samples
00157     {
00158        if(un_min>aun_red_buffer[g])
00159            un_min=aun_red_buffer[g];    //update signal min
00160         if(un_max<aun_red_buffer[g])
00161         {
00162             un_max=aun_red_buffer[g];    //update signal max
00163             ppg_location = g+1;
00164     }
00165     }
00166 bc.printf( "ppg_location = %f\n", ppg_location);
00167 
00168 delta_t = (2*(ppg_location - ecg_location))/1000;
00169 bc.printf( "delta_t = %f\n", delta_t);
00170 
00171 PWV = d/delta_t;
00172 bc.printf( "PWV = %f\n", PWV);
00173 
00174 SBP = 0.0508955*PWV+62.559;
00175 DBP = 0.0494*PWV + 17.480;
00176 
00177 
00178 bc.printf( "SBP = %f\n", SBP);
00179 bc.printf( "DBP = %f\n", DBP);
00180 
00181 screen_bp1(SBP,DBP);
00182 */
00183 
00184 //-----------------WRITE ECG &PPG
00185  /*  
00186 sd_open_BPfile(1);
00187 for (int w=0; w < 1000; w++)
00188 {
00189 ecg_value =  ecg_buf[w];   
00190 sd_write(ecg_value);
00191 }
00192 
00193 
00194 
00195 for (int w=0; w < n_red_buffer_length; w++)
00196 {
00197 red_value =  aun_red_buffer[w];   
00198 sd_write(red_value); 
00199 }
00200 sd_close();*/
00201 
00202 //----------- FINISHED WRITING ECG & PPG TO SD CARD
00203 
00204    
00205 
00206         
00207  //}    // End of main function
00208 
00209 
00210 //-----------------------------------------------NEW BP code starts--------------------------------///
00211 
00212 #include "mbed.h"
00213 #include "MAX30102.h" // BP ADD
00214 #include <string.h>
00215 #include <stdio.h>
00216 //#include <iostream>
00217 #include "ds3231.h"
00218 #include "SDFileSystem.h"
00219 #include "rtc.h"
00220 #include "sdcard.h"
00221 #include "ec_bp.h"
00222 #include "bp.h"
00223 #include "display_modules.h"
00224 
00225 using namespace std;
00226 
00227 Serial bc(USBTX,USBRX);
00228 DigitalIn INTR(PTC7);
00229 
00230 Timer t; 
00231 
00232 //FILE *ecg;
00233 //FILE *ppg;
00234     
00235 void bp(int pid) {
00236     uint8_t uch_dummy;
00237     uint32_t ppgdata; 
00238 
00239     //int dummyconcat = 700000;
00240     int concatenate_value2 = 0;
00241     unsigned char chk = 1;
00242 
00243     int drum11[64];
00244     uint32_t drum21[64];
00245 
00246     // Variables for file operations
00247    // uint32_t amax=0;
00248     //uint32_t bmax = 0;
00249     //uint32_t a;
00250     //uint32_t b;
00251     
00252    //int aloc = 0;
00253    //int bloc = 0;
00254    //int samples = 0;
00255 
00256     bc.baud(baud_rate);
00257 
00258     freqset();               // setting the frequency
00259     setupfunc();              
00260     ecgsetupfunc();
00261         
00262     maxim_max30102_reset();  // PPG reset
00263     wait_ms(20);
00264     maxim_max30102_init();
00265         
00266     chk = 1;
00267         
00268     wait(2);
00269     //bc.printf("BP start\n");  
00270 
00271     for(int i=0; i<500; i++)
00272     {
00273         
00274         concatenate_value2= readvalue();
00275         
00276         maxim_max30102_read_reg(0,&uch_dummy);
00277         while(INTR.read()==1); 
00278         maxim_max30102_read_fifo(&ppgdata); 
00279             
00280     }   
00281 
00282 
00283     sd_open_BP_ECGfile(pid) ; //   sd_open_BP_ECGfile(123) draft file for testing
00284     sd_open_BP_PPGfile(pid) ; // sd_open_BP_PPGfile(123) draft file for testing
00285 
00286     FILE *ecg = NULL;
00287     FILE *ppg = NULL;
00288     int *drum_ecg = NULL; // pointers to ecg buffer
00289     uint32_t *drum_ppg = NULL; // pointer to ppg buffer
00290     
00291     drum_ecg = drum11;
00292     drum_ppg = drum21;
00293     
00294     for(int j =0 ; j<16; j++) // changed to 16 from 8
00295     {
00296         for(int i=0; i<64; i++)
00297         {
00298             concatenate_value2 = readvalue();
00299             drum11[i]=concatenate_value2;   //drum11[i]=concatenate_value2;      drum11[i]=dummyconcat;
00300          
00301             maxim_max30102_read_reg(0,&uch_dummy);
00302             while(INTR.read()==1); 
00303             maxim_max30102_read_fifo(&ppgdata); 
00304             drum21[i]=ppgdata;    
00305         }
00306                            
00307             ecg = sd_BP_ecgwrite(drum_ecg); //
00308             ppg = sd_BP_ppgwrite(drum_ppg);
00309         
00310         
00311     } 
00312     
00313     for(int j =0 ; j<10; j++) // changed to 16 from 8
00314     {
00315         for(int i=0; i<64; i++)
00316         {
00317             maxim_max30102_read_reg(0,&uch_dummy);
00318             while(INTR.read()==1); 
00319             maxim_max30102_read_fifo(&ppgdata); 
00320             drum21[i]=ppgdata; 
00321         }
00322         
00323          ppg = sd_BP_ppgwrite (drum_ppg);
00324         
00325      }  
00326     fclose(ecg);
00327     fclose(ppg);
00328      
00329     maxim_max30102_reset();
00330     
00331     //-----Reading from file starts //
00332     
00333     //std::ifstream ec1("/sd/123_BP_ECG.csv");
00334     //std::ifstream pp1("/sd/123_BP_PPG.csv");
00335     
00336     int eloc = 0;
00337     int ploc = 0;
00338    
00339    
00340    eloc = sd_BP_ECGMAX(pid); // eloc = sd_BP_ECGMAX(123);
00341    ploc = sd_BP_PPGMAX(pid); // ploc = sd_BP_PPGMAX(123);
00342     
00343    //bc.printf("The ECG max is at : %d", eloc);
00344    //bc.printf("The PPG Max is at : %d", ploc);
00345    
00346    float PWV = 0;
00347    int d = 210000;
00348    int delta_t = 0;
00349    float SBP = 0;
00350    float DBP = 0;
00351    
00352    delta_t  = (2*(ploc - eloc));
00353    PWV = d/delta_t;
00354    
00355     SBP = 0.0508955*PWV+62.559;
00356     DBP = (0.0494*PWV + 17.480)+10;
00357     
00358     screen_bp1(SBP,DBP);
00359     
00360     create_single_BPfile(pid);
00361  
00362  }