-
Notifications
You must be signed in to change notification settings - Fork 10
/
DroneNavigationState.m
483 lines (449 loc) · 12.2 KB
/
DroneNavigationState.m
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//
// DroneNavigationState.m
// ARDrone
//
// Created by Daniel Eggert on 29/12/2013.
// Copyright (c) 2013 Chris Eidhof. All rights reserved.
//
#import "DroneNavigationState.h"
/// These are defined in navdata_keys.h
typedef enum DroneNavigationTags_e : int32_t {
NAVDATA_DEMO_TAG = 0,
NAVDATA_TIME_TAG = 1,
NAVDATA_RAW_MEASURES_TAG = 2,
NAVDATA_PHYS_MEASURES_TAG = 3,
NAVDATA_GYROS_OFFSETS_TAG = 4,
NAVDATA_EULER_ANGLES_TAG = 5,
NAVDATA_REFERENCES_TAG = 6,
NAVDATA_TRIMS_TAG = 7,
NAVDATA_RC_REFERENCES_TAG = 8,
NAVDATA_PWM_TAG = 9,
NAVDATA_ALTITUDE_TAG = 10,
NAVDATA_VISION_RAW_TAG = 11,
NAVDATA_VISION_OF_TAG = 12,
NAVDATA_VISION_TAG = 13,
NAVDATA_VISION_PERF_TAG = 14,
NAVDATA_TRACKERS_SEND_TAG = 15,
NAVDATA_VISION_DETECT_TAG = 16,
NAVDATA_WATCHDOG_TAG = 17,
NAVDATA_ADC_DATA_FRAME_TAG = 18,
NAVDATA_VIDEO_STREAM_TAG = 19,
NAVDATA_GAMES_TAG = 20,
NAVDATA_PRESSURE_RAW_TAG = 21,
NAVDATA_MAGNETO_TAG = 22,
NAVDATA_WIND_TAG = 23,
NAVDATA_KALMAN_PRESSURE_TAG = 24,
NAVDATA_HDVIDEO_STREAM_TAG = 25,
NAVDATA_WIFI_TAG = 26,
NAVDATA_CKS_TAG = 0xFFFF,
} DroneNavigationTags;
// C.f. <https://projects.ardrone.org/embedded/ardrone-api/d6/dfd/struct__navdata__demo__t.html>
// With @c tag and @c size stripped
typedef struct __attribute__((packed)) {
uint32_t controlState;
uint32_t batteryLevel;
float pitch;
float roll;
float yaw;
uint32_t altitude;
float speedX;
float speedY;
float speedZ;
} DroneNavigationReadouts;
uint32_t const WiFiLinkQualityUnknown = 0xffff;
@interface DroneNavigationState ()
@property (nonatomic) uint32_t state;
@property (nonatomic) uint32_t sequenceNumber;
@property (nonatomic) DroneNavigationReadouts readouts;
@property (nonatomic) BOOL hasReadouts;
@end
@implementation DroneNavigationState
- (id)init
{
self = [super init];
if (self) {
_WiFiLinkQuality = WiFiLinkQualityUnknown;
}
return self;
}
+ (instancetype)stateFromNavigationData:(NSData *)data;
{
DroneNavigationState *state = [[self alloc] init];
if (! [state parseData:data]) {
return nil;
}
return state;
}
- (BOOL)parseData:(NSData *)data;
{
if ([data length] < sizeof(int32_t)) {
NSLog(@"Invalid navigation data (too short). Ignoring.");
return NO;
}
NSUInteger loc = 0;
uint32_t header = 0;
static uint32_t const magicHeaderValue = 0x55667788;
[data getBytes:&header range:NSMakeRange(loc, sizeof(header))];
loc += sizeof(header);
header = NSSwapLittleIntToHost(header);
if (header != magicHeaderValue) {
NSLog(@"Invalid navigation data (header value doesn't match). Ignoring.");
return NO;
}
uint32_t state = 0;
[data getBytes:&state range:NSMakeRange(loc, sizeof(state))];
loc += sizeof(state);
state = NSSwapLittleIntToHost(state);
self.state = state;
uint32_t sequence = 0;
[data getBytes:&sequence range:NSMakeRange(loc, sizeof(sequence))];
loc += sizeof(sequence);
sequence = NSSwapLittleIntToHost(sequence);
self.sequenceNumber = sequence;
// Skip 'vision tag'
loc += 4;
while (loc + (2 * sizeof(uint16_t)) <= [data length]) {
uint16_t option_tag = 0;
uint16_t option_length = 0;
[data getBytes:&option_tag range:NSMakeRange(loc, sizeof(option_tag))];
loc += sizeof(option_tag);
option_tag = NSSwapLittleShortToHost(option_tag);
[data getBytes:&option_length range:NSMakeRange(loc, sizeof(option_length))];
loc += sizeof(option_length);
option_length = NSSwapLittleShortToHost(option_length);
// The data structures found inside these are defined inside navdata_common.h
DroneNavigationTags tag = option_tag;
switch (tag) {
case NAVDATA_DEMO_TAG: {
DroneNavigationReadouts readouts = {};
if (sizeof(readouts) <= option_length) {
[data getBytes:&readouts range:NSMakeRange(loc, sizeof(readouts))];
// TODO: swap little to host
self.readouts = readouts;
self.hasReadouts = YES;
}
break;
}
case NAVDATA_WIFI_TAG: {
uint32_t linkQuality = 0;
[data getBytes:&linkQuality range:NSMakeRange(loc, sizeof(linkQuality))];
_WiFiLinkQuality = linkQuality;
break;
}
default:
break;
}
loc += option_length - 4;
}
return YES;
}
- (NSString *)description;
{
NSMutableString *description = [NSMutableString string];
[description appendFormat:@"%x ", self.sequenceNumber];
switch (self.controlState) {
case DroneControlStateDefault:
[description appendString:@"state = Default"];
break;
case DroneControlStateInit:
[description appendString:@"state = Init"];
break;
case DroneControlStateLanded:
[description appendString:@"state = Landed"];
break;
case DroneControlStateFlying:
[description appendString:@"state = Flying"];
break;
case DroneControlStateHovering:
[description appendString:@"state = Hovering"];
break;
case DroneControlStateTest:
[description appendString:@"state = Test"];
break;
case DroneControlStateTakeoff:
[description appendString:@"state = Takeoff"];
break;
case DroneControlStateGotoFix:
[description appendString:@"state = GotoFix"];
break;
case DroneControlStateLanding:
[description appendString:@"state = Landing"];
break;
case DroneControlStateInvalid:
default:
break;
}
NSMutableArray *flags = [NSMutableArray array];
if (self.flying) {
[flags addObject:@"flying"];
}
if (self.videoEnabled) {
[flags addObject:@"videoEnabled"];
}
if (self.visionEnabled) {
[flags addObject:@"visionEnabled"];
}
if (self.altitudeControlActive) {
[flags addObject:@"altitudeControlActive"];
}
if (self.userFeedbackOn) {
[flags addObject:@"userFeedbackOn"];
}
if (self.controlReceived) {
[flags addObject:@"controlReceived"];
}
if (self.trimReceived) {
[flags addObject:@"trimReceived"];
}
if (self.trimRunning) {
[flags addObject:@"trimRunning"];
}
if (self.trimSucceeded) {
[flags addObject:@"trimSucceeded"];
}
if (self.navDataDemoOnly) {
[flags addObject:@"navDataDemoOnly"];
}
if (self.navDataBootstrap) {
[flags addObject:@"navDataBootstrap"];
}
if (self.motorsDown) {
[flags addObject:@"motorsDown"];
}
if (self.gyrometersDown) {
[flags addObject:@"gyrometersDown"];
}
if (self.batteryTooLow) {
[flags addObject:@"batteryTooLow"];
}
if (self.batteryTooHigh) {
[flags addObject:@"batteryTooHigh"];
}
if (self.timerElapsed) {
[flags addObject:@"timerElapsed"];
}
if (self.notEnoughPower) {
[flags addObject:@"notEnoughPower"];
}
if (self.angelsOutOufRange) {
[flags addObject:@"angelsOutOufRange"];
}
if (self.tooMuchWind) {
[flags addObject:@"tooMuchWind"];
}
if (self.ultrasonicSensorDeaf) {
[flags addObject:@"ultrasonicSensorDeaf"];
}
if (self.cutoutSystemDetected) {
[flags addObject:@"cutoutSystemDetected"];
}
if (self.PICVersionNumberOK) {
[flags addObject:@"PICVersionNumberOK"];
}
if (self.ATCodedThreadOn) {
[flags addObject:@"ATCodedThreadOn"];
}
if (self.navDataThreadOn) {
[flags addObject:@"navDataThreadOn"];
}
if (self.videoThreadOn) {
[flags addObject:@"videoThreadOn"];
}
if (self.acquisitionThreadOn) {
[flags addObject:@"acquisitionThreadOn"];
}
if (self.controlWatchdogDelayed) {
[flags addObject:@"controlWatchdogDelayed"];
}
if (self.ADCWatchdogDelayed) {
[flags addObject:@"ADCWatchdogDelayed"];
}
if (self.communicationProblemOccurred) {
[flags addObject:@"communicationProblemOccurred"];
}
if (self.emergency) {
[flags addObject:@"emergency"];
}
[description appendFormat:@", flags = [%@]", [flags componentsJoinedByString:@", "]];
if (self.controlAlgorithm == EulerAnglesControlAlgorithm) {
[description appendString:@", control = EulerAngles"];
} else if (self.controlAlgorithm == AugularSpeedControlAlgorithm) {
[description appendString:@", control = AugularSpeed"];
}
if (self.WiFiLinkQuality != WiFiLinkQualityUnknown) {
[description appendFormat:@"WiFi Link: %u", self.WiFiLinkQuality];
}
if (self.hasReadouts) {
[description appendFormat:@", battery = %u", self.batteryLevel];
[description appendFormat:@", pitch = %g", self.pitch];
[description appendFormat:@", roll = %g", self.roll];
[description appendFormat:@", yaw = %g", self.yaw];
[description appendFormat:@", altitude = %g", self.altitude];
[description appendFormat:@", speed = %g / %g / %g", self.speedX, self.speedY, self.speedZ];
}
return description;
}
- (BOOL)flying;
{
return (self.state & 1) != 0;
}
- (BOOL)videoEnabled;
{
return (self.state & (1 << 1)) != 0;
}
- (BOOL)visionEnabled;
{
return (self.state & (1 << 2)) != 0;
}
- (DroneControlAlgorithm)controlAlgorithm;
{
return (self.state & (1 << 3)) != 0 ? AugularSpeedControlAlgorithm : EulerAnglesControlAlgorithm;
}
- (BOOL)altitudeControlActive;
{
return (self.state & (1 << 4)) != 0;
}
- (BOOL)userFeedbackOn;
{
return (self.state & (1 << 5)) != 0;
}
- (BOOL)controlReceived;
{
return (self.state & (1 << 6)) != 0;
}
- (BOOL)trimReceived;
{
return (self.state & (1 << 7)) != 0;
}
- (BOOL)trimRunning;
{
return (self.state & (1 << 8)) != 0;
}
- (BOOL)trimSucceeded;
{
return (self.state & (1 << 9)) != 0;
}
- (BOOL)navDataDemoOnly;
{
return (self.state & (1 << 10)) != 0;
}
- (BOOL)navDataBootstrap;
{
return (self.state & (1 << 11)) != 0;
}
- (BOOL)motorsDown;
{
return (self.state & (1 << 12)) != 0;
}
- (BOOL)gyrometersDown;
{
return (self.state & (1 << 14)) != 0;
}
- (BOOL)batteryTooLow;
{
return (self.state & (1 << 15)) != 0;
}
- (BOOL)batteryTooHigh;
{
return (self.state & (1 << 16)) != 0;
}
- (BOOL)timerElapsed;
{
return (self.state & (1 << 17)) != 0;
}
- (BOOL)notEnoughPower;
{
return (self.state & (1 << 18)) != 0;
}
- (BOOL)angelsOutOufRange;
{
return (self.state & (1 << 19)) != 0;
}
- (BOOL)tooMuchWind;
{
return (self.state & (1 << 20)) != 0;
}
- (BOOL)ultrasonicSensorDeaf;
{
return (self.state & (1 << 21)) != 0;
}
- (BOOL)cutoutSystemDetected;
{
return (self.state & (1 << 22)) != 0;
}
- (BOOL)PICVersionNumberOK;
{
return (self.state & (1 << 23)) != 0;
}
- (BOOL)ATCodedThreadOn;
{
return (self.state & (1 << 24)) != 0;
}
- (BOOL)navDataThreadOn;
{
return (self.state & (1 << 25)) != 0;
}
- (BOOL)videoThreadOn;
{
return (self.state & (1 << 26)) != 0;
}
- (BOOL)acquisitionThreadOn;
{
return (self.state & (1 << 27)) != 0;
}
- (BOOL)controlWatchdogDelayed;
{
return (self.state & (1 << 28)) != 0;
}
- (BOOL)ADCWatchdogDelayed;
{
return (self.state & (1 << 29)) != 0;
}
- (BOOL)communicationProblemOccurred;
{
return (self.state & (1 << 30)) != 0;
}
- (BOOL)emergency;
{
return (self.state & (1 << 31)) != 0;
}
- (DroneControlState)controlState
{
if (! self.hasReadouts) {
return DroneControlStateInvalid;
}
return (DroneControlState) ((self.readouts.controlState >> 16) + 1);
}
- (uint32_t)batteryLevel;
{
return self.readouts.batteryLevel;
}
- (double)pitch;
{
return self.readouts.pitch * 0.001;
}
- (double)roll;
{
return self.readouts.roll * 0.001;
}
- (double)yaw;
{
return self.readouts.yaw * 0.001;
}
- (double)altitude;
{
return self.readouts.altitude * 0.001;
}
- (double)speedX;
{
return self.readouts.speedX;
}
- (double)speedY;
{
return self.readouts.speedY;
}
- (double)speedZ;
{
return self.readouts.speedZ;
}
@end