dji-sdk / Mobile-SDK-iOS

DJI Mobile SDK for iOS: http://developer.dji.com/mobile-sdk/
Other
581 stars 256 forks source link

Cannot get values using DJIGimbalParamAttitudeInDegrees #531

Closed Filet-0-Fish closed 2 years ago

Filet-0-Fish commented 2 years ago

Hello. I have reported this problem on the forum, but since I could not find a solution, I am posting it here. https://forum.dji.com/forum.php?mod=viewthread&tid=269885

What I want to do

The code

let gimbalAttitudeKey = DJIGimbalKey(param: DJIGimbalParamAttitudeInDegrees)
DJISDKManager.keyManager()?.startListeningForChanges(on: gimbalAttitudeKey!, withListener: self, andUpdate: {(oldValue: DJIKeyedValue?, newValue: DJIKeyedValue?) in
if newValue != nil {
    let gimbalAttitudes = newValue!.value! as! DJIGimbalAttitude

    //Display the value in the label text
    self.RollLabel.text = "Roll: \(gimbalAttitudes.roll)"
    self.PitchLabel.text = "Pitch: \(gimbalAttitudes.pitch)"
    self.YawLabel.text = "Yaw: \(gimbalAttitudes.yaw)"
  }
})

The problem that is occurring

I hope this issue will be resolved soon.

DJI-William commented 2 years ago

I suggest you an easier way of doing this. Instead of using key, you can just use delegate of flightController. Inside the delegate, find flightController:didUpdateState and you can access attitude. It is consisting of 3 double values.

Filet-0-Fish commented 2 years ago

Thanks for the reply, DJI-William. I know what I can get from DJIFlightControllerState is the attitude of the aircraft. I want to get where the camera is pointing. Maybe I am just missing something and can also get the gimbal attitude from DJIFlightControllerState?

Filet-0-Fish commented 2 years ago

(Sorry, I am new to Github and closed it by mistake.)

DJI-William commented 2 years ago

I am very sorry, I have missed reading, You wan to get the gimbal attitude. This is the same thing. There is a delegate in Gimbal class, you can get DJIGimbalState through this, and you can get DJIGimbalState.attitudeInDegrees. It is a struct and contains 3 float numbers which represent 3 angles.

Filet-0-Fish commented 2 years ago

Thanks for telling me. I am not sure how to get the DJIGimbalState from the Gimbal delegate. Should I delegate the Gimbal class to the view controller class? I would appreciate it if you could provide a sample code.

DJI-William commented 2 years ago

It is in the Sample demo. Link:https://github.com/dji-sdk/Mobile-SDK-iOS/blob/master/Sample%20Code/ObjcSampleCode/DJISdkDemo/Demo/Gimbal/PushInfo/GimbalPushInfoViewController.m

Filet-0-Fish commented 2 years ago

Thanks for the sample code. But sorry, do you have this sample in swift version instead of objective-c?

DJI-William commented 2 years ago

I am sorry, I didn't find it in the Swift demo.

Filet-0-Fish commented 2 years ago

I was able to get it to work by adding this code to the ViewController! Thanks so much for your support!

class DroneConnectionView: ... DJIGimbalDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        let gimbal = fetchGimbal()
        if gimbal != nil {
            gimbal?.delegate = self
        }
    }

    func fetchGimbal() -> DJIGimbal? {
        guard let product = DJISDKManager.product() else {
            return nil
        }

        if product is DJIAircraft || product is DJIHandheld {
            return product.gimbal
        }
        return nil
    }

    func gimbal(_ gimbal: DJIGimbal, didUpdate state: DJIGimbalState) {
        self.RollLabel.text = "Roll: \(state.attitudeInDegrees.roll)"
        self.PitchLabel.text = "Pitch: \(state.attitudeInDegrees.pitch)"
        self.YawLabel.text = "Yaw: \(state.attitudeInDegrees.yaw)"
    }
}