Hi there,
We are trying to implement flight plans in our app. More specifically we want to translate our UI into commands to be sent to the drone, then create a mavlink file, store it locally and then send it to the drone.
The connection seems to be working fine and the mavlink file is being uploaded to the drone correctly. The problem comes when we try to execute the file, it shows the incorrectFlightPlanFile error.
I upload our mavlink file as well as the portion of the code in charge of creating it (see the mavlink file attached inside the zip file). Just to provide a little bit of context the onOpened method in the class below is called when the drone is connected and the onClosed when the drone disconnects. If you can help us figure out this issue we will really appreciate it. Thanks in advance.
class ParrotGridFlightProvider: DroneSessionManagerDelegate {
static let shared = ParrotGridFlightProvider()
private var session: DroneSession?
private var pilotingItf: Ref<FlightPlanPilotingItf>?
private var missionStarted = false
func onOpened(session: DroneSession) {
self.session = session
self.pilotingItf = (session as! ParrotDroneSession).adapter.drone.getPilotingItf(PilotingItfs.flightPlan) { [weak self] value in
let result = self?.activateMission()
print("Matias Test", "Drone can fly: \(String(describing: result))")
print("Matias Test", "Drone state: \(String(describing: self?.pilotingItf?.value?.state))" )
print("Matias Test", "Drone error: \(String(describing: self?.pilotingItf?.value?.latestActivationError))")
print("Matias Test", "Drone is paused: \(String(describing: self?.pilotingItf?.value?.isPaused))")
}
}
func onClosed(session: DroneSession) {
self.session = nil
self.pilotingItf = nil
}
public func startMission(commands: [ParrotGridFlightCommand]) {
let filePath = self.writeFile(commands: commands)
self.pilotingItf?.value?.uploadFlightPlan(filepath: filePath)
}
private func activateMission() -> Bool {
if let pilotingItf = pilotingItf?.value {
if pilotingItf.state == .idle && !missionStarted {
missionStarted = true
return pilotingItf.activate(restart: true)
}
}
return missionStarted ? true : false
}
private func getDirectory() -> URL {
let fileManager = FileManager.default
let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let flightPlanFolderPath = documentPath.appendingPathComponent("flightPlans")
try? fileManager.createDirectory(at: flightPlanFolderPath, withIntermediateDirectories: false, attributes: nil)
return flightPlanFolderPath
}
private func writeFile(commands: [ParrotGridFlightCommand]) -> String {
let path = getDirectory().appendingPathComponent("\(Date().toServerDate()).mavlink").path
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: path) {
do {
try "".write(toFile: path, atomically: true, encoding: .utf8)
}
catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
}
if let fileHandle = FileHandle(forWritingAtPath: path) {
fileHandle.seekToEndOfFile()
let header = "QGC WPL 120"
fileHandle.write(header.data(using: .utf8)!)
let newLine = "\n"
fileHandle.write(newLine.data(using: .utf8)!)
for command in commands {
fileHandle.seekToEndOfFile()
let value = "\(command.index) \(command.currentWP) \(command.coordFrame) \(command.command.rawValue) \(command.param1) \(command.param2) \(command.param3) \(command.param4) \(command.longitude) \(command.latitude) \(command.altitude) \(command.autoContinue ? 1 : 0)"
fileHandle.write(value.data(using: .utf8)!)
let newLine = "\n"
fileHandle.write(newLine.data(using: .utf8)!)
}
fileHandle.closeFile()
}
return path
}
}
2021-06-07T13:55:08.022.mavlink.zip (348 Bytes)