You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过DJI SDK监听遥控器摇杆值更新并触发任务停止动作

How to Stop DJI Drone Timeline Task When Remote Controller Sticks Are Operated

Hey there! Let's fix this up for you. You want to automatically halt the drone's Timeline task whenever the user moves the remote controller sticks, and you're trying to use the DJIStick class but haven't got it working right. Here's a clear, step-by-step implementation:


Step 1: Get Remote Controller & Stick Instances

First, you need to access the remote controller from the connected DJI product, then grab references to both the left and right sticks. Make sure you do this after the DJI SDK is fully initialized to avoid null pointer exceptions.

// Get the connected product's remote controller
RemoteController remoteController = DJISDKManager.getInstance().getProduct().getRemoteController();

if (remoteController != null) {
    DJIStick leftStick = remoteController.getLeftStick();
    DJIStick rightStick = remoteController.getRightStick();
}

Step 2: Set Up Stick Value Listeners

The DJIStick class lets you listen for changes in the stick's X/Y axis values. Sticks sit at a neutral value (0.0f) when untouched—we'll check if the values deviate beyond a small threshold to detect intentional user input.

// Threshold to filter out minor stick jitter (adjust based on your needs)
private static final float STICK_OPERATION_THRESHOLD = 0.1f;

// Listen for left stick movements
if (leftStick != null) {
    leftStick.setStickValueListener((xValue, yValue) -> {
        // Check if stick is moved significantly from neutral
        if (Math.abs(xValue) > STICK_OPERATION_THRESHOLD || Math.abs(yValue) > STICK_OPERATION_THRESHOLD) {
            stopActiveTimelineTask();
        }
    });
}

// Repeat for right stick
if (rightStick != null) {
    rightStick.setStickValueListener((xValue, yValue) -> {
        if (Math.abs(xValue) > STICK_OPERATION_THRESHOLD || Math.abs(yValue) > STICK_OPERATION_THRESHOLD) {
            stopActiveTimelineTask();
        }
    });
}

Step 3: Implement the Timeline Stop Logic

Create a method to handle stopping the Timeline task. Depending on your use case, you might use stop() (halts immediately) or cancel() (clears the task entirely)—pick the one that fits your workflow.

// Assume you have a reference to your active Timeline instance
private Timeline droneTimeline;

private void stopActiveTimelineTask() {
    if (droneTimeline != null && droneTimeline.isRunning()) {
        droneTimeline.stop();
        // Optional: Log the action for debugging
        Log.d("DroneControl", "Timeline task stopped due to user stick input");
    }
}

Step 4: Clean Up Listeners to Avoid Memory Leaks

Don't forget to remove the stick listeners when your activity/fragment is destroyed to prevent memory leaks.

@Override
protected void onDestroy() {
    super.onDestroy();
    if (remoteController != null) {
        DJIStick leftStick = remoteController.getLeftStick();
        DJIStick rightStick = remoteController.getRightStick();
        
        if (leftStick != null) leftStick.setStickValueListener(null);
        if (rightStick != null) rightStick.setStickValueListener(null);
    }
}

Key Notes

  • Always validate that the remote controller and stick instances aren't null before using them—this prevents crashes if the drone disconnects mid-operation.
  • Adjust the STICK_OPERATION_THRESHOLD if you find minor stick jitter is triggering the stop accidentally (higher values mean more intentional movement is required).
  • If you only care about specific stick axes (e.g., only vertical movement), modify the condition to check only that axis value.

内容的提问来源于stack exchange,提问作者Mohamad Ghaith Alzin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 21:37:38