2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
GPS/TinyGPS.h@43:9a285515f33a, 2019-01-03 (annotated)
- Committer:
- shimniok
- Date:
- Thu Jan 03 19:07:20 2019 +0000
- Revision:
- 43:9a285515f33a
- Parent:
- 42:8d99f64f5898
implemented tinygps serial, parsing, and callback
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 43:9a285515f33a | 1 | /* |
shimniok | 43:9a285515f33a | 2 | TinyGPS - a small GPS library for Arduino providing basic NMEA parsing |
shimniok | 43:9a285515f33a | 3 | Copyright (C) 2008-9 Mikal Hart |
shimniok | 43:9a285515f33a | 4 | All rights reserved. |
shimniok | 43:9a285515f33a | 5 | |
shimniok | 43:9a285515f33a | 6 | This library is free software; you can redistribute it and/or |
shimniok | 43:9a285515f33a | 7 | modify it under the terms of the GNU Lesser General Public |
shimniok | 43:9a285515f33a | 8 | License as published by the Free Software Foundation; either |
shimniok | 43:9a285515f33a | 9 | version 2.1 of the License, or (at your option) any later version. |
shimniok | 43:9a285515f33a | 10 | |
shimniok | 43:9a285515f33a | 11 | This library is distributed in the hope that it will be useful, |
shimniok | 43:9a285515f33a | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
shimniok | 43:9a285515f33a | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
shimniok | 43:9a285515f33a | 14 | Lesser General Public License for more details. |
shimniok | 43:9a285515f33a | 15 | |
shimniok | 43:9a285515f33a | 16 | You should have received a copy of the GNU Lesser General Public |
shimniok | 43:9a285515f33a | 17 | License along with this library; if not, write to the Free Software |
shimniok | 43:9a285515f33a | 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
shimniok | 43:9a285515f33a | 19 | |
shimniok | 43:9a285515f33a | 20 | Ported to mbed by Michael Shimniok |
shimniok | 43:9a285515f33a | 21 | */ |
shimniok | 43:9a285515f33a | 22 | |
shimniok | 43:9a285515f33a | 23 | #include "mbed.h" |
shimniok | 43:9a285515f33a | 24 | #include "GPS.h" |
shimniok | 43:9a285515f33a | 25 | |
shimniok | 43:9a285515f33a | 26 | #ifndef TinyGPS_h |
shimniok | 43:9a285515f33a | 27 | #define TinyGPS_h |
shimniok | 43:9a285515f33a | 28 | |
shimniok | 43:9a285515f33a | 29 | #define _GPS_VERSION 9 // software version of this library |
shimniok | 43:9a285515f33a | 30 | #define _GPS_MPH_PER_KNOT 1.15077945 |
shimniok | 43:9a285515f33a | 31 | #define _GPS_MPS_PER_KNOT 0.51444444 |
shimniok | 43:9a285515f33a | 32 | #define _GPS_KMPH_PER_KNOT 1.852 |
shimniok | 43:9a285515f33a | 33 | #define _GPS_MILES_PER_METER 0.00062137112 |
shimniok | 43:9a285515f33a | 34 | #define _GPS_KM_PER_METER 0.001 |
shimniok | 43:9a285515f33a | 35 | //#define _GPS_NO_STATS |
shimniok | 43:9a285515f33a | 36 | |
shimniok | 43:9a285515f33a | 37 | /** TinyGPS - a small GPS library for Arduino providing basic NMEA parsing Copyright (C) 2008-9 Mikal Hart |
shimniok | 43:9a285515f33a | 38 | * All rights reserved. Modified by Michael Shimniok |
shimniok | 43:9a285515f33a | 39 | */ |
shimniok | 43:9a285515f33a | 40 | |
shimniok | 43:9a285515f33a | 41 | class TinyGPS: public GPS { |
shimniok | 43:9a285515f33a | 42 | public: |
shimniok | 43:9a285515f33a | 43 | typedef uint8_t byte; |
shimniok | 43:9a285515f33a | 44 | |
shimniok | 43:9a285515f33a | 45 | /** Create a new GPS parsing object for parsing NMEA sentences |
shimniok | 43:9a285515f33a | 46 | */ |
shimniok | 43:9a285515f33a | 47 | TinyGPS(); |
shimniok | 43:9a285515f33a | 48 | |
shimniok | 43:9a285515f33a | 49 | /** Parse a single character received from GPS |
shimniok | 43:9a285515f33a | 50 | * |
shimniok | 43:9a285515f33a | 51 | * @param c is the character received from the GPS |
shimniok | 43:9a285515f33a | 52 | * @returns 1 when all sentences received |
shimniok | 43:9a285515f33a | 53 | */ |
shimniok | 43:9a285515f33a | 54 | virtual int parse(char c); |
shimniok | 43:9a285515f33a | 55 | |
shimniok | 43:9a285515f33a | 56 | /** Return the date and time from the parsed NMEA sentences |
shimniok | 43:9a285515f33a | 57 | * |
shimniok | 43:9a285515f33a | 58 | * @returns date as an integer value |
shimniok | 43:9a285515f33a | 59 | * @returns time as an integer value |
shimniok | 43:9a285515f33a | 60 | * @returns fix_age in milliseconds if available |
shimniok | 43:9a285515f33a | 61 | */ |
shimniok | 43:9a285515f33a | 62 | inline void get_datetime(unsigned long *date, unsigned long *time) |
shimniok | 43:9a285515f33a | 63 | { |
shimniok | 43:9a285515f33a | 64 | if (date) *date = _date; |
shimniok | 43:9a285515f33a | 65 | if (time) *time = _time; |
shimniok | 43:9a285515f33a | 66 | } |
shimniok | 43:9a285515f33a | 67 | |
shimniok | 43:9a285515f33a | 68 | /** signed altitude in centimeters (from GPGGA sentence) |
shimniok | 43:9a285515f33a | 69 | * @returns altitude in centimeters, integer |
shimniok | 43:9a285515f33a | 70 | */ |
shimniok | 43:9a285515f33a | 71 | inline long altitude() { return _altitude; } |
shimniok | 43:9a285515f33a | 72 | |
shimniok | 43:9a285515f33a | 73 | /** course in last full GPRMC sentence in 100th of a degree |
shimniok | 43:9a285515f33a | 74 | * @returns course as an integer, 100ths of a degree |
shimniok | 43:9a285515f33a | 75 | */ |
shimniok | 43:9a285515f33a | 76 | inline unsigned long course() { return _course; } |
shimniok | 43:9a285515f33a | 77 | |
shimniok | 43:9a285515f33a | 78 | /** speed in last full GPRMC sentence in 100ths of a knot |
shimniok | 43:9a285515f33a | 79 | * @returns speed in 100ths of a knot |
shimniok | 43:9a285515f33a | 80 | */ |
shimniok | 43:9a285515f33a | 81 | unsigned long speed() { return _speed; } |
shimniok | 43:9a285515f33a | 82 | |
shimniok | 43:9a285515f33a | 83 | /* horizontal dilution of position in last full GPGGA sentence in 100ths |
shimniok | 43:9a285515f33a | 84 | * @returns hdop in 100ths |
shimniok | 43:9a285515f33a | 85 | */ |
shimniok | 43:9a285515f33a | 86 | unsigned long hdop() { return _hdop; } |
shimniok | 43:9a285515f33a | 87 | |
shimniok | 43:9a285515f33a | 88 | /** number of satellites tracked in last full GPGGA sentence |
shimniok | 43:9a285515f33a | 89 | * @returns number of satellites tracked |
shimniok | 43:9a285515f33a | 90 | */ |
shimniok | 43:9a285515f33a | 91 | unsigned long sat_count() { return _sat_count; } |
shimniok | 43:9a285515f33a | 92 | |
shimniok | 43:9a285515f33a | 93 | #ifndef _GPS_NO_STATS |
shimniok | 43:9a285515f33a | 94 | void stats(unsigned long *chars, unsigned short *good_sentences, unsigned short *failed_cs); |
shimniok | 43:9a285515f33a | 95 | #endif |
shimniok | 43:9a285515f33a | 96 | |
shimniok | 43:9a285515f33a | 97 | /** Convert date and time of last parsed sentence to integers |
shimniok | 43:9a285515f33a | 98 | * |
shimniok | 43:9a285515f33a | 99 | * @returns year |
shimniok | 43:9a285515f33a | 100 | * @returns month |
shimniok | 43:9a285515f33a | 101 | * @returns day of month |
shimniok | 43:9a285515f33a | 102 | * @returns hour |
shimniok | 43:9a285515f33a | 103 | * @returns minute |
shimniok | 43:9a285515f33a | 104 | * @returns second |
shimniok | 43:9a285515f33a | 105 | * @returns hundreths |
shimniok | 43:9a285515f33a | 106 | * @returns fix_age in milliseconds if available |
shimniok | 43:9a285515f33a | 107 | */ |
shimniok | 43:9a285515f33a | 108 | inline void crack_datetime(int *year, byte *month, byte *day, |
shimniok | 43:9a285515f33a | 109 | byte *hour, byte *minute, byte *second, byte *hundredths = 0, unsigned long *fix_age = 0) |
shimniok | 43:9a285515f33a | 110 | { |
shimniok | 43:9a285515f33a | 111 | unsigned long date, time; |
shimniok | 43:9a285515f33a | 112 | get_datetime(&date, &time); |
shimniok | 43:9a285515f33a | 113 | if (year) |
shimniok | 43:9a285515f33a | 114 | { |
shimniok | 43:9a285515f33a | 115 | *year = date % 100; |
shimniok | 43:9a285515f33a | 116 | *year += *year > 80 ? 1900 : 2000; |
shimniok | 43:9a285515f33a | 117 | } |
shimniok | 43:9a285515f33a | 118 | if (month) *month = (date / 100) % 100; |
shimniok | 43:9a285515f33a | 119 | if (day) *day = date / 10000; |
shimniok | 43:9a285515f33a | 120 | if (hour) *hour = time / 1000000; |
shimniok | 43:9a285515f33a | 121 | if (minute) *minute = (time / 10000) % 100; |
shimniok | 43:9a285515f33a | 122 | if (second) *second = (time / 100) % 100; |
shimniok | 43:9a285515f33a | 123 | if (hundredths) *hundredths = time % 100; |
shimniok | 43:9a285515f33a | 124 | } |
shimniok | 43:9a285515f33a | 125 | |
shimniok | 43:9a285515f33a | 126 | /** returns altitude as a float |
shimniok | 43:9a285515f33a | 127 | */ |
shimniok | 43:9a285515f33a | 128 | inline double f_altitude() { return altitude() / 100.0; } |
shimniok | 43:9a285515f33a | 129 | |
shimniok | 43:9a285515f33a | 130 | /** returns course as a float |
shimniok | 43:9a285515f33a | 131 | */ |
shimniok | 43:9a285515f33a | 132 | inline double f_course() { return course() / 100.0; } |
shimniok | 43:9a285515f33a | 133 | |
shimniok | 43:9a285515f33a | 134 | /** returns speed in knots as a float |
shimniok | 43:9a285515f33a | 135 | */ |
shimniok | 43:9a285515f33a | 136 | inline double f_speed_knots() { return speed() / 100.0; } |
shimniok | 43:9a285515f33a | 137 | |
shimniok | 43:9a285515f33a | 138 | /** returns speed in mph as a float |
shimniok | 43:9a285515f33a | 139 | */ |
shimniok | 43:9a285515f33a | 140 | inline double f_speed_mph() { return _GPS_MPH_PER_KNOT * f_speed_knots(); } |
shimniok | 43:9a285515f33a | 141 | |
shimniok | 43:9a285515f33a | 142 | /** returns speed in meters per second as a float |
shimniok | 43:9a285515f33a | 143 | */ |
shimniok | 43:9a285515f33a | 144 | inline double f_speed_mps() { return _GPS_MPS_PER_KNOT * f_speed_knots(); } |
shimniok | 43:9a285515f33a | 145 | |
shimniok | 43:9a285515f33a | 146 | /** returns speed in km per hour as a float |
shimniok | 43:9a285515f33a | 147 | */ |
shimniok | 43:9a285515f33a | 148 | inline double f_speed_kmph() { return _GPS_KMPH_PER_KNOT * f_speed_knots(); } |
shimniok | 43:9a285515f33a | 149 | |
shimniok | 43:9a285515f33a | 150 | /** returns hdop as a float |
shimniok | 43:9a285515f33a | 151 | */ |
shimniok | 43:9a285515f33a | 152 | inline double f_hdop() { return hdop() / 100.0; } |
shimniok | 43:9a285515f33a | 153 | |
shimniok | 43:9a285515f33a | 154 | /** @returns library version |
shimniok | 43:9a285515f33a | 155 | */ |
shimniok | 43:9a285515f33a | 156 | static int library_version() { return _GPS_VERSION; } |
shimniok | 43:9a285515f33a | 157 | |
shimniok | 43:9a285515f33a | 158 | /** determine if all sentences parsed |
shimniok | 43:9a285515f33a | 159 | * |
shimniok | 43:9a285515f33a | 160 | */ |
shimniok | 43:9a285515f33a | 161 | inline bool ready() { return (_rmc_ready && _gga_ready); } |
shimniok | 43:9a285515f33a | 162 | |
shimniok | 43:9a285515f33a | 163 | /** determine if GSV sentence parsed since last reset_ready() |
shimniok | 43:9a285515f33a | 164 | */ |
shimniok | 43:9a285515f33a | 165 | inline bool gsv_ready() { return _gsv_ready; } |
shimniok | 43:9a285515f33a | 166 | |
shimniok | 43:9a285515f33a | 167 | inline bool gga_ready() { return _gga_ready; } |
shimniok | 43:9a285515f33a | 168 | |
shimniok | 43:9a285515f33a | 169 | inline bool rmc_ready() { return _rmc_ready; } |
shimniok | 43:9a285515f33a | 170 | |
shimniok | 43:9a285515f33a | 171 | /** Reset the ready flags for all the parsed sentences |
shimniok | 43:9a285515f33a | 172 | */ |
shimniok | 43:9a285515f33a | 173 | inline void reset_ready() { _gsv_ready = _rmc_ready = _gga_ready = false; } |
shimniok | 43:9a285515f33a | 174 | |
shimniok | 43:9a285515f33a | 175 | enum {GPS_INVALID_AGE = 0xFFFFFFFF, GPS_INVALID_ANGLE = 999999999, GPS_INVALID_ALTITUDE = 999999999, GPS_INVALID_DATE = 0, |
shimniok | 43:9a285515f33a | 176 | GPS_INVALID_TIME = 0xFFFFFFFF, GPS_INVALID_SPEED = 999999999, GPS_INVALID_FIX_TIME = 0xFFFFFFFF}; |
shimniok | 43:9a285515f33a | 177 | |
shimniok | 43:9a285515f33a | 178 | private: |
shimniok | 43:9a285515f33a | 179 | enum {_GPS_SENTENCE_GPGGA, _GPS_SENTENCE_GPRMC, _GPS_SENTENCE_GPGSV, _GPS_SENTENCE_OTHER}; |
shimniok | 43:9a285515f33a | 180 | |
shimniok | 43:9a285515f33a | 181 | // properties |
shimniok | 43:9a285515f33a | 182 | unsigned long _time, _new_time; |
shimniok | 43:9a285515f33a | 183 | unsigned long _date, _new_date; |
shimniok | 43:9a285515f33a | 184 | long _latitude, _new_latitude; |
shimniok | 43:9a285515f33a | 185 | long _longitude, _new_longitude; |
shimniok | 43:9a285515f33a | 186 | long _altitude, _new_altitude; |
shimniok | 43:9a285515f33a | 187 | unsigned long _speed, _new_speed; |
shimniok | 43:9a285515f33a | 188 | unsigned long _course, _new_course; |
shimniok | 43:9a285515f33a | 189 | unsigned long _hdop, _new_hdop; |
shimniok | 43:9a285515f33a | 190 | unsigned int _sat_count, _new_sat_count; |
shimniok | 43:9a285515f33a | 191 | unsigned long _last_time_fix, _new_time_fix; |
shimniok | 43:9a285515f33a | 192 | unsigned long _last_position_fix, _new_position_fix; |
shimniok | 43:9a285515f33a | 193 | |
shimniok | 43:9a285515f33a | 194 | // parsing state variables |
shimniok | 43:9a285515f33a | 195 | byte _parity; |
shimniok | 43:9a285515f33a | 196 | bool _is_checksum_term; |
shimniok | 43:9a285515f33a | 197 | char _term[15]; |
shimniok | 43:9a285515f33a | 198 | byte _sentence_type; |
shimniok | 43:9a285515f33a | 199 | byte _term_number; |
shimniok | 43:9a285515f33a | 200 | uint8_t _term_offset; |
shimniok | 43:9a285515f33a | 201 | bool _gps_data_good; |
shimniok | 43:9a285515f33a | 202 | bool _rmc_ready; |
shimniok | 43:9a285515f33a | 203 | bool _gga_ready; |
shimniok | 43:9a285515f33a | 204 | bool _gsv_ready; |
shimniok | 43:9a285515f33a | 205 | |
shimniok | 43:9a285515f33a | 206 | #ifndef _GPS_NO_STATS |
shimniok | 43:9a285515f33a | 207 | // statistics |
shimniok | 43:9a285515f33a | 208 | unsigned long _encoded_characters; |
shimniok | 43:9a285515f33a | 209 | unsigned short _good_sentences; |
shimniok | 43:9a285515f33a | 210 | unsigned short _failed_checksum; |
shimniok | 43:9a285515f33a | 211 | unsigned short _passed_checksum; |
shimniok | 43:9a285515f33a | 212 | #endif |
shimniok | 43:9a285515f33a | 213 | |
shimniok | 43:9a285515f33a | 214 | // internal utilities |
shimniok | 43:9a285515f33a | 215 | int from_hex(char a); |
shimniok | 43:9a285515f33a | 216 | int parse_int(); |
shimniok | 43:9a285515f33a | 217 | long parse_decimal(); |
shimniok | 43:9a285515f33a | 218 | unsigned long parse_degrees(); |
shimniok | 43:9a285515f33a | 219 | bool term_complete(); |
shimniok | 43:9a285515f33a | 220 | bool gpsisdigit(char c) { return c >= '0' && c <= '9'; } |
shimniok | 43:9a285515f33a | 221 | long gpsatol(const char *str); |
shimniok | 43:9a285515f33a | 222 | int gpsstrcmp(const char *str1, const char *str2); |
shimniok | 43:9a285515f33a | 223 | }; |
shimniok | 43:9a285515f33a | 224 | |
shimniok | 43:9a285515f33a | 225 | // Arduino 0012 workaround |
shimniok | 43:9a285515f33a | 226 | #undef int |
shimniok | 43:9a285515f33a | 227 | #undef char |
shimniok | 43:9a285515f33a | 228 | #undef long |
shimniok | 43:9a285515f33a | 229 | #undef byte |
shimniok | 43:9a285515f33a | 230 | #undef double |
shimniok | 43:9a285515f33a | 231 | #undef abs |
shimniok | 43:9a285515f33a | 232 | #undef round |
shimniok | 43:9a285515f33a | 233 | |
shimniok | 43:9a285515f33a | 234 | #endif |