forked from Parrot-Developers/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniDroneDeviceController.m
More file actions
590 lines (501 loc) · 23 KB
/
MiniDroneDeviceController.m
File metadata and controls
590 lines (501 loc) · 23 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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
Copyright (C) 2014 Parrot SA
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Parrot nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
//
// MiniDroneDeviceController.m
// ARFreeFlight
//
// Created by Hugo Grostabussiat on 09/12/2013.
// Copyright (c) 2013 Parrot SA. All rights reserved.
//
#import <libARSAL/ARSAL.h>
#import <libARCommands/ARCommands.h>
#import "MiniDroneDeviceController+libARCommands.h"
#import "MiniDroneDeviceController+libARCommandsDebug.h"
#import "MiniDroneARNetworkConfig.h"
#import "DeviceControllerProtected.h"
#import "MiniDronePhotoRecordController.h"
NSString* MiniDroneDeviceControllerFlyingStateChangedNotification = @"MiniDroneDeviceControllerFlyingStateChangedNotification";
NSString* MiniDroneDeviceControllerEmergencyStateChangedNotification = @"MiniDroneDeviceControllerEmergencyStateChangedNotification";
NSString* MiniDroneDeviceControllerDebug1ReceivedNotification = @"MiniDroneDeviceControllerDebug1ReceivedNotification";
NSString* MiniDroneDeviceControllerDebug2ReceivedNotification = @"MiniDroneDeviceControllerDebug2ReceivedNotification";
NSString* MiniDroneDeviceControllerDebug3ReceivedNotification = @"MiniDroneDeviceControllerDebug3ReceivedNotification";
static const char* TAG = "MiniDroneDeviceController";
static const NSTimeInterval LOOP_INTERVAL = 0.05;
/* Used for ARNetwork notification data. */
typedef enum ARNetworkSendMetadataType {
kARNetworkSendMetadataTypeTest1,
kARNetworkSendMetadataTypeTest2,
kARNetworkSendMetadataTypeTest3,
} eARNetworkSendMetadataType;
typedef struct
{
MiniDronePilotingData_t pilotingData;
} MiniDroneData_t;
@interface MiniDroneDeviceController () <ARNetworkSendStatusDelegate>
@property (atomic, readonly) eDEVICE_CONTROLLER_STATE state;
@property (nonatomic) NSRecursiveLock *stateLock;
@property (nonatomic) MiniDroneData_t droneState; // Current MiniDrone state. Lock before use.
@property (nonatomic) NSRecursiveLock* droneStateLock; // Lock for the MiniDrone state.
@property (atomic) BOOL startCancelled;
@property (nonatomic) BOOL initialSettingsReceived;
@property (nonatomic) NSCondition *initialSettingsReceivedCondition;
@property (nonatomic) BOOL initialStatesReceived;
@property (nonatomic) NSCondition *initialStatesReceivedCondition;
@property (atomic) BOOL isInPause;
@property (atomic) NSTimeInterval currentLoopInterval;
@end
@implementation MiniDroneDeviceController
- (id)initWithService:(ARService*)service;
{
MiniDroneARNetworkConfig* netConfig = [[MiniDroneARNetworkConfig alloc] init];
self = [super initWithARNetworkConfig:netConfig withARService:service withBridgeDeviceController:nil withLoopInterval:LOOP_INTERVAL];
if (self != nil) {
_stateLock = [[NSRecursiveLock alloc] init];
_state = DEVICE_CONTROLLER_STATE_STOPPED;
_droneStateLock = [[NSRecursiveLock alloc] init];
_startCancelled = NO;
_initialSettingsReceived = NO;
_initialSettingsReceivedCondition = [[NSCondition alloc] init];
_initialStatesReceived = NO;
_initialStatesReceivedCondition = [[NSCondition alloc] init];
}
return self;
}
- (void)start
{
[_stateLock lock];
if (_state == DEVICE_CONTROLLER_STATE_STOPPED)
{
_state = DEVICE_CONTROLLER_STATE_STARTING;
_startCancelled = NO;
_initialSettingsReceived = NO;
_initialStatesReceived = NO;
/* Asynchronously start the base controller. */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:DeviceControllerWillStartNotification object:self];
BOOL failed = NO;
if ([self privateStart] == NO)
{
ARSAL_PRINT(ARSAL_PRINT_ERROR, TAG, "Failed to start the controller.");
failed = YES;
}
if (!failed && !_startCancelled)
{
/* Go to the STARTED state and notify. */
[_stateLock lock];
_state = DEVICE_CONTROLLER_STATE_STARTED;
[[NSNotificationCenter defaultCenter] postNotificationName:DeviceControllerDidStartNotification object:self];
[_stateLock unlock];
}
else
{
/* We failed to start. Go to the STOPPING state and stop in the background. */
[_stateLock lock];
_state = DEVICE_CONTROLLER_STATE_STOPPING;
if (failed) {
[[NSNotificationCenter defaultCenter] postNotificationName:DeviceControllerDidFailNotification object:self];
} // No else: Do not send failure notification for a cancelled start.
[_stateLock unlock];
[self privateStop];
/* Go to the STOPPED state and notify. */
[_stateLock lock];
_state = DEVICE_CONTROLLER_STATE_STOPPED;
[[NSNotificationCenter defaultCenter] postNotificationName:DeviceControllerDidStopNotification object:self];
[_stateLock unlock];
}
});
}
[_stateLock unlock];
}
- (void)stop
{
[_stateLock lock];
if (_state == DEVICE_CONTROLLER_STATE_STARTED)
{
/* Go to the stopping state. */
_state = DEVICE_CONTROLLER_STATE_STOPPING;
/* Do the actual stop process in the background. */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:DeviceControllerWillStopNotification object:self];
[[NSNotificationCenter defaultCenter] removeObserver:self name:DeviceControllerSettingsStateAllSettingsChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:DeviceControllerCommonStateAllStatesChangedNotification object:nil];
/* Perform the actual stop. */
[self privateStop];
/* Go to the STOPPED state and notify. */
[_stateLock lock];
_state = DEVICE_CONTROLLER_STATE_STOPPED;
[[NSNotificationCenter defaultCenter] postNotificationName:DeviceControllerDidStopNotification object:self];
[_stateLock unlock];
});
}
else if (_state == DEVICE_CONTROLLER_STATE_STARTING && !_startCancelled)
{
/* Go to the stopping state and request cancellation. */
_state = DEVICE_CONTROLLER_STATE_STOPPING;
_startCancelled = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:DeviceControllerWillStopNotification object:self];
[[NSNotificationCenter defaultCenter] removeObserver:self name:DeviceControllerSettingsStateAllSettingsChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:DeviceControllerCommonStateAllStatesChangedNotification object:nil];
[super cancelBaseControllerStart];
[_initialSettingsReceivedCondition lock];
[_initialSettingsReceivedCondition signal];
[_initialSettingsReceivedCondition unlock];
[_initialStatesReceivedCondition lock];
[_initialStatesReceivedCondition signal];
[_initialStatesReceivedCondition unlock];
});
}
[_stateLock unlock];
}
- (void)pause:(BOOL)pause
{
[_stateLock lock];
_isInPause = pause;
[_stateLock unlock];
}
- (void)looperThreadRoutine:(id)userData
{
NSTimeInterval lastInterval = [NSDate timeIntervalSinceReferenceDate];
_currentLoopInterval = self.loopInterval;
while (![NSThread currentThread].isCancelled)
{
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceReferenceDate:lastInterval + self.currentLoopInterval]];
lastInterval = [NSDate timeIntervalSinceReferenceDate];
[self controllerLoop];
}
}
- (void)controllerLoop
{
eDEVICE_CONTROLLER_STATE currentState;
MiniDroneData_t localState;
BOOL isRunning = NO;
[_stateLock lock];
currentState = _state;
isRunning = !_isInPause;
[_stateLock unlock];
if (isRunning)
{
switch(currentState)
{
case DEVICE_CONTROLLER_STATE_STARTED:
// Make a copy of the drone state.
[_droneStateLock lock];
localState = _droneState;
[_droneStateLock unlock];
/*NSLog(@"====> %d, %d, %d, %d, %d, %f", (uint8_t)(localState.pilotingData.active ? 1 : 0),
(int8_t)(localState.pilotingData.roll * 100.f),
(int8_t)(localState.pilotingData.pitch * 100.f),
(int8_t)(localState.pilotingData.yaw * 100.f),
(int8_t)(localState.pilotingData.gaz * 100.f),
localState.pilotingData.heading);*/
[self MiniDroneDeviceController_SendPilotingPCMD:[MiniDroneARNetworkConfig c2dNackId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withFlag:(uint8_t)(localState.pilotingData.active ? 1 : 0) withRoll:(int8_t)(localState.pilotingData.roll * 100.f) withPitch:(int8_t)(localState.pilotingData.pitch * 100.f) withYaw:(int8_t)(localState.pilotingData.yaw * 100.f) withGaz:(int8_t)(localState.pilotingData.gaz * 100.f) withPsi:localState.pilotingData.heading];
break;
case DEVICE_CONTROLLER_STATE_STOPPING:
case DEVICE_CONTROLLER_STATE_STARTING:
case DEVICE_CONTROLLER_STATE_STOPPED:
default:
// DO NOT SEND DATA
break;
}
}
}
#pragma mark - HUD-called methods.
- (void)userCommandsActivationChanged:(BOOL)activated
{
[_droneStateLock lock];
_droneState.pilotingData.active = activated;
[_droneStateLock unlock];
}
- (void)userGazChanged:(float)gaz
{
[_droneStateLock lock];
_droneState.pilotingData.gaz = gaz;
[_droneStateLock unlock];
}
- (void)userPitchChanged:(float)pitch
{
[_droneStateLock lock];
_droneState.pilotingData.pitch = pitch;
[_droneStateLock unlock];
}
- (void)userRollChanged:(float)roll
{
[_droneStateLock lock];
_droneState.pilotingData.roll = roll;
[_droneStateLock unlock];
}
- (void)userYawChanged:(float)yaw
{
[_droneStateLock lock];
_droneState.pilotingData.yaw = yaw;
[_droneStateLock unlock];
}
- (void)userHeadingChanged:(float)heading
{
[_droneStateLock lock];
_droneState.pilotingData.heading = heading;
[_droneStateLock unlock];
}
- (void)userRequestedEmergency
{
// Send the emergency command
[self MiniDroneDeviceController_SendPilotingEmergency:[MiniDroneARNetworkConfig c2dEmergencyId] withSendPolicy:ARNETWORK_SEND_POLICY_RETRY withCompletionBlock:nil];
}
- (void)userRequestedTakeOff
{
// Send the emergency command
[self MiniDroneDeviceController_SendPilotingTakeOff:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil];
}
- (void)userRequestedLanding
{
// Send the emergency command
[self MiniDroneDeviceController_SendPilotingLanding:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil];
}
- (void)userRequestFlip:(eARCOMMANDS_MINIDRONE_ANIMATIONS_FLIP_DIRECTION)flipDirection
{
[self MiniDroneDeviceController_SendAnimationsFlip:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withDirection:flipDirection];
}
- (void)userRequestCap:(int16_t)offset
{
[self MiniDroneDeviceController_SendAnimationsCap:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withOffset:offset];
}
- (void)userRequestedFlatTrim
{
[self MiniDroneDeviceController_SendPilotingFlatTrim:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil];
}
- (void)userRequestedRecordPicture:(int)massStorageId
{
[self MiniDroneDeviceController_SendMediaRecordPicture:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withMass_storage_id:massStorageId];
}
- (void)userRequestSetAutoTakeOffMode:(uint8_t)state
{
[self MiniDroneDeviceController_SendPilotingAutoTakeOffMode:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withState:state];
}
- (BOOL)userSetDebug1Value:(int8_t)value
{
BOOL result = [self MiniDroneDeviceController_SendDebugTest1:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_RETRY withCompletionBlock:nil withT1Args:value];
[self postDebug1AckNotification];
return result;
}
- (BOOL)userSetDebug2Value:(int8_t)value
{
BOOL result = [self MiniDroneDeviceController_SendDebugTest2:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_RETRY withCompletionBlock:nil withT2Args:value];
[self postDebug2AckNotification];
return result;
}
- (BOOL)userSetDebug3Value:(int8_t)value
{
BOOL result = [self MiniDroneDeviceController_SendDebugTest3:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_RETRY withCompletionBlock:nil withT3Args:value];
[self postDebug1AckNotification];
return result;
}
- (void)userRequestedPilotingSettingsMaxAltitude:(float)maxAltitude
{
[self MiniDroneDeviceController_SendPilotingSettingsMaxAltitude:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withCurrent:maxAltitude];
}
- (void)userRequestedPilotingSettingsMaxTilt:(float)maxTilt
{
[self MiniDroneDeviceController_SendPilotingSettingsMaxTilt:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withCurrent:maxTilt];
}
- (void)userRequestedSpeedSettingsMaxRotationSpeed:(float)maxRotationSpeed
{
[self MiniDroneDeviceController_SendSpeedSettingsMaxRotationSpeed:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withCurrent:maxRotationSpeed];
}
- (void)userRequestedSpeedSettingsMaxVerticalSpeed:(float)maxVerticalSpeed
{
[self MiniDroneDeviceController_SendSpeedSettingsMaxVerticalSpeed:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withCurrent:maxVerticalSpeed];
}
- (void)userRequestedSpeedSettingsWheels:(BOOL)present
{
[self MiniDroneDeviceController_SendSpeedSettingsWheels:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withPresent:present ? 1 : 0];
}
- (void)userRequestedSpeedSettingsCutOut:(BOOL)enable;
{
[self MiniDroneDeviceController_SendSettingsCutOutMode:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withEnable:enable ? 1 : 0];
}
- (void)userRequestedSettingsReset
{
[self DeviceController_SendSettingsReset:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil];
}
- (void)userRequestedSettingsProductName:(NSString *)productName
{
[self DeviceController_SendSettingsProductName:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil withName:(char *)[productName cStringUsingEncoding:NSUTF8StringEncoding]];
}
- (void)userRequestedReboot
{
[self DeviceController_SendCommonReboot:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_DROP withCompletionBlock:nil];
}
// The code to send the command is not autogenerated, so send it manually.
- (void)userEnteredPilotingHud:(BOOL)inHud
{
u_int8_t cmdbuf[128];
int32_t actualSize = 0;
eARCOMMANDS_GENERATOR_ERROR cmdError;
BOOL sentStatus;
// Send isPilotingChanged command
sentStatus = NO;
cmdError = ARCOMMANDS_Generator_GenerateCommonControllerStateIsPilotingChanged(cmdbuf, sizeof(cmdbuf), &actualSize, (inHud ? 1 : 0));
if (cmdError == ARCOMMANDS_GENERATOR_OK)
{
sentStatus = [self sendData:cmdbuf withSize:actualSize onBufferWithId:[MiniDroneARNetworkConfig c2dAckId]withSendPolicy:ARNETWORK_SEND_POLICY_RETRY withCompletionBlock:nil];
}
if (!sentStatus)
{
ARSAL_PRINT(ARSAL_PRINT_ERROR, TAG, "Failed to send isPilotingChanged command.");
}
}
#pragma mark - Send notifications
- (void)postDebug1AckNotification
{
[[NSNotificationCenter defaultCenter] postNotificationName:MiniDroneDeviceControllerDebug1ReceivedNotification object:self];
}
- (void)postDebug2AckNotification
{
[[NSNotificationCenter defaultCenter] postNotificationName:MiniDroneDeviceControllerDebug2ReceivedNotification object:self];
}
- (void)postDebug3AckNotification
{
[[NSNotificationCenter defaultCenter] postNotificationName:MiniDroneDeviceControllerDebug3ReceivedNotification object:self];
}
#pragma mark - Miscellaneous private methods.
- (void)initDroneState
{
[_droneStateLock lock];
_droneState.pilotingData = (MiniDronePilotingData_t){ NO, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
[_droneStateLock unlock];
}
- (BOOL)privateStart
{
BOOL failed = NO;
/* Initialize initial commands state. */
[self initDroneState];
if (!failed && !_startCancelled)
{
failed = ([self startBaseController] == BASE_DEVICE_CONTROLLER_START_RETVAL_FAILED);
if (failed) {
ARSAL_PRINT(ARSAL_PRINT_ERROR, TAG, "Failed to start the base controller.");
}
}
if (!failed && !_startCancelled)
{
// Register ARCommands callbacks.
[self registerMiniDroneARCommandsCallbacks];
#ifdef ARCOMMANDS_HAS_DEBUG_COMMANDS
[self registerMiniDroneDebugARCommandsCallbacks];
#endif
}
if (!failed && !_startCancelled && !self.fastReconnection)
{
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setLocale:[NSLocale systemLocale]];
// Set date
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
[self DeviceController_SendCommonCurrentDate:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_RETRY withCompletionBlock:nil withDate:(char *)[[dateFormatter stringFromDate:currentDate] cStringUsingEncoding:NSUTF8StringEncoding]];
// Set time
[dateFormatter setDateFormat:@"'T'HHmmssZZZ"];
[self DeviceController_SendCommonCurrentTime:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_RETRY withCompletionBlock:nil withTime:(char *)[[dateFormatter stringFromDate:currentDate] cStringUsingEncoding:NSUTF8StringEncoding]];
}
if (!failed && !_startCancelled && !self.fastReconnection)
{
// Attempt to get initial settings
[[NSNotificationCenter defaultCenter] postNotificationName:DeviceControllerNotificationAllSettingsDidStart object:self userInfo:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stateAllSettingsChanged:) name:DeviceControllerSettingsStateAllSettingsChangedNotification object:nil];
[self DeviceController_SendSettingsAllSettings:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_RETRY withCompletionBlock:nil];
[_initialSettingsReceivedCondition lock];
[_initialSettingsReceivedCondition wait];
if (!_initialSettingsReceived)
{
ARSAL_PRINT(ARSAL_PRINT_ERROR, TAG, "Initial settings retrieval timed out.");
failed = YES;
}
[_initialSettingsReceivedCondition unlock];
}
if (!failed && !_startCancelled)
{
// Attempt to get initial states
[[NSNotificationCenter defaultCenter] postNotificationName:DeviceControllerNotificationAllStatesDidStart object:self userInfo:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stateAllStatesChanged:) name:DeviceControllerCommonStateAllStatesChangedNotification object:nil];
[self DeviceController_SendCommonAllStates:[MiniDroneARNetworkConfig c2dAckId] withSendPolicy:ARNETWORK_SEND_POLICY_RETRY withCompletionBlock:nil];
[_initialStatesReceivedCondition lock];
[_initialStatesReceivedCondition wait];
if (!_initialStatesReceived)
{
ARSAL_PRINT(ARSAL_PRINT_ERROR, TAG, "Initial states retrieval timed out.");
failed = YES;
}
[_initialStatesReceivedCondition unlock];
}
if (!failed && !_startCancelled)
{
[self registerCurrentProduct];
_photoRecordController = [[MiniDronePhotoRecordController alloc] init];
_photoRecordController.deviceController = self;
}
return (failed == NO);
}
- (void)privateStop
{
_photoRecordController = nil;
[self unregisterMiniDroneARCommandsCallbacks];
#ifdef ARCOMMANDS_HAS_DEBUG_COMMANDS
[self unregisterMiniDroneDebugARCommandsCallbacks];
#endif
[self stopBaseController];
}
- (void)stateAllSettingsChanged:(NSNotification *)notification
{
[_initialSettingsReceivedCondition lock];
_initialSettingsReceived = YES;
[_initialSettingsReceivedCondition signal];
[_initialSettingsReceivedCondition unlock];
}
- (void)stateAllStatesChanged:(NSNotification *)notification
{
[_initialStatesReceivedCondition lock];
_initialStatesReceived = YES;
[_initialStatesReceivedCondition signal];
[_initialStatesReceivedCondition unlock];
}
#pragma mark - ARNetwork acknowledged commands.
- (void)debugTest1ValueAcknowledged
{
[self postDebug1AckNotification];
}
- (void)debugTest2ValueAcknowledged
{
[self postDebug2AckNotification];
}
- (void)debugTest3ValueAcknowledged
{
[self postDebug3AckNotification];
}
@end