library for parsing GPS NMEA strings using serial receive interrupt

Fork of GPSINT by Joseph Bradshaw

GPSINT library

Library class for intercepting NMEA-0183 ASCII strings from a GPS using a serial interrupt at 4800 baud, error checking, and parsing using the scanf functions to update object associated variables

include the mbed library with this snippet

// mbed GPS Parse Program, tested using the Trimble Copernicus II
#include "mbed.h"
#include "GPSINT.h"

#define TIMEZONE -4 //Eastern Standard Time

Serial pc(USBTX,USBRX);
GPSINT gps(p28, p27);   //Tx,Rx
DigitalOut led1(LED1);

// ---------------- MAIN ------------------------
int main() {        
    pc.baud(921600);
  
    while(1) {
        if(gps.lock != 0){
            led1=!led1;
            
            int gpsHour = (int)((int)gps.utc_time/10000) + TIMEZONE;
            if(gpsHour < 0)
                gpsHour += 24;
            
            int gpsMin = (int)((int)gps.utc_time/100%100);
            int gpsSec = (int)gps.utc_time % 100;
      
            pc.printf("lock=%d %2d:%02d:%02d  %f %c %f %c\r\n", gps.lock, gpsHour,gpsMin,gpsSec,gps.nmea_longitude, gps.ns, gps.nmea_latitude, gps.ew);
            wait(.9);
        }
        else{
            pc.printf("No Lock\r\n");
            wait(1);    
        }        
    }//while(1)
}//main
Committer:
jebradshaw
Date:
Wed Nov 05 16:43:55 2014 +0000
Revision:
1:c266b90b4c74
Parent:
0:f3a7d716faea
Uses serial receive interrupt to extract, checksum validate, and parse gps nmea messages at 4800 baud

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 1:c266b90b4c74 1 /* GPSINT.cpp
jebradshaw 1:c266b90b4c74 2 * jbradshaw (20141101)
jebradshaw 1:c266b90b4c74 3 * GPS functions are work of Tyler Weavers mbed gps library page
jebradshaw 1:c266b90b4c74 4 * (http://mbed.org/users/tylerjw/code/GPS/file/39d75e44b214/GPS.cpp)
jebradshaw 0:f3a7d716faea 5 *
jebradshaw 0:f3a7d716faea 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
jebradshaw 0:f3a7d716faea 7 * of this software and associated documentation files (the "Software"), to deal
jebradshaw 0:f3a7d716faea 8 * in the Software without restriction, including without limitation the rights
jebradshaw 0:f3a7d716faea 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jebradshaw 0:f3a7d716faea 10 * copies of the Software, and to permit persons to whom the Software is
jebradshaw 0:f3a7d716faea 11 * furnished to do so, subject to the following conditions:
jebradshaw 0:f3a7d716faea 12 *
jebradshaw 0:f3a7d716faea 13 * The above copyright notice and this permission notice shall be included in
jebradshaw 0:f3a7d716faea 14 * all copies or substantial portions of the Software.
jebradshaw 0:f3a7d716faea 15 *
jebradshaw 0:f3a7d716faea 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jebradshaw 0:f3a7d716faea 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jebradshaw 0:f3a7d716faea 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jebradshaw 0:f3a7d716faea 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jebradshaw 0:f3a7d716faea 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jebradshaw 0:f3a7d716faea 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jebradshaw 0:f3a7d716faea 22 * THE SOFTWARE.
jebradshaw 0:f3a7d716faea 23 */
jebradshaw 0:f3a7d716faea 24
jebradshaw 0:f3a7d716faea 25 /* This GPSINT library class retrieves, parses, and updates variables using
jebradshaw 0:f3a7d716faea 26 a serial receive interrupt. The library was written using the Trimble
jebradshaw 0:f3a7d716faea 27 Copernicus II moduls bus should work on any NMEA 4800 baud output device.
jebradshaw 0:f3a7d716faea 28 The relatively slow 4800 baud allows for plenty of processing time during
jebradshaw 0:f3a7d716faea 29 byte receptions in the interrupt. Using the mbed LPC 100MHz unit, it takes
jebradshaw 0:f3a7d716faea 30 about 340us to process, and parse the 2 default GPGGA and GPZTG strings.*/
jebradshaw 0:f3a7d716faea 31
jebradshaw 0:f3a7d716faea 32 #include "mbed.h"
jebradshaw 0:f3a7d716faea 33
jebradshaw 0:f3a7d716faea 34 #ifndef GPSINT_H
jebradshaw 0:f3a7d716faea 35 #define GPSINT_H
jebradshaw 0:f3a7d716faea 36
jebradshaw 0:f3a7d716faea 37 #define PI (3.14159265359)
jebradshaw 0:f3a7d716faea 38 #define GPSBUFSIZE 256 // GPS buffer size
jebradshaw 0:f3a7d716faea 39
jebradshaw 0:f3a7d716faea 40 /**
jebradshaw 0:f3a7d716faea 41 * GPSINT Class.
jebradshaw 0:f3a7d716faea 42 */
jebradshaw 0:f3a7d716faea 43
jebradshaw 0:f3a7d716faea 44 class GPSINT{
jebradshaw 0:f3a7d716faea 45 public:
jebradshaw 0:f3a7d716faea 46 /**
jebradshaw 0:f3a7d716faea 47 * Constructor.
jebradshaw 0:f3a7d716faea 48 * @param gps - Serial port pins attached to the gps
jebradshaw 0:f3a7d716faea 49 */
jebradshaw 0:f3a7d716faea 50 GPSINT(PinName tx, PinName rx);
jebradshaw 1:c266b90b4c74 51 int nmea_validate(char *nmeastr); //runs the checksum calculation on the GPS NMEA string
jebradshaw 1:c266b90b4c74 52 void parseGPSString(char *GPSstrParse); //uses scanf to parse NMEA string into variables
jebradshaw 1:c266b90b4c74 53 void GPSSerialRecvInterrupt(void); //fills temprpary buffer for processing
jebradshaw 1:c266b90b4c74 54 float nmea_to_dec(float deg_coord, char nsew); //convert nmea format to decimal format
jebradshaw 0:f3a7d716faea 55 float calc_course_to(float pointLat, float pontLong);
jebradshaw 0:f3a7d716faea 56 double calc_dist_to_mi(float pointLat, float pontLong);
jebradshaw 0:f3a7d716faea 57 double calc_dist_to_ft(float pointLat, float pontLong);
jebradshaw 0:f3a7d716faea 58 double calc_dist_to_km(float pointLat, float pontLong);
jebradshaw 0:f3a7d716faea 59 double calc_dist_to_m(float pointLat, float pontLong);
jebradshaw 0:f3a7d716faea 60
jebradshaw 0:f3a7d716faea 61 // GPSINT variables
jebradshaw 0:f3a7d716faea 62 char GPSbuf[GPSBUFSIZE]; // Receive buffer.
jebradshaw 0:f3a7d716faea 63 char Temp_GPSbuf[GPSBUFSIZE];// Receive buffer.
jebradshaw 0:f3a7d716faea 64 char GPSidx; // Read by background, written by Int Handler.
jebradshaw 0:f3a7d716faea 65 int GPSstate; //set when successful GPS string is received
jebradshaw 0:f3a7d716faea 66
jebradshaw 0:f3a7d716faea 67 // calculated values
jebradshaw 0:f3a7d716faea 68 float dec_longitude;
jebradshaw 0:f3a7d716faea 69 float dec_latitude;
jebradshaw 0:f3a7d716faea 70 float altitude_ft;
jebradshaw 0:f3a7d716faea 71
jebradshaw 0:f3a7d716faea 72 // GGA - Global Positioning System Fixed Data
jebradshaw 0:f3a7d716faea 73 float nmea_longitude;
jebradshaw 0:f3a7d716faea 74 float nmea_latitude;
jebradshaw 0:f3a7d716faea 75 float utc_time;
jebradshaw 0:f3a7d716faea 76 char ns, ew;
jebradshaw 0:f3a7d716faea 77 int lock;
jebradshaw 0:f3a7d716faea 78 int satelites;
jebradshaw 0:f3a7d716faea 79 float hdop;
jebradshaw 0:f3a7d716faea 80 float msl_altitude;
jebradshaw 0:f3a7d716faea 81 char msl_units;
jebradshaw 0:f3a7d716faea 82
jebradshaw 0:f3a7d716faea 83 // RMC - Recommended Minimmum Specific GNS Data
jebradshaw 0:f3a7d716faea 84 char rmc_status;
jebradshaw 0:f3a7d716faea 85 float speed_k;
jebradshaw 0:f3a7d716faea 86 float course_d;
jebradshaw 0:f3a7d716faea 87 int date;
jebradshaw 0:f3a7d716faea 88
jebradshaw 0:f3a7d716faea 89 // GLL
jebradshaw 0:f3a7d716faea 90 char gll_status;
jebradshaw 0:f3a7d716faea 91
jebradshaw 0:f3a7d716faea 92 // VTG - Course over ground, ground speed
jebradshaw 0:f3a7d716faea 93 float course_t; // ground speed true
jebradshaw 0:f3a7d716faea 94 char course_t_unit;
jebradshaw 0:f3a7d716faea 95 float course_m; // magnetic
jebradshaw 0:f3a7d716faea 96 char course_m_unit;
jebradshaw 0:f3a7d716faea 97 char speed_k_unit;
jebradshaw 0:f3a7d716faea 98 float speed_km; // speek km/hr
jebradshaw 0:f3a7d716faea 99 char speed_km_unit;
jebradshaw 0:f3a7d716faea 100 private:
jebradshaw 0:f3a7d716faea 101 Serial _gps;
jebradshaw 0:f3a7d716faea 102 };
jebradshaw 0:f3a7d716faea 103
jebradshaw 0:f3a7d716faea 104 #endif /* GPSINT_H */