Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

NMEA/NMEA.cpp

Committer:
MAA
Date:
2017-02-17
Revision:
0:b3313c5ffca3
Child:
1:f347c4ef25fa
Child:
2:39c4a85dc2a4

File content as of revision 0:b3313c5ffca3:

#include "NMEA.h"

//constructor with a MODSERIAL pointer to the GPS comms as argument
NMEA::NMEA(){    
    
    currentGGAString.resize(128);
    currentGPRMCString.resize(128);
    currentUTCFromGPRMC.resize(20);
    currentDATEFromGPRMC.resize(10);
    
    chkSumGps.resize(2);
       
    iterationIndex = 0;    
};

//Returns true if GPS CRC corresponds to calculated CRC, else false.
bool NMEA::ValidateData(string cstStr, Serial *debg){
    
    //debg->printf("Starting data validation!\r\n");
    
    //Temporary variables used locally only
    chkSum = 0;
    
    //If for some reason the index is wrong, and the first character is not 
    //the beginning dollar sign of a gps string, return false at once.
    if(cstStr[0] != '$'){
        return false;
    }
    //debg->printf("First character of gps string is $\r\n");
    
    iterationIndex = 0;
              
    //initializing calculated checksum
    chkSumGps = "  ";
    
    //debg->printf("Rx checksum string cleared!\r\n");
    
    //Calculating checksum to verify string integrity
    for(char i = 1; i < cstStr.size(); i++){
        //If at end of line for crc check, break
        if(cstStr[i] == '*' || cstStr[i] == '\0'){
            iterationIndex = i;
            break;    
        }
        //XOR crc calculation
        chkSum ^= cstStr[i];
            
    }
    
    //debg->printf("The calculated checksum is: %d \r\n", chkSum);
    
    //Moving the checksum received into chkSumGps
    iterationIndex++;
    chkSumGps[0] = cstStr[iterationIndex];
    iterationIndex++;
    chkSumGps[1] = cstStr[iterationIndex];
    
    GpsCheck = strtol(chkSumGps.c_str(), NULL ,16);

    //debg->printf("The received checksum is: %d \r\n", GpsCheck);
    
    //Verify received and calculated crc equality
    if(GpsCheck == chkSum){
        return true;    
    }     

    //If unequal crc bytes found return false
    return false;    
};

    
//Store function to move GGA to currentGGAString, 
//or GPRMC to currentGPRMCString
void NMEA::StoreString(string cstStr, Serial * debg){
    static std::string gga = "$GPGGA";
    static std::string rmc = "$GPRMC";
    
    //debg->printf("Storing received strings\r\n");
    
    //If received string is a GPGGA string
    //copy GPGGA string to currentGGAString
    if(cstStr.compare(0, 6, gga) == 0){
        //replace current valid gga string with new valid gga string
        currentGGAString.replace(0, 128, cstStr); 
        //debg->printf("GGA string replaced with: ");  
        //debg->printf(currentGGAString.c_str());  
        //debg->printf("\r\n");          
    }

    //If received string is a GPRMC string
    //copy GPRMC string to currentGPRMCString
    if(cstStr.compare(0, 6, rmc) == 0){
        //replace current valid rmc string with new valid rmc string
        currentGPRMCString.replace(0, 128, cstStr);
        //debg->printf("GPRMC string replaced with: ");  
        //debg->printf(currentGPRMCString.c_str());
        //debg->printf("\r\n");              
    }                 
        
};


//Parser / formatter function to get current UTC timestamp from GPRMC
void NMEA::ParseCurrentUTCFromGPRMC(){
 
     //rewrite    

};

//Parser getting current Date from GPRMC
void NMEA::ParseCurrentDateFromGPRMC(){

    //rewrite
        
};

//Verification of gps fix, returns gps fix value character in ascii
bool  NMEA::GGAFixVerification(Serial * debg){
    //accessing the string after the sixth comma, which is the gga fix column
    debg->printf("Commencing GGA fix verification\r\n");
    
    char commaCount = 0;
    char index = 0;
    
    while((commaCount < 7) && (currentGGAString[index] != '\0')){
        if(currentGGAString[index] == ','){
            commaCount += 1;    
        }
        
        
        //if gga fix is 1 or 2, gga fix is sufficient, return true
        if(commaCount == 6){   
            if(currentGGAString[index+1] == '1'){
                debg->printf("Valid GGA fix present(1)!\r\n"); 
                return true;    
            }
            
            if(currentGGAString[index+1] == '2'){
                debg->printf("Valid GGA fix present(2)!\r\n"); 
                return true;    
            }
            
            break;
        }
        
        index += 1;
        
    }
    
    debg->printf("Invalid GGA fix!\r\n");           
    return false;
};