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

Revision:
6:204487243310
Parent:
5:8444ec4245e7
--- a/GPS/GPS.cpp	Thu Feb 23 05:40:54 2012 +0000
+++ b/GPS/GPS.cpp	Fri Feb 24 21:28:33 2012 +0000
@@ -30,6 +30,8 @@
     speed_k_unit = ' ';
     speed_km = 0.0; // speek km/hr
     speed_km_unit = ' ';
+
+    altitude_ft = 0.0;
 #ifdef OPEN_LOG
     is_logging = false;
 #endif
@@ -63,29 +65,30 @@
 int GPS::sample() {
     int line_parsed = 0;
 
-    while (_gps.readable()) {
+    if (_gps.readable()) {
         getline();
-
+    
 #ifdef OPEN_LOG
         if (is_logging && lock) {
-            _openLog.write(msg);
+            format_for_log();
+            _openLog.write(bfr);
         }
 #endif
 
         // 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) {
+        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,%c,%f,%c,%f,%c,%f,%f,%d", &utc_time, &rmc_status, &nmea_latitude, &ns, &nmea_longitude, &ew, &speed_k, &course_d, &date) >= 1) {
+        else if (sscanf(msg, "GPRMC,%f,%c,%f,%c,%f,%c,%f,%f,%d", &utc_time, &rmc_status, &nmea_latitude, &ns, &nmea_longitude, &ew, &speed_k, &course_d, &date) >= 1) {
             line_parsed = RMC;
         }
         // GLL - Geographic Position-Lat/Lon
-        else if (sscanf(msg, "$GPGLL,%f,%c,%f,%c,%f,%c", &nmea_latitude, &ns, &nmea_longitude, &ew, &utc_time, &gll_status) >= 1) {
+        else if (sscanf(msg, "GPGLL,%f,%c,%f,%c,%f,%c", &nmea_latitude, &ns, &nmea_longitude, &ew, &utc_time, &gll_status) >= 1) {
             line_parsed = GLL;
         }
         // VTG-Course Over Ground and Ground Speed
-        else if (sscanf(msg, "$GPVTG,%f,%c,%f,%c,%f,%c,%f,%c", &course_t, &course_t_unit, &course_m, &course_m_unit, &speed_k, &speed_k_unit, &speed_km, &speed_km_unit) >= 1) {
+        else if (sscanf(msg, "GPVTG,%f,%c,%f,%c,%f,%c,%f,%c", &course_t, &course_t_unit, &course_m, &course_m_unit, &speed_k, &speed_k_unit, &speed_km, &speed_km_unit) >= 1) {
             line_parsed = VTG;
         }
     }
@@ -110,15 +113,29 @@
 }
 
 void GPS::getline() {
-    for(int i=0; i<1022; i++) {
+    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+1] = '\n';
-            msg[i+2] = 0;
+        if (msg[i] == '\r') {
+            msg[i] = 0;
             return;
         }
     }
-    error("Overflowed message limit");
+    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) {
+            bfr[i+1] = '\r';
+            bfr[i+2] = '\n';
+            bfr[i+3] = 0;
+            return;
+        }
+    }
+    error("Overflow in format");
 }
 
 float GPS::get_msl_altitude() {
@@ -191,4 +208,11 @@
         return 0.0;
     else
         return speed_km;
+}
+
+float GPS::get_altitude_ft() {
+    if (!lock)
+        return 0.0;
+    else
+        return 3.280839895*msl_altitude;
 }
\ No newline at end of file