GPS library.

Dependents:   B_test_gpsXbee

Files at this revision

API Documentation at this revision

Comitter:
odaa
Date:
Wed Oct 13 10:54:36 2021 +0000
Commit message:
2nd test

Changed in this revision

getGPS.cpp Show annotated file Show diff for this revision Revisions of this file
getGPS.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4c52e9f8484a getGPS.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/getGPS.cpp	Wed Oct 13 10:54:36 2021 +0000
@@ -0,0 +1,57 @@
+#include "mbed.h"
+#include "getGPS.h"
+
+GPS::GPS(PinName gpstx,PinName gpsrx): _gps(gpstx,gpsrx)
+{
+    latitude = 0;
+    longitude = 0;
+    _gps.baud(GPSBAUD);
+    _gps.printf("$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29");
+}
+
+bool GPS::getgps()
+{
+    char gps_data[256];
+    int i;
+    
+    do {
+        while(_gps.getc() != '$'); //$マークまで読み飛ばし
+        i = 0;
+
+        /* gpa_data初期化 */
+        for(int j = 0; j < 256; j++)
+            gps_data[j] = '\0';
+
+        /* NMEAから一行読み込み */
+        while((gps_data[i] = _gps.getc()) != '\r') {
+            i++;
+            if(i == 256) {
+                i = 255;
+                break;
+            }
+        }
+    } while(strstr(gps_data, "GPGGA") == NULL); //GGAセンテンスまで一行ずつ読み込み続ける
+    
+    int rlock;
+    char ns,ew;
+    double w_time, raw_longitude, raw_latitude;
+    int satnum;
+    double hdop;
+
+    if(sscanf(gps_data, "GPGGA, %lf, %lf, %c, %lf, %c, %d, %d, %lf", &w_time, &raw_latitude, &ns, &raw_longitude, &ew, &rlock, &satnum, &hdop) > 1) {
+        /* 座標1(度部分) */
+        int latitude_dd = (int)(raw_latitude / 100);
+        int longitude_dd = (int)(raw_longitude / 100);
+
+        /* 座標2(分部分 → 度) */
+        double latitude_md = (raw_latitude - latitude_dd * 100) / 60;
+        double longitude_md = (raw_longitude - longitude_dd * 100) / 60;
+
+        /* 座標1 + 2 */
+        latitude = latitude_dd + latitude_md;
+        longitude = longitude_dd + longitude_md;
+
+        return true;
+    } else
+        return false; //GGAセンテンスの情報が欠けている時
+}
diff -r 000000000000 -r 4c52e9f8484a getGPS.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/getGPS.h	Wed Oct 13 10:54:36 2021 +0000
@@ -0,0 +1,19 @@
+#ifndef GPS_H
+#define GPS_H
+
+#define GPSBAUD 9600 //ボーレート
+
+class GPS {
+public:
+    GPS(PinName gpstx,PinName gpsrx);
+    
+    bool getgps();
+
+    double longitude;
+    double latitude;
+    
+private:
+    Serial _gps;
+};
+ 
+#endif //GPS_H
\ No newline at end of file