Skip to main content

虚拟摇杆使用教程

功能概述​

虚拟摇杆(Virtual Stick)功能允许开发者通过 APP 模拟遥控器摇杆操作来控制无人机飞行。通过虚拟摇杆,开发者可以实现自动化飞行控制、远程指令飞行等场景。

对应调试工具页面: Virtual Stick Page(VirtualStickFragment)

主要功能​

  • 双摇杆控制(左杆、右杆,映射关系由操控模式决定)
  • 支持多种操控模式(美国手 / 日本手 / 中国手)
  • 档位切换(舒适 / 标准 / 狂暴 / 低速)
  • 云台俯仰角控制(SeekBar)
  • 虚拟按键命令(暂停长按/短按、起飞返航、C1键)
  • 实时飞行遥测数据显示(经纬度、高度、距离、速度、姿态角)
  • 视频预览和镜头切换

核心 SDK 接口​

1. 发送虚拟控制指令​

MSDK 3.0 使用 FlightMissionKey.KeyUAVVirtualRCControl 配合 setFrequencyReport() 进行定频上报虚拟摇杆控制指令:

val keyVirtualRCControl = KeyTools.createKey(FlightMissionKey.KeyUAVVirtualRCControl)
val keyManager = DeviceManager.getDeviceManager().getFirstDroneDevice()?.getKeyManager()

// 构建虚拟控制消息
val bean = VirtualControlMessageBean(
timestamp = System.currentTimeMillis(),
sequence = sequence++,
turnYaw = (yawValue * 100f).toInt(), // 偏航:左转/右转 (-100 ~ 100)
forwardOrBackward = (pitchValue * 100f).toInt(), // 俯仰:前进/后退 (-100 ~ 100)
raiseOrFall = (throttleValue * 100f).toInt(), // 油门:上升/下降 (-100 ~ 100)
leftOrRight = (rollValue * 100f).toInt(), // 横滚:向左/向右 (-100 ~ 100)
gimbalPitch = 0, // 云台俯仰角偏移
button = VirtualJoystickButton.BUTTON_NOT_OPER // 虚拟按键
)

// 使用定频上报方式发送
keyManager?.setFrequencyReport(keyVirtualRCControl, bean)

2. 操控模式说明​

不同操控模式下,左右摇杆的映射关系不同:

操控模式左杆 X左杆 Y右杆 X右杆 Y
美国手 (AMERICA_HAND)左转/右转上升/下降向左/向右前进/后退
日本手 (JAPANESE_HAND)左转/右转前进/后退向左/向右上升/下降
中国手 (CHINESE_HAND)向左/向右前进/后退左转/右转上升/下降

查询和设置操控模式:

val remoteKeyManager = DeviceManager.getDeviceManager().getLocalRemoteDevice().getKeyManager()

// 查询当前操控模式
remoteKeyManager.getValue(
KeyTools.createKey(RemoteControllerKey.KeyRCRockerControlMode),
object : CommonCallbacks.CompletionCallbackWithParam<RcOperateModeEnum> {
override fun onSuccess(t: RcOperateModeEnum?) {
// 当前模式: AMERICA_HAND / JAPANESE_HAND / CHINESE_HAND
}
override fun onFailure(error: IAutelCode, msg: String?) { }
})

// 设置操控模式
remoteKeyManager.setValue(
KeyTools.createKey(RemoteControllerKey.KeyRCRockerControlMode),
RcOperateModeEnum.AMERICA_HAND,
object : CommonCallbacks.CompletionCallback {
override fun onSuccess() { /* 设置成功 */ }
override fun onFailure(code: IAutelCode, msg: String?) { /* 设置失败 */ }
})

3. 档位设置​

不同档位对应不同的满杆速度限制:

档位水平速度(前/后)水平速度(左/右)垂直速度(上/下)偏航角速度
舒适 (SMOOTH)10/10 m/s10/10 m/s5/4 m/s90°/s
标准 (NORMAL)15/15 m/s10/10 m/s6/6 m/s90°/s
狂暴 (SPORT)23/18 m/s20/20 m/s6/6 m/s120°/s
低速 (LOW_SPEED)3/3 m/s3/3 m/s3/3 m/s90°/s
val keyManager = DeviceManager.getDeviceManager().getFirstDroneDevice()?.getKeyManager()

// 查询当前档位
keyManager?.getValue(
KeyTools.createKey(FlightPropertyKey.KeyGearLever),
object : CommonCallbacks.CompletionCallbackWithParam<GearLevelEnum> {
override fun onSuccess(t: GearLevelEnum?) {
// 当前档位: SMOOTH / NORMAL / SPORT / LOW_SPEED
}
override fun onFailure(error: IAutelCode, msg: String?) { }
})

// 设置档位
keyManager?.setValue(
KeyTools.createKey(FlightPropertyKey.KeyGearLever),
GearLevelEnum.NORMAL,
object : CommonCallbacks.CompletionCallback {
override fun onSuccess() { /* 设置成功 */ }
override fun onFailure(code: IAutelCode, msg: String?) { /* 设置失败 */ }
})

4. 云台俯仰控制​

通过 VirtualControlMessageBean 的 gimbalPitch 字段控制云台俯仰角:

val bean = VirtualControlMessageBean(
timestamp = System.currentTimeMillis(),
sequence = sequence++,
gimbalPitch = progress - 100, // SeekBar范围 0-200,减去100得到 -100~100
button = VirtualJoystickButton.BUTTON_NOT_OPER
)
keyManager?.setFrequencyReport(keyVirtualRCControl, bean)

5. 虚拟按键​

支持通过虚拟摇杆发送按键指令:

// 可用的按键类型
VirtualJoystickButton.BUTTON_NOT_OPER // 无操作
VirtualJoystickButton.BUTTON_PAUSE_LONG // 暂停长按
VirtualJoystickButton.BUTTON_PAUSE_SHORT // 暂停短按
VirtualJoystickButton.BUTTON_TAKEOFF_RETURN // 起飞/返航
VirtualJoystickButton.BUTTON_C1 // C1键

// 发送按键指令
val bean = VirtualControlMessageBean(
timestamp = System.currentTimeMillis(),
sequence = sequence++,
button = VirtualJoystickButton.BUTTON_TAKEOFF_RETURN
)
keyManager?.setFrequencyReport(keyVirtualRCControl, bean)

6. 启停电机​

keyManager?.performAction(
KeyTools.createKey(FlightControlKey.KeyStartStopMotor),
true, // true: 启动, false: 停止
object : CommonCallbacks.CompletionCallbackWithParam<Void> {
override fun onSuccess(t: Void?) { /* 成功 */ }
override fun onFailure(error: IAutelCode, msg: String?) { /* 失败 */ }
})

7. 实时飞行数据获取​

val keyHfDroneData = KeyTools.createKey(CommonKey.KeyDroneSystemStatusHFNtfy)
val stateData = DeviceManager.getDeviceManager().getFirstDroneDevice()
?.getKeyManager()?.getCacheValue(keyHfDroneData)

stateData?.let {
val latitude = it.droneLatitude
val longitude = it.droneLongitude
val altitude = it.altitude
val distance = it.distance
val velocityX = it.velocityX
val velocityY = it.velocityY
val velocityZ = it.velocityZ

// 飞机姿态
val dronePitch = it.droneAttitude?.getPitchDegree()
val droneRoll = it.droneAttitude?.getRollDegree()
val droneYaw = it.droneAttitude?.getYawDegree()

// 云台姿态
val gimbalPitch = it.gimbalAttitude?.getPitchDegree()
val gimbalRoll = it.gimbalAttitude?.getRollDegree()
val gimbalYaw = it.gimbalAttitude?.getYawDegree()
}

8. 视频预览与镜头切换​

// 获取可用镜头列表
val droneDevice = DeviceManager.getDeviceManager().getFirstDroneDevice()
val gimbalType = droneDevice?.getGimbalDeviceType()
val lensList = droneDevice?.getCameraAbilitySetManger()?.getLensList(gimbalType)

// 根据镜头类型获取通道ID
val channelId = when (lensType) {
LensTypeEnum.Zoom, LensTypeEnum.TeleZoom -> SDKConstants.getZoomChancelId()
LensTypeEnum.WideAngle -> SDKConstants.getWideAngleChannelId()
LensTypeEnum.Thermal, LensTypeEnum.TeleThermal -> SDKConstants.getInfraredChannelId()
LensTypeEnum.NightVision -> SDKConstants.getNightVisionChannelId()
else -> SDKConstants.getZoomChancelId()
}

// 创建播放器
val player = AutelPlayer(channelId)
player.addVideoView(playerView)

注意事项​

  1. MSDK 3.0 使用 setFrequencyReport() 进行定频上报控制指令,SDK 内部处理发送频率
  2. VirtualControlMessageBean 中的摇杆值范围为 -100 ~ 100(整数),表示杆量百分比
  3. 操控模式影响摇杆轴映射关系,发送前需根据当前模式进行转换
  4. 发送按键指令时,按键状态会叠加在摇杆指令上一起发送
  5. 云台俯仰控制 SeekBar 松手后应回到中位(值=0),避免持续运动
  6. 飞行过程中应持续监听飞行状态数据,确保飞行安全
  7. 图传为按需拉流(AutelPlayer(deviceId, lensId))。不同机型可用镜头列表不同,Demo 会在 initVideo() 中若默认镜头不在机上则自动选用第一个可用镜头,避免首次未触发 Spinner 回调时出现黑屏

参考 Demo​

完整代码示例请参考调试工具模块中的 VirtualStickFragment.kt。