Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. //
  2. // HotpointMissionViewController.m
  3. // DJISdkDemo
  4. //
  5. // Copyright © 2015 DJI. All rights reserved.
  6. //
  7. /**
  8. * This file demonstrates the process to start a hotpoint mission. In this demo, the aircraft will use the home location as the hotpoint.
  9. * Therefore, before the home location is ready, the prepare button will not be enabled.
  10. *
  11. * CAUTION: it is highly recommended to run this sample using the simulator.
  12. */
  13. #import <DJISDK/DJISDK.h>
  14. #import "HotpointMissionViewController.h"
  15. #import "DemoAlertView.h"
  16. #import "DemoUtilityMacro.h"
  17.  
  18. @interface HotpointMissionViewController ()
  19.  
  20. @property(nonatomic, weak)DJIHotpointMissionOperator *hotpointOperator;
  21.  
  22. @end
  23.  
  24. @implementation HotpointMissionViewController
  25.  
  26. @synthesize homeLocation = _homeLocation;
  27.  
  28. -(void)viewWillAppear:(BOOL)animated {
  29. [super viewWillAppear:animated];
  30. // Hotpoint mission required the aircraft location before initializing the mission
  31. // Therefore, we disable the prepare button until the aircraft location is valid
  32. [self.prepareButton setEnabled:CLLocationCoordinate2DIsValid(self.homeLocation)];
  33. self.hotpointOperator = [[DJISDKManager missionControl] hotpointMissionOperator];
  34. }
  35.  
  36. -(void)setHomeLocation:(CLLocationCoordinate2D)homeLocation {
  37. _homeLocation = homeLocation;
  38. self.prepareButton.enabled = NO;
  39. }
  40.  
  41.  
  42. /**
  43. * Prepare the hotpoint mission.
  44. */
  45. -(DJIMission*) initializeMission {
  46. DJIHotpointMission* mission = [[DJIHotpointMission alloc] init];
  47. mission.hotpoint = self.homeLocation;
  48. mission.altitude = 20.0;
  49. mission.radius = 10.0;
  50. mission.angularVelocity = 5.0;
  51.  
  52. return mission;
  53. }
  54.  
  55. #pragma mark - Execution
  56.  
  57. - (IBAction)onStartButtonClicked:(id)sender
  58. {
  59. WeakRef(target);
  60. [self.hotpointOperator startMission:(DJIHotpointMission*)[self initializeMission] withCompletion:^(NSError * _Nullable error) {
  61. if (error) {
  62. ShowResult(@"ERROR: startMissionExecutionWithCompletion:%@", error.description);
  63. [target.hotpointOperator removeListenerOfEvents:target];
  64.  
  65. }
  66. else {
  67. ShowResult(@"SUCCESS: startMissionExecutionWithCompletion");
  68. }
  69. }];
  70.  
  71. [self.hotpointOperator addListenerToEvents:self withQueue:nil andBlock:^(DJIHotpointMissionEvent * _Nonnull event) {
  72. [target showHotpointMissionStatus:event];
  73. }];
  74. }
  75.  
  76. - (IBAction)onStopButtonClicked:(id)sender
  77. {
  78. WeakRef(target);
  79. [self.hotpointOperator stopMissionWithCompletion:^(NSError * _Nullable error) {
  80. if (error) {
  81. ShowResult(@"ERROR: stopMissionExecutionWithCompletion:. %@", error.description);
  82. }
  83. else {
  84. ShowResult(@"SUCCESS: stopMissionExecutionWithCompletion");
  85. [target.hotpointOperator removeListenerOfEvents:target];
  86. }
  87. }];
  88. }
  89.  
  90. - (IBAction)onDownloadButtonClicked:(id)sender
  91. {
  92. WeakRef(target);
  93. [self.hotpointOperator getExecutingMissionWithCompletion:^(DJIHotpointMission * _Nullable mission, NSError * _Nullable error) {
  94. if (error) {
  95. ShowResult(@"get Executing Mission Error:%@", error.description);
  96. } else {
  97. [target showHotpointMission:mission];
  98. }
  99. }];
  100. }
  101.  
  102. - (IBAction)onPauseButtonClicked:(id)sender
  103. {
  104. [self.hotpointOperator pauseMissionWithCompletion:^(NSError * _Nullable error) {
  105. if (error) {
  106. ShowResult(@"ERROR: pauseMissionWithCompletion:%@", error.description);
  107. }
  108. else {
  109. ShowResult(@"SUCCESS: pauseMissionWithCompletion");
  110. }
  111. }];
  112. }
  113.  
  114. - (IBAction)onResumeButtonClicked:(id)sender
  115. {
  116. [self.hotpointOperator resumeMissionWithCompletion:^(NSError * _Nullable error) {
  117. if (error) {
  118. ShowResult(@"ERROR: resumeMissionWithCompletion: %@", error.description);
  119. }
  120. else {
  121. ShowResult(@"SUCCESS: resumeMissionWithCompletion ");
  122. }
  123. }];
  124. }
  125.  
  126.  
  127. /**
  128. * Method to display the current status of the hotpoint mission.
  129. */
  130. -(void) showHotpointMissionStatus:(DJIHotpointMissionEvent*)event {
  131.  
  132. NSMutableString* statusStr = [NSMutableString new];
  133. [statusStr appendFormat:@"previousState:%@\n", [[self class] descriptionForState:event.previousState]];
  134. [statusStr appendFormat:@"currentState:%@\n", [[self class] descriptionForState:event.currentState]];
  135. [statusStr appendFormat:@"Current radius: %f\n", event.radius];
  136.  
  137. if (event.error) {
  138. ShowResult(@"Hotpoint Mission Executing Error:%@", event.error.description);
  139. [self.hotpointOperator removeListenerOfEvents:self];
  140. }
  141.  
  142. [self.statusLabel setText:statusStr];
  143. }
  144.  
  145. -(void) showHotpointMission:(DJIHotpointMission*)hpMission {
  146. NSMutableString* missionInfo = [NSMutableString stringWithString:@"The hotpoint mission is downloaded successfully: \n"];
  147. [missionInfo appendString:[NSString stringWithFormat:@"Hotpoint: (%f, %f)\n", hpMission.hotpoint.latitude, hpMission.hotpoint.longitude]];
  148. [missionInfo appendString:[NSString stringWithFormat:@"Altitude: %f\n", hpMission.altitude]];
  149. [missionInfo appendString:[NSString stringWithFormat:@"Radius: %f\n", hpMission.radius]];
  150. [missionInfo appendString:[NSString stringWithFormat:@"AngularVelocity: %ld\n", (long)hpMission.angularVelocity]];
  151. [self.statusLabel setText:missionInfo];
  152.  
  153. }
  154.  
  155. +(NSString *)descriptionForState:(DJIHotpointMissionState)state {
  156. switch (state) {
  157. case DJIHotpointMissionStateExecutionPaused:
  158. return @"Paused";
  159. case DJIHotpointMissionStateUnknown:
  160. return @"Unknown";
  161. case DJIHotpointMissionStateExecuting:
  162. return @"Executing";
  163. case DJIHotpointMissionStateRecovering:
  164. return @"Recovering";
  165. case DJIHotpointMissionStateDisconnected:
  166. return @"Disconnected";
  167. case DJIHotpointMissionStateNotSupported:
  168. return @"NotSupported";
  169. case DJIHotpointMissionStateReadyToStart:
  170. return @"ReadyToStart";
  171. case DJIHotpointMissionStateExecutingInitialPhase:
  172. return @"Initial Phase";
  173. }
  174.  
  175. return nil;
  176. }
  177.  
  178.  
  179. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement