如何使用DJI SDK收听遥控器粘贴值更新



当无人机遥控器的摇杆发生变化时,我需要帮助来触发操作。事实上,我想在用户开始玩棍子时自动停止无人机的时间线任务。

我想我需要继续使用DJIStick类作为文档中的引用来侦听sticks值,但我无法正确处理!

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

要获取有关遥控器操纵杆状态的最新信息,您可以根据文档连接到RemoteController类的setHardwareStateCallback,即updates the Remote Controller's current hardware state (e.g. the state of the physical buttons and joysticks)

这将使您能够非常频繁地响应操纵手柄的位置。

然而,您将不得不在一段时间内存储以前的值,并创建一些逻辑来确定用户在这段时间内是否一直在更改棒的值。

一个更简单的选项可能是在用户界面中为用户提供停止任务的选项。另外,使用遥控器停止任务的另一个紧急选项(如果是您的用例(是在遥控器上切换飞行模式,这将自动停止任务。

我已经使用以下逻辑实现了监听棒中的变化:

  1. 设置ViewController类的委托
  2. 定义一个新函数,返回并获取连接设备(drone(的DJIRemoteController实例
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. 然后,在无人机状态飞行后(在我的情况下是在无人机启动时间线任务后(,您需要调用上一个函数

let remote=self.fetchRemoteController((

远程?。delegate=自身

  1. 现在,当棒发生变化时,应自动触发以下功能

    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)")
    }
    }