GPSParser.cpp

Committer:
slisgrinder
Date:
2012-03-27
Revision:
0:01c9f535e512
Child:
1:e143afe9a7ea

File content as of revision 0:01c9f535e512:

#include "GPSParser.h"


const int GLL = 0, RMC = 0, VTG = 1, GGA = 1, GSA = 0, GSV = 0, GRS = 0, GST = 0, MALM = 0, MEPH = 0, MDGP = 0, MDBG = 0, ZDA = 0, MCHN = 0; //refer to http://aprs.gids.nl/nmea/#gll for NMEA 0183 sentences
volatile bool buffer_ready = false;
volatile int num_sentences = 0;
volatile int ctr = 0;
typedef struct
{
    float latitude, lat_deg, lat_min, deg_latitude, longitude, long_deg, long_min, deg_longitude, altitude, HDOP, geoidal_sep, AODC, heading, mag_heading, speed_knots, speed_kph;
    char ns, ew, mode;
    int hours, minutes, seconds, m_seconds, fix, sats_used, DGPS_ID;
    unsigned short checksum_gga, checksum_vtg;
} GPS;

GPS _gps;

void gps_init (int num_sentence, MODSERIAL &gps)
{
    num_sentences = num_sentence;
    // BEGIN CONFIGURATION OF GPS use http://www.hhhh.org/wiml/proj/nmeaxor.html for checksum calculations...
    gps.printf ("$PMTK313,1*2E\r\n"); //enable SBAS satellite searching
    gps.printf ("$PMTK301,2*2E\r\n"); //set DGPS mode to WAAS
    gps.printf ("$PMTK314,%d,%d,%d,%d,%d,%d,%d,%d,0,0,0,0,0,%d,%d,%d,%d,%d,%d*28\r\n", GLL, RMC, VTG, GGA, GSA, GSV, GRS, GST, MALM, MEPH, MDGP, MDBG, ZDA, MCHN); //enable NMEA output sentences and frequencies
    // END CONFIGURATION OF GPS
    wait (0.1);
    gps.rxBufferFlush ();
    gps.attach (&ready_buffer, MODSERIAL::RxIrq);    
}

void ready_buffer (MODSERIAL_IRQ_INFO *q)
{
    MODSERIAL *serial = q->serial;
    
    if (serial->rxGetLastChar() == '\n')
        ctr++;
    
    if (ctr == num_sentences)
    {
        buffer_ready = true;
        ctr = 0;
    }
}

void read_gps (MODSERIAL &gps, MODSERIAL &pc)
{
    char buff [num_sentences][128];
    gps.attach (&ready_buffer, MODSERIAL::RxIrq);
        
    //while (!buffer_ready);
    
    for (int i = 0; i < num_sentences; i++)
    {
        for (int k = 0; k < 128; k++)
        {
            buff [i][k] = gps.getc();
            if (buff [i][k] == ',' && buff [i][k - 1] == ',') // if the current and last character read were both ',' then assign the next buff position ',' and the current buff position a '0' and increment k by one.
            {
                buff [i][k + 1] = buff [i][k];
                buff [i][k] = '0';
                k++;
            }
            if (buff [i][k] == '*' && buff [i][k - 1] == ',') // if the current character is '*' and last character read were both ',' then assign the next buff position '*' and the current buff position a '0' and increment k by one.
            {
                buff [i][k + 1] = buff [i][k];
                buff [i][k] = '0';
                k++;
            }
            //pc.putc (buff [i][k]);
            if (buff [i][k] == '\n')
            {
                k++;
                buff [i][k] = '\0';
                break;
            }
        }
    }
    
    for (int i = 0; i < num_sentences; i++)
    {
        if (!sscanf (buff [i], "$GPGGA,%2d%2d%2d.%3d,%f,%c,%f,%c,%d,%d,%f,%f,M,%f,M,%f,%X*%hX\r\n", &_gps.hours, &_gps.minutes, &_gps.seconds, &_gps.m_seconds, &_gps.latitude, &_gps.ns, &_gps.longitude, &_gps.ew, &_gps.fix, &_gps.sats_used, &_gps.HDOP, &_gps.altitude, &_gps.geoidal_sep, &_gps.AODC, &_gps.DGPS_ID, &_gps.checksum_gga))
            sscanf (buff [i + 1], "$GPGGA,%2d%2d%2d.%3d,%f,%c,%f,%c,%d,%d,%f,%f,M,%f,M,%f,%X*%hX\r\n", &_gps.hours, &_gps.minutes, &_gps.seconds, &_gps.m_seconds, &_gps.latitude, &_gps.ns, &_gps.longitude, &_gps.ew, &_gps.fix, &_gps.sats_used, &_gps.HDOP, &_gps.altitude, &_gps.geoidal_sep, &_gps.AODC, &_gps.DGPS_ID, &_gps.checksum_gga);
        
        if(!sscanf (buff [i], "$GPVTG,%f,T,%f,M,%f,N,%f,K,%c*%hX\r\n", &_gps.heading, &_gps.mag_heading, &_gps.speed_knots, &_gps.speed_kph, &_gps.mode, &_gps.checksum_vtg))
            sscanf (buff [i + 1], "$GPVTG,%f,T,%f,M,%f,N,%f,K,%c*%hX\r\n", &_gps.heading, &_gps.mag_heading, &_gps.speed_knots, &_gps.speed_kph, &_gps.mode, &_gps.checksum_vtg);
            
        pc.printf ("%s", buff [i]);
    }
    if (_gps.ns == 'S')
        _gps.latitude *= -1.0f;
        
    if (_gps.ew == 'W')
        _gps.longitude *= -1.0f;
    
    _gps.lat_deg = (int)(_gps.latitude / 100);
    _gps.lat_min = _gps.latitude - (_gps.lat_deg * 100.0f);
    _gps.deg_latitude = _gps.lat_deg + _gps.lat_min / 60.0f;
    _gps.long_deg = (int)(_gps.longitude /100);
    _gps.long_min = _gps.longitude - (_gps.long_deg * 100.0f);
    _gps.deg_longitude = _gps.long_deg + _gps.long_min / 60.0f;
    
    //pc.txBufferFlush();
    //pc.printf ("%X,%X,[HH:MM:SS:SSS %02d:%02d:%02d:%03d],%f,%f,%f,%c\r\n", _gps.checksum_gga, _gps.checksum_vtg, _gps.hours, _gps.minutes, _gps.seconds, _gps.m_seconds, _gps.deg_latitude, _gps.deg_longitude, _gps.heading, _gps.mode);
    
    //gps.txBufferFlush ();
    gps.rxBufferFlush ();
    buffer_ready = false;
}