Dji Mobile SDK 基础实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dji Mobile SDK 基础实现相关的知识,希望对你有一定的参考价值。
Dji Mobile SDK 基础实现(二)
本文简要介绍如何通过调用DJI Mobile SDK,实现获取和释放无人机的控制权限、模拟遥控器按钮控制无人机的飞行、获取无人机的回传视频、获取无人机参数、实现和onboard SDK的数据透传。
下面逐个介绍以上内容,余下内容在前面文章已作介绍(文章末尾附上GitHub链接):
- 获取无人机的回传视频
- 获取无人机参数
- 实现和onboard SDK的数据透传
获取无人机的回传视频
1、调用并实例化TextureView控件,并在调用类中实现SurfaceTextureListener接口;
2、设置回调接口并初始化回传视频:
/**
*设置回调接口
*/
protected DJICodecManager mCodecManager = null;//视频编码
private DJIFlightControllerDelegate.FlightControllerReceivedDataFromExternalDeviceCallback mRecvCallback = null;
protected CameraReceivedVideoDataCallback mReceivedVideoDataCallBack = null;
protected DJIOnReceivedVideoCallback mOnReceivedVideoCallback = null;
private DJIBaseProduct mProduct = null;//无人机实例
//设置视频的回调
mReceivedVideoDataCallBack = new CameraReceivedVideoDataCallback() {
@Override
public void onResult(byte[] videoBuffer, int size) {
count++;
if (mCodecManager != null) {
mCodecManager.sendDataToDecoder(videoBuffer, size);
} else {
Log.e(TAG, "mCodecManager is null");
}
}
};
/**
*初始化回传视频
*/
private void initVideo() {
try {
mProduct = VirtualStickApplication.getProductInstance();
} catch (Exception exception) {
mProduct = null;
}
if (null == mProduct || !mProduct.isConnected()) {
mCamera = null;
} else {
if (null != mVideoSurface) {
mVideoSurface.setSurfaceTextureListener(this);
}
if (!mProduct.getModel().equals(Model.UnknownAircraft)) {
mCamera = mProduct.getCamera();
if (mCamera != null) {
mCamera.setDJICameraReceivedVideoDataCallback(mReceivedVideoDataCallBack);
}
} else {
if (null != mProduct.getAirLink()) {
if (null != mProduct.getAirLink().getLBAirLink()) {
// Set the callback
mProduct.getAirLink().getLBAirLink().setDJIOnReceivedVideoCallback(mOnReceivedVideoCallback);
mProduct.getAirLink().getLBAirLink().setFPVQualityLatency(DJILBAirLink.LBAirLinkFPVVideoQualityLatency.LowLatency,new DJIBaseComponent.DJICompletionCallback() {
@Override
public void onResult(DJIError djiError) {
if (djiError != null)
VirtualStickApplication.logError("setFPVQualityLatency()error:", djiError);
}
});
}
}
}
}
}
获取无人机参数
获取获取无人机的剩余电量,以百分比的形式返回
private DJIBaseProduct mProduct = null;//无人机实例
try {
mProduct = VirtualStickApplication.getProductInstance();
} catch (Exception exception) {
mProduct = null;
}
if(mProduct != null && mProduct.isConnected()) {
//获取无人机的剩余电量,以百分比的形式返回
try{
mProduct.getBattery().setBatteryStateUpdateCallback(new DJIBattery.DJIBatteryStateUpdateCallback() {
@Override
public void onResult(DJIBatteryState djiBatteryState) {
if(djiBatteryState == null){
VirtualStickApplication.logInfo("电量为空");
}else{
StringBuffer mBuffer = new StringBuffer();
mBuffer.append(djiBatteryState.getBatteryEnergyRemainingPercent());
tvBattery.setText(mBuffer.toString()+"%");
if(djiBatteryState.getBatteryEnergyRemainingPercent() != 0 && djiBatteryState.getBatteryEnergyRemainingPercent() <= 30){
if(entry){
AlertDialog.Builder builder = new AlertDialog.Builder(VirtualStickActivity.this);
builder.setTitle("警告" )
.setMessage("电池电量不足!" )
.setPositiveButton("确认" , null )
.setNegativeButton("取消" , null)
.show();
entry = false;
}
}else{
entry = true;
}
}
}
});
}catch(Exception e){
Toast.makeText(VirtualStickActivity.this,"1"+e.toString(),Toast.LENGTH_LONG).show();
e.printStackTrace();
}
获取飞行器所在所在高度、飞行速度、无人机姿态、GPS信号
1、获取flightController 对象;
DJIFlightController flightController;
public void setAircraft(DJIBaseProduct djiAircraft){
if(djiAircraft==null){
return;
}
flightController=((DJIAircraft)djiAircraft).getFlightController();
if(flightController!=null) {
}
}
2、通过flightController对象调用方法获取飞行器所在所在高度、飞行速度、无人机姿态、GPS信号;
所在高度
public String getHeight(){
if(flightController != null){
DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
if (currentState != null) {
return currentState.getAircraftLocation().getAltitude() + "";
}
}
return null;
}
飞行速度
//获取前后速度
public String getX(){
if(flightController != null){
DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
if (currentState != null) {
currentState.getGpsSignalStatus();
return currentState.getVelocityX() + "";
}
}
return null;
}
//获取左右速度
public String getY(){
if(flightController != null){
DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
if (currentState != null) {
return currentState.getVelocityY() + "";
}
}
return null;
}
//获取上下速度
public String getZ(){
if(flightController != null){
DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
if (currentState != null) {
return currentState.getVelocityZ() + "";
}
}
return null;
}
无人机姿态(roll、yaw、pitch)
public String getYaw(){
if(flightController != null){
DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
if (currentState != null) {
return "pitch:"+currentState.getAttitude().pitch+" Yaw:"+currentState.getAttitude().yaw
+" Roll:"+currentState.getAttitude().roll;
}
}
return null;
}
GPS信号
//获取GPS信号
public String getGPS(){
if(flightController != null){
DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
if (currentState != null) {
return currentState.getGpsSignalStatus().value()+"";
}
}
return null;
}
实现和onboard SDK的数据透传
发送数据到onboard SDK device
1、获取flightController 对象,如上所示;
2、通过flightController对象调用响应函数实现数据发送;
public void sendDataToOnboard(byte[] data){
if(flightController.isOnboardSDKDeviceAvailable()){
flightController.sendDataToOnboardSDKDevice(data, new DJIBaseComponent.DJICompletionCallback() {
@Override
public void onResult(DJIError djiError) {
if(djiError!=null) {
VirtualStickApplication.logError("SendDataToOnBoard error:", djiError);
}else{
VirtualStickApplication.logInfo("发送指令成功!");
}
}
});
}
}
}
从onboard SDK获取数据
1、获取flightController 对象,如上所示;
2、通过flightController对象调用响应函数实现数据回传;
/**
*设置回调接口
*/
flightController.setReceiveExternalDeviceDataCallback(flightControllerReceivedDataFromExternalDeviceCallback);
/**
*初始化并接受回传数据
*/
flightControllerReceivedDataFromExternalDeviceCallback = new DJIFlightControllerDelegate.FlightControllerReceivedDataFromExternalDeviceCallback() {
@Override
public void onResult(byte[] bytes) {
}
};
注意事项:
1、在向onboard SDK device发送数据时,发送的数据需要调用String.getbytes(),转换为byte[]类型的数据;
2、在onboard device端,由于接收到的数据为int8类型的,因此需要强制转换为char类型的数据,再减去 ‘0’才可得到发送的值;
3、接收到的数据需要进行组装才可得到大于一位的数据。
[Github链接](https://github.com/MrJoeyM/sky-app)
以上是关于Dji Mobile SDK 基础实现的主要内容,如果未能解决你的问题,请参考以下文章
尝试流式传输到自定义 RTMP 时,DJI Mobile SDK 中出现 LiveStreamManager 错误 -3?