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:
Wed Dec 12 17:22:45 2012 +0000
Revision:
4:6e2d98b5cb86
Parent:
3:465354a08ff8
removed serial functions

Who changed what in which revision?

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