GPS/GNSS UBX library for UART
Dependencies: Vector3
GPSUBX_UART.hpp@7:580cdef44531, 2021-09-17 (annotated)
- Committer:
- cocorlow
- Date:
- Fri Sep 17 16:40:42 2021 +0000
- Revision:
- 7:580cdef44531
- Parent:
- 6:bde0ea0e2bfc
- Child:
- 8:1eb5f701a0d0
API ^o^
Who changed what in which revision?
User | Revision | Line number | New 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 | 6:bde0ea0e2bfc | 110 | *@code |
cocorlow | 6:bde0ea0e2bfc | 111 | *#include "mbed.h" |
cocorlow | 6:bde0ea0e2bfc | 112 | *#include "GPSUBX_UART.cpp" |
cocorlow | 6:bde0ea0e2bfc | 113 | * |
cocorlow | 6:bde0ea0e2bfc | 114 | *Serial pc(USBTX, USBRX, 115200); |
cocorlow | 6:bde0ea0e2bfc | 115 | * |
cocorlow | 6:bde0ea0e2bfc | 116 | *GPSUBX_UART gps(PD_1, PD_0); // mbed Nucleo 767 |
cocorlow | 6:bde0ea0e2bfc | 117 | * |
cocorlow | 6:bde0ea0e2bfc | 118 | *void Display() |
cocorlow | 6:bde0ea0e2bfc | 119 | *{ |
cocorlow | 6:bde0ea0e2bfc | 120 | * pc.printf("POSLLH: %d, %f, %f, %f\r\n", gps.iTOW_POSLLH, gps.Longitude, gps.Latitude, gps.Height); |
cocorlow | 6:bde0ea0e2bfc | 121 | * pc.printf("VELNED: %d, %f, %f, %f\r\n", gps.iTOW_VELNED, gps.VelocityNED.x, gps.VelocityNED.y, gps.VelocityNED.z); |
cocorlow | 6:bde0ea0e2bfc | 122 | * pc.printf("TIMEUTC: %4d/%2d/%2d %2d:%2d %2d\r\n", gps.Year, gps.Month, gps.Day, gps.Hours, gps.Minutes, gps.Seconds); |
cocorlow | 6:bde0ea0e2bfc | 123 | *} |
cocorlow | 6:bde0ea0e2bfc | 124 | * |
cocorlow | 6:bde0ea0e2bfc | 125 | *int main() |
cocorlow | 6:bde0ea0e2bfc | 126 | *{ |
cocorlow | 6:bde0ea0e2bfc | 127 | * gps.Attach(); |
cocorlow | 6:bde0ea0e2bfc | 128 | * int timer = 0; |
cocorlow | 6:bde0ea0e2bfc | 129 | * while (1) |
cocorlow | 6:bde0ea0e2bfc | 130 | * { |
cocorlow | 6:bde0ea0e2bfc | 131 | * gps.Update(); |
cocorlow | 6:bde0ea0e2bfc | 132 | * wait(0.1); |
cocorlow | 6:bde0ea0e2bfc | 133 | * timer++; |
cocorlow | 6:bde0ea0e2bfc | 134 | * if (timer >= 5) |
cocorlow | 6:bde0ea0e2bfc | 135 | * { |
cocorlow | 6:bde0ea0e2bfc | 136 | * Display(); |
cocorlow | 6:bde0ea0e2bfc | 137 | * timer = 0; |
cocorlow | 6:bde0ea0e2bfc | 138 | * } |
cocorlow | 6:bde0ea0e2bfc | 139 | * } |
cocorlow | 6:bde0ea0e2bfc | 140 | *} |
cocorlow | 7:580cdef44531 | 141 | *@endcode |
cocorlow | 5:a92850395dcb | 142 | */ |
cocorlow | 0:cf7c726ec8a1 | 143 | class GPSUBX_UART |
cocorlow | 0:cf7c726ec8a1 | 144 | { |
cocorlow | 0:cf7c726ec8a1 | 145 | private: |
cocorlow | 2:6218fe8e54f4 | 146 | |
cocorlow | 1:71f5168e48c8 | 147 | RawSerial serial; |
cocorlow | 0:cf7c726ec8a1 | 148 | char receive_buffer[RECEIVE_SIZE]; |
cocorlow | 0:cf7c726ec8a1 | 149 | char sentence_buffer[SENTENCE_SIZE]; |
cocorlow | 0:cf7c726ec8a1 | 150 | static const float Radius = 6378136.6f; |
cocorlow | 2:6218fe8e54f4 | 151 | volatile int receive_index; |
cocorlow | 0:cf7c726ec8a1 | 152 | |
cocorlow | 1:71f5168e48c8 | 153 | void Receive(); |
cocorlow | 1:71f5168e48c8 | 154 | void Decode(char buffer[], int mc, int mi); |
cocorlow | 0:cf7c726ec8a1 | 155 | |
cocorlow | 0:cf7c726ec8a1 | 156 | public: |
cocorlow | 2:6218fe8e54f4 | 157 | |
cocorlow | 2:6218fe8e54f4 | 158 | int dummy; // Compiler bug? If "dummy" does not exist, the value of "Year" (under "public:") may include bugs. |
cocorlow | 5:a92850395dcb | 159 | /**Year*/ |
cocorlow | 2:6218fe8e54f4 | 160 | volatile int Year; |
cocorlow | 5:a92850395dcb | 161 | /**Month*/ |
cocorlow | 2:6218fe8e54f4 | 162 | volatile int Month; |
cocorlow | 5:a92850395dcb | 163 | /**Day*/ |
cocorlow | 2:6218fe8e54f4 | 164 | volatile int Day; |
cocorlow | 5:a92850395dcb | 165 | /**Hours*/ |
cocorlow | 2:6218fe8e54f4 | 166 | volatile int Hours; |
cocorlow | 5:a92850395dcb | 167 | /**Minutes*/ |
cocorlow | 2:6218fe8e54f4 | 168 | volatile int Minutes; |
cocorlow | 5:a92850395dcb | 169 | /**Seconds*/ |
cocorlow | 2:6218fe8e54f4 | 170 | volatile int Seconds; |
cocorlow | 5:a92850395dcb | 171 | /**Longitude (deg)*/ |
cocorlow | 5:a92850395dcb | 172 | volatile float Longitude; |
cocorlow | 5:a92850395dcb | 173 | /**Latitude (deg)*/ |
cocorlow | 2:6218fe8e54f4 | 174 | volatile float Latitude; |
cocorlow | 5:a92850395dcb | 175 | /**Height [m]*/ |
cocorlow | 2:6218fe8e54f4 | 176 | volatile float Height; |
cocorlow | 5:a92850395dcb | 177 | /**iTOW of POSLLH [ms]*/ |
cocorlow | 2:6218fe8e54f4 | 178 | volatile int iTOW_POSLLH; |
cocorlow | 5:a92850395dcb | 179 | /**iTOW of POSLLH [ms]*/ |
cocorlow | 2:6218fe8e54f4 | 180 | volatile Vector3 VelocityNED; |
cocorlow | 5:a92850395dcb | 181 | /**Speed [m/s]*/ |
cocorlow | 2:6218fe8e54f4 | 182 | volatile float Speed; |
cocorlow | 5:a92850395dcb | 183 | /**Ground speed [m/s]*/ |
cocorlow | 2:6218fe8e54f4 | 184 | volatile float GroundSpeed; |
cocorlow | 5:a92850395dcb | 185 | /**Heading angle (deg) */ |
cocorlow | 2:6218fe8e54f4 | 186 | volatile float Heading; |
cocorlow | 5:a92850395dcb | 187 | /**iTOW of VELNED [ms]*/ |
cocorlow | 2:6218fe8e54f4 | 188 | volatile int iTOW_VELNED; |
cocorlow | 1:71f5168e48c8 | 189 | |
cocorlow | 5:a92850395dcb | 190 | /**Time zone*/ |
cocorlow | 2:6218fe8e54f4 | 191 | volatile int TimeZone; |
cocorlow | 0:cf7c726ec8a1 | 192 | |
cocorlow | 0:cf7c726ec8a1 | 193 | Vector3 UniversalZeroPosition; |
cocorlow | 0:cf7c726ec8a1 | 194 | Vector3 UniversalZeroUnitN; |
cocorlow | 0:cf7c726ec8a1 | 195 | Vector3 UniversalZeroUnitE; |
cocorlow | 0:cf7c726ec8a1 | 196 | Vector3 UniversalZeroUnitD; |
cocorlow | 0:cf7c726ec8a1 | 197 | Vector3 UniversalPosition; |
cocorlow | 5:a92850395dcb | 198 | /**Position NED [m]*/ |
cocorlow | 0:cf7c726ec8a1 | 199 | Vector3 PositionNED; |
cocorlow | 3:6c8715574bb8 | 200 | |
cocorlow | 5:a92850395dcb | 201 | /**Constructor |
cocorlow | 3:6c8715574bb8 | 202 | *@param tx UART TX pin |
cocorlow | 3:6c8715574bb8 | 203 | *@param rx UART RX pin |
cocorlow | 3:6c8715574bb8 | 204 | *@param baud UART baud rate (default: 38400) |
cocorlow | 3:6c8715574bb8 | 205 | *@param timezone Time zone (default: +9) |
cocorlow | 3:6c8715574bb8 | 206 | */ |
cocorlow | 3:6c8715574bb8 | 207 | GPSUBX_UART(PinName tx, PinName rx, int baud = 38400, int timezone = +9); |
cocorlow | 5:a92850395dcb | 208 | /**call Update() regularly in the main loop |
cocorlow | 5:a92850395dcb | 209 | */ |
cocorlow | 1:71f5168e48c8 | 210 | void Update(); |
cocorlow | 5:a92850395dcb | 211 | /**call Attach() once before measuring position |
cocorlow | 5:a92850395dcb | 212 | */ |
cocorlow | 0:cf7c726ec8a1 | 213 | void Attach(); |
cocorlow | 0:cf7c726ec8a1 | 214 | |
cocorlow | 0:cf7c726ec8a1 | 215 | Vector3 ToUniversalUnit(); |
cocorlow | 0:cf7c726ec8a1 | 216 | Vector3 ToUniversal(); |
cocorlow | 0:cf7c726ec8a1 | 217 | void CalculateUnit(); |
cocorlow | 5:a92850395dcb | 218 | /**call Calculate() before calculating PositionNED |
cocorlow | 5:a92850395dcb | 219 | */ |
cocorlow | 0:cf7c726ec8a1 | 220 | void Calculate(); |
cocorlow | 0:cf7c726ec8a1 | 221 | Vector3 Calculate(Vector3 position); |
cocorlow | 0:cf7c726ec8a1 | 222 | |
cocorlow | 0:cf7c726ec8a1 | 223 | static void Checksum(char payload[], int n, char* ck_a, char* ck_b); |
cocorlow | 0:cf7c726ec8a1 | 224 | }; |
cocorlow | 0:cf7c726ec8a1 | 225 | #endif |