a program to get GPS latitude and longitude and precision, with a simple validation.

Dependencies:   mbed

GPS/GPS.cpp

Committer:
giovanniwsn
Date:
2018-05-25
Revision:
0:cc406c7b09be

File content as of revision 0:cc406c7b09be:

/*LIB GPS
author: Giovanni Migon
GPS GP 735T
*/

#include "GPS.h"

GPS::GPS(PinName pinTx, PinName pinRx, int Baud) : _serial( pinTx, pinRx, Baud){
    _pinTx = pinTx;
    _pinRx = pinRx;
    _Baud = Baud;
    _count_rx = 0;
    memset(&_buf_rx,0,sizeof(_buf_rx));
    
    lat = 0;
    lon = 0;
    pdop = 0;
}

GPS::~GPS() {
}

/*void GPS::SerialRecvInterrupt (void)
{
    if( _serial.readable() ) 
        _buf_rx[_count_rx++%sizeof(_buf_rx)] = _serial.getc();
}*/

void GPS::printRX(void) {
    
    uint8_t buf_cp[512];
    unsigned short i=0;
    memcpy(buf_cp,_buf_rx,sizeof(_buf_rx));
    //Print all buffer
    for(i = 0; i < sizeof(buf_cp); i++) {
        //putc(buf_cp[i]);
        printf("%c",buf_cp[i]);
    }
    // Buffer clear
    //memset(&buf_cp,0,sizeof(buf_cp));
}

void GPS::printGPS(void) {
    //$GPGLL,3003.66092,S,05110.42152,W,165141.00,A,A*6E  52 caracters
    // 1o: split in substrings; 
    // 2o: validate a substring; 
    // 3o: print correct substring;
    
    uint8_t buf_cp[512];
    uint8_t buf_gpgll[52];      // gps cmd gpgll
    uint8_t buf_gpgsa[64];      // gps cmd gpgsa
    
    unsigned short i = 0;
    uint8_t j = 0;
    uint8_t count_p = 0;
    
    // Restart variables
    count_p = 0;
    lat = 0;
    lon = 0;
    pdop = 0;
    
    memcpy(buf_cp,_buf_rx,sizeof(_buf_rx));
    
    int size_msg = sizeof(buf_cp) - sizeof(buf_gpgsa);
    //char * gpgll;
    
    for(i = 0; i < size_msg ; i++) {
        // Detect start command character
        if( buf_cp[i] == '$' ) {
            // Detect $GPGLL
            if( buf_cp[i+4] == 'L' && buf_cp[i+5] == 'L' ) {
                memcpy(buf_gpgll,&buf_cp[i],sizeof(buf_gpgll));
                // Validation Conditions of $GPGLL
                if( (buf_gpgll[18] == 'S' || buf_gpgll[18] == 'N') && (buf_gpgll[32] == 'E' || buf_gpgll[32] == 'W') ) {
                    lon = atof((char *)&buf_gpgll[20]);
                    lat = atof((char *)&buf_gpgll[7]);
                    //printf("%s\n",buf_gpgll);
                    //printf("Lat = %f;  Lon = %f;\n",lat, lon);
                } else {
                    //printf("Error  GPGLL\n");
                    count_p--;
                }
            }
            // Detect $GPGSA
            if( (buf_cp[i+4] == 'S') && (buf_cp[i+5] == 'A') ) {
                memcpy(buf_gpgsa,&buf_cp[i],sizeof(buf_gpgsa));
                //printf("%s@\n",buf_gpgsa);
                // Validation Conditions
                for(j = 20; j < sizeof(buf_gpgsa); j++) {
                    if(buf_gpgsa[j] == '.' && count_p == 0) {
                        pdop = buf_gpgsa[j-1] - 0x30;
                        count_p++;
                        //printf("Precision = %d;\n",pdop);
                    }
                }
            }
        }
    }
    // Verify precision and correct data
    if( (((pdop > 0) && (pdop < 5)) && (count_p == 1) ) && lat != 0 ) {
        printf("Pre = %d;  Lat = %f;  Lon = %f;\n", pdop, lat, lon);
    } else {
        printf("Invalid Precision\n");
    }
}