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