Eigen
Dependencies: Vector3
Dependents: optWingforHAPS_Eigen hexaTest_Eigen
GPSUBX_UART.cpp@20:efa07b922ba7, 2022-03-31 (annotated)
- Committer:
- NaotoMorita
- Date:
- Thu Mar 31 02:15:19 2022 +0000
- Revision:
- 20:efa07b922ba7
- Parent:
- 18:ba361d6ab9cd
- Child:
- 21:712076ece407
newer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cocorlow | 0:cf7c726ec8a1 | 1 | #include "GPSUBX_UART.hpp" |
cocorlow | 0:cf7c726ec8a1 | 2 | #include "mbed.h" |
cocorlow | 0:cf7c726ec8a1 | 3 | #include <math.h> |
cocorlow | 0:cf7c726ec8a1 | 4 | #define M_PI 3.14159265358979f |
cocorlow | 0:cf7c726ec8a1 | 5 | |
NaotoMorita | 20:efa07b922ba7 | 6 | |
cocorlow | 1:71f5168e48c8 | 7 | GPSUBX_UART::GPSUBX_UART(PinName tx, PinName rx, int baud, int timezone) |
cocorlow | 1:71f5168e48c8 | 8 | :serial(tx, rx, baud), TimeZone(timezone), receive_index(0) |
cocorlow | 0:cf7c726ec8a1 | 9 | { |
cocorlow | 0:cf7c726ec8a1 | 10 | } |
cocorlow | 0:cf7c726ec8a1 | 11 | |
cocorlow | 0:cf7c726ec8a1 | 12 | void GPSUBX_UART::Checksum(char payload[], int n, char* ck_a, char* ck_b) |
cocorlow | 0:cf7c726ec8a1 | 13 | { |
cocorlow | 0:cf7c726ec8a1 | 14 | int ca = 0; |
cocorlow | 0:cf7c726ec8a1 | 15 | int cb = 0; |
cocorlow | 0:cf7c726ec8a1 | 16 | for (int i = 0; i < n+4; i++) |
cocorlow | 0:cf7c726ec8a1 | 17 | { |
cocorlow | 0:cf7c726ec8a1 | 18 | ca += (unsigned char)payload[i+2]; |
cocorlow | 0:cf7c726ec8a1 | 19 | cb += ca; |
cocorlow | 0:cf7c726ec8a1 | 20 | } |
cocorlow | 0:cf7c726ec8a1 | 21 | *ck_a = (char)(ca & 0xff); |
cocorlow | 0:cf7c726ec8a1 | 22 | *ck_b = (char)(cb & 0xff); |
cocorlow | 0:cf7c726ec8a1 | 23 | } |
cocorlow | 0:cf7c726ec8a1 | 24 | |
cocorlow | 0:cf7c726ec8a1 | 25 | void GPSUBX_UART::Receive() |
cocorlow | 0:cf7c726ec8a1 | 26 | { |
cocorlow | 0:cf7c726ec8a1 | 27 | while (serial.readable()) |
cocorlow | 0:cf7c726ec8a1 | 28 | { |
cocorlow | 0:cf7c726ec8a1 | 29 | char c; |
cocorlow | 0:cf7c726ec8a1 | 30 | c = serial.getc(); |
cocorlow | 0:cf7c726ec8a1 | 31 | receive_buffer[receive_index] = c; |
cocorlow | 1:71f5168e48c8 | 32 | receive_index = (receive_index + 1) % RECEIVE_SIZE; |
cocorlow | 1:71f5168e48c8 | 33 | } |
cocorlow | 1:71f5168e48c8 | 34 | } |
cocorlow | 1:71f5168e48c8 | 35 | |
cocorlow | 1:71f5168e48c8 | 36 | |
cocorlow | 1:71f5168e48c8 | 37 | void GPSUBX_UART::Update() |
cocorlow | 1:71f5168e48c8 | 38 | { |
cocorlow | 1:71f5168e48c8 | 39 | volatile static int sentence_start = 0; |
cocorlow | 1:71f5168e48c8 | 40 | volatile static int sentence_length = 0; |
cocorlow | 1:71f5168e48c8 | 41 | volatile static int sentence_counter = 0; |
cocorlow | 1:71f5168e48c8 | 42 | volatile static int read_index = 0; |
cocorlow | 1:71f5168e48c8 | 43 | |
cocorlow | 1:71f5168e48c8 | 44 | volatile static char m_class = 0x00; |
cocorlow | 1:71f5168e48c8 | 45 | volatile static char m_id = 0x00; |
cocorlow | 1:71f5168e48c8 | 46 | |
cocorlow | 1:71f5168e48c8 | 47 | while (read_index != receive_index) |
cocorlow | 1:71f5168e48c8 | 48 | { |
cocorlow | 1:71f5168e48c8 | 49 | char c; |
cocorlow | 1:71f5168e48c8 | 50 | c = receive_buffer[read_index]; |
cocorlow | 0:cf7c726ec8a1 | 51 | if (sentence_counter >= 2) |
cocorlow | 0:cf7c726ec8a1 | 52 | { |
cocorlow | 0:cf7c726ec8a1 | 53 | sentence_counter++; |
cocorlow | 0:cf7c726ec8a1 | 54 | if (sentence_counter == 3) |
cocorlow | 0:cf7c726ec8a1 | 55 | { |
cocorlow | 0:cf7c726ec8a1 | 56 | m_class = c; |
cocorlow | 0:cf7c726ec8a1 | 57 | } |
cocorlow | 0:cf7c726ec8a1 | 58 | else if (sentence_counter == 4) |
cocorlow | 0:cf7c726ec8a1 | 59 | { |
cocorlow | 0:cf7c726ec8a1 | 60 | m_id = c; |
cocorlow | 0:cf7c726ec8a1 | 61 | } |
cocorlow | 0:cf7c726ec8a1 | 62 | else if (sentence_counter == 5) |
cocorlow | 0:cf7c726ec8a1 | 63 | { |
cocorlow | 0:cf7c726ec8a1 | 64 | } |
cocorlow | 0:cf7c726ec8a1 | 65 | else if (sentence_counter == 6) |
cocorlow | 0:cf7c726ec8a1 | 66 | { |
cocorlow | 1:71f5168e48c8 | 67 | int sss = (read_index+RECEIVE_SIZE-1)%RECEIVE_SIZE; |
cocorlow | 0:cf7c726ec8a1 | 68 | sentence_length = (int)(c << 8 | receive_buffer[sss]); |
cocorlow | 0:cf7c726ec8a1 | 69 | } |
cocorlow | 0:cf7c726ec8a1 | 70 | else if (sentence_counter >= sentence_length+8) |
cocorlow | 0:cf7c726ec8a1 | 71 | { |
cocorlow | 0:cf7c726ec8a1 | 72 | for (int i = 0; i < sentence_length+8; i++) |
cocorlow | 0:cf7c726ec8a1 | 73 | { |
cocorlow | 0:cf7c726ec8a1 | 74 | sentence_buffer[i] = receive_buffer[(sentence_start+i)%RECEIVE_SIZE]; |
cocorlow | 0:cf7c726ec8a1 | 75 | } |
cocorlow | 0:cf7c726ec8a1 | 76 | char ca, cb; |
cocorlow | 0:cf7c726ec8a1 | 77 | Checksum(sentence_buffer, sentence_length, &ca, &cb); |
cocorlow | 0:cf7c726ec8a1 | 78 | if (ca == sentence_buffer[sentence_length+6] && cb == sentence_buffer[sentence_length+7]) |
cocorlow | 0:cf7c726ec8a1 | 79 | { |
cocorlow | 0:cf7c726ec8a1 | 80 | Decode(sentence_buffer, m_class, m_id); |
cocorlow | 0:cf7c726ec8a1 | 81 | } |
cocorlow | 0:cf7c726ec8a1 | 82 | sentence_start = 0; |
cocorlow | 0:cf7c726ec8a1 | 83 | sentence_length = 0; |
cocorlow | 0:cf7c726ec8a1 | 84 | sentence_counter = 0; |
cocorlow | 0:cf7c726ec8a1 | 85 | m_class = 0x00; |
cocorlow | 0:cf7c726ec8a1 | 86 | m_id = 0x00; |
cocorlow | 0:cf7c726ec8a1 | 87 | } |
cocorlow | 0:cf7c726ec8a1 | 88 | } |
cocorlow | 0:cf7c726ec8a1 | 89 | |
cocorlow | 1:71f5168e48c8 | 90 | int ss = (read_index+RECEIVE_SIZE-1)%RECEIVE_SIZE; |
cocorlow | 0:cf7c726ec8a1 | 91 | if (c == 0x62 && receive_buffer[ss] == 0xb5) |
cocorlow | 0:cf7c726ec8a1 | 92 | { |
cocorlow | 0:cf7c726ec8a1 | 93 | sentence_start = ss; |
cocorlow | 0:cf7c726ec8a1 | 94 | sentence_counter = 2; |
cocorlow | 0:cf7c726ec8a1 | 95 | } |
cocorlow | 1:71f5168e48c8 | 96 | read_index = (read_index + 1) % RECEIVE_SIZE; |
cocorlow | 0:cf7c726ec8a1 | 97 | } |
cocorlow | 0:cf7c726ec8a1 | 98 | } |
cocorlow | 0:cf7c726ec8a1 | 99 | |
cocorlow | 0:cf7c726ec8a1 | 100 | void GPSUBX_UART::Attach() |
cocorlow | 0:cf7c726ec8a1 | 101 | { |
cocorlow | 0:cf7c726ec8a1 | 102 | serial.attach(this, &GPSUBX_UART::Receive, Serial::RxIrq); |
cocorlow | 0:cf7c726ec8a1 | 103 | } |
cocorlow | 0:cf7c726ec8a1 | 104 | |
cocorlow | 0:cf7c726ec8a1 | 105 | void GPSUBX_UART::Decode(char buffer[], int mc, int mi) |
cocorlow | 0:cf7c726ec8a1 | 106 | { |
cocorlow | 0:cf7c726ec8a1 | 107 | // POSLLH |
cocorlow | 0:cf7c726ec8a1 | 108 | if (mc == 0x01 && mi == 0x02) |
cocorlow | 0:cf7c726ec8a1 | 109 | { |
cocorlow | 0:cf7c726ec8a1 | 110 | POSLLH posllh; |
cocorlow | 0:cf7c726ec8a1 | 111 | for (int i = 0; i < POSLLH_LEN; i++) |
cocorlow | 0:cf7c726ec8a1 | 112 | { |
cocorlow | 0:cf7c726ec8a1 | 113 | posllh.byte_data[i] = buffer[i+6]; |
cocorlow | 0:cf7c726ec8a1 | 114 | } |
cocorlow | 0:cf7c726ec8a1 | 115 | iTOW_POSLLH = posllh.data.iTOW; |
cocorlow | 0:cf7c726ec8a1 | 116 | Longitude = (float)posllh.data.lon * 1e-7f; |
cocorlow | 0:cf7c726ec8a1 | 117 | Latitude = (float)posllh.data.lat * 1e-7f; |
cocorlow | 0:cf7c726ec8a1 | 118 | Height = (float)posllh.data.height / 1000.0f; |
NaotoMorita | 18:ba361d6ab9cd | 119 | Hacc = (float)posllh.data.hAcc / 1000.0f; |
cocorlow | 2:6218fe8e54f4 | 120 | // pc.printf("!%d, %f, %f, %f\r\n", iTOW_POSLLH, Longitude, Latitude, Height); |
cocorlow | 0:cf7c726ec8a1 | 121 | } |
NaotoMorita | 13:facd8e54f2eb | 122 | // STATUS |
NaotoMorita | 13:facd8e54f2eb | 123 | if (mc == 0x01 && mi == 0x03) |
cocorlow | 10:a90d07e4c34d | 124 | { |
NaotoMorita | 13:facd8e54f2eb | 125 | STATUS status; |
NaotoMorita | 13:facd8e54f2eb | 126 | for (int i = 0; i < STATUS_LEN; i++) |
cocorlow | 10:a90d07e4c34d | 127 | { |
NaotoMorita | 14:1ed344c662d2 | 128 | status.byte_data[i] = buffer[i+6]; |
cocorlow | 10:a90d07e4c34d | 129 | } |
NaotoMorita | 14:1ed344c662d2 | 130 | iTOW_STATUS = status.data.iTOW; |
NaotoMorita | 14:1ed344c662d2 | 131 | gpsFix = status.data.gpsFix; |
cocorlow | 10:a90d07e4c34d | 132 | } |
cocorlow | 0:cf7c726ec8a1 | 133 | // TIMEUTC |
cocorlow | 0:cf7c726ec8a1 | 134 | else if (mc == 0x01 && mi == 0x21) |
cocorlow | 0:cf7c726ec8a1 | 135 | { |
cocorlow | 0:cf7c726ec8a1 | 136 | TIMEUTC timeutc; |
cocorlow | 0:cf7c726ec8a1 | 137 | for (int i = 0; i < TIMEUTC_LEN; i++) |
cocorlow | 0:cf7c726ec8a1 | 138 | { |
cocorlow | 0:cf7c726ec8a1 | 139 | timeutc.byte_data[i] = buffer[i+6]; |
cocorlow | 0:cf7c726ec8a1 | 140 | } |
cocorlow | 0:cf7c726ec8a1 | 141 | Year = timeutc.data.year; |
cocorlow | 0:cf7c726ec8a1 | 142 | Month = timeutc.data.month; |
cocorlow | 0:cf7c726ec8a1 | 143 | Day = timeutc.data.day; |
cocorlow | 1:71f5168e48c8 | 144 | Hours = (int)timeutc.data.hour + TimeZone; |
cocorlow | 0:cf7c726ec8a1 | 145 | if (Hours >= 24) |
cocorlow | 0:cf7c726ec8a1 | 146 | { |
cocorlow | 0:cf7c726ec8a1 | 147 | Hours -= 24; |
cocorlow | 0:cf7c726ec8a1 | 148 | Day += 1; |
cocorlow | 0:cf7c726ec8a1 | 149 | } |
cocorlow | 0:cf7c726ec8a1 | 150 | else if (Hours < 0) |
cocorlow | 0:cf7c726ec8a1 | 151 | { |
cocorlow | 0:cf7c726ec8a1 | 152 | Hours += 24; |
cocorlow | 0:cf7c726ec8a1 | 153 | Day -= 1; |
cocorlow | 0:cf7c726ec8a1 | 154 | } |
cocorlow | 0:cf7c726ec8a1 | 155 | Minutes = timeutc.data.min; |
cocorlow | 0:cf7c726ec8a1 | 156 | Seconds = timeutc.data.sec; |
cocorlow | 2:6218fe8e54f4 | 157 | // pc.printf("&%4d/%2d/%2d %2d:%2d %2d\r\n", Year, Month, Day, Hours, Minutes, Seconds); |
cocorlow | 0:cf7c726ec8a1 | 158 | } |
cocorlow | 0:cf7c726ec8a1 | 159 | // VELNED |
cocorlow | 0:cf7c726ec8a1 | 160 | if (mc == 0x01 && mi == 0x12) |
cocorlow | 0:cf7c726ec8a1 | 161 | { |
cocorlow | 0:cf7c726ec8a1 | 162 | VELNED velned; |
cocorlow | 0:cf7c726ec8a1 | 163 | for (int i = 0; i < VELNED_LEN; i++) |
cocorlow | 0:cf7c726ec8a1 | 164 | { |
cocorlow | 0:cf7c726ec8a1 | 165 | velned.byte_data[i] = buffer[i+6]; |
cocorlow | 0:cf7c726ec8a1 | 166 | } |
cocorlow | 0:cf7c726ec8a1 | 167 | iTOW_VELNED = velned.data.iTOW; |
cocorlow | 0:cf7c726ec8a1 | 168 | VelocityNED.x = (float)velned.data.velN / 100.0f; |
cocorlow | 0:cf7c726ec8a1 | 169 | VelocityNED.y = (float)velned.data.velE / 100.0f; |
cocorlow | 0:cf7c726ec8a1 | 170 | VelocityNED.z = (float)velned.data.velD / 100.0f; |
cocorlow | 0:cf7c726ec8a1 | 171 | Speed = (float)velned.data.speed / 100.0f; |
cocorlow | 0:cf7c726ec8a1 | 172 | GroundSpeed = (float)velned.data.gSpeed / 100.0f; |
cocorlow | 0:cf7c726ec8a1 | 173 | Heading = (float)velned.data.heading * 1e-5f; |
cocorlow | 2:6218fe8e54f4 | 174 | // pc.printf("#%d, %f, %f, %f\r\n", iTOW_VELNED, VelocityNED.x, VelocityNED.y, VelocityNED.z); |
cocorlow | 0:cf7c726ec8a1 | 175 | } |
cocorlow | 0:cf7c726ec8a1 | 176 | } |
cocorlow | 0:cf7c726ec8a1 | 177 | |
cocorlow | 0:cf7c726ec8a1 | 178 | Vector3 GPSUBX_UART::ToUniversalUnit() |
cocorlow | 0:cf7c726ec8a1 | 179 | { |
cocorlow | 0:cf7c726ec8a1 | 180 | // 東経180度、北緯0度で精度最大 |
cocorlow | 0:cf7c726ec8a1 | 181 | float pi_2_theta = Latitude * M_PI / 180.0f; |
cocorlow | 0:cf7c726ec8a1 | 182 | float pi_phi = ((Longitude > 0.0f) ? (Longitude - 180.0f) : (Longitude + 180.0f)) * M_PI / 180.0f; |
cocorlow | 0:cf7c726ec8a1 | 183 | float x = - cosf(pi_2_theta) * cosf(pi_phi); |
cocorlow | 0:cf7c726ec8a1 | 184 | float y = - cosf(pi_2_theta) * sinf(pi_phi); |
cocorlow | 0:cf7c726ec8a1 | 185 | float z = sinf(pi_2_theta); |
cocorlow | 0:cf7c726ec8a1 | 186 | Vector3 v(x, y, z); |
NaotoMorita | 12:2ffb2fcaac23 | 187 | |
cocorlow | 0:cf7c726ec8a1 | 188 | return v; |
cocorlow | 0:cf7c726ec8a1 | 189 | } |
cocorlow | 0:cf7c726ec8a1 | 190 | |
cocorlow | 0:cf7c726ec8a1 | 191 | Vector3 GPSUBX_UART::ToUniversal() |
cocorlow | 0:cf7c726ec8a1 | 192 | { |
cocorlow | 0:cf7c726ec8a1 | 193 | Vector3 v = ToUniversalUnit(); |
cocorlow | 0:cf7c726ec8a1 | 194 | return (Radius + Height) * v; |
cocorlow | 0:cf7c726ec8a1 | 195 | } |
cocorlow | 0:cf7c726ec8a1 | 196 | |
cocorlow | 0:cf7c726ec8a1 | 197 | void GPSUBX_UART::CalculateUnit() |
cocorlow | 0:cf7c726ec8a1 | 198 | { |
cocorlow | 0:cf7c726ec8a1 | 199 | Vector3 _d = -1.0f * ToUniversalUnit(); |
cocorlow | 0:cf7c726ec8a1 | 200 | |
cocorlow | 0:cf7c726ec8a1 | 201 | UniversalZeroPosition = -(Radius+Height)*_d; |
cocorlow | 0:cf7c726ec8a1 | 202 | Vector3 _z(0.0f, 0.0f, 1.0f); |
cocorlow | 0:cf7c726ec8a1 | 203 | Vector3 _e = _d * _z; |
cocorlow | 0:cf7c726ec8a1 | 204 | Vector3 _n = _e * _d; |
osaka | 15:e77382079cd9 | 205 | UniversalZeroUnitN = _n / _n.Norm(); |
osaka | 15:e77382079cd9 | 206 | UniversalZeroUnitE = _e / _e.Norm(); |
NaotoMorita | 16:8181909e88f8 | 207 | UniversalZeroUnitD = _d / _d.Norm(); |
NaotoMorita | 12:2ffb2fcaac23 | 208 | //twelite.printf("%f %f %f \r\n",_n,_e,_d); |
cocorlow | 0:cf7c726ec8a1 | 209 | } |
cocorlow | 0:cf7c726ec8a1 | 210 | |
cocorlow | 0:cf7c726ec8a1 | 211 | void GPSUBX_UART::Calculate() |
cocorlow | 0:cf7c726ec8a1 | 212 | { |
cocorlow | 0:cf7c726ec8a1 | 213 | UniversalPosition = ToUniversal(); |
cocorlow | 0:cf7c726ec8a1 | 214 | Vector3 relative = UniversalPosition - UniversalZeroPosition; |
cocorlow | 0:cf7c726ec8a1 | 215 | Vector3 _position(relative % UniversalZeroUnitN, relative % UniversalZeroUnitE, relative % UniversalZeroUnitD); |
cocorlow | 0:cf7c726ec8a1 | 216 | PositionNED = _position; |
cocorlow | 0:cf7c726ec8a1 | 217 | } |
cocorlow | 0:cf7c726ec8a1 | 218 | |
cocorlow | 0:cf7c726ec8a1 | 219 | Vector3 GPSUBX_UART::Calculate(Vector3 position) |
cocorlow | 0:cf7c726ec8a1 | 220 | { |
cocorlow | 0:cf7c726ec8a1 | 221 | UniversalPosition = ToUniversal(); |
cocorlow | 0:cf7c726ec8a1 | 222 | Vector3 relative = position - UniversalZeroPosition; |
cocorlow | 0:cf7c726ec8a1 | 223 | Vector3 _position(relative % UniversalZeroUnitN, relative % UniversalZeroUnitE, relative % UniversalZeroUnitD); |
cocorlow | 0:cf7c726ec8a1 | 224 | return _position; |
cocorlow | 0:cf7c726ec8a1 | 225 | } |