GPS/GNSS UBX library for UART

Dependencies:   Vector3

Dependents:   GPS_0002

Committer:
cocorlow
Date:
Fri Sep 17 16:30:14 2021 +0000
Revision:
5:a92850395dcb
Parent:
4:59f0f651296c
Child:
6:bde0ea0e2bfc
api reference

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cocorlow 0:cf7c726ec8a1 1 #ifndef __GPSUBX_UART_HPP__
cocorlow 0:cf7c726ec8a1 2 #define __GPSUBX_UART_HPP__
cocorlow 0:cf7c726ec8a1 3
cocorlow 0:cf7c726ec8a1 4 #include "mbed.h"
cocorlow 0:cf7c726ec8a1 5 #include "Vector3.hpp"
cocorlow 0:cf7c726ec8a1 6
cocorlow 0:cf7c726ec8a1 7 #define POSECEF_LEN 20
cocorlow 0:cf7c726ec8a1 8 #define POSLLH_LEN 28
cocorlow 0:cf7c726ec8a1 9 #define TIMEUTC_LEN 20
cocorlow 0:cf7c726ec8a1 10 #define VELECEF_LEN 20
cocorlow 0:cf7c726ec8a1 11 #define VELNED_LEN 36
cocorlow 0:cf7c726ec8a1 12
cocorlow 0:cf7c726ec8a1 13 #define UBX_SYNC 0x62 << 8 | 0xb5
cocorlow 0:cf7c726ec8a1 14
cocorlow 1:71f5168e48c8 15 #define RECEIVE_SIZE 1024
cocorlow 0:cf7c726ec8a1 16 #define SENTENCE_SIZE 64
cocorlow 0:cf7c726ec8a1 17
cocorlow 0:cf7c726ec8a1 18 extern Serial pc;
cocorlow 0:cf7c726ec8a1 19
cocorlow 0:cf7c726ec8a1 20 //// 0x01 0x01
cocorlow 0:cf7c726ec8a1 21 //union POSECEF
cocorlow 0:cf7c726ec8a1 22 //{
cocorlow 0:cf7c726ec8a1 23 // char byte_data[POSECEF_LEN+8];
cocorlow 0:cf7c726ec8a1 24 // struct
cocorlow 0:cf7c726ec8a1 25 // {
cocorlow 0:cf7c726ec8a1 26 // unsigned short sync;
cocorlow 0:cf7c726ec8a1 27 // char m_class;
cocorlow 0:cf7c726ec8a1 28 // char m_id;
cocorlow 0:cf7c726ec8a1 29 // unsigned short pay_len;
cocorlow 0:cf7c726ec8a1 30 // unsigned int iTOW;
cocorlow 0:cf7c726ec8a1 31 // int ecefX;
cocorlow 0:cf7c726ec8a1 32 // int ecefY;
cocorlow 0:cf7c726ec8a1 33 // int ecefZ;
cocorlow 0:cf7c726ec8a1 34 // unsigned int pAcc;
cocorlow 0:cf7c726ec8a1 35 // char check_a;
cocorlow 0:cf7c726ec8a1 36 // char check_b;
cocorlow 0:cf7c726ec8a1 37 // } data;
cocorlow 0:cf7c726ec8a1 38 //};
cocorlow 0:cf7c726ec8a1 39
cocorlow 0:cf7c726ec8a1 40 // 0x01 0x02
cocorlow 0:cf7c726ec8a1 41 union POSLLH
cocorlow 0:cf7c726ec8a1 42 {
cocorlow 0:cf7c726ec8a1 43 char byte_data[POSLLH_LEN];
cocorlow 0:cf7c726ec8a1 44 struct
cocorlow 0:cf7c726ec8a1 45 {
cocorlow 0:cf7c726ec8a1 46 unsigned int iTOW;
cocorlow 0:cf7c726ec8a1 47 int lon;
cocorlow 0:cf7c726ec8a1 48 int lat;
cocorlow 0:cf7c726ec8a1 49 int height;
cocorlow 0:cf7c726ec8a1 50 int hMSL;
cocorlow 0:cf7c726ec8a1 51 unsigned int hAcc;
cocorlow 0:cf7c726ec8a1 52 unsigned int vAcc;
cocorlow 0:cf7c726ec8a1 53 } data;
cocorlow 0:cf7c726ec8a1 54 };
cocorlow 0:cf7c726ec8a1 55
cocorlow 0:cf7c726ec8a1 56 // 0x01 0x21
cocorlow 0:cf7c726ec8a1 57 union TIMEUTC
cocorlow 0:cf7c726ec8a1 58 {
cocorlow 0:cf7c726ec8a1 59 char byte_data[TIMEUTC_LEN];
cocorlow 0:cf7c726ec8a1 60 struct
cocorlow 0:cf7c726ec8a1 61 {
cocorlow 0:cf7c726ec8a1 62 unsigned int iTOW;
cocorlow 0:cf7c726ec8a1 63 unsigned int tAcc;
cocorlow 0:cf7c726ec8a1 64 int nano;
cocorlow 0:cf7c726ec8a1 65 unsigned short year;
cocorlow 0:cf7c726ec8a1 66 unsigned char month;
cocorlow 0:cf7c726ec8a1 67 unsigned char day;
cocorlow 0:cf7c726ec8a1 68 unsigned char hour;
cocorlow 0:cf7c726ec8a1 69 unsigned char min;
cocorlow 0:cf7c726ec8a1 70 unsigned char sec;
cocorlow 0:cf7c726ec8a1 71 unsigned char valid;
cocorlow 0:cf7c726ec8a1 72 } data;
cocorlow 0:cf7c726ec8a1 73 };
cocorlow 0:cf7c726ec8a1 74
cocorlow 0:cf7c726ec8a1 75 //// 0x01 0x11
cocorlow 0:cf7c726ec8a1 76 //union VELECEF
cocorlow 0:cf7c726ec8a1 77 //{
cocorlow 0:cf7c726ec8a1 78 // char byte_data[VELECEF_LEN];
cocorlow 0:cf7c726ec8a1 79 // struct
cocorlow 0:cf7c726ec8a1 80 // {
cocorlow 0:cf7c726ec8a1 81 // unsigned int iTOW;
cocorlow 0:cf7c726ec8a1 82 // int ecefVX;
cocorlow 0:cf7c726ec8a1 83 // int ecefVY;
cocorlow 0:cf7c726ec8a1 84 // int ecefVZ;
cocorlow 0:cf7c726ec8a1 85 // unsigned int sAcc;
cocorlow 0:cf7c726ec8a1 86 // } data;
cocorlow 0:cf7c726ec8a1 87 //};
cocorlow 0:cf7c726ec8a1 88
cocorlow 0:cf7c726ec8a1 89 // 0x01 0x12
cocorlow 0:cf7c726ec8a1 90 union VELNED
cocorlow 0:cf7c726ec8a1 91 {
cocorlow 0:cf7c726ec8a1 92 char byte_data[VELNED_LEN];
cocorlow 0:cf7c726ec8a1 93 struct
cocorlow 0:cf7c726ec8a1 94 {
cocorlow 0:cf7c726ec8a1 95 unsigned int iTOW;
cocorlow 0:cf7c726ec8a1 96 int velN;
cocorlow 0:cf7c726ec8a1 97 int velE;
cocorlow 0:cf7c726ec8a1 98 int velD;
cocorlow 0:cf7c726ec8a1 99 unsigned int speed;
cocorlow 0:cf7c726ec8a1 100 unsigned int gSpeed;
cocorlow 0:cf7c726ec8a1 101 signed int heading;
cocorlow 0:cf7c726ec8a1 102 unsigned int sAcc;
cocorlow 0:cf7c726ec8a1 103 unsigned int cAcc;
cocorlow 0:cf7c726ec8a1 104 } data;
cocorlow 0:cf7c726ec8a1 105 };
cocorlow 4:59f0f651296c 106
cocorlow 0:cf7c726ec8a1 107
cocorlow 5:a92850395dcb 108 /**GPS/GNSS library for UART (UBX protocol)
cocorlow 5:a92850395dcb 109 * RX pin is required.
cocorlow 5:a92850395dcb 110 */
cocorlow 0:cf7c726ec8a1 111 class GPSUBX_UART
cocorlow 0:cf7c726ec8a1 112 {
cocorlow 0:cf7c726ec8a1 113 private:
cocorlow 2:6218fe8e54f4 114
cocorlow 1:71f5168e48c8 115 RawSerial serial;
cocorlow 0:cf7c726ec8a1 116 char receive_buffer[RECEIVE_SIZE];
cocorlow 0:cf7c726ec8a1 117 char sentence_buffer[SENTENCE_SIZE];
cocorlow 0:cf7c726ec8a1 118 static const float Radius = 6378136.6f;
cocorlow 2:6218fe8e54f4 119 volatile int receive_index;
cocorlow 0:cf7c726ec8a1 120
cocorlow 1:71f5168e48c8 121 void Receive();
cocorlow 1:71f5168e48c8 122 void Decode(char buffer[], int mc, int mi);
cocorlow 0:cf7c726ec8a1 123
cocorlow 0:cf7c726ec8a1 124 public:
cocorlow 2:6218fe8e54f4 125
cocorlow 2:6218fe8e54f4 126 int dummy; // Compiler bug? If "dummy" does not exist, the value of "Year" (under "public:") may include bugs.
cocorlow 5:a92850395dcb 127 /**Year*/
cocorlow 2:6218fe8e54f4 128 volatile int Year;
cocorlow 5:a92850395dcb 129 /**Month*/
cocorlow 2:6218fe8e54f4 130 volatile int Month;
cocorlow 5:a92850395dcb 131 /**Day*/
cocorlow 2:6218fe8e54f4 132 volatile int Day;
cocorlow 5:a92850395dcb 133 /**Hours*/
cocorlow 2:6218fe8e54f4 134 volatile int Hours;
cocorlow 5:a92850395dcb 135 /**Minutes*/
cocorlow 2:6218fe8e54f4 136 volatile int Minutes;
cocorlow 5:a92850395dcb 137 /**Seconds*/
cocorlow 2:6218fe8e54f4 138 volatile int Seconds;
cocorlow 5:a92850395dcb 139 /**Longitude (deg)*/
cocorlow 5:a92850395dcb 140 volatile float Longitude;
cocorlow 5:a92850395dcb 141 /**Latitude (deg)*/
cocorlow 2:6218fe8e54f4 142 volatile float Latitude;
cocorlow 5:a92850395dcb 143 /**Height [m]*/
cocorlow 2:6218fe8e54f4 144 volatile float Height;
cocorlow 5:a92850395dcb 145 /**iTOW of POSLLH [ms]*/
cocorlow 2:6218fe8e54f4 146 volatile int iTOW_POSLLH;
cocorlow 5:a92850395dcb 147 /**iTOW of POSLLH [ms]*/
cocorlow 2:6218fe8e54f4 148 volatile Vector3 VelocityNED;
cocorlow 5:a92850395dcb 149 /**Speed [m/s]*/
cocorlow 2:6218fe8e54f4 150 volatile float Speed;
cocorlow 5:a92850395dcb 151 /**Ground speed [m/s]*/
cocorlow 2:6218fe8e54f4 152 volatile float GroundSpeed;
cocorlow 5:a92850395dcb 153 /**Heading angle (deg) */
cocorlow 2:6218fe8e54f4 154 volatile float Heading;
cocorlow 5:a92850395dcb 155 /**iTOW of VELNED [ms]*/
cocorlow 2:6218fe8e54f4 156 volatile int iTOW_VELNED;
cocorlow 1:71f5168e48c8 157
cocorlow 5:a92850395dcb 158 /**Time zone*/
cocorlow 2:6218fe8e54f4 159 volatile int TimeZone;
cocorlow 0:cf7c726ec8a1 160
cocorlow 0:cf7c726ec8a1 161 Vector3 UniversalZeroPosition;
cocorlow 0:cf7c726ec8a1 162 Vector3 UniversalZeroUnitN;
cocorlow 0:cf7c726ec8a1 163 Vector3 UniversalZeroUnitE;
cocorlow 0:cf7c726ec8a1 164 Vector3 UniversalZeroUnitD;
cocorlow 0:cf7c726ec8a1 165 Vector3 UniversalPosition;
cocorlow 5:a92850395dcb 166 /**Position NED [m]*/
cocorlow 0:cf7c726ec8a1 167 Vector3 PositionNED;
cocorlow 3:6c8715574bb8 168
cocorlow 5:a92850395dcb 169 /**Constructor
cocorlow 3:6c8715574bb8 170 *@param tx UART TX pin
cocorlow 3:6c8715574bb8 171 *@param rx UART RX pin
cocorlow 3:6c8715574bb8 172 *@param baud UART baud rate (default: 38400)
cocorlow 3:6c8715574bb8 173 *@param timezone Time zone (default: +9)
cocorlow 3:6c8715574bb8 174 */
cocorlow 3:6c8715574bb8 175 GPSUBX_UART(PinName tx, PinName rx, int baud = 38400, int timezone = +9);
cocorlow 5:a92850395dcb 176 /**call Update() regularly in the main loop
cocorlow 5:a92850395dcb 177 */
cocorlow 1:71f5168e48c8 178 void Update();
cocorlow 5:a92850395dcb 179 /**call Attach() once before measuring position
cocorlow 5:a92850395dcb 180 */
cocorlow 0:cf7c726ec8a1 181 void Attach();
cocorlow 0:cf7c726ec8a1 182
cocorlow 0:cf7c726ec8a1 183 Vector3 ToUniversalUnit();
cocorlow 0:cf7c726ec8a1 184 Vector3 ToUniversal();
cocorlow 0:cf7c726ec8a1 185 void CalculateUnit();
cocorlow 5:a92850395dcb 186 /**call Calculate() before calculating PositionNED
cocorlow 5:a92850395dcb 187 */
cocorlow 0:cf7c726ec8a1 188 void Calculate();
cocorlow 0:cf7c726ec8a1 189 Vector3 Calculate(Vector3 position);
cocorlow 0:cf7c726ec8a1 190
cocorlow 0:cf7c726ec8a1 191 static void Checksum(char payload[], int n, char* ck_a, char* ck_b);
cocorlow 0:cf7c726ec8a1 192 };
cocorlow 0:cf7c726ec8a1 193 #endif