Eigen
Dependencies: Vector3
Dependents: optWingforHAPS_Eigen hexaTest_Eigen
GPSUBX_UART.hpp@18:ba361d6ab9cd, 2022-02-02 (annotated)
- Committer:
- NaotoMorita
- Date:
- Wed Feb 02 08:42:25 2022 +0000
- Revision:
- 18:ba361d6ab9cd
- Parent:
- 13:facd8e54f2eb
- Child:
- 21:712076ece407
gps.Hacc
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 |
NaotoMorita | 13:facd8e54f2eb | 9 | #define STATUS_LEN 16 |
cocorlow | 0:cf7c726ec8a1 | 10 | #define TIMEUTC_LEN 20 |
cocorlow | 0:cf7c726ec8a1 | 11 | #define VELECEF_LEN 20 |
cocorlow | 0:cf7c726ec8a1 | 12 | #define VELNED_LEN 36 |
cocorlow | 0:cf7c726ec8a1 | 13 | |
cocorlow | 0:cf7c726ec8a1 | 14 | #define UBX_SYNC 0x62 << 8 | 0xb5 |
cocorlow | 0:cf7c726ec8a1 | 15 | |
cocorlow | 1:71f5168e48c8 | 16 | #define RECEIVE_SIZE 1024 |
cocorlow | 0:cf7c726ec8a1 | 17 | #define SENTENCE_SIZE 64 |
cocorlow | 0:cf7c726ec8a1 | 18 | |
NaotoMorita | 12:2ffb2fcaac23 | 19 | //extern Serial twelite; |
cocorlow | 0:cf7c726ec8a1 | 20 | |
cocorlow | 0:cf7c726ec8a1 | 21 | //// 0x01 0x01 |
cocorlow | 0:cf7c726ec8a1 | 22 | //union POSECEF |
cocorlow | 0:cf7c726ec8a1 | 23 | //{ |
cocorlow | 0:cf7c726ec8a1 | 24 | // char byte_data[POSECEF_LEN+8]; |
cocorlow | 0:cf7c726ec8a1 | 25 | // struct |
cocorlow | 0:cf7c726ec8a1 | 26 | // { |
cocorlow | 0:cf7c726ec8a1 | 27 | // unsigned short sync; |
cocorlow | 0:cf7c726ec8a1 | 28 | // char m_class; |
cocorlow | 0:cf7c726ec8a1 | 29 | // char m_id; |
cocorlow | 0:cf7c726ec8a1 | 30 | // unsigned short pay_len; |
cocorlow | 0:cf7c726ec8a1 | 31 | // unsigned int iTOW; |
cocorlow | 0:cf7c726ec8a1 | 32 | // int ecefX; |
cocorlow | 0:cf7c726ec8a1 | 33 | // int ecefY; |
cocorlow | 0:cf7c726ec8a1 | 34 | // int ecefZ; |
cocorlow | 0:cf7c726ec8a1 | 35 | // unsigned int pAcc; |
cocorlow | 0:cf7c726ec8a1 | 36 | // char check_a; |
cocorlow | 0:cf7c726ec8a1 | 37 | // char check_b; |
cocorlow | 0:cf7c726ec8a1 | 38 | // } data; |
cocorlow | 0:cf7c726ec8a1 | 39 | //}; |
cocorlow | 0:cf7c726ec8a1 | 40 | |
cocorlow | 0:cf7c726ec8a1 | 41 | // 0x01 0x02 |
cocorlow | 0:cf7c726ec8a1 | 42 | union POSLLH |
cocorlow | 0:cf7c726ec8a1 | 43 | { |
cocorlow | 0:cf7c726ec8a1 | 44 | char byte_data[POSLLH_LEN]; |
cocorlow | 0:cf7c726ec8a1 | 45 | struct |
cocorlow | 0:cf7c726ec8a1 | 46 | { |
cocorlow | 0:cf7c726ec8a1 | 47 | unsigned int iTOW; |
cocorlow | 0:cf7c726ec8a1 | 48 | int lon; |
cocorlow | 0:cf7c726ec8a1 | 49 | int lat; |
cocorlow | 0:cf7c726ec8a1 | 50 | int height; |
cocorlow | 0:cf7c726ec8a1 | 51 | int hMSL; |
cocorlow | 0:cf7c726ec8a1 | 52 | unsigned int hAcc; |
cocorlow | 0:cf7c726ec8a1 | 53 | unsigned int vAcc; |
cocorlow | 0:cf7c726ec8a1 | 54 | } data; |
cocorlow | 0:cf7c726ec8a1 | 55 | }; |
cocorlow | 0:cf7c726ec8a1 | 56 | |
NaotoMorita | 13:facd8e54f2eb | 57 | // 0x01 0x03 |
NaotoMorita | 13:facd8e54f2eb | 58 | union STATUS |
cocorlow | 10:a90d07e4c34d | 59 | { |
NaotoMorita | 13:facd8e54f2eb | 60 | char byte_data[STATUS_LEN]; |
cocorlow | 10:a90d07e4c34d | 61 | struct |
cocorlow | 10:a90d07e4c34d | 62 | { |
cocorlow | 10:a90d07e4c34d | 63 | unsigned int iTOW; |
NaotoMorita | 13:facd8e54f2eb | 64 | unsigned char gpsFix; |
NaotoMorita | 13:facd8e54f2eb | 65 | unsigned char flags; |
NaotoMorita | 13:facd8e54f2eb | 66 | unsigned char fixStat; |
NaotoMorita | 13:facd8e54f2eb | 67 | unsigned char flags2; |
NaotoMorita | 13:facd8e54f2eb | 68 | unsigned int ttff; |
NaotoMorita | 13:facd8e54f2eb | 69 | unsigned int msss; |
cocorlow | 10:a90d07e4c34d | 70 | } data; |
cocorlow | 10:a90d07e4c34d | 71 | }; |
cocorlow | 10:a90d07e4c34d | 72 | |
cocorlow | 0:cf7c726ec8a1 | 73 | // 0x01 0x21 |
cocorlow | 0:cf7c726ec8a1 | 74 | union TIMEUTC |
cocorlow | 0:cf7c726ec8a1 | 75 | { |
cocorlow | 0:cf7c726ec8a1 | 76 | char byte_data[TIMEUTC_LEN]; |
cocorlow | 0:cf7c726ec8a1 | 77 | struct |
cocorlow | 0:cf7c726ec8a1 | 78 | { |
cocorlow | 0:cf7c726ec8a1 | 79 | unsigned int iTOW; |
cocorlow | 0:cf7c726ec8a1 | 80 | unsigned int tAcc; |
cocorlow | 0:cf7c726ec8a1 | 81 | int nano; |
cocorlow | 0:cf7c726ec8a1 | 82 | unsigned short year; |
cocorlow | 0:cf7c726ec8a1 | 83 | unsigned char month; |
cocorlow | 0:cf7c726ec8a1 | 84 | unsigned char day; |
cocorlow | 0:cf7c726ec8a1 | 85 | unsigned char hour; |
cocorlow | 0:cf7c726ec8a1 | 86 | unsigned char min; |
cocorlow | 0:cf7c726ec8a1 | 87 | unsigned char sec; |
cocorlow | 0:cf7c726ec8a1 | 88 | unsigned char valid; |
cocorlow | 0:cf7c726ec8a1 | 89 | } data; |
cocorlow | 0:cf7c726ec8a1 | 90 | }; |
cocorlow | 0:cf7c726ec8a1 | 91 | |
cocorlow | 0:cf7c726ec8a1 | 92 | //// 0x01 0x11 |
cocorlow | 0:cf7c726ec8a1 | 93 | //union VELECEF |
cocorlow | 0:cf7c726ec8a1 | 94 | //{ |
cocorlow | 0:cf7c726ec8a1 | 95 | // char byte_data[VELECEF_LEN]; |
cocorlow | 0:cf7c726ec8a1 | 96 | // struct |
cocorlow | 0:cf7c726ec8a1 | 97 | // { |
cocorlow | 0:cf7c726ec8a1 | 98 | // unsigned int iTOW; |
cocorlow | 0:cf7c726ec8a1 | 99 | // int ecefVX; |
cocorlow | 0:cf7c726ec8a1 | 100 | // int ecefVY; |
cocorlow | 0:cf7c726ec8a1 | 101 | // int ecefVZ; |
cocorlow | 0:cf7c726ec8a1 | 102 | // unsigned int sAcc; |
cocorlow | 0:cf7c726ec8a1 | 103 | // } data; |
cocorlow | 0:cf7c726ec8a1 | 104 | //}; |
cocorlow | 0:cf7c726ec8a1 | 105 | |
cocorlow | 0:cf7c726ec8a1 | 106 | // 0x01 0x12 |
cocorlow | 0:cf7c726ec8a1 | 107 | union VELNED |
cocorlow | 0:cf7c726ec8a1 | 108 | { |
cocorlow | 0:cf7c726ec8a1 | 109 | char byte_data[VELNED_LEN]; |
cocorlow | 0:cf7c726ec8a1 | 110 | struct |
cocorlow | 0:cf7c726ec8a1 | 111 | { |
cocorlow | 0:cf7c726ec8a1 | 112 | unsigned int iTOW; |
cocorlow | 0:cf7c726ec8a1 | 113 | int velN; |
cocorlow | 0:cf7c726ec8a1 | 114 | int velE; |
cocorlow | 0:cf7c726ec8a1 | 115 | int velD; |
cocorlow | 0:cf7c726ec8a1 | 116 | unsigned int speed; |
cocorlow | 0:cf7c726ec8a1 | 117 | unsigned int gSpeed; |
cocorlow | 0:cf7c726ec8a1 | 118 | signed int heading; |
cocorlow | 0:cf7c726ec8a1 | 119 | unsigned int sAcc; |
cocorlow | 0:cf7c726ec8a1 | 120 | unsigned int cAcc; |
cocorlow | 0:cf7c726ec8a1 | 121 | } data; |
cocorlow | 0:cf7c726ec8a1 | 122 | }; |
cocorlow | 4:59f0f651296c | 123 | |
cocorlow | 0:cf7c726ec8a1 | 124 | |
cocorlow | 5:a92850395dcb | 125 | /**GPS/GNSS library for UART (UBX protocol) |
cocorlow | 5:a92850395dcb | 126 | * RX pin is required. |
cocorlow | 8:1eb5f701a0d0 | 127 | @code |
cocorlow | 8:1eb5f701a0d0 | 128 | #include "mbed.h" |
cocorlow | 8:1eb5f701a0d0 | 129 | #include "GPSUBX_UART.cpp" |
cocorlow | 8:1eb5f701a0d0 | 130 | |
cocorlow | 8:1eb5f701a0d0 | 131 | Serial pc(USBTX, USBRX, 115200); |
cocorlow | 8:1eb5f701a0d0 | 132 | |
cocorlow | 8:1eb5f701a0d0 | 133 | GPSUBX_UART gps(PD_1, PD_0); // mbed Nucleo 767 |
cocorlow | 8:1eb5f701a0d0 | 134 | |
cocorlow | 8:1eb5f701a0d0 | 135 | void Display() |
cocorlow | 8:1eb5f701a0d0 | 136 | { |
cocorlow | 8:1eb5f701a0d0 | 137 | pc.printf("POSLLH: %d, %f, %f, %f\r\n", gps.iTOW_POSLLH, gps.Longitude, gps.Latitude, gps.Height); |
cocorlow | 8:1eb5f701a0d0 | 138 | pc.printf("VELNED: %d, %f, %f, %f\r\n", gps.iTOW_VELNED, gps.VelocityNED.x, gps.VelocityNED.y, gps.VelocityNED.z); |
cocorlow | 8:1eb5f701a0d0 | 139 | pc.printf("TIMEUTC: %4d/%2d/%2d %2d:%2d %2d\r\n", gps.Year, gps.Month, gps.Day, gps.Hours, gps.Minutes, gps.Seconds); |
cocorlow | 8:1eb5f701a0d0 | 140 | } |
cocorlow | 8:1eb5f701a0d0 | 141 | |
cocorlow | 8:1eb5f701a0d0 | 142 | int main() |
cocorlow | 8:1eb5f701a0d0 | 143 | { |
cocorlow | 8:1eb5f701a0d0 | 144 | gps.Attach(); |
cocorlow | 8:1eb5f701a0d0 | 145 | int timer = 0; |
cocorlow | 8:1eb5f701a0d0 | 146 | while (1) |
cocorlow | 8:1eb5f701a0d0 | 147 | { |
cocorlow | 8:1eb5f701a0d0 | 148 | gps.Update(); |
cocorlow | 8:1eb5f701a0d0 | 149 | wait(0.1); |
cocorlow | 8:1eb5f701a0d0 | 150 | timer++; |
cocorlow | 8:1eb5f701a0d0 | 151 | if (timer >= 5) |
cocorlow | 8:1eb5f701a0d0 | 152 | { |
cocorlow | 8:1eb5f701a0d0 | 153 | Display(); |
cocorlow | 8:1eb5f701a0d0 | 154 | timer = 0; |
cocorlow | 8:1eb5f701a0d0 | 155 | } |
cocorlow | 8:1eb5f701a0d0 | 156 | } |
cocorlow | 8:1eb5f701a0d0 | 157 | } |
cocorlow | 8:1eb5f701a0d0 | 158 | @endcode |
cocorlow | 5:a92850395dcb | 159 | */ |
cocorlow | 0:cf7c726ec8a1 | 160 | class GPSUBX_UART |
cocorlow | 0:cf7c726ec8a1 | 161 | { |
cocorlow | 0:cf7c726ec8a1 | 162 | private: |
cocorlow | 2:6218fe8e54f4 | 163 | |
cocorlow | 1:71f5168e48c8 | 164 | RawSerial serial; |
cocorlow | 0:cf7c726ec8a1 | 165 | char receive_buffer[RECEIVE_SIZE]; |
cocorlow | 0:cf7c726ec8a1 | 166 | char sentence_buffer[SENTENCE_SIZE]; |
cocorlow | 0:cf7c726ec8a1 | 167 | static const float Radius = 6378136.6f; |
cocorlow | 2:6218fe8e54f4 | 168 | volatile int receive_index; |
cocorlow | 0:cf7c726ec8a1 | 169 | |
cocorlow | 1:71f5168e48c8 | 170 | void Receive(); |
cocorlow | 1:71f5168e48c8 | 171 | void Decode(char buffer[], int mc, int mi); |
cocorlow | 0:cf7c726ec8a1 | 172 | |
cocorlow | 0:cf7c726ec8a1 | 173 | public: |
cocorlow | 2:6218fe8e54f4 | 174 | |
cocorlow | 2:6218fe8e54f4 | 175 | int dummy; // Compiler bug? If "dummy" does not exist, the value of "Year" (under "public:") may include bugs. |
cocorlow | 5:a92850395dcb | 176 | /**Year*/ |
cocorlow | 2:6218fe8e54f4 | 177 | volatile int Year; |
cocorlow | 5:a92850395dcb | 178 | /**Month*/ |
cocorlow | 2:6218fe8e54f4 | 179 | volatile int Month; |
cocorlow | 5:a92850395dcb | 180 | /**Day*/ |
cocorlow | 2:6218fe8e54f4 | 181 | volatile int Day; |
cocorlow | 5:a92850395dcb | 182 | /**Hours*/ |
cocorlow | 2:6218fe8e54f4 | 183 | volatile int Hours; |
cocorlow | 5:a92850395dcb | 184 | /**Minutes*/ |
cocorlow | 2:6218fe8e54f4 | 185 | volatile int Minutes; |
cocorlow | 5:a92850395dcb | 186 | /**Seconds*/ |
cocorlow | 2:6218fe8e54f4 | 187 | volatile int Seconds; |
cocorlow | 5:a92850395dcb | 188 | /**Longitude (deg)*/ |
cocorlow | 5:a92850395dcb | 189 | volatile float Longitude; |
cocorlow | 5:a92850395dcb | 190 | /**Latitude (deg)*/ |
cocorlow | 2:6218fe8e54f4 | 191 | volatile float Latitude; |
cocorlow | 5:a92850395dcb | 192 | /**Height [m]*/ |
cocorlow | 2:6218fe8e54f4 | 193 | volatile float Height; |
cocorlow | 5:a92850395dcb | 194 | /**iTOW of POSLLH [ms]*/ |
NaotoMorita | 18:ba361d6ab9cd | 195 | volatile float Hacc; |
NaotoMorita | 18:ba361d6ab9cd | 196 | /**Horizontal acculacy of POSLLH [mm]*/ |
cocorlow | 2:6218fe8e54f4 | 197 | volatile int iTOW_POSLLH; |
cocorlow | 9:874d7633e358 | 198 | /**Velocity NED [m/s]*/ |
cocorlow | 2:6218fe8e54f4 | 199 | volatile Vector3 VelocityNED; |
cocorlow | 5:a92850395dcb | 200 | /**Speed [m/s]*/ |
cocorlow | 2:6218fe8e54f4 | 201 | volatile float Speed; |
cocorlow | 5:a92850395dcb | 202 | /**Ground speed [m/s]*/ |
cocorlow | 2:6218fe8e54f4 | 203 | volatile float GroundSpeed; |
cocorlow | 5:a92850395dcb | 204 | /**Heading angle (deg) */ |
cocorlow | 2:6218fe8e54f4 | 205 | volatile float Heading; |
cocorlow | 5:a92850395dcb | 206 | /**iTOW of VELNED [ms]*/ |
cocorlow | 2:6218fe8e54f4 | 207 | volatile int iTOW_VELNED; |
cocorlow | 1:71f5168e48c8 | 208 | |
NaotoMorita | 13:facd8e54f2eb | 209 | /**iTOW of STATUS [ms]*/ |
NaotoMorita | 13:facd8e54f2eb | 210 | volatile int iTOW_STATUS; |
NaotoMorita | 13:facd8e54f2eb | 211 | /**gpsFix Status*/ |
NaotoMorita | 13:facd8e54f2eb | 212 | volatile char gpsFix; |
cocorlow | 10:a90d07e4c34d | 213 | |
cocorlow | 10:a90d07e4c34d | 214 | |
cocorlow | 5:a92850395dcb | 215 | /**Time zone*/ |
cocorlow | 2:6218fe8e54f4 | 216 | volatile int TimeZone; |
cocorlow | 0:cf7c726ec8a1 | 217 | |
cocorlow | 0:cf7c726ec8a1 | 218 | Vector3 UniversalZeroPosition; |
cocorlow | 0:cf7c726ec8a1 | 219 | Vector3 UniversalZeroUnitN; |
cocorlow | 0:cf7c726ec8a1 | 220 | Vector3 UniversalZeroUnitE; |
cocorlow | 0:cf7c726ec8a1 | 221 | Vector3 UniversalZeroUnitD; |
cocorlow | 0:cf7c726ec8a1 | 222 | Vector3 UniversalPosition; |
cocorlow | 5:a92850395dcb | 223 | /**Position NED [m]*/ |
cocorlow | 0:cf7c726ec8a1 | 224 | Vector3 PositionNED; |
cocorlow | 3:6c8715574bb8 | 225 | |
cocorlow | 5:a92850395dcb | 226 | /**Constructor |
cocorlow | 3:6c8715574bb8 | 227 | *@param tx UART TX pin |
cocorlow | 3:6c8715574bb8 | 228 | *@param rx UART RX pin |
cocorlow | 3:6c8715574bb8 | 229 | *@param baud UART baud rate (default: 38400) |
cocorlow | 3:6c8715574bb8 | 230 | *@param timezone Time zone (default: +9) |
cocorlow | 3:6c8715574bb8 | 231 | */ |
cocorlow | 3:6c8715574bb8 | 232 | GPSUBX_UART(PinName tx, PinName rx, int baud = 38400, int timezone = +9); |
cocorlow | 5:a92850395dcb | 233 | /**call Update() regularly in the main loop |
cocorlow | 5:a92850395dcb | 234 | */ |
cocorlow | 1:71f5168e48c8 | 235 | void Update(); |
cocorlow | 5:a92850395dcb | 236 | /**call Attach() once before measuring position |
cocorlow | 5:a92850395dcb | 237 | */ |
cocorlow | 0:cf7c726ec8a1 | 238 | void Attach(); |
cocorlow | 0:cf7c726ec8a1 | 239 | |
cocorlow | 0:cf7c726ec8a1 | 240 | Vector3 ToUniversalUnit(); |
cocorlow | 0:cf7c726ec8a1 | 241 | Vector3 ToUniversal(); |
cocorlow | 0:cf7c726ec8a1 | 242 | void CalculateUnit(); |
cocorlow | 5:a92850395dcb | 243 | /**call Calculate() before calculating PositionNED |
cocorlow | 5:a92850395dcb | 244 | */ |
cocorlow | 0:cf7c726ec8a1 | 245 | void Calculate(); |
cocorlow | 0:cf7c726ec8a1 | 246 | Vector3 Calculate(Vector3 position); |
cocorlow | 0:cf7c726ec8a1 | 247 | |
cocorlow | 0:cf7c726ec8a1 | 248 | static void Checksum(char payload[], int n, char* ck_a, char* ck_b); |
cocorlow | 0:cf7c726ec8a1 | 249 | }; |
cocorlow | 0:cf7c726ec8a1 | 250 | #endif |