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 基礎實現(二)