简体   繁体   中英

Accessing state information in swift - DJI Mobile SDK iOS

I can't get my head around how to get out simple status data, like the current gimbal pitch for example.

I have not found a solid connection between the DJI SDK and what actually works in xcode. The SDK gives me hints and together with xcode autocompletion a go forwards, slowly..

Class GimbalState has member getAttitudeInDegrees() with description: "The current gimbal attitude in degrees. Roll, pitch and yaw are 0 if the gimbal is level with the aircraft and points in the forward direction of North Pole." - Great!

However, it does not autocomplete in xcode nor does it compile.

Other approaches tested:

var gimbalStateInformation = DJIGimbalState()
    print(gimbalStateInformatoin.attitudeInDegrees.pitch.description)

--> All pitch roll and yaw values come out as 0.0

var gimbalStateInformation = DJUGimbalAttitude()
    print(gimbalStateInformatoin.pitch.description)

--> All pitch roll and yaw values come out as 0.0

I've tried to reach the information via keys, but my app crashes when I run the code.

func getGimbalAttitude(){
    // Get the key
    guard let gimbalAttitudeKey = DJIGimbalKey(param: DJIGimbalParamAttitudeInDegrees) else {
        print("Could not create DJIGimbalParamAttitudeInDegrees key")
        return
    }
    // Get the keyManager
    guard let keyManager = DJISDKManager.keyManager() else {
        print("Could not get the key manager, manke sure you are registered")
        return
    }
    // Test if key is available
    let testing = keyManager.isKeySupported(gimbalAttitudeKey)
    self.statusLabel.text = String(testing)    // This comes out true

    // Use key to retreive info
    let gimbalAttitudeValue = keyManager.getValueFor(gimbalAttitudeKey)
    let gimbalAttitude = gimbalAttitudeValue?.value as! DJIGimbalState
    _ = gimbalAttitude.attitudeInDegrees.pitch
// --> Application crashes on the line above
}

I'm working towards a Mavic Mini.

Please advise in general terms how to connect the DJI Mobile SDK to Swift and specifically how I can read out the current gimbal pitch value.

You can access the current Gimbal pitch through its didUpdate state delegate function

import DJISDK
class GimbalController: NSObject, DJIGimbalDelegate {
  let gimbal: DJIGimbal
    init(gimbal: DJIGimbal) {
      self.gimbal = gimbal
      super.init()
      gimbal.delegate = self
    }

    func gimbal(_ gimbal: DJIGimbal, didUpdate state: DJIGimbalState) {
            print(state.attitudeInDegrees.pitch)
    }
}

// create an instance of the custom gimbal controller in some other class
// and pass it the gimbal instance
if let aircraft = DJISDKManager.product() as? DJIAircraft {
  if let gimbal = aircraft.gimbal {
    let gimbalController = GimbalController(gimbal: gimbal)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM