Generation 3 of the Harp project

Dependencies:   Servo TMP36 GZ buffered-serial1 chan_fatfs_sd nmea_parser watchdog mbed-rtos mbed

Fork of HARP2 by Tyler Weaver

Committer:
tylerjw
Date:
Wed Feb 22 04:15:52 2012 +0000
Revision:
1:2ace7946a246
Parent:
0:ce5f06c3895f
Child:
2:0c9ade531a5b
RMC messages and non-waiting sample function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerjw 0:ce5f06c3895f 1 #include "GPS.h"
tylerjw 0:ce5f06c3895f 2
tylerjw 0:ce5f06c3895f 3 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
tylerjw 1:2ace7946a246 4 _gps.baud(4800);
tylerjw 0:ce5f06c3895f 5 nmea_longitude = 0.0;
tylerjw 0:ce5f06c3895f 6 nmea_latitude = 0.0;
tylerjw 0:ce5f06c3895f 7 utc_time = 0;
tylerjw 0:ce5f06c3895f 8 ns = ' ';
tylerjw 0:ce5f06c3895f 9 ew = ' ';
tylerjw 0:ce5f06c3895f 10 lock = 0;
tylerjw 0:ce5f06c3895f 11 satelites = 0;
tylerjw 0:ce5f06c3895f 12 hdop = 0.0;
tylerjw 0:ce5f06c3895f 13 msl_altitude = 0.0;
tylerjw 0:ce5f06c3895f 14 msl_units = ' ';
tylerjw 1:2ace7946a246 15
tylerjw 0:ce5f06c3895f 16 rmc_status = ' ';
tylerjw 0:ce5f06c3895f 17 ground_speed_k = 0.0;
tylerjw 0:ce5f06c3895f 18 ground_course_d = 0.0;
tylerjw 1:2ace7946a246 19 date = 0;
tylerjw 1:2ace7946a246 20
tylerjw 0:ce5f06c3895f 21 dec_longitude = 0.0;
tylerjw 1:2ace7946a246 22 dec_latitude = 0.0;
tylerjw 0:ce5f06c3895f 23 }
tylerjw 0:ce5f06c3895f 24
tylerjw 0:ce5f06c3895f 25 float GPS::nmea_to_dec(float deg_coord, char nsew) {
tylerjw 0:ce5f06c3895f 26 int degree = (int)(deg_coord/100);
tylerjw 0:ce5f06c3895f 27 float minutes = deg_coord - degree*100;
tylerjw 0:ce5f06c3895f 28 float dec_deg = minutes / 60;
tylerjw 0:ce5f06c3895f 29 float decimal = degree + dec_deg;
tylerjw 1:2ace7946a246 30 if (nsew == 'S' || nsew == 'W') { // return negative
tylerjw 0:ce5f06c3895f 31 decimal *= -1;
tylerjw 0:ce5f06c3895f 32 }
tylerjw 0:ce5f06c3895f 33 return decimal;
tylerjw 0:ce5f06c3895f 34 }
tylerjw 0:ce5f06c3895f 35
tylerjw 0:ce5f06c3895f 36 int GPS::sample() {
tylerjw 1:2ace7946a246 37 int line_parsed = 0;
tylerjw 1:2ace7946a246 38
tylerjw 1:2ace7946a246 39 if (_gps.readable()) {
tylerjw 0:ce5f06c3895f 40 getline();
tylerjw 0:ce5f06c3895f 41
tylerjw 0:ce5f06c3895f 42 // Check if it is a GPGGA msg (matches both locked and non-locked msg)
tylerjw 1:2ace7946a246 43 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 1:2ace7946a246 44 line_parsed = 1;
tylerjw 1:2ace7946a246 45 }
tylerjw 1:2ace7946a246 46 // Check if it is a GPRMC msg
tylerjw 1:2ace7946a246 47 else if (sscanf(msg, "GPRMC,%f,%c,%f,%c,%f,%c,%f,%f,%d", &utc_time, &rmc_status, &nmea_latitude, &ns, &nmea_longitude, &ew, &ground_speed_k, &ground_course_d, &date) >= 1) {
tylerjw 1:2ace7946a246 48 line_parsed = 1;
tylerjw 0:ce5f06c3895f 49 }
tylerjw 1:2ace7946a246 50 if (!lock) {
tylerjw 1:2ace7946a246 51 return NO_LOCK;
tylerjw 1:2ace7946a246 52 } else if (line_parsed) {
tylerjw 1:2ace7946a246 53 return PARSED;
tylerjw 1:2ace7946a246 54 } else {
tylerjw 1:2ace7946a246 55 return NOT_PARSED;
tylerjw 0:ce5f06c3895f 56 }
tylerjw 1:2ace7946a246 57 } else {
tylerjw 1:2ace7946a246 58 return NO_MSG;
tylerjw 0:ce5f06c3895f 59 }
tylerjw 0:ce5f06c3895f 60 }
tylerjw 0:ce5f06c3895f 61
tylerjw 0:ce5f06c3895f 62 float GPS::trunc(float v) {
tylerjw 1:2ace7946a246 63 if (v < 0.0) {
tylerjw 0:ce5f06c3895f 64 v*= -1.0;
tylerjw 0:ce5f06c3895f 65 v = floor(v);
tylerjw 0:ce5f06c3895f 66 v*=-1.0;
tylerjw 0:ce5f06c3895f 67 } else {
tylerjw 0:ce5f06c3895f 68 v = floor(v);
tylerjw 0:ce5f06c3895f 69 }
tylerjw 0:ce5f06c3895f 70 return v;
tylerjw 0:ce5f06c3895f 71 }
tylerjw 0:ce5f06c3895f 72
tylerjw 0:ce5f06c3895f 73 void GPS::getline() {
tylerjw 1:2ace7946a246 74 while (_gps.getc() != '$'); // wait for the start of a line
tylerjw 1:2ace7946a246 75 for (int i=0; i<256; i++) {
tylerjw 0:ce5f06c3895f 76 msg[i] = _gps.getc();
tylerjw 1:2ace7946a246 77 if (msg[i] == '\r') {
tylerjw 0:ce5f06c3895f 78 msg[i] = 0;
tylerjw 0:ce5f06c3895f 79 return;
tylerjw 0:ce5f06c3895f 80 }
tylerjw 0:ce5f06c3895f 81 }
tylerjw 0:ce5f06c3895f 82 error("Overflowed message limit");
tylerjw 0:ce5f06c3895f 83 }
tylerjw 0:ce5f06c3895f 84
tylerjw 0:ce5f06c3895f 85 float GPS::get_msl_altitude() {
tylerjw 1:2ace7946a246 86 if (!lock)
tylerjw 0:ce5f06c3895f 87 return 0.0;
tylerjw 0:ce5f06c3895f 88 else
tylerjw 0:ce5f06c3895f 89 return msl_altitude;
tylerjw 0:ce5f06c3895f 90 }
tylerjw 0:ce5f06c3895f 91
tylerjw 0:ce5f06c3895f 92 int GPS::get_satelites() {
tylerjw 1:2ace7946a246 93 if (!lock)
tylerjw 0:ce5f06c3895f 94 return 0;
tylerjw 0:ce5f06c3895f 95 else
tylerjw 0:ce5f06c3895f 96 return satelites;
tylerjw 0:ce5f06c3895f 97 }
tylerjw 0:ce5f06c3895f 98
tylerjw 0:ce5f06c3895f 99 float GPS::get_nmea_longitude() {
tylerjw 1:2ace7946a246 100 if (!lock)
tylerjw 0:ce5f06c3895f 101 return 0.0;
tylerjw 0:ce5f06c3895f 102 else
tylerjw 0:ce5f06c3895f 103 return nmea_longitude;
tylerjw 0:ce5f06c3895f 104 }
tylerjw 0:ce5f06c3895f 105
tylerjw 0:ce5f06c3895f 106 float GPS::get_dec_longitude() {
tylerjw 1:2ace7946a246 107 dec_longitude = nmea_to_dec(nmea_longitude, ew);
tylerjw 1:2ace7946a246 108 if (!lock)
tylerjw 0:ce5f06c3895f 109 return 0.0;
tylerjw 0:ce5f06c3895f 110 else
tylerjw 0:ce5f06c3895f 111 return dec_longitude;
tylerjw 0:ce5f06c3895f 112 }
tylerjw 0:ce5f06c3895f 113
tylerjw 0:ce5f06c3895f 114 float GPS::get_nmea_latitude() {
tylerjw 1:2ace7946a246 115 if (!lock)
tylerjw 0:ce5f06c3895f 116 return 0.0;
tylerjw 0:ce5f06c3895f 117 else
tylerjw 0:ce5f06c3895f 118 return nmea_latitude;
tylerjw 0:ce5f06c3895f 119 }
tylerjw 0:ce5f06c3895f 120
tylerjw 0:ce5f06c3895f 121 float GPS::get_dec_latitude() {
tylerjw 1:2ace7946a246 122 dec_latitude = nmea_to_dec(nmea_latitude, ns);
tylerjw 1:2ace7946a246 123 if (!lock)
tylerjw 0:ce5f06c3895f 124 return 0.0;
tylerjw 0:ce5f06c3895f 125 else
tylerjw 0:ce5f06c3895f 126 return nmea_latitude;
tylerjw 0:ce5f06c3895f 127 }