Last version of TinyGPS library
Dependents: Nucleo_gps mbed_2018 mbed_2019_rx3 mbed_2019 ... more
TinyGPS.h@0:44bee9056857, 2016-05-21 (annotated)
- Committer:
- dROb
- Date:
- Sat May 21 12:39:15 2016 +0000
- Revision:
- 0:44bee9056857
Updated TinyGPS library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dROb | 0:44bee9056857 | 1 | /* |
dROb | 0:44bee9056857 | 2 | TinyGPS - a small GPS library for Arduino providing basic NMEA parsing |
dROb | 0:44bee9056857 | 3 | Based on work by and "distance_to" and "course_to" courtesy of Maarten Lamers. |
dROb | 0:44bee9056857 | 4 | Suggestion to add satellites(), course_to(), and cardinal(), by Matt Monson. |
dROb | 0:44bee9056857 | 5 | Precision improvements suggested by Wayne Holder. |
dROb | 0:44bee9056857 | 6 | Copyright (C) 2008-2013 Mikal Hart |
dROb | 0:44bee9056857 | 7 | All rights reserved. |
dROb | 0:44bee9056857 | 8 | |
dROb | 0:44bee9056857 | 9 | This library is free software; you can redistribute it and/or |
dROb | 0:44bee9056857 | 10 | modify it under the terms of the GNU Lesser General Public |
dROb | 0:44bee9056857 | 11 | License as published by the Free Software Foundation; either |
dROb | 0:44bee9056857 | 12 | version 2.1 of the License, or (at your option) any later version. |
dROb | 0:44bee9056857 | 13 | |
dROb | 0:44bee9056857 | 14 | This library is distributed in the hope that it will be useful, |
dROb | 0:44bee9056857 | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
dROb | 0:44bee9056857 | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
dROb | 0:44bee9056857 | 17 | Lesser General Public License for more details. |
dROb | 0:44bee9056857 | 18 | |
dROb | 0:44bee9056857 | 19 | You should have received a copy of the GNU Lesser General Public |
dROb | 0:44bee9056857 | 20 | License along with this library; if not, write to the Free Software |
dROb | 0:44bee9056857 | 21 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
dROb | 0:44bee9056857 | 22 | */ |
dROb | 0:44bee9056857 | 23 | |
dROb | 0:44bee9056857 | 24 | #ifndef TinyGPS_h |
dROb | 0:44bee9056857 | 25 | #define TinyGPS_h |
dROb | 0:44bee9056857 | 26 | |
dROb | 0:44bee9056857 | 27 | /*#if defined(ARDUINO) && ARDUINO >= 100 |
dROb | 0:44bee9056857 | 28 | #include "Arduino.h" |
dROb | 0:44bee9056857 | 29 | #else |
dROb | 0:44bee9056857 | 30 | #include "WProgram.h" |
dROb | 0:44bee9056857 | 31 | #endif |
dROb | 0:44bee9056857 | 32 | */ |
dROb | 0:44bee9056857 | 33 | |
dROb | 0:44bee9056857 | 34 | |
dROb | 0:44bee9056857 | 35 | #include "mbed.h" |
dROb | 0:44bee9056857 | 36 | #include "types.h" |
dROb | 0:44bee9056857 | 37 | |
dROb | 0:44bee9056857 | 38 | #include <stdlib.h> |
dROb | 0:44bee9056857 | 39 | |
dROb | 0:44bee9056857 | 40 | #define _GPS_VERSION 13 // software version of this library |
dROb | 0:44bee9056857 | 41 | #define _GPS_MPH_PER_KNOT 1.15077945 |
dROb | 0:44bee9056857 | 42 | #define _GPS_MPS_PER_KNOT 0.51444444 |
dROb | 0:44bee9056857 | 43 | #define _GPS_KMPH_PER_KNOT 1.852 |
dROb | 0:44bee9056857 | 44 | #define _GPS_MILES_PER_METER 0.00062137112 |
dROb | 0:44bee9056857 | 45 | #define _GPS_KM_PER_METER 0.001 |
dROb | 0:44bee9056857 | 46 | // #define _GPS_NO_STATS |
dROb | 0:44bee9056857 | 47 | |
dROb | 0:44bee9056857 | 48 | #define TWO_PI 6.283185 |
dROb | 0:44bee9056857 | 49 | |
dROb | 0:44bee9056857 | 50 | //Convert from radians to degrees. |
dROb | 0:44bee9056857 | 51 | #define toDegrees(x) (x * 57.2957795) |
dROb | 0:44bee9056857 | 52 | //Convert from degrees to radians. |
dROb | 0:44bee9056857 | 53 | #define toRadians(x) (x * 0.01745329252) |
dROb | 0:44bee9056857 | 54 | |
dROb | 0:44bee9056857 | 55 | class TinyGPS |
dROb | 0:44bee9056857 | 56 | { |
dROb | 0:44bee9056857 | 57 | public: |
dROb | 0:44bee9056857 | 58 | enum { |
dROb | 0:44bee9056857 | 59 | GPS_INVALID_AGE = 0xFFFFFFFF, GPS_INVALID_ANGLE = 999999999, |
dROb | 0:44bee9056857 | 60 | GPS_INVALID_ALTITUDE = 999999999, GPS_INVALID_DATE = 0, |
dROb | 0:44bee9056857 | 61 | GPS_INVALID_TIME = 0xFFFFFFFF, GPS_INVALID_SPEED = 999999999, |
dROb | 0:44bee9056857 | 62 | GPS_INVALID_FIX_TIME = 0xFFFFFFFF, GPS_INVALID_SATELLITES = 0xFF, |
dROb | 0:44bee9056857 | 63 | GPS_INVALID_HDOP = 0xFFFFFFFF |
dROb | 0:44bee9056857 | 64 | }; |
dROb | 0:44bee9056857 | 65 | |
dROb | 0:44bee9056857 | 66 | static const float GPS_INVALID_F_ANGLE, GPS_INVALID_F_ALTITUDE, GPS_INVALID_F_SPEED; |
dROb | 0:44bee9056857 | 67 | |
dROb | 0:44bee9056857 | 68 | TinyGPS(); |
dROb | 0:44bee9056857 | 69 | bool encode(char c); // process one character received from GPS |
dROb | 0:44bee9056857 | 70 | TinyGPS &operator << (char c) {encode(c); return *this;} |
dROb | 0:44bee9056857 | 71 | |
dROb | 0:44bee9056857 | 72 | // lat/long in MILLIONTHs of a degree and age of fix in milliseconds |
dROb | 0:44bee9056857 | 73 | // (note: versions 12 and earlier gave lat/long in 100,000ths of a degree. |
dROb | 0:44bee9056857 | 74 | void get_position(long *latitude, long *longitude, unsigned long *fix_age = 0); |
dROb | 0:44bee9056857 | 75 | |
dROb | 0:44bee9056857 | 76 | // date as ddmmyy, time as hhmmsscc, and age in milliseconds |
dROb | 0:44bee9056857 | 77 | void get_datetime(unsigned long *date, unsigned long *time, unsigned long *age = 0); |
dROb | 0:44bee9056857 | 78 | |
dROb | 0:44bee9056857 | 79 | // signed altitude in centimeters (from GPGGA sentence) |
dROb | 0:44bee9056857 | 80 | inline long altitude() { return _altitude; } |
dROb | 0:44bee9056857 | 81 | |
dROb | 0:44bee9056857 | 82 | // course in last full GPRMC sentence in 100th of a degree |
dROb | 0:44bee9056857 | 83 | inline unsigned long course() { return _course; } |
dROb | 0:44bee9056857 | 84 | |
dROb | 0:44bee9056857 | 85 | // speed in last full GPRMC sentence in 100ths of a knot |
dROb | 0:44bee9056857 | 86 | inline unsigned long speed() { return _speed; } |
dROb | 0:44bee9056857 | 87 | |
dROb | 0:44bee9056857 | 88 | // satellites used in last full GPGGA sentence |
dROb | 0:44bee9056857 | 89 | inline unsigned short satellites() { return _numsats; } |
dROb | 0:44bee9056857 | 90 | |
dROb | 0:44bee9056857 | 91 | // horizontal dilution of precision in 100ths |
dROb | 0:44bee9056857 | 92 | inline unsigned long hdop() { return _hdop; } |
dROb | 0:44bee9056857 | 93 | |
dROb | 0:44bee9056857 | 94 | void f_get_position(float *latitude, float *longitude, unsigned long *fix_age = 0); |
dROb | 0:44bee9056857 | 95 | void crack_datetime(int *year, byte *month, byte *day, |
dROb | 0:44bee9056857 | 96 | byte *hour, byte *minute, byte *second, byte *hundredths = 0, unsigned long *fix_age = 0); |
dROb | 0:44bee9056857 | 97 | float f_altitude(); |
dROb | 0:44bee9056857 | 98 | float f_course(); |
dROb | 0:44bee9056857 | 99 | float f_speed_knots(); |
dROb | 0:44bee9056857 | 100 | float f_speed_mph(); |
dROb | 0:44bee9056857 | 101 | float f_speed_mps(); |
dROb | 0:44bee9056857 | 102 | float f_speed_kmph(); |
dROb | 0:44bee9056857 | 103 | |
dROb | 0:44bee9056857 | 104 | static int library_version() { return _GPS_VERSION; } |
dROb | 0:44bee9056857 | 105 | |
dROb | 0:44bee9056857 | 106 | static float distance_between (float lat1, float long1, float lat2, float long2); |
dROb | 0:44bee9056857 | 107 | static float course_to (float lat1, float long1, float lat2, float long2); |
dROb | 0:44bee9056857 | 108 | static const char *cardinal(float course); |
dROb | 0:44bee9056857 | 109 | |
dROb | 0:44bee9056857 | 110 | #ifndef _GPS_NO_STATS |
dROb | 0:44bee9056857 | 111 | void stats(unsigned long *chars, unsigned short *good_sentences, unsigned short *failed_cs); |
dROb | 0:44bee9056857 | 112 | #endif |
dROb | 0:44bee9056857 | 113 | |
dROb | 0:44bee9056857 | 114 | private: |
dROb | 0:44bee9056857 | 115 | enum {_GPS_SENTENCE_GPGGA, _GPS_SENTENCE_GPRMC, _GPS_SENTENCE_OTHER}; |
dROb | 0:44bee9056857 | 116 | |
dROb | 0:44bee9056857 | 117 | // properties |
dROb | 0:44bee9056857 | 118 | unsigned long _time, _new_time; |
dROb | 0:44bee9056857 | 119 | unsigned long _date, _new_date; |
dROb | 0:44bee9056857 | 120 | long _latitude, _new_latitude; |
dROb | 0:44bee9056857 | 121 | long _longitude, _new_longitude; |
dROb | 0:44bee9056857 | 122 | long _altitude, _new_altitude; |
dROb | 0:44bee9056857 | 123 | unsigned long _speed, _new_speed; |
dROb | 0:44bee9056857 | 124 | unsigned long _course, _new_course; |
dROb | 0:44bee9056857 | 125 | unsigned long _hdop, _new_hdop; |
dROb | 0:44bee9056857 | 126 | unsigned short _numsats, _new_numsats; |
dROb | 0:44bee9056857 | 127 | |
dROb | 0:44bee9056857 | 128 | unsigned long _last_time_fix, _new_time_fix; |
dROb | 0:44bee9056857 | 129 | unsigned long _last_position_fix, _new_position_fix; |
dROb | 0:44bee9056857 | 130 | |
dROb | 0:44bee9056857 | 131 | // parsing state variables |
dROb | 0:44bee9056857 | 132 | byte _parity; |
dROb | 0:44bee9056857 | 133 | bool _is_checksum_term; |
dROb | 0:44bee9056857 | 134 | char _term[15]; |
dROb | 0:44bee9056857 | 135 | byte _sentence_type; |
dROb | 0:44bee9056857 | 136 | byte _term_number; |
dROb | 0:44bee9056857 | 137 | byte _term_offset; |
dROb | 0:44bee9056857 | 138 | bool _gps_data_good; |
dROb | 0:44bee9056857 | 139 | |
dROb | 0:44bee9056857 | 140 | #ifndef _GPS_NO_STATS |
dROb | 0:44bee9056857 | 141 | // statistics |
dROb | 0:44bee9056857 | 142 | unsigned long _encoded_characters; |
dROb | 0:44bee9056857 | 143 | unsigned short _good_sentences; |
dROb | 0:44bee9056857 | 144 | unsigned short _failed_checksum; |
dROb | 0:44bee9056857 | 145 | unsigned short _passed_checksum; |
dROb | 0:44bee9056857 | 146 | #endif |
dROb | 0:44bee9056857 | 147 | |
dROb | 0:44bee9056857 | 148 | // internal utilities |
dROb | 0:44bee9056857 | 149 | int from_hex(char a); |
dROb | 0:44bee9056857 | 150 | unsigned long parse_decimal(); |
dROb | 0:44bee9056857 | 151 | unsigned long parse_degrees(); |
dROb | 0:44bee9056857 | 152 | bool term_complete(); |
dROb | 0:44bee9056857 | 153 | bool gpsisdigit(char c) { return c >= '0' && c <= '9'; } |
dROb | 0:44bee9056857 | 154 | long gpsatol(const char *str); |
dROb | 0:44bee9056857 | 155 | int gpsstrcmp(const char *str1, const char *str2); |
dROb | 0:44bee9056857 | 156 | }; |
dROb | 0:44bee9056857 | 157 | |
dROb | 0:44bee9056857 | 158 | #if !defined(ARDUINO) |
dROb | 0:44bee9056857 | 159 | // Arduino 0012 workaround |
dROb | 0:44bee9056857 | 160 | #undef int |
dROb | 0:44bee9056857 | 161 | #undef char |
dROb | 0:44bee9056857 | 162 | #undef long |
dROb | 0:44bee9056857 | 163 | #undef byte |
dROb | 0:44bee9056857 | 164 | #undef float |
dROb | 0:44bee9056857 | 165 | #undef abs |
dROb | 0:44bee9056857 | 166 | #undef round |
dROb | 0:44bee9056857 | 167 | #endif |
dROb | 0:44bee9056857 | 168 | |
dROb | 0:44bee9056857 | 169 | #endif |