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、设置回调接口并初始化回传视频:

 1 /**
 2 *设置回调接口
 3 */
 4 protected DJICodecManager mCodecManager = null;//视频编码
 5 private DJIFlightControllerDelegate.FlightControllerReceivedDataFromExternalDeviceCallback mRecvCallback = null;
 6 protected CameraReceivedVideoDataCallback mReceivedVideoDataCallBack = null;
 7 protected DJIOnReceivedVideoCallback mOnReceivedVideoCallback = null;
 8 private DJIBaseProduct mProduct = null;//无人机实例
 9 //设置视频的回调
10         mReceivedVideoDataCallBack = new CameraReceivedVideoDataCallback() {
11             @Override
12             public void onResult(byte[] videoBuffer, int size) {
13                 count++;
14                 if (mCodecManager != null) {
15                         mCodecManager.sendDataToDecoder(videoBuffer, size);
16                 } else {
17                     Log.e(TAG, "mCodecManager is null");
18                 }
19             }
20         };
21 
22 /**
23 *初始化回传视频
24 */
25  private void initVideo() {
26         try {
27             mProduct = VirtualStickApplication.getProductInstance();
28         } catch (Exception exception) {
29             mProduct = null;
30         }
31 
32         if (null == mProduct || !mProduct.isConnected()) {
33             mCamera = null;
34         } else {
35             if (null != mVideoSurface) {
36                 mVideoSurface.setSurfaceTextureListener(this);
37             }
38 
39             if (!mProduct.getModel().equals(Model.UnknownAircraft)) {
40                 mCamera = mProduct.getCamera();
41                 if (mCamera != null) {
42                     mCamera.setDJICameraReceivedVideoDataCallback(mReceivedVideoDataCallBack);
43                 }
44             } else {
45                 if (null != mProduct.getAirLink()) {
46                     if (null != mProduct.getAirLink().getLBAirLink()) {
47                         // Set the callback
48                         mProduct.getAirLink().getLBAirLink().setDJIOnReceivedVideoCallback(mOnReceivedVideoCallback);
49                         mProduct.getAirLink().getLBAirLink().setFPVQualityLatency(DJILBAirLink.LBAirLinkFPVVideoQualityLatency.LowLatency,new DJIBaseComponent.DJICompletionCallback() {
50                             @Override
51                             public void onResult(DJIError djiError) {
52                                 if (djiError != null)
53                                     VirtualStickApplication.logError("setFPVQualityLatency()error:", djiError);
54                             }
55                         });
56                     }
57                 }
58             }
59         }
60     }

 

获取无人机参数


获取获取无人机的剩余电量,以百分比的形式返回

 1 private DJIBaseProduct mProduct = null;//无人机实例
 2  try {
 3                 mProduct = VirtualStickApplication.getProductInstance();
 4             } catch (Exception exception) {
 5                 mProduct = null;
 6             }
 7             if(mProduct != null && mProduct.isConnected()) {
 8                 //获取无人机的剩余电量,以百分比的形式返回
 9                 try{
10                     mProduct.getBattery().setBatteryStateUpdateCallback(new DJIBattery.DJIBatteryStateUpdateCallback() {
11                         @Override
12                         public void onResult(DJIBatteryState djiBatteryState) {
13                             if(djiBatteryState == null){
14                                 VirtualStickApplication.logInfo("电量为空");
15                             }else{
16                                 StringBuffer mBuffer = new StringBuffer();
17                                 mBuffer.append(djiBatteryState.getBatteryEnergyRemainingPercent());
18                                 tvBattery.setText(mBuffer.toString()+"%");
19                                 if(djiBatteryState.getBatteryEnergyRemainingPercent() != 0 && djiBatteryState.getBatteryEnergyRemainingPercent() <= 30){
20                                       if(entry){
21                                           AlertDialog.Builder builder =  new  AlertDialog.Builder(VirtualStickActivity.this);
22                                           builder.setTitle("警告" )
23                                                   .setMessage("电池电量不足!" )
24                                                   .setPositiveButton("确认" ,  null )
25                                                   .setNegativeButton("取消" , null)
26                                                   .show();
27                                           entry = false;
28                                       }
29                                 }else{
30                                     entry = true;
31                                 }
32                             }
33                         }
34                     });
35                 }catch(Exception e){
36                     Toast.makeText(VirtualStickActivity.this,"1"+e.toString(),Toast.LENGTH_LONG).show();
37                     e.printStackTrace();
38                 }

 

获取飞行器所在所在高度、飞行速度、无人机姿态、GPS信号 
1、获取flightController 对象;

 1 DJIFlightController flightController;
 2 public void setAircraft(DJIBaseProduct djiAircraft){
 3         if(djiAircraft==null){
 4 
 5             return;
 6         }
 7         flightController=((DJIAircraft)djiAircraft).getFlightController();
 8         if(flightController!=null) {
 9 
10         }
11 
12     }

 

2、通过flightController对象调用方法获取飞行器所在所在高度、飞行速度、无人机姿态、GPS信号; 
所在高度

1 public String getHeight(){
2         if(flightController != null){
3             DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
4             if (currentState != null) {
5                 return currentState.getAircraftLocation().getAltitude() + "";
6             }
7         }
8         return null;
9     }

 

飞行速度

 1 //获取前后速度
 2     public String getX(){
 3         if(flightController != null){
 4         DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
 5             if (currentState != null) {
 6                 currentState.getGpsSignalStatus();
 7                 return currentState.getVelocityX() + "";
 8             }
 9         }
10 
11         return null;
12     }
13 
14     //获取左右速度
15     public String getY(){
16         if(flightController != null){
17             DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
18             if (currentState != null) {
19                 return currentState.getVelocityY() + "";
20             }
21         }
22         return null;
23     }
24 
25     //获取上下速度
26     public String getZ(){
27         if(flightController != null){
28             DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
29             if (currentState != null) {
30                 return currentState.getVelocityZ() + "";
31             }
32         }
33         return null;
34     }

 

无人机姿态(roll、yaw、pitch)

 1 public String getYaw(){
 2         if(flightController != null){
 3             DJIFlightControllerDataType.DJIFlightControllerCurrentState currentState = flightController.getCurrentState();
 4             if (currentState != null) {
 5                 return "pitch:"+currentState.getAttitude().pitch+"  Yaw:"+currentState.getAttitude().yaw
 6                         +"  Roll:"+currentState.getAttitude().roll;
 7             }
 8         }
 9         return null;
10     }

 

GPS信号


实现和onboard SDK的数据透传


发送数据到onboard SDK device 
1、获取flightController 对象,如上所示; 
2、通过flightController对象调用响应函数实现数据发送;

 1 public void sendDataToOnboard(byte[] data){
 2             if(flightController.isOnboardSDKDeviceAvailable()){
 3                 flightController.sendDataToOnboardSDKDevice(data, new DJIBaseComponent.DJICompletionCallback() {
 4                     @Override
 5                     public void onResult(DJIError djiError) {
 6                         if(djiError!=null) {
 7                             VirtualStickApplication.logError("SendDataToOnBoard error:", djiError);
 8                         }else{
 9                             VirtualStickApplication.logInfo("发送指令成功!");
10                         }
11                     }
12                 });
13             }
14         }
15     }

 

从onboard SDK获取数据 
1、获取flightController 对象,如上所示; 
2、通过flightController对象调用响应函数实现数据回传;

 1 /**
 2 *设置回调接口
 3 */
 4 flightController.setReceiveExternalDeviceDataCallback(flightControllerReceivedDataFromExternalDeviceCallback);
 5 /**
 6 *初始化并接受回传数据
 7 */
 8  flightControllerReceivedDataFromExternalDeviceCallback = new DJIFlightControllerDelegate.FlightControllerReceivedDataFromExternalDeviceCallback() {
 9             @Override
10             public void onResult(byte[] bytes) {
11 
12             }
13         };

 

注意事项: 
  1、在向onboard SDK device发送数据时,发送的数据需要调用String.getbytes(),转换为byte[]类型的数据; 
  2、在onboard device端,由于接收到的数据为int8类型的,因此需要强制转换为char类型的数据,再减去 ‘0’才可得到发送的值; 
  3、接收到的数据需要进行组装才可得到大于一位的数据。


[Github链接](https://github.com/MrJoeyM/sky-app)

 

 

以上是关于Dji Mobile SDK 基础实现的主要内容,如果未能解决你的问题,请参考以下文章

Dji Mobile SDK 基础实现

Dji Mobile SDK 基础实现

DJI Mobile SDK 中的避障

DJI Mobile SDK 新教程

尝试流式传输到自定义 RTMP 时,DJI Mobile SDK 中出现 LiveStreamManager 错误 -3?

DJI Mobile SDK,Android Studio Emulator SDK注册失败