nmea gps library - without any serial

Dependents:   HARP2 HARP3 20180621_FT813

Fork of GPS_parser by Tyler Weaver

NMEA GPS Serial Output parser.

Routine taken from NMEA Software Standard (NMEA 0183) http://www.winsystems.com/software/nmea.pdf

Only handles GGA and RMC Messages

Committer:
tylerjw
Date:
Fri Dec 07 19:18:35 2012 +0000
Revision:
3:465354a08ff8
Parent:
2:682663c5b1ee
Child:
4:6e2d98b5cb86
0.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerjw 0:3611af72bfd7 1 #include "GPS.h"
tylerjw 0:3611af72bfd7 2
tylerjw 0:3611af72bfd7 3 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
tylerjw 0:3611af72bfd7 4 _gps.baud(4800);
tylerjw 0:3611af72bfd7 5 nmea_longitude = 0.0;
tylerjw 0:3611af72bfd7 6 nmea_latitude = 0.0;
tylerjw 0:3611af72bfd7 7 utc_time = 0;
tylerjw 0:3611af72bfd7 8 ns = ' ';
tylerjw 0:3611af72bfd7 9 ew = ' ';
tylerjw 0:3611af72bfd7 10 lock = 0;
tylerjw 0:3611af72bfd7 11 satelites = 0;
tylerjw 0:3611af72bfd7 12 hdop = 0.0;
tylerjw 0:3611af72bfd7 13 msl_altitude = 0.0;
tylerjw 0:3611af72bfd7 14 msl_units = ' ';
tylerjw 0:3611af72bfd7 15
tylerjw 0:3611af72bfd7 16 rmc_status = ' ';
tylerjw 0:3611af72bfd7 17 speed_k = 0.0;
tylerjw 0:3611af72bfd7 18 course_d = 0.0;
tylerjw 0:3611af72bfd7 19 date = 0;
tylerjw 0:3611af72bfd7 20
tylerjw 0:3611af72bfd7 21 dec_longitude = 0.0;
tylerjw 0:3611af72bfd7 22 dec_latitude = 0.0;
tylerjw 0:3611af72bfd7 23
tylerjw 0:3611af72bfd7 24 gll_status = ' ';
tylerjw 0:3611af72bfd7 25
tylerjw 0:3611af72bfd7 26 course_t = 0.0; // ground speed true
tylerjw 0:3611af72bfd7 27 course_t_unit = ' ';
tylerjw 0:3611af72bfd7 28 course_m = 0.0; // magnetic
tylerjw 0:3611af72bfd7 29 course_m_unit = ' ';
tylerjw 0:3611af72bfd7 30 speed_k_unit = ' ';
tylerjw 0:3611af72bfd7 31 speed_km = 0.0; // speek km/hr
tylerjw 0:3611af72bfd7 32 speed_km_unit = ' ';
tylerjw 0:3611af72bfd7 33
tylerjw 0:3611af72bfd7 34 altitude_ft = 0.0;
tylerjw 0:3611af72bfd7 35 }
tylerjw 0:3611af72bfd7 36
tylerjw 0:3611af72bfd7 37 float GPS::nmea_to_dec(float deg_coord, char nsew) {
tylerjw 0:3611af72bfd7 38 int degree = (int)(deg_coord/100);
tylerjw 0:3611af72bfd7 39 float minutes = deg_coord - degree*100;
tylerjw 0:3611af72bfd7 40 float dec_deg = minutes / 60;
tylerjw 0:3611af72bfd7 41 float decimal = degree + dec_deg;
tylerjw 0:3611af72bfd7 42 if (nsew == 'S' || nsew == 'W') { // return negative
tylerjw 0:3611af72bfd7 43 decimal *= -1;
tylerjw 0:3611af72bfd7 44 }
tylerjw 0:3611af72bfd7 45 return decimal;
tylerjw 0:3611af72bfd7 46 }
tylerjw 0:3611af72bfd7 47
tylerjw 0:3611af72bfd7 48 int GPS::sample() {
tylerjw 0:3611af72bfd7 49 int line_parsed = 0;
tylerjw 0:3611af72bfd7 50
tylerjw 0:3611af72bfd7 51 if (_gps.readable()) {
tylerjw 0:3611af72bfd7 52 getline();
tylerjw 0:3611af72bfd7 53
tylerjw 0:3611af72bfd7 54 // Check if it is a GPGGA msg (matches both locked and non-locked msg)
tylerjw 0:3611af72bfd7 55 if (sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c", &utc_time, &nmea_latitude, &ns, &nmea_longitude, &ew, &lock, &satelites, &hdop, &msl_altitude, &msl_units) >= 1) {
tylerjw 0:3611af72bfd7 56 line_parsed = GGA;
tylerjw 0:3611af72bfd7 57 }
tylerjw 0:3611af72bfd7 58 // Check if it is a GPRMC msg
tylerjw 2:682663c5b1ee 59 else if (sscanf(msg, "GPRMC,%f,%f,%c,%f,%c,%f,%f,%d", &utc_time, &nmea_latitude, &ns, &nmea_longitude, &ew, &speed_k, &course_d, &date) >= 1) {
tylerjw 0:3611af72bfd7 60 line_parsed = RMC;
tylerjw 0:3611af72bfd7 61 }
tylerjw 0:3611af72bfd7 62
tylerjw 0:3611af72bfd7 63 if(satelites == 0) {
tylerjw 0:3611af72bfd7 64 lock = 0;
tylerjw 0:3611af72bfd7 65 }
tylerjw 0:3611af72bfd7 66 }
tylerjw 0:3611af72bfd7 67 if (!lock) {
tylerjw 0:3611af72bfd7 68 return NO_LOCK;
tylerjw 0:3611af72bfd7 69 } else if (line_parsed) {
tylerjw 0:3611af72bfd7 70 return line_parsed;
tylerjw 0:3611af72bfd7 71 } else {
tylerjw 0:3611af72bfd7 72 return NOT_PARSED;
tylerjw 0:3611af72bfd7 73 }
tylerjw 0:3611af72bfd7 74 }
tylerjw 0:3611af72bfd7 75
tylerjw 0:3611af72bfd7 76
tylerjw 0:3611af72bfd7 77 // INTERNAL FUNCTINS ////////////////////////////////////////////////////////////
tylerjw 0:3611af72bfd7 78 float GPS::trunc(float v) {
tylerjw 0:3611af72bfd7 79 if (v < 0.0) {
tylerjw 0:3611af72bfd7 80 v*= -1.0;
tylerjw 0:3611af72bfd7 81 v = floor(v);
tylerjw 0:3611af72bfd7 82 v*=-1.0;
tylerjw 0:3611af72bfd7 83 } else {
tylerjw 0:3611af72bfd7 84 v = floor(v);
tylerjw 0:3611af72bfd7 85 }
tylerjw 0:3611af72bfd7 86 return v;
tylerjw 0:3611af72bfd7 87 }
tylerjw 0:3611af72bfd7 88
tylerjw 0:3611af72bfd7 89 void GPS::getline() {
tylerjw 0:3611af72bfd7 90 while (_gps.getc() != '$'); // wait for the start of a line
tylerjw 0:3611af72bfd7 91 for (int i=0; i<1022; i++) {
tylerjw 0:3611af72bfd7 92 msg[i] = _gps.getc();
tylerjw 0:3611af72bfd7 93 if (msg[i] == '\r') {
tylerjw 0:3611af72bfd7 94 msg[i] = 0;
tylerjw 0:3611af72bfd7 95 return;
tylerjw 0:3611af72bfd7 96 }
tylerjw 0:3611af72bfd7 97 }
tylerjw 0:3611af72bfd7 98 error("Overflow in getline");
tylerjw 0:3611af72bfd7 99 }
tylerjw 0:3611af72bfd7 100
tylerjw 0:3611af72bfd7 101 void GPS::format_for_log() {
tylerjw 0:3611af72bfd7 102 bfr[0] = '$';
tylerjw 0:3611af72bfd7 103 for (int i = 0; i < 1022; i++) {
tylerjw 0:3611af72bfd7 104 bfr[i+1] = msg[i];
tylerjw 0:3611af72bfd7 105 if (msg[i] == 0 || msg[i] =='$') {
tylerjw 0:3611af72bfd7 106 bfr[i+1] = '\r';
tylerjw 0:3611af72bfd7 107 bfr[i+2] = '\n';
tylerjw 0:3611af72bfd7 108 bfr[i+3] = 0;
tylerjw 0:3611af72bfd7 109 return;
tylerjw 0:3611af72bfd7 110 }
tylerjw 0:3611af72bfd7 111 }
tylerjw 0:3611af72bfd7 112 error("Overflow in format");
tylerjw 0:3611af72bfd7 113 }
tylerjw 0:3611af72bfd7 114
tylerjw 0:3611af72bfd7 115 // GET FUNCTIONS /////////////////////////////////////////////////////////////////
tylerjw 0:3611af72bfd7 116 float GPS::get_msl_altitude() {
tylerjw 0:3611af72bfd7 117 if (!lock)
tylerjw 0:3611af72bfd7 118 return 0.0;
tylerjw 0:3611af72bfd7 119 else
tylerjw 0:3611af72bfd7 120 return msl_altitude;
tylerjw 0:3611af72bfd7 121 }
tylerjw 0:3611af72bfd7 122
tylerjw 0:3611af72bfd7 123 int GPS::get_satelites() {
tylerjw 0:3611af72bfd7 124 if (!lock)
tylerjw 0:3611af72bfd7 125 return 0;
tylerjw 0:3611af72bfd7 126 else
tylerjw 0:3611af72bfd7 127 return satelites;
tylerjw 0:3611af72bfd7 128 }
tylerjw 0:3611af72bfd7 129
tylerjw 0:3611af72bfd7 130 float GPS::get_nmea_longitude() {
tylerjw 0:3611af72bfd7 131 if (!lock)
tylerjw 0:3611af72bfd7 132 return 0.0;
tylerjw 0:3611af72bfd7 133 else
tylerjw 0:3611af72bfd7 134 return nmea_longitude;
tylerjw 0:3611af72bfd7 135 }
tylerjw 0:3611af72bfd7 136
tylerjw 0:3611af72bfd7 137 float GPS::get_dec_longitude() {
tylerjw 0:3611af72bfd7 138 dec_longitude = nmea_to_dec(nmea_longitude, ew);
tylerjw 0:3611af72bfd7 139 if (!lock)
tylerjw 0:3611af72bfd7 140 return 0.0;
tylerjw 0:3611af72bfd7 141 else
tylerjw 0:3611af72bfd7 142 return dec_longitude;
tylerjw 0:3611af72bfd7 143 }
tylerjw 0:3611af72bfd7 144
tylerjw 0:3611af72bfd7 145 float GPS::get_nmea_latitude() {
tylerjw 0:3611af72bfd7 146 if (!lock)
tylerjw 0:3611af72bfd7 147 return 0.0;
tylerjw 0:3611af72bfd7 148 else
tylerjw 0:3611af72bfd7 149 return nmea_latitude;
tylerjw 0:3611af72bfd7 150 }
tylerjw 0:3611af72bfd7 151
tylerjw 0:3611af72bfd7 152 float GPS::get_dec_latitude() {
tylerjw 0:3611af72bfd7 153 dec_latitude = nmea_to_dec(nmea_latitude, ns);
tylerjw 0:3611af72bfd7 154 if (!lock)
tylerjw 0:3611af72bfd7 155 return 0.0;
tylerjw 0:3611af72bfd7 156 else
tylerjw 0:3611af72bfd7 157 return dec_latitude;
tylerjw 0:3611af72bfd7 158 }
tylerjw 0:3611af72bfd7 159
tylerjw 0:3611af72bfd7 160 float GPS::get_course_t() {
tylerjw 0:3611af72bfd7 161 if (!lock)
tylerjw 0:3611af72bfd7 162 return 0.0;
tylerjw 0:3611af72bfd7 163 else
tylerjw 0:3611af72bfd7 164 return course_t;
tylerjw 0:3611af72bfd7 165 }
tylerjw 0:3611af72bfd7 166
tylerjw 0:3611af72bfd7 167 float GPS::get_course_m() {
tylerjw 0:3611af72bfd7 168 if (!lock)
tylerjw 0:3611af72bfd7 169 return 0.0;
tylerjw 0:3611af72bfd7 170 else
tylerjw 0:3611af72bfd7 171 return course_m;
tylerjw 0:3611af72bfd7 172 }
tylerjw 0:3611af72bfd7 173
tylerjw 0:3611af72bfd7 174 float GPS::get_speed_k() {
tylerjw 0:3611af72bfd7 175 if (!lock)
tylerjw 0:3611af72bfd7 176 return 0.0;
tylerjw 0:3611af72bfd7 177 else
tylerjw 0:3611af72bfd7 178 return speed_k;
tylerjw 0:3611af72bfd7 179 }
tylerjw 0:3611af72bfd7 180
tylerjw 0:3611af72bfd7 181 float GPS::get_speed_km() {
tylerjw 0:3611af72bfd7 182 if (!lock)
tylerjw 0:3611af72bfd7 183 return 0.0;
tylerjw 0:3611af72bfd7 184 else
tylerjw 0:3611af72bfd7 185 return speed_km;
tylerjw 0:3611af72bfd7 186 }
tylerjw 0:3611af72bfd7 187
tylerjw 0:3611af72bfd7 188 float GPS::get_altitude_ft() {
tylerjw 0:3611af72bfd7 189 if (!lock)
tylerjw 0:3611af72bfd7 190 return 0.0;
tylerjw 0:3611af72bfd7 191 else
tylerjw 0:3611af72bfd7 192 return 3.280839895*msl_altitude;
tylerjw 0:3611af72bfd7 193 }
tylerjw 0:3611af72bfd7 194
tylerjw 0:3611af72bfd7 195 // NAVIGATION FUNCTIONS ////////////////////////////////////////////////////////////
tylerjw 0:3611af72bfd7 196 float GPS::calc_course_to(float pointLat, float pontLong) {
tylerjw 0:3611af72bfd7 197 const double d2r = PI / 180.0;
tylerjw 0:3611af72bfd7 198 const double r2d = 180.0 / PI;
tylerjw 0:3611af72bfd7 199 double dlat = abs(pointLat - get_dec_latitude()) * d2r;
tylerjw 0:3611af72bfd7 200 double dlong = abs(pontLong - get_dec_longitude()) * d2r;
tylerjw 0:3611af72bfd7 201 double y = sin(dlong) * cos(pointLat * d2r);
tylerjw 0:3611af72bfd7 202 double x = cos(get_dec_latitude()*d2r)*sin(pointLat*d2r) - sin(get_dec_latitude()*d2r)*cos(pointLat*d2r)*cos(dlong);
tylerjw 2:682663c5b1ee 203 return 360.0-(atan2(y,x)*r2d);
tylerjw 0:3611af72bfd7 204 }
tylerjw 0:3611af72bfd7 205
tylerjw 0:3611af72bfd7 206 /*
tylerjw 0:3611af72bfd7 207 var y = Math.sin(dLon) * Math.cos(lat2);
tylerjw 0:3611af72bfd7 208 var x = Math.cos(lat1)*Math.sin(lat2) -
tylerjw 0:3611af72bfd7 209 Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
tylerjw 0:3611af72bfd7 210 var brng = Math.atan2(y, x).toDeg();
tylerjw 0:3611af72bfd7 211 */
tylerjw 0:3611af72bfd7 212
tylerjw 0:3611af72bfd7 213 /*
tylerjw 0:3611af72bfd7 214 The Haversine formula according to Dr. Math.
tylerjw 0:3611af72bfd7 215 http://mathforum.org/library/drmath/view/51879.html
tylerjw 0:3611af72bfd7 216
tylerjw 0:3611af72bfd7 217 dlon = lon2 - lon1
tylerjw 0:3611af72bfd7 218 dlat = lat2 - lat1
tylerjw 0:3611af72bfd7 219 a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
tylerjw 0:3611af72bfd7 220 c = 2 * atan2(sqrt(a), sqrt(1-a))
tylerjw 0:3611af72bfd7 221 d = R * c
tylerjw 0:3611af72bfd7 222
tylerjw 0:3611af72bfd7 223 Where
tylerjw 0:3611af72bfd7 224 * dlon is the change in longitude
tylerjw 0:3611af72bfd7 225 * dlat is the change in latitude
tylerjw 0:3611af72bfd7 226 * c is the great circle distance in Radians.
tylerjw 0:3611af72bfd7 227 * R is the radius of a spherical Earth.
tylerjw 0:3611af72bfd7 228 * The locations of the two points in
tylerjw 0:3611af72bfd7 229 spherical coordinates (longitude and
tylerjw 0:3611af72bfd7 230 latitude) are lon1,lat1 and lon2, lat2.
tylerjw 0:3611af72bfd7 231 */
tylerjw 0:3611af72bfd7 232 double GPS::calc_dist_to_mi(float pointLat, float pontLong) {
tylerjw 0:3611af72bfd7 233 const double d2r = PI / 180.0;
tylerjw 0:3611af72bfd7 234 double dlat = pointLat - get_dec_latitude();
tylerjw 0:3611af72bfd7 235 double dlong = pontLong - get_dec_longitude();
tylerjw 0:3611af72bfd7 236 double a = pow(sin(dlat/2.0),2.0) + cos(get_dec_latitude()*d2r) * cos(pointLat*d2r) * pow(sin(dlong/2.0),2.0);
tylerjw 0:3611af72bfd7 237 double c = 2.0 * asin(sqrt(abs(a)));
tylerjw 0:3611af72bfd7 238 double d = 63.765 * c;
tylerjw 0:3611af72bfd7 239
tylerjw 0:3611af72bfd7 240 return d;
tylerjw 0:3611af72bfd7 241 }
tylerjw 0:3611af72bfd7 242
tylerjw 0:3611af72bfd7 243 double GPS::calc_dist_to_ft(float pointLat, float pontLong) {
tylerjw 0:3611af72bfd7 244 return calc_dist_to_mi(pointLat, pontLong)*5280.0;
tylerjw 0:3611af72bfd7 245 }
tylerjw 0:3611af72bfd7 246
tylerjw 0:3611af72bfd7 247 double GPS::calc_dist_to_km(float pointLat, float pontLong) {
tylerjw 0:3611af72bfd7 248 return calc_dist_to_mi(pointLat, pontLong)*1.609344;
tylerjw 0:3611af72bfd7 249 }
tylerjw 0:3611af72bfd7 250
tylerjw 0:3611af72bfd7 251 double GPS::calc_dist_to_m(float pointLat, float pontLong) {
tylerjw 0:3611af72bfd7 252 return calc_dist_to_mi(pointLat, pontLong)*1609.344;
tylerjw 0:3611af72bfd7 253 }
tylerjw 0:3611af72bfd7 254
tylerjw 0:3611af72bfd7 255