nmea gps library - without any serial
Fork of GPS by
Revision 4:6e2d98b5cb86, committed 2012-12-12
- Comitter:
- tylerjw
- Date:
- Wed Dec 12 17:22:45 2012 +0000
- Parent:
- 3:465354a08ff8
- Child:
- 5:94daced1e61a
- Commit message:
- removed serial functions
Changed in this revision
GPS.cpp | Show annotated file Show diff for this revision Revisions of this file |
GPS.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/GPS.cpp Fri Dec 07 19:18:35 2012 +0000 +++ b/GPS.cpp Wed Dec 12 17:22:45 2012 +0000 @@ -1,7 +1,7 @@ #include "GPS.h" -GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) { - _gps.baud(4800); +GPS::GPS() +{ nmea_longitude = 0.0; nmea_latitude = 0.0; utc_time = 0; @@ -34,7 +34,8 @@ altitude_ft = 0.0; } -float GPS::nmea_to_dec(float deg_coord, char nsew) { +float GPS::nmea_to_dec(float deg_coord, char nsew) +{ int degree = (int)(deg_coord/100); float minutes = deg_coord - degree*100; float dec_deg = minutes / 60; @@ -45,24 +46,21 @@ return decimal; } -int GPS::sample() { +int GPS::sample(char *msg) +{ int line_parsed = 0; - if (_gps.readable()) { - getline(); + // Check if it is a GPGGA msg (matches both locked and non-locked msg) + 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) { + line_parsed = GGA; + } + // Check if it is a GPRMC msg + 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) { + line_parsed = RMC; + } - // Check if it is a GPGGA msg (matches both locked and non-locked msg) - 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) { - line_parsed = GGA; - } - // Check if it is a GPRMC msg - 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) { - line_parsed = RMC; - } - - if(satelites == 0) { - lock = 0; - } + if(satelites == 0) { + lock = 0; } if (!lock) { return NO_LOCK; @@ -75,7 +73,8 @@ // INTERNAL FUNCTINS //////////////////////////////////////////////////////////// -float GPS::trunc(float v) { +float GPS::trunc(float v) +{ if (v < 0.0) { v*= -1.0; v = floor(v); @@ -86,55 +85,33 @@ return v; } -void GPS::getline() { - while (_gps.getc() != '$'); // wait for the start of a line - for (int i=0; i<1022; i++) { - msg[i] = _gps.getc(); - if (msg[i] == '\r') { - msg[i] = 0; - return; - } - } - error("Overflow in getline"); -} - -void GPS::format_for_log() { - bfr[0] = '$'; - for (int i = 0; i < 1022; i++) { - bfr[i+1] = msg[i]; - if (msg[i] == 0 || msg[i] =='$') { - bfr[i+1] = '\r'; - bfr[i+2] = '\n'; - bfr[i+3] = 0; - return; - } - } - error("Overflow in format"); -} - // GET FUNCTIONS ///////////////////////////////////////////////////////////////// -float GPS::get_msl_altitude() { +float GPS::get_msl_altitude() +{ if (!lock) return 0.0; else return msl_altitude; } -int GPS::get_satelites() { +int GPS::get_satelites() +{ if (!lock) return 0; else return satelites; } -float GPS::get_nmea_longitude() { +float GPS::get_nmea_longitude() +{ if (!lock) return 0.0; else return nmea_longitude; } -float GPS::get_dec_longitude() { +float GPS::get_dec_longitude() +{ dec_longitude = nmea_to_dec(nmea_longitude, ew); if (!lock) return 0.0; @@ -142,14 +119,16 @@ return dec_longitude; } -float GPS::get_nmea_latitude() { +float GPS::get_nmea_latitude() +{ if (!lock) return 0.0; else return nmea_latitude; } -float GPS::get_dec_latitude() { +float GPS::get_dec_latitude() +{ dec_latitude = nmea_to_dec(nmea_latitude, ns); if (!lock) return 0.0; @@ -157,35 +136,40 @@ return dec_latitude; } -float GPS::get_course_t() { +float GPS::get_course_t() +{ if (!lock) return 0.0; else return course_t; } -float GPS::get_course_m() { +float GPS::get_course_m() +{ if (!lock) return 0.0; else return course_m; } -float GPS::get_speed_k() { +float GPS::get_speed_k() +{ if (!lock) return 0.0; else return speed_k; } -float GPS::get_speed_km() { +float GPS::get_speed_km() +{ if (!lock) return 0.0; else return speed_km; } -float GPS::get_altitude_ft() { +float GPS::get_altitude_ft() +{ if (!lock) return 0.0; else @@ -193,7 +177,8 @@ } // NAVIGATION FUNCTIONS //////////////////////////////////////////////////////////// -float GPS::calc_course_to(float pointLat, float pontLong) { +float GPS::calc_course_to(float pointLat, float pontLong) +{ const double d2r = PI / 180.0; const double r2d = 180.0 / PI; double dlat = abs(pointLat - get_dec_latitude()) * d2r; @@ -201,7 +186,7 @@ double y = sin(dlong) * cos(pointLat * d2r); double x = cos(get_dec_latitude()*d2r)*sin(pointLat*d2r) - sin(get_dec_latitude()*d2r)*cos(pointLat*d2r)*cos(dlong); return 360.0-(atan2(y,x)*r2d); -} +} /* var y = Math.sin(dLon) * Math.cos(lat2); @@ -213,43 +198,46 @@ /* The Haversine formula according to Dr. Math. http://mathforum.org/library/drmath/view/51879.html - + dlon = lon2 - lon1 dlat = lat2 - lat1 a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 - c = 2 * atan2(sqrt(a), sqrt(1-a)) + c = 2 * atan2(sqrt(a), sqrt(1-a)) d = R * c - + Where * dlon is the change in longitude * dlat is the change in latitude * c is the great circle distance in Radians. * R is the radius of a spherical Earth. - * The locations of the two points in - spherical coordinates (longitude and + * The locations of the two points in + spherical coordinates (longitude and latitude) are lon1,lat1 and lon2, lat2. */ -double GPS::calc_dist_to_mi(float pointLat, float pontLong) { +double GPS::calc_dist_to_mi(float pointLat, float pontLong) +{ const double d2r = PI / 180.0; double dlat = pointLat - get_dec_latitude(); double dlong = pontLong - get_dec_longitude(); double a = pow(sin(dlat/2.0),2.0) + cos(get_dec_latitude()*d2r) * cos(pointLat*d2r) * pow(sin(dlong/2.0),2.0); double c = 2.0 * asin(sqrt(abs(a))); double d = 63.765 * c; - + return d; } -double GPS::calc_dist_to_ft(float pointLat, float pontLong) { +double GPS::calc_dist_to_ft(float pointLat, float pontLong) +{ return calc_dist_to_mi(pointLat, pontLong)*5280.0; } -double GPS::calc_dist_to_km(float pointLat, float pontLong) { +double GPS::calc_dist_to_km(float pointLat, float pontLong) +{ return calc_dist_to_mi(pointLat, pontLong)*1.609344; } -double GPS::calc_dist_to_m(float pointLat, float pontLong) { +double GPS::calc_dist_to_m(float pointLat, float pontLong) +{ return calc_dist_to_mi(pointLat, pontLong)*1609.344; } - \ No newline at end of file
--- a/GPS.h Fri Dec 07 19:18:35 2012 +0000 +++ b/GPS.h Wed Dec 12 17:22:45 2012 +0000 @@ -18,13 +18,13 @@ /** Create the GPS interface, connected to the specified serial port */ - GPS(PinName tx, PinName rx); + GPS(); /** Sample the incoming GPS data, returning whether there is a lock * * @return 1 if there was a lock when the sample was taken (and therefore .longitude and .latitude are valid), else 0 */ - int sample(); + int sample(char *); float get_nmea_longitude(); float get_nmea_latitude(); float get_dec_longitude(); @@ -47,13 +47,7 @@ private: float nmea_to_dec(float, char); float trunc(float v); - void getline(); - void format_for_log(void); - Serial _gps; - char msg[1024]; - char bfr[1030]; - bool is_logging; // calculated values float dec_longitude; float dec_latitude;