Code for autonomous ground vehicle, Data Bus, 3rd place winner in 2012 Sparkfun AVC.

Dependencies:   Watchdog mbed Schedule SimpleFilter LSM303DLM PinDetect DebounceIn Servo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TinyGPS.h Source File

TinyGPS.h

00001 /*
00002   TinyGPS - a small GPS library for Arduino providing basic NMEA parsing
00003   Copyright (C) 2008-9 Mikal Hart
00004   All rights reserved.
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Lesser General Public
00008   License as published by the Free Software Foundation; either
00009   version 2.1 of the License, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Lesser General Public License for more details.
00015 
00016   You should have received a copy of the GNU Lesser General Public
00017   License along with this library; if not, write to the Free Software
00018   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019   
00020   Ported to mbed by Michael Shimniok
00021 */
00022 
00023 #include "mbed.h"
00024 #include "types.h"
00025 
00026 #ifndef TinyGPS_h
00027 #define TinyGPS_h
00028 
00029 #define _GPS_VERSION 9 // software version of this library
00030 #define _GPS_MPH_PER_KNOT 1.15077945
00031 #define _GPS_MPS_PER_KNOT 0.51444444
00032 #define _GPS_KMPH_PER_KNOT 1.852
00033 #define _GPS_MILES_PER_METER 0.00062137112
00034 #define _GPS_KM_PER_METER 0.001
00035 //#define _GPS_NO_STATS
00036 
00037 /** TinyGPS - a small GPS library for Arduino providing basic NMEA parsing Copyright (C) 2008-9 Mikal Hart
00038  * All rights reserved. Modified by Michael Shimniok 
00039  */
00040 
00041 class TinyGPS
00042 {
00043   public:
00044   
00045     /** Create a new GPS parsing object for parsing NMEA sentences
00046      */
00047     TinyGPS();
00048     
00049     /** Parse a single character received from GPS
00050      *
00051      * @param c is the character received from the GPS
00052      * @returns true if processing ok
00053      */
00054     bool encode(char c);
00055     
00056     /** Shorthand operator for encode()
00057      */
00058     TinyGPS &operator << (char c) {encode(c); return *this;}
00059     
00060     /** Return lat/long in hundred thousandths of a degree and age of fix in milliseconds
00061      * @returns latitude is the latitude of the most recent fix that was parsed
00062      * @returns longitude is the longitude of the most recent fix that was parsed
00063      * @returns fix_age is the age of the fix (if available from the NMEA sentences being parsed)
00064      */
00065     inline void get_position(long *latitude, long *longitude, unsigned long *fix_age = 0)
00066     {
00067       if (latitude) *latitude = _latitude;
00068       if (longitude) *longitude = _longitude;
00069       if (fix_age) *fix_age = _last_position_fix == GPS_INVALID_FIX_TIME ? 
00070         GPS_INVALID_AGE : millis() - _last_position_fix;
00071     }
00072 
00073     /** Return the date and time from the parsed NMEA sentences
00074      *
00075      * @returns date as an integer value
00076      * @returns time as an integer value
00077      * @returns fix_age in milliseconds if available
00078      */
00079     inline void get_datetime(unsigned long *date, unsigned long *time, unsigned long *fix_age = 0)
00080     {
00081       if (date) *date = _date;
00082       if (time) *time = _time;
00083       if (fix_age) *fix_age = _last_time_fix == GPS_INVALID_FIX_TIME ? 
00084         GPS_INVALID_AGE : millis() - _last_time_fix;
00085     }
00086 
00087     /** signed altitude in centimeters (from GPGGA sentence)
00088      * @returns altitude in centimeters, integer
00089      */
00090     inline long altitude() { return _altitude; }
00091 
00092     /** course in last full GPRMC sentence in 100th of a degree
00093      * @returns course as an integer, 100ths of a degree
00094      */
00095     inline unsigned long course() { return _course; }
00096     
00097     /** speed in last full GPRMC sentence in 100ths of a knot
00098      * @returns speed in 100ths of a knot
00099      */
00100     unsigned long speed() { return _speed; }
00101 
00102     /* horizontal dilution of position in last full GPGGA sentence in 100ths
00103      * @returns hdop in 100ths
00104      */
00105     unsigned long hdop() { return _hdop; }
00106 
00107     /** number of satellites tracked in last full GPGGA sentence
00108      * @returns number of satellites tracked 
00109      */
00110     unsigned long sat_count() { return _sat_count; }
00111 
00112 #ifndef _GPS_NO_STATS
00113     void stats(unsigned long *chars, unsigned short *good_sentences, unsigned short *failed_cs);
00114 #endif
00115 
00116     /** returns position as double precision
00117      *
00118      * @returns latitude as double precision
00119      * @returns longitude as double precision
00120      * @returns fix age in milliseconds if available
00121      */
00122     inline void f_get_position(double *latitude, double *longitude, unsigned long *fix_age = 0)
00123     {
00124       long lat, lon;
00125       get_position(&lat, &lon, fix_age);
00126       // mes 04/27/12 increased fractional precision to 7 digits, was 5
00127       *latitude = lat / 10000000.0;
00128       *longitude = lon / 10000000.0;
00129     }
00130 
00131     /** Convert date and time of last parsed sentence to integers
00132      *
00133      * @returns year
00134      * @returns month
00135      * @returns day of month
00136      * @returns hour
00137      * @returns minute
00138      * @returns second
00139      * @returns hundreths
00140      * @returns fix_age in milliseconds if available
00141      */
00142     inline void crack_datetime(int *year, byte *month, byte *day, 
00143       byte *hour, byte *minute, byte *second, byte *hundredths = 0, unsigned long *fix_age = 0)
00144     {
00145       unsigned long date, time;
00146       get_datetime(&date, &time, fix_age);
00147       if (year) 
00148       {
00149         *year = date % 100;
00150         *year += *year > 80 ? 1900 : 2000;
00151       }
00152       if (month) *month = (date / 100) % 100;
00153       if (day) *day = date / 10000;
00154       if (hour) *hour = time / 1000000;
00155       if (minute) *minute = (time / 10000) % 100;
00156       if (second) *second = (time / 100) % 100;
00157       if (hundredths) *hundredths = time % 100;
00158     }
00159 
00160     /** returns altitude as a float
00161     */
00162     inline double f_altitude()    { return altitude() / 100.0; }
00163     
00164     /** returns course as a float
00165     */
00166     inline double f_course()      { return course() / 100.0; }
00167     
00168     /** returns speed in knots as a float
00169     */
00170     inline double f_speed_knots() { return speed() / 100.0; }
00171     
00172     /** returns speed in mph as a float 
00173     */
00174     inline double f_speed_mph()   { return _GPS_MPH_PER_KNOT * f_speed_knots(); }
00175     
00176     /** returns speed in meters per second as a float
00177     */
00178     inline double f_speed_mps()   { return _GPS_MPS_PER_KNOT * f_speed_knots(); }
00179     
00180     /** returns speed in km per hour as a float
00181     */
00182     inline double f_speed_kmph()  { return _GPS_KMPH_PER_KNOT * f_speed_knots(); }
00183     
00184     /** returns hdop as a float
00185     */
00186     inline double f_hdop()      { return hdop() / 100.0; }
00187 
00188     /** @returns library version
00189     */
00190     static int library_version () { return _GPS_VERSION; }
00191 
00192     /** determine if RMC sentence parsed since last reset_ready()
00193      */
00194     inline bool rmc_ready() { return _rmc_ready; }
00195 
00196     /** determine if GGA sentence parsed since last reset_ready()
00197      */
00198     inline bool gga_ready() { return _gga_ready; }
00199 
00200     /** determine if GSV sentence parsed since last reset_ready()
00201      */
00202     inline bool gsv_ready() { return _gsv_ready; }
00203     
00204     /** Reset the ready flags for all the parsed sentences
00205      */
00206     inline void reset_ready() { _gsv_ready = _rmc_ready = _gga_ready = false; }
00207 
00208     enum {GPS_INVALID_AGE = 0xFFFFFFFF, GPS_INVALID_ANGLE = 999999999, GPS_INVALID_ALTITUDE = 999999999, GPS_INVALID_DATE = 0,
00209       GPS_INVALID_TIME = 0xFFFFFFFF, GPS_INVALID_SPEED = 999999999, GPS_INVALID_FIX_TIME = 0xFFFFFFFF};
00210 
00211 private:
00212     enum {_GPS_SENTENCE_GPGGA, _GPS_SENTENCE_GPRMC, _GPS_SENTENCE_GPGSV, _GPS_SENTENCE_OTHER};
00213     
00214     // properties
00215     unsigned long _time, _new_time;
00216     unsigned long _date, _new_date;
00217     long _latitude, _new_latitude;
00218     long _longitude, _new_longitude;
00219     long _altitude, _new_altitude;
00220     unsigned long  _speed, _new_speed;
00221     unsigned long  _course, _new_course;
00222     unsigned long  _hdop, _new_hdop;
00223     unsigned int _sat_count, _new_sat_count;
00224     unsigned long _last_time_fix, _new_time_fix;
00225     unsigned long _last_position_fix, _new_position_fix;
00226 
00227     // parsing state variables
00228     byte _parity;
00229     bool _is_checksum_term;
00230     char _term[15];
00231     byte _sentence_type;
00232     byte _term_number;
00233     byte _term_offset;
00234     bool _gps_data_good;
00235     bool _rmc_ready;
00236     bool _gga_ready;
00237     bool _gsv_ready;
00238 
00239 #ifndef _GPS_NO_STATS
00240     // statistics
00241     unsigned long _encoded_characters;
00242     unsigned short _good_sentences;
00243     unsigned short _failed_checksum;
00244     unsigned short _passed_checksum;
00245 #endif
00246 
00247     // internal utilities
00248     int from_hex(char a);
00249     int parse_int();
00250     unsigned long parse_decimal();
00251     unsigned long parse_degrees();
00252     bool term_complete();
00253     bool gpsisdigit(char c) { return c >= '0' && c <= '9'; }
00254     long gpsatol(const char *str);
00255     int gpsstrcmp(const char *str1, const char *str2);
00256 };
00257 
00258 // Arduino 0012 workaround
00259 #undef int
00260 #undef char
00261 #undef long
00262 #undef byte
00263 #undef double
00264 #undef abs
00265 #undef round 
00266 
00267 #endif