SDP Code

Dependencies:   BufferedSoftSerial GPRS SDP mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "GPS.h"
00003 #include "gprs.h"
00004 #include "BufferedSoftSerial.h"
00005 #include "gprs.h"
00006 Serial pc(USBTX, USBRX);
00007 GPS uGPS(p13, p14);
00008 Serial uBP_RN41(p9,p10);
00009 BufferedSoftSerial uBP(p19,p18);
00010 GPRS uGSM(p28,p27,9600,"66426640");
00011 
00012 //Power Management Variables
00013 DigitalOut pBP(p5); //For toggling the BP power
00014 DigitalOut BPbutton(p25);//For instigating BP measurement
00015 DigitalOut pRN41(p6);//For toggling the RN41's power.
00016 DigitalOut pGSM(p7);//For toggling the GSM's power.
00017 DigitalOut GSM_pwk(p21); //For using the GSM power key (doesn't turn off Power LED, waste of power).
00018 DigitalOut pGPS(p8); //For toggling the GPS's power.
00019 DigitalOut Buzzer(p23);
00020 DigitalOut LED(p24);
00021 
00022 //GPS Variables
00023 double lat, hlat=25.289536; //default longitude and latitude to Hamad Medical Corporation
00024 double lon, hlon=51.522364; 
00025 GPS_Time t; //Structure has: hour, minute, second, day, month, year in it
00026 
00027 //BP Variables
00028 char BP[15]={0}; //Buffer for Blood Pressure Reading
00029 int SYS=120,DIA=80;
00030 int hSYS=0, hDIA=0; //History of Systolic and Diastolic
00031 
00032 //RN41-Nonin Variables
00033 char FirstBit=129,FalsePulse=5,SpO2=99,LastBit=100;
00034 int HR=70;
00035 
00036 //Power Management Functions
00037 void ToggleBP(bool state);
00038 void ToggleRN41(bool state);
00039 void ToggleGSM(bool state);
00040 void ToggleGPS(bool state);
00041 
00042 //GPS Functions
00043 void UpdateGPS();
00044 bool CheckTime();
00045 
00046 //BP Functions
00047 void InstigateReading();
00048 void UpdateBP();
00049 
00050 //RN41-Nonin Functions
00051 void initializeRN41();
00052 void UpdateNonin(); //Function to update the measurements
00053 
00054 //QEWS variables & function
00055 int HRScore,SPScore,BPScore,QEWS_Score;
00056 void QEWS();
00057 
00058 //GSM Variables
00059 char message[160]; //Length of SMS message is 160.
00060 int msglength=0;
00061 void FormatData();
00062 void SendMessageGSM();
00063 
00064 //MAIN
00065 int main(){
00066 // Define Baudrates
00067     pBP=0; //For toggling the BP power
00068     BPbutton=1;//For instigating BP measurement
00069     pRN41=0;//For toggling the RN41's power.
00070     pGSM=0;//For toggling the GSM's power.
00071     GSM_pwk=0; //For using the GSM power key (doesn't turn off Power LED, waste of power).
00072     pGPS=0; //For toggling the GPS's power.
00073     uBP_RN41.baud(9600);
00074     while(1){
00075        wait(3);
00076        UpdateGPS();
00077        while(!CheckTime());
00078        UpdateNonin();
00079        UpdateBP();
00080        QEWS();
00081        FormatData();
00082        if(QEWS_Score>=1)
00083        SendMessageGSM();
00084     }
00085     
00086 }
00087 
00088 // Power Functions
00089 void ToggleBP(bool state){
00090     if(state){
00091         pRN41=0; wait(2);
00092         pBP=1;  wait(2);
00093     }
00094     if(!state){
00095         pRN41=0; wait(2);
00096         pBP=0; wait(2);
00097     }
00098 }
00099 
00100 void ToggleRN41(bool state){
00101     if(state){
00102         pBP=0; wait(2); 
00103         pRN41=1; wait(2);
00104     }
00105     if(!state){
00106         pRN41=0; wait(2);
00107         pBP=0; wait(2);
00108     }
00109 }
00110 
00111 void ToggleGSM(bool state){
00112     if(state){
00113         pGSM=1;
00114         wait(2);
00115             GSM_pwk=0; wait(1);
00116             GSM_pwk=1; 
00117         wait(10);
00118     }
00119     if(!state){
00120         GSM_pwk=0; wait(1);
00121         GSM_pwk=1; 
00122         wait(5);
00123         pGSM=0;
00124     }
00125 }
00126 
00127 void ToggleGPS(bool state){
00128     if(state){
00129         pGPS=1;
00130         wait(5); //Waiting for 2 seconds. But is it enough to let the GPS module acquire GPS?
00131     }
00132     if(!state){
00133         pGPS=0;
00134         wait(2);
00135     }
00136 }
00137 
00138 //GPS Functions
00139 void UpdateGPS(){
00140     ToggleGPS(1);
00141     //Keep updating over the course of 20 seconds, to get a sure location.
00142     
00143     for(int i=0; i<20; i++){    
00144         lat=uGPS.latitude();
00145         lon=uGPS.longitude();
00146         uGPS.timeNow(&t);
00147         pc.printf("before history lat:%f, lon:%f \n\r",lat,lon);
00148         pc.printf("Time: %d, %d, %d \n\r", t.hour, t.minute,t.second);
00149         wait(10);
00150     }
00151     pGPS=0;
00152     if( (lat <=0) && (lon <= 0)){ // if there is an error in the location (i.e. not in Qatar) then it will use the last known location
00153             lat=hlat; // take the last known location.
00154             lon=hlon;
00155         }
00156         pc.printf("after history lat:%f, lon:%f \n\r",lat,lon);
00157     
00158    hlat=lat;
00159    hlon=lon;
00160    ToggleGPS(0);
00161 }
00162 
00163 //RN41 Functions
00164 void UpdateNonin(){
00165     ToggleRN41(1);
00166     wait(10);
00167     if(uBP_RN41.readable()){
00168         FirstBit =uBP_RN41.getc(); wait(0.5);
00169         FalsePulse=uBP_RN41.getc(); wait(0.5);
00170         HR= FalsePulse | ((FirstBit&=0x03)<<7); wait(0.5);
00171         SpO2=uBP_RN41.getc(); wait(0.5);
00172         LastBit=uBP_RN41.getc(); wait(0.5);
00173         pc.printf("SPO2: %d %d %d %d %d\n\r", FirstBit,FalsePulse,HR,SpO2,LastBit);
00174     }
00175     ToggleRN41(0);
00176 }
00177 
00178 //BP Functions
00179 void InstigateReading(){
00180     wait(5);
00181     BPbutton=0; wait(1);
00182     BPbutton=1; wait(2);
00183     wait(70);
00184     BPbutton=0;wait(1);
00185     BPbutton=1;
00186 }
00187 
00188 void UpdateBP(){
00189     
00190     ToggleBP(1);
00191     uBP.baud(9600);
00192     SYS=0; DIA=0;
00193     for(int i=0;i<3;i++){//Beep 3 times to warn the patient that the device will start taking measurement
00194         Buzzer=1;LED=1;wait(0.5);
00195         Buzzer=0;LED=0;wait(0.5);
00196     }
00197     LED=1;
00198     InstigateReading();
00199     while(1){
00200         if(uBP.readable()){
00201             for(int i=0; i<15; i++)
00202                 BP[i]=uBP.getc(); //Acquire 15 bytes
00203         }
00204         if(BP[14]==0x0A){ //Transmission is okay
00205             //      Hundreds            Tens                Units
00206             SYS=((BP[1]-0x30)*100)+ ((BP[2]-0x030)*10) + ((BP[3]-0x30)*1);
00207             DIA=((BP[6]-0x30)*100)+ ((BP[7]-0x030)*10) + ((BP[8]-0x30)*1);
00208             pc.printf("BP Before History: %d %d\n\r",SYS,DIA);
00209             hSYS=SYS; hDIA=DIA; //Recording values, for history.
00210             pc.printf("BP After History: %d %d\n\r",SYS,DIA);
00211             break;
00212         }
00213     }
00214     //If values are unchanged, use the last stored in history
00215     if(SYS==0 && DIA==0){
00216         SYS=hSYS; DIA=hDIA;
00217     }
00218     LED=0;
00219     ToggleBP(0);
00220 }
00221 
00222 void QEWS(){
00223 //Calculating Oxygen Saturation Score
00224                                 (SYS >= 110 && SYS <= 179) ? BPScore=0: BPScore=BPScore;
00225     ((SYS >= 90 && SYS <=109)||(SYS >= 180 && SYS <= 199)) ? BPScore=1: BPScore=BPScore;
00226                      ((SYS >= 71 && SYS <=89)||SYS >= 200) ? BPScore=2: BPScore=BPScore;
00227                                                 (SYS < 70) ? BPScore=3: BPScore=BPScore;
00228 //Calculating Oxygen Saturation Score
00229                 (SpO2>=95) ? SPScore=0: SPScore=SPScore;
00230     (SpO2>=93 && SpO2<=95) ? SPScore=1: SPScore=SPScore;
00231     (SpO2>=90 && SpO2<=92) ? SPScore=2: SPScore=SPScore;
00232                 (SpO2<=90) ? SPScore=3: SPScore=SPScore;
00233 //Calculating Heart Rate Score
00234                           ((HR >= 51) && (HR <= 100)) ? HRScore=0: HRScore=HRScore;
00235     ((HR >= 41 && HR <=50)||(HR >= 101 && HR <= 110)) ? HRScore=1: HRScore=HRScore;
00236     ((HR >= 31 && HR <=40)||(HR >= 111 && HR <= 129)) ? HRScore=2: HRScore=HRScore;
00237                             ((HR < 31 )|| (HR > 129)) ? HRScore=3: HRScore=HRScore;
00238     QEWS_Score=HRScore+SPScore+BPScore;
00239 }
00240 
00241 //GSM Functions
00242 void FormatData(){
00243 /* This message has a total of 60 characters */
00244     msglength=sprintf(message,"SYS: %d\n\rDIA: %d\n\rHR: %d\n\rSpO2: %d\n\rLAT: %f\n\rLON: %f\n\rQEWS: %d\n\rTIME: %d:%d",SYS,DIA,HR,SpO2,lat,lon,QEWS_Score,t.hour,t.minute);
00245 }
00246 
00247 void SendMessageGSM(){
00248    ToggleGSM(1);
00249    wait(10);
00250    uGSM.sendSMS("66426640",message);
00251    wait(15);
00252    ToggleGSM(0);
00253 }
00254 
00255 //CONFIGURATION FUNCTIONS
00256 void initializeRN41(){
00257     ToggleRN41(1); //Turn RN41 on.
00258     uBP_RN41.printf("$$$"); wait(0.5); // Entering Command Mode
00259     uBP_RN41.printf("SR,001C0500D414\r"); wait(0.5); // Setting MAC Address
00260     uBP_RN41.printf("SP,681010\r"); wait(0.5); //Setting Pass-key
00261     uBP_RN41.printf("SU,96\r"); wait(0.5); //Setting Baudrate to 9600
00262     uBP_RN41.printf("SM,6\r"); wait(0.5); //Setting Mode to Pairing Mode (6)
00263     uBP_RN41.printf("R,1\r"); wait(0.5); //Rebooting
00264 }
00265 
00266 bool CheckTime(){
00267     UpdateGPS();
00268     if(t.hour==0 && t.minute==0)
00269         return 1;
00270     if(t.hour==2 && t.minute==0)
00271         return 1;
00272     if(t.hour==4 && t.minute==0)
00273         return 1;
00274     if(t.hour==6 && t.minute==0)
00275         return 1;
00276     if(t.hour==7 && t.minute==4)
00277         return 1;
00278     if(t.hour==8 && t.minute==8)
00279         return 1;
00280     if(t.hour==9 && t.minute==12)
00281         return 1;
00282     if(t.hour==10 && t.minute==16)
00283         return 1;
00284     if(t.hour==11 && t.minute==20)
00285         return 1;
00286     if(t.hour==12 && t.minute==24)
00287         return 1;
00288     if(t.hour==13 && t.minute==28)
00289         return 1;
00290     if(t.hour==14 && t.minute==32)
00291         return 1;
00292     if(t.hour==15 && t.minute==36)
00293         return 1;
00294     if(t.hour==16 && t.minute==40)
00295         return 1;
00296     if(t.hour==17 && t.minute==44)
00297         return 1;
00298     if(t.hour==18 && t.minute==48)
00299         return 1;
00300     if(t.hour==19 && t.minute==52)
00301         return 1;
00302     if(t.hour==20 && t.minute==56)
00303         return 1;
00304     if(t.hour==22 && t.minute==0)
00305         return 1;
00306     else
00307         return 0;
00308 }