u-blox UBX library for I2C

Dependencies:   Vector3

It will not work

Files at this revision

API Documentation at this revision

Comitter:
cocorlow
Date:
Sun Sep 12 16:38:51 2021 +0000
Commit message:
TIMEUTC test

Changed in this revision

DataStructure.hpp Show annotated file Show diff for this revision Revisions of this file
GPSUBX.cpp Show annotated file Show diff for this revision Revisions of this file
GPSUBX.hpp Show annotated file Show diff for this revision Revisions of this file
Vector3.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 6256752abece DataStructure.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DataStructure.hpp	Sun Sep 12 16:38:51 2021 +0000
@@ -0,0 +1,53 @@
+#ifndef OBSVDSTRUCT
+#define OBSVDSTRUCT
+
+//全ての観測データを収納する型
+typedef struct obsvdData{
+    
+    int Tim;
+    
+    int Lon;
+    int Lat;
+    int GHeight; //GPS高度
+    
+    int VelN;
+    int VelE;
+    int VelD;
+    
+    int Acc[3];
+    int Gyr[3];
+    int Mag[3];
+    
+    int PHeight; //気圧
+    
+    
+}obsvdData;
+
+typedef struct timData{
+    int Year;
+    int Month;
+    int Day;
+    int Hour;
+    int Min;
+    int Sec;
+}tim;
+
+typedef enum state{
+    WAIT,
+    MANUAL,
+    AUTO,
+    READ,
+    STREAM,
+    CONFIG    
+}state;
+
+//ログの状態を表す
+typedef enum logstate{
+    STOP,
+    STBY,
+    RUNTIME,
+    FIN
+}logstate;
+
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 6256752abece GPSUBX.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPSUBX.cpp	Sun Sep 12 16:38:51 2021 +0000
@@ -0,0 +1,104 @@
+#include "GPSUBX.hpp"
+#include "mbed.h"
+
+GPSUBX::GPSUBX(PinName sda, PinName scl, char address = GPS_ADDRESS, int timedifference = 9, int hz = 100000)
+    :i2c(sda, scl), Address(address), TimeDifference(timedifference)
+{
+    i2c.frequency(hz);
+}
+
+void GPSUBX::Checksum(char payload[], int n, char* ck_a, char* ck_b)
+{
+    int ca = 0;
+    int cb = 0;
+    for (int i = 0; i < n+4; i++)
+    {
+        ca += (unsigned char)payload[i+2];
+        cb += ca;
+    }
+    *ck_a = (char)(ca & 0xff);
+    *ck_b = (char)(cb & 0xff);
+}
+
+int GPSUBX::GetGPSData(GPSData* pdata)
+{
+    POSLLH posllh;
+    char command[8] = {0xb5, 0x62, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00};
+    Checksum(command, 0, &command[6], &command[7]);
+    
+    i2c.write(Address << 1, command, 8);
+    i2c.read(Address << 1 | 1, posllh.byte_data, POSLLH_LEN+8);
+    
+    for (int i = 0; i < POSLLH_LEN+8; i++)
+    {
+        pc.printf("%x ", posllh.byte_data[i]);
+    }
+    pc.printf("\r\n");
+    if (posllh.data.sync == UBX_SYNC && posllh.data.m_class == 0x01 && posllh.data.m_id == 0x02 && posllh.data.pay_len == POSLLH_LEN)
+    {
+        char ca, cb;
+        Checksum(posllh.byte_data, POSLLH_LEN, &ca, &cb);
+        if (posllh.data.check_a == ca && posllh.data.check_b == cb)
+        {
+            pdata->Longitude = (float)posllh.data.lon * 1e-7f;
+            pdata->Latitude = (float)posllh.data.lat * 1e-7f;
+            pdata->Height = (float)posllh.data.height / 1000.0f;
+            pc.printf("%f, %f, %f\r\n", pdata->Longitude, pdata->Latitude, pdata->Height);
+        }
+        else
+        {
+            pc.printf("#1\r\n");
+            return 0;
+        }
+            pc.printf("#2\r\n");
+        return 1;
+    }
+    else
+    {
+        pc.printf("#3\r\n");
+        return 0;
+    }
+}
+
+int GPSUBX::GetTimeData(GPSData* pdata)
+{
+    TIMEUTC timeutc;
+    char command[8] = {0xb5, 0x62, 0x01, 0x21, 0x00, 0x00, 0x00, 0x00};
+    Checksum(command, 0, &command[6], &command[7]);
+    
+    i2c.write(Address << 1, command, 8);
+    i2c.read(Address << 1 | 1, timeutc.byte_data, TIMEUTC_LEN+8);
+    
+    for (int i = 0; i < TIMEUTC_LEN+8; i++)
+    {
+        pc.printf("%x ", timeutc.byte_data[i]);
+    }
+    pc.printf("\r\n");
+    if (timeutc.data.sync == UBX_SYNC && timeutc.data.m_class == 0x01 && timeutc.data.m_id == 0x21 && timeutc.data.pay_len == TIMEUTC_LEN)
+    {
+        char ca, cb;
+        Checksum(timeutc.byte_data, TIMEUTC_LEN, &ca, &cb);
+        if (timeutc.data.check_a == ca && timeutc.data.check_b == cb)
+        {
+            pdata->Year = timeutc.data.year;
+            pdata->Month = timeutc.data.month;
+            pdata->Day = timeutc.data.day;
+            pdata->Hours = timeutc.data.hour;
+            pdata->Minutes = timeutc.data.min;
+            pdata->Seconds = timeutc.data.sec;
+            pc.printf("%4d/%2d/%2d %2d:%2d %2d\r\n", pdata->Year, pdata->Month, pdata->Day, pdata->Hours, pdata->Minutes, pdata->Seconds);
+        }
+        else
+        {
+            pc.printf("#4\r\n");
+            return 0;
+        }
+            pc.printf("#5\r\n");
+        return 1;
+    }
+    else
+    {
+        pc.printf("#6\r\n");
+        return 0;
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 6256752abece GPSUBX.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPSUBX.hpp	Sun Sep 12 16:38:51 2021 +0000
@@ -0,0 +1,182 @@
+#ifndef __GPSUBX_HPP__
+#define __GPSUBX_HPP__
+
+#include "mbed.h"
+#include "Vector3.hpp"
+
+#define GPS_ADDRESS 0x23
+
+#define POSECEF_LEN 20
+#define POSLLH_LEN 28
+#define TIMEUTC_LEN 20
+#define VELECEF_LEN 20
+#define VELNED_LEN 36
+
+
+#define UBX_SYNC 0x62 << 8 | 0xb5
+
+extern Serial pc;
+
+struct GPSData
+{
+    int Year;
+    int Month;
+    int Day;
+    int Hours;
+    int Minutes;
+    int Seconds;
+    int iTOW;
+    float Longitude;
+    float Latitude;
+    float Height;
+    float Pressure;
+    Vector3 VelocityNED;
+    Vector3 PositionNED;
+    Vector3 PositionECEF;
+};
+
+enum GPSState
+{
+    WAIT,
+    MANUAL,
+    AUTO,
+    READ,
+    STREAM,
+    CONFIG    
+};
+
+enum LogState
+{
+    STOP,
+    STBY,
+    RUNTIME,
+    FIN
+};
+
+// 0x01 0x01
+union POSECEF
+{
+    char byte_data[POSECEF_LEN+8];
+    struct
+    {
+        unsigned short sync;
+        char m_class;
+        char m_id;
+        unsigned short pay_len;
+        unsigned int iTOW;
+        int ecefX;
+        int ecefY;
+        int ecefZ;
+        unsigned int pAcc;
+        char check_a;
+        char check_b;
+    } data;
+};
+
+// 0x01 0x02
+union POSLLH
+{
+    char byte_data[POSLLH_LEN+8];
+    struct
+    {
+        unsigned short sync;
+        char m_class;
+        char m_id;
+        unsigned short pay_len;
+        unsigned int iTOW;
+        int lon;
+        int lat;
+        int height;
+        int hMSL;
+        unsigned int hAcc;
+        unsigned int vAcc;
+        char check_a;
+        char check_b;
+    } data;
+};
+
+// 0x01 0x21
+union TIMEUTC
+{
+    char byte_data[TIMEUTC_LEN+8];
+    struct
+    {
+        unsigned short sync;
+        char m_class;
+        char m_id;
+        unsigned short pay_len;
+        unsigned int iTOW;
+        unsigned int tAcc;
+        int nano;
+        unsigned short year;
+        unsigned char month;
+        unsigned char day;
+        unsigned char hour;
+        unsigned char min;
+        unsigned char sec;
+        unsigned char valid;
+        char check_a;
+        char check_b;
+    } data;
+};
+
+// 0x01 0x11
+union VELECEF
+{
+    char byte_data[VELECEF_LEN+8];
+    struct
+    {
+        unsigned short sync;
+        char m_class;
+        char m_id;
+        unsigned short pay_len;
+        unsigned int iTOW;
+        int ecefVX;
+        int ecefVY;
+        int ecefVZ;
+        unsigned int sAcc;
+        char check_a;
+        char check_b;
+    } data;  
+};
+
+// 0x01 0x12
+union VELNED
+{
+    char byte_data[VELNED_LEN+8];
+    struct
+    {
+        unsigned short sync;
+        char m_class;
+        char m_id;
+        unsigned short pay_len;
+        unsigned int iTOW;
+        int velN;
+        int velE;
+        int velD;
+        unsigned int speed;
+        unsigned int gSpeed;
+        signed int heading;
+        unsigned int sAcc;
+        unsigned int cAcc;
+        char check_a;
+        char check_b;
+    } data;
+};
+
+class GPSUBX
+{
+private:
+    I2C i2c;
+    char Address;
+public:
+    int TimeDifference;
+    
+    GPSUBX(PinName sda, PinName scl, char address, int timedifference, int hz);
+    int GetGPSData(GPSData* pdata);
+    int GetTimeData(GPSData* pdata);
+    
+    static void Checksum(char payload[], int n, char* ck_a, char* ck_b);
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 6256752abece Vector3.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Vector3.lib	Sun Sep 12 16:38:51 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Gaku0606/code/Vector3/#d68f06f4d554