Dependents:   Lab3_Surveillance 6adcSerial

Files at this revision

API Documentation at this revision

Comitter:
keerthanasp
Date:
Fri Jan 21 13:26:20 2011 +0000
Child:
1:b4de63a99f18
Commit message:

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPS.cpp	Fri Jan 21 13:26:20 2011 +0000
@@ -0,0 +1,54 @@
+#include "GPS.h"
+GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
+    _gp.baud(4800);
+    longitude = 0.0;
+    latitude = 0.0;
+}
+int GPS::sample() {
+    float time;
+    char ns,ew;
+    int lock;
+    
+    while(1){
+        getline();
+        if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d",&time, &latitude, &ns, &longitude, &ew, &lock) >=1) {
+            if(!lock) {
+                longitude = 0.0;
+                latitude = 0.0;
+                return 0;
+            } else {
+                if(ns =='s') {  latitude   *= -1.0; }
+                if(ew =='w') {  longitude  *= -1.0; }
+                float degrees = trunc(latitude / 100.0f);
+                float minutes = latitude - (degrees * 100.0f);
+                latitude = degrees + minutes / 60.0f;
+                degrees = trunc(longitude / 100.0f * 0.01f);
+                minutes = longitude - (degrees * 100.0f);
+                longitude = degrees + minute /60.0f;
+                return 1;
+            }
+         }
+    }
+}
+float GPS::trunc(float v) {
+    if(v<0.0) {
+        v*= -1.0;
+        v = floor(v);
+        v*=-1.0;
+    } else {
+        v = floor(v);
+    }
+    return;
+}
+void GPS::getline() {
+    while(_gps.get() != '$');
+    for(int i=0; i<256; i++) {
+        msg[i] = _gps.getc();
+        if(msg[i] == '\r') {
+            msg[i] = 0;
+            return;
+        }
+    }
+    error("overflowed message limit");
+}
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPS.h	Fri Jan 21 13:26:20 2011 +0000
@@ -0,0 +1,16 @@
+#include "mbed.h"
+#ifndef MBED_GPS_H
+#define MBED_GPS_H
+class GPS {
+public:
+    GPS(PinName tx, PinName rx);
+    int sample();
+    float longitude;
+    float latitude;
+private:
+    float trunc ( float v);
+    void getline();
+    serial_gps;
+    char msg[256];
+};
+#endif