Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Branch:
MbedBMAGThrRev
Revision:
55:06c5f76e1a8c
Parent:
54:d4d20a744b87
Child:
58:6545ef27c228
--- a/main.cpp	Mon Jan 07 14:17:53 2019 +0000
+++ b/main.cpp	Fri Feb 15 12:53:30 2019 +0000
@@ -31,10 +31,12 @@
 bool lastErrStatus = true;
 bool firstErrsWritten = false;
 bool gpsStringsReceived = false;
-bool dateChanged = true;
+//bool dateChanged = true;
 char tmpGpsRxString[128];
 char timer_ms[5];
 int secCount = 0;
+int latlongupdatecnt = 21;
+int altitudeupdatecnt = 13;
 int missingGpsCnt = 0;
 int GpsCntWithoutMagData = 0;
 int magCntWithoutGpsData = 0;
@@ -70,11 +72,13 @@
 bool BMAG_Data_Rdy = false;
 bool magTimeSetManually = false;
 bool magStringsReceived = false;
+bool magParseError = false;
 char tmpBMAGRxString[128];
 char magnTarr[15];
 int magTimePromptCount = 0;
 int timeSetManuallyCount = 0;
 int tmpTime = 0;
+int parsed = 0;
 char tmpChar1[5];
 char tmpChar2[5];
 int lastGPRMC_CNT = 0;
@@ -207,7 +211,7 @@
     
     //ensure date change in sps file at midnight
     if(hours == 00 && minutes == 00 && seconds == 00){
-        dateChanged = false;    
+        //dateChanged = false;    
     }
 } 
 
@@ -220,8 +224,7 @@
 //test without mag data
 void testMagWithoutMag(void){
     ///used for test of date change
-    BMAG_Data_Rdy = true;
-     ///    
+    BMAG_Data_Rdy = true;  
 }
 
 //bmag interrupt enable
@@ -247,7 +250,7 @@
     memset(timer_ms,'\0',5);
     memset(time_buffer,'\0',32);    
     
-    t.start();
+    t.start(); 
     
     EA_OLED();
     
@@ -326,9 +329,7 @@
     
     //Opening a file on usb disk
     FILE * fp;
-    FILE * e_fp;
     fp = fopen(currentFilename.c_str(), "a");
-    e_fp = fopen("Errorlog.txt", "a");
     wait_ms(100);
     
     //initializing SPS generation 
@@ -598,10 +599,10 @@
         if(BMAG_Data_Rdy) {
             
             //reset last read ms val
-            memset(timer_ms,'\0',5);
+            memset(timer_ms,'\0',5); 
             
             //read time since pps interrupt
-            tmpTime = t.read_ms();
+            tmpTime = t.read_ms(); 
             snprintf (timer_ms, 5, "%d",tmpTime);    
                          
             if(RTC_set){
@@ -618,10 +619,11 @@
              
                 //SS
                 gpsNMEA.currentUTCFromGPRMC[6] = tmpTime[6];
-                gpsNMEA.currentUTCFromGPRMC[7] = tmpTime[7];                   
+                gpsNMEA.currentUTCFromGPRMC[7] = tmpTime[7];
+                
+                                   
             }
             
-            
             if(strlen(timer_ms) == 1){
                 gpsNMEA.currentUTCFromGPRMC[9] = '0';
                 gpsNMEA.currentUTCFromGPRMC[10] = '0';
@@ -670,10 +672,29 @@
             
             //clear tmpRxBuffer
             memset(tmpBMAGRxString,'\0',128);
-
-            //parse bmag string
-            magParser.parseBMAGString(BMAG_String_Buff);
+          
+            //parse mag string  
+            parsed = magParser.parseBMAGString(BMAG_String_Buff);
             
+            if(parsed == -1){
+                //initiate error string.
+                spsGen.UpdateCurrentErrString("ERRS", IDENTIFIERID, GROUP, gpsNMEA.currentDATEFromGPRMC, gpsNMEA.currentUTCFromGPRMC, TIMEZONE, ENCODING, SOURCEIDENTIFICATION, INTERPRETERID, DATALINEVERSION, FWSRCVERSION, FWIVERSION, gpsNMEA.currentLatitude, gpsNMEA.currentLongitude, GGA_Fix_Present, magParser.getMagTimeStr(), magParser.getMagNTStr(), magParser.getMagSq(), &dbg, magParseError);
+                writeToUsb(spsGen.getCurrentErrString(), fp);
+                magParseError = true;
+                lastErrStatus = false;
+                firstErrsWritten = true;    
+            }
+            
+            if(parsed == 0 && firstErrsWritten && magParseError){
+                //write erre string
+                spsGen.UpdateCurrentErrString("ERRE", IDENTIFIERID, GROUP, gpsNMEA.currentDATEFromGPRMC, gpsNMEA.currentUTCFromGPRMC, TIMEZONE, ENCODING, SOURCEIDENTIFICATION, INTERPRETERID, DATALINEVERSION, FWSRCVERSION, FWIVERSION, gpsNMEA.currentLatitude, gpsNMEA.currentLongitude, GGA_Fix_Present, magParser.getMagTimeStr(), magParser.getMagNTStr(), magParser.getMagSq(), &dbg, magParseError);
+                writeToUsb(spsGen.getCurrentErrString(), fp);
+                lastErrStatus = true;
+                firstErrsWritten = false;
+                magParseError = false;                   
+            }
+            
+                        
             //reset counter containing gps string count without mag data
             GpsCntWithoutMagData = 0;
 
@@ -699,10 +720,10 @@
             batteryvoltage.assign(batteryvoltagearr);
 
             //generate default sps string
-            spsGen.UpdateCurrentString(TAG, IDENTIFIERID, GROUP, gpsNMEA.currentDATEFromGPRMC, gpsNMEA.currentUTCFromGPRMC, TIMEZONE, ENCODING, SOURCEIDENTIFICATION, INTERPRETERID, DATALINEVERSION, FWSRCVERSION, FWIVERSION, BARCODE,gpsNMEA.currentLatitude, gpsNMEA.currentLongitude, GGA_Fix_Present, batteryvoltage, PreassureArr, TemperatureArr, HumidityArr, magParser.getMagTimeStr(), magParser.getMagNTStr(), magParser.getMagSq(), &dbg);
+            spsGen.UpdateCurrentString(TAG, IDENTIFIERID, GROUP, gpsNMEA.currentDATEFromGPRMC, gpsNMEA.currentUTCFromGPRMC, TIMEZONE, ENCODING, SOURCEIDENTIFICATION, INTERPRETERID, DATALINEVERSION, FWSRCVERSION, FWIVERSION, BARCODE,gpsNMEA.currentLatitude, gpsNMEA.currentLongitude, GGA_Fix_Present, batteryvoltage, PreassureArr, TemperatureArr, HumidityArr,gpsNMEA.currentAltitude ,magParser.getMagTimeStr(), magParser.getMagNTStr(), magParser.getMagSq(), &dbg);
 
-            //write data strings to sps file if GGA fix is present, and date has changed, or gps override is activated
-            if(GGA_Fix_Present && (dateChanged || GPS_Override_Active)) {
+            //write data strings to sps file if GGA fix is present, or gps override is activated
+            if(GGA_Fix_Present || GPS_Override_Active) {
 
                 missingGpsCnt = 0;
                 
@@ -723,7 +744,7 @@
                     RTC_set = false;  
                     
                     //error end string
-                    spsGen.UpdateCurrentErrString("ERRE", IDENTIFIERID, GROUP, gpsNMEA.currentDATEFromGPRMC, gpsNMEA.currentUTCFromGPRMC, TIMEZONE, ENCODING, SOURCEIDENTIFICATION, INTERPRETERID, DATALINEVERSION, FWSRCVERSION, FWIVERSION, gpsNMEA.currentLatitude, gpsNMEA.currentLongitude, GGA_Fix_Present, magParser.getMagTimeStr(), magParser.getMagNTStr(), magParser.getMagSq(), &dbg);
+                    spsGen.UpdateCurrentErrString("ERRE", IDENTIFIERID, GROUP, gpsNMEA.currentDATEFromGPRMC, gpsNMEA.currentUTCFromGPRMC, TIMEZONE, ENCODING, SOURCEIDENTIFICATION, INTERPRETERID, DATALINEVERSION, FWSRCVERSION, FWIVERSION, gpsNMEA.currentLatitude, gpsNMEA.currentLongitude, GGA_Fix_Present, magParser.getMagTimeStr(), magParser.getMagNTStr(), magParser.getMagSq(), &dbg, magParseError);
                     writeToUsb(spsGen.getCurrentErrString(), fp);
                     lastErrStatus = true;
                     firstErrsWritten = false;
@@ -731,8 +752,8 @@
                 }
             }
 
-            //write data strings to sps file, with error messages as defined in *.sps definition if date has changed, or gps override is activated
-            if(!GGA_Fix_Present && (dateChanged || GPS_Override_Active)) {
+            //write data strings to sps file, with error messages as defined in *.sps definition if gga fix is not present, or gps override is activated
+            if(!GGA_Fix_Present ||  GPS_Override_Active) {
 
                 if(missingGpsCnt <=  GPSACQTIMELIMITINSECONDS) {
                     missingGpsCnt += 1;
@@ -749,7 +770,7 @@
                 }
 
                 if(lastErrStatus && !firstErrsWritten) {
-                    spsGen.UpdateCurrentErrString("ERRS", IDENTIFIERID, GROUP, gpsNMEA.currentDATEFromGPRMC, gpsNMEA.currentUTCFromGPRMC, TIMEZONE, ENCODING, SOURCEIDENTIFICATION, INTERPRETERID, DATALINEVERSION, FWSRCVERSION, FWIVERSION, gpsNMEA.currentLatitude, gpsNMEA.currentLongitude, GGA_Fix_Present, magParser.getMagTimeStr(), magParser.getMagNTStr(), magParser.getMagSq(), &dbg);
+                    spsGen.UpdateCurrentErrString("ERRS", IDENTIFIERID, GROUP, gpsNMEA.currentDATEFromGPRMC, gpsNMEA.currentUTCFromGPRMC, TIMEZONE, ENCODING, SOURCEIDENTIFICATION, INTERPRETERID, DATALINEVERSION, FWSRCVERSION, FWIVERSION, gpsNMEA.currentLatitude, gpsNMEA.currentLongitude, GGA_Fix_Present, magParser.getMagTimeStr(), magParser.getMagNTStr(), magParser.getMagSq(), &dbg, magParseError);
                     writeToUsb(spsGen.getCurrentErrString(), fp);
                     lastErrStatus = false;
                     firstErrsWritten = true;
@@ -773,7 +794,8 @@
         if(GPS_Data_Rdy){
                  
             gpsRunning = true;      
-            
+            latlongupdatecnt += 1;
+            altitudeupdatecnt += 1;
             missingGpsConnectionCounter = 0;
             
             if(gpsStringsReceived == false){
@@ -823,23 +845,23 @@
             
             //parse current date 
             gpsNMEA.ParseCurrentDateFromGPRMC();
-            
-            //ensure that date changes after midnight
-            if(dateChanged){
-                dateBeforeChange = gpsNMEA.currentDATEFromGPRMC;                
-            }
-            
-            if(!dateChanged && (dateBeforeChange != gpsNMEA.currentDATEFromGPRMC)){
-                dateChanged = true;
-            }            
-            
+                   
             //parse current time
             gpsNMEA.ParseCurrentUTCFromGPRMC();
             
             //parse gps coordinates
-            gpsNMEA.ParseCurrentLatitudeFromGPRMC();
-            gpsNMEA.ParseCurrentLongitudeFromGPRMC();
-  
+            if(latlongupdatecnt > 20){
+                gpsNMEA.ParseCurrentLatitudeFromGPRMC();
+                gpsNMEA.ParseCurrentLongitudeFromGPRMC();
+                latlongupdatecnt = 0;
+            }
+            
+            //parse altitude above sealevel
+            if(altitudeupdatecnt > 12){
+                gpsNMEA.ParseCurrentAltitudeFromGGA();
+                altitudeupdatecnt = 0;
+            }
+            
             //clearing flags
             GPS_Data_Valid = false;
             GPS_Data_Rdy = false;