Search code examples
objective-cswiftsdkframeworksdji-sdk

How to listen to the remote controller sticks values updates using DJI SDK?


I need help in triggering an action when a change in the sticks of the remote controller of the drone is held. Actually, I want to stop the timeline mission of the drone automatically when a user starts playing with the sticks.

I think I need to keep listening to the sticks values using DJIStick class as a reference from the documentation but I could not handle that correctly!

https://developer.dji.com/api-reference/android-api/Components/Stick/DJIStick.html?Phclickrefb=1011lcQs3Ept


Solution

  • I have achieved listening to the change in the sticks using the following logic:

    1. Set the delegate for the ViewController class
    2. Define a new function that returns and fetches the DJIRemoteController instance of the connected device (drone)
    class ViewController: DJIRemoteControllerDelegate {
    
    func fetchRemoteController() -> DJIRemoteController? {
            if !(DJISDKManager.product() != nil){
                return nil
            }
            if DJISDKManager.product() is DJIAircraft {
                return (DJISDKManager.product() as! DJIAircraft).remoteController
            }
            return nil
        }
    }
    
    1. Then, you need to call the previous function when after the drone state is flying (in my case after the drone started the timeline mission)

    let remote = self.fetchRemoteController()

    remote?.delegate = self

    1. Now, the following function should be triggered automatically when a change occurs the sticks

      func remoteController(_ rc: DJIRemoteController, didUpdate state: 
      DJIRCHardwareState) {
      
           if state.rightStick.verticalPosition > 0 || state.rightStick.verticalPosition < 0 || state.rightStick.horizontalPosition > 0 || state.rightStick.horizontalPosition < 0 || state.leftStick.verticalPosition > 0 || state.leftStick.verticalPosition < 0 || state.leftStick.horizontalPosition > 0 || state.leftStick.horizontalPosition < 0 {
                   print("Detected move in Sticks while mission is started so stop the mission")
                   print("Right Stick vertical position is \(state.rightStick.verticalPosition) and horizontal position \(state.rightStick.horizontalPosition)")
                   print("Left Stick Value is \(state.leftStick.verticalPosition) and horizontal position \(state.leftStick.horizontalPosition)")
           }
      
       }