Swift开发DJI Mobile SDK项目:Mission Control使用疑问及需求咨询
Hey Jake, let's tackle your problem step by step. First off, let's address whether Mission Control is the right fit for your simple use case, then walk through a more straightforward solution using DJI's core Swift APIs.
Short answer: No, not really. Mission Control is built for complex, multi-step missions like waypoint navigation, orbit flights, or custom automated routes. For your specific need—just flying to a specified height and recording a video of the area below—it’s overkill. Using the basic flight control and camera APIs directly will be simpler, less error-prone, and easier to debug.
Here’s a step-by-step approach to achieve your goal without Mission Control:
1. Validate Connection & Get Core Components
First, make sure your app has a connected drone, then grab references to the flight controller and camera:
import DJISDK // Check if a product is connected and get flight controller/camera guard let product = DJISDKManager.product(), let flightController = product.flightController, let camera = product.camera else { print("Drone not connected or core components unavailable") return }
2. Fly to the Target Height
Use the flight controller’s built-in method to take off directly to your desired height (units are meters):
let targetHeight: Float = 12.0 // Adjust this to your preferred height flightController.takeoffToHeight(targetHeight, withCompletion: { error in if let error = error { print("Failed to reach target height: \(error.localizedDescription)") return } print("Successfully reached \(targetHeight) meters! Starting video recording...") // Trigger recording once we're at the right height self.startVideoRecording(with: camera) })
3. Start Video Recording
Once the drone is at the target height, initiate recording via the camera API:
func startVideoRecording(with camera: DJICamera) { camera.startRecordingVideo(completion: { error in if let error = error { print("Failed to start recording: \(error.localizedDescription)") } else { print("Video recording started successfully") // Add logic here to stop recording after a set time, or via user input // Example: Stop recording after 10 seconds DispatchQueue.main.asyncAfter(deadline: .now() + 10) { camera.stopRecordingVideo(completion: { stopError in if let stopError = stopError { print("Failed to stop recording: \(stopError.localizedDescription)") } else { print("Recording stopped successfully") } }) } } }) }
If you still want to use Mission Control for learning purposes, you can create a single-waypoint mission where the only waypoint is at your target height. Here’s a quick snippet:
// Create a waypoint at the drone's current latitude/longitude, target height guard let currentLocation = flightController.location else { return } let waypoint = DJIWaypoint(coordinate: currentLocation.coordinate, altitude: targetHeight) waypoint.actionRepeatTimes = 1 waypoint.heading = 0 // Keep drone facing forward (adjust as needed) let mission = DJIWaypointMission(waypoints: [waypoint]) mission.maxFlightSpeed = 5.0 mission.autoFlightSpeed = 3.0 // Start the mission, and trigger recording when it begins flightController.startMission(mission, withCompletion: { error in if let error = error { print("Mission failed to start: \(error.localizedDescription)") } else { self.startVideoRecording(with: camera) } })
Again, this is unnecessary for your simple use case, but it’s an option if you want to experiment with Mission Control.
- Permissions: Ensure your app has the necessary iOS permissions (location, camera, and background modes for drone control) added to your
Info.plist. - Flight Safety: Always check that the drone has strong GPS signal, sufficient battery, and is not in a restricted flight zone before initiating any commands.
- Recording Management: Use the camera’s
mediaManagerto access and download recorded videos once the mission is complete.
内容的提问来源于stack exchange,提问作者Jake




