-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNazaDecoder.cpp
More file actions
250 lines (216 loc) · 7.31 KB
/
NazaDecoder.cpp
File metadata and controls
250 lines (216 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <Arduino.h>
#include "NazaDecoder.h"
NazaDecoder::NazaDecoder()
: sequence(0), count(0), messageId(0), messageLength(0), checksum1(0), checksum2(0), magXMin(0), magXMax(0), magYMin(0), magYMax(0), longitude(0), latitude(0), altitude(0), speed(0), fix(NO_FIX), satellites(0), heading(0), courseOverGround(0), verticalSpeedIndicator(0), horizontalDilutionOfPrecision(
0), verticalDilutionOfPrecision(0), year(0), month(0), day(0), hour(0), minute(0), second(0), lastLock(0), locked(0) {
}
double NazaDecoder::getLatitude() {
return latitude;
}
double NazaDecoder::getLongitude() {
return longitude;
}
double NazaDecoder::getAltitude() {
return altitude;
}
double NazaDecoder::getSpeed() {
return speed;
}
NazaDecoder::FixType NazaDecoder::getFixType() {
return fix;
}
uint8_t NazaDecoder::getSatellites() {
return satellites;
}
double NazaDecoder::getHeading() {
return heading;
}
double NazaDecoder::getCourseOverGround() {
return courseOverGround;
}
double NazaDecoder::getVerticalSpeedIndicator() {
return verticalSpeedIndicator;
}
double NazaDecoder::getHorizontalDilutionOfPrecision() {
return horizontalDilutionOfPrecision;
}
double NazaDecoder::getVerticalDilutionOfPrecision() {
return verticalDilutionOfPrecision;
}
uint8_t NazaDecoder::getYear() {
return year;
}
uint8_t NazaDecoder::getMonth() {
return month;
}
uint8_t NazaDecoder::getDay() {
return day;
}
uint8_t NazaDecoder::getHour() {
return hour;
}
uint8_t NazaDecoder::getMinute() {
return minute;
}
uint8_t NazaDecoder::getSecond() {
return second;
}
NazaDecoder::VersionType NazaDecoder::getFirmwareVersion() {
return firmwareVersion;
}
NazaDecoder::VersionType NazaDecoder::getHardwareVersion() {
return hardwareVersion;
}
uint8_t NazaDecoder::isLocked() {
return locked;
}
uint8_t NazaDecoder::decode(int16_t input) {
// header (part 1 - 0x55)
if ((sequence == 0) && (input == 0x55)) {
sequence++;
}
// header (part 2 - 0xaa)
else if ((sequence == 1) && (input == 0xaa)) {
checksum1 = 0;
checksum2 = 0;
sequence++;
} else if (sequence == 2) {
messageId = input;
updateChecksum(input);
sequence++;
}
// message id
// message payload length (should match message id)
// store payload in buffer
else if ((sequence == 3)
&& (((messageId == NAZA_MESSAGE_GPS_TYPE) && (input == NAZA_MESSAGE_GPS_SIZE)) || ((messageId == NAZA_MESSAGE_MAGNETOMETER_TYPE) && (input == NAZA_MESSAGE_MAGNETOMETER_SIZE)) || ((messageId == NAZA_MESSAGE_MODULE_VERSION_TYPE) && (input == NAZA_MESSAGE_MODULE_VERSION_SIZE)))) {
messageLength = input;
count = 0;
updateChecksum(input);
sequence++;
} else if (sequence == 4) {
payload[count++] = input;
updateChecksum(input);
if (count >= messageLength) {
sequence++;
}
}
// verify checksum #1
else if ((sequence == 5) && (input == checksum1)) {
sequence++;
}
// verify checksum #2
else if ((sequence == 6) && (input == checksum2)) {
sequence++;
} else {
sequence = 0;
}
// all data in buffer
if (sequence == 7) {
sequence = 0;
// Decode GPS data
if (messageId == NAZA_MESSAGE_GPS_TYPE) {
uint8_t mask = payload[NAZA_MESSAGE_POS_XM];
uint32_t time = pack4(NAZA_MESSAGE_POS_DT, mask);
second = time & 0x3f;
time >>= 6;
minute = time & 0x3f;
time >>= 6;
hour = time & 0x0f;
time >>= 4;
day = time & 0x1f;
time >>= 5;
if (hour > 7) {
day++;
}
month = time & 0x0f;
time >>= 4;
year = time & 0x7f;
longitude = (double) pack4(NAZA_MESSAGE_POS_LO, mask) / 10000000;
latitude = (double) pack4(NAZA_MESSAGE_POS_LA, mask) / 10000000;
altitude = (double) pack4(NAZA_MESSAGE_POS_AL, mask) / 1000;
double northVelocity = (double) pack4(NAZA_MESSAGE_POS_NV, mask) / 100;
double eastVelocity = (double) pack4(NAZA_MESSAGE_POS_EV, mask) / 100;
speed = sqrt(northVelocity * northVelocity + eastVelocity * eastVelocity);
courseOverGround = atan2(eastVelocity, northVelocity) * 180.0 / M_PI;
if (courseOverGround < 0) {
courseOverGround += 360.0;
}
verticalSpeedIndicator = -(double) pack4(NAZA_MESSAGE_POS_DV, mask) / 100;
verticalDilutionOfPrecision = (double) pack2(NAZA_MESSAGE_POS_VD, mask) / 100;
double ndop = (double) pack2(NAZA_MESSAGE_POS_ND, mask) / 100;
double edop = (double) pack2(NAZA_MESSAGE_POS_ED, mask) / 100;
horizontalDilutionOfPrecision = sqrt(ndop * ndop + edop * edop);
satellites = payload[NAZA_MESSAGE_POS_NS];
uint8_t fixType = payload[NAZA_MESSAGE_POS_FT] ^ mask;
uint8_t fixFlags = payload[NAZA_MESSAGE_POS_SF] ^ mask;
switch (fixType) {
case 2:
fix = FIX_2D;
break;
case 3:
fix = FIX_3D;
break;
default:
fix = NO_FIX;
break;
}
if ((fix != NO_FIX) && (fixFlags & 0x02)) {
fix = FIX_DGPS;
}
uint16_t lock = pack2(NAZA_MESSAGE_POS_SN, 0x00);
locked = (lock == lastLock + 1);
lastLock = lock;
}
// Decode magnetometer data (not tilt compensated)
// To calculate the heading (not tilt compensated) you need to do atan2 on the resulting y any a values, convert radians to degrees and add 360 if the result is negative.
else if (messageId == NAZA_MESSAGE_MAGNETOMETER_TYPE) {
uint8_t mask = payload[4];
mask = (((mask ^ (mask >> 4)) & 0x0F) | ((mask << 3) & 0xF0)) ^ (((mask & 0x01) << 3) | ((mask & 0x01) << 7));
int16_t x = pack2(NAZA_MESSAGE_POS_CX, mask);
int16_t y = pack2(NAZA_MESSAGE_POS_CY, mask);
if (x > magXMax) {
magXMax = x;
}
if (x < magXMin) {
magXMin = x;
}
if (y > magYMax) {
magYMax = y;
}
if (y < magYMin) {
magYMin = y;
}
heading = computeVectorAngle(y - ((magYMax + magYMin) / 2), x - ((magXMax + magXMin) / 2));
} else if (messageId == NAZA_MESSAGE_MODULE_VERSION_TYPE) {
firmwareVersion.version = pack4(NAZA_MESSAGE_POS_FW, 0x00);
hardwareVersion.version = pack4(NAZA_MESSAGE_POS_HW, 0x00);
}
return messageId;
} else {
return NAZA_MESSAGE_NONE_TYPE;
}
}
void NazaDecoder::updateChecksum(int16_t input) {
checksum1 += input;
checksum2 += checksum1;
}
int32_t NazaDecoder::pack4(uint8_t i, uint8_t mask) {
union {
uint32_t d;
uint8_t b[4];
} v;
for (int j = 0; j < 4; j++)
v.b[j] = payload[i + j] ^ mask;
return v.d;
}
int16_t NazaDecoder::pack2(uint8_t i, uint8_t mask) {
union {
uint16_t d;
uint8_t b[2];
} v;
for (int j = 0; j < 2; j++) {
v.b[j] = payload[i + j] ^ mask;
}
return v.d;
}