DJI Drone SDK Implementation for Android
Serhiy Nadolinskyi

Intro

In short, this article is about drone Android development using drone’s own SDK or DJI Software Development Kit.

DJI is a technology company from China established in 2006 by Frank Wang with a headquarter in Shenzhen, Guangdong province, China. In accordance with The Economist, the company is in the foreground of the civilian drone industry.

The good news is that DJI has its SDK and drone`s remote controllers allow us to connect iOS or Android devices. DJI mobile SDK level 1 allows us to get a raw sensors data, taking photos, get real-time video, etc. DJI mobile SDK level 2 can even control the drone itself, but we will not consider that here.

Objective

So let’s do something useful with that =) For example, let’s measure a distance from the point below our drone to some object in the center of the real-time video view. As far or fast as the drone’s camera outputs its attitude independent to drone’s attitude – we can do it like a piece of cake. So all we going to need is drone’s altitude and camera pitch.

The Math

DJI

On this picture, you can see simplified math model. Point A is our drone, AC segment is an altitude and CB segment is distance, which we need to find out. The math is really simple at this point as far as we assume that Earth surface is flat and has no height differences. So the formula is

CB = AC * tan(α)

And as you might already calculate result is 10.

DJI SDK Overview

You can see below a scheme of connections between system units.

Mostly, the remote controller used for drone and cameras’ gimbal control, and connected Tablet\Phone used for real-time video output, some sensors information etc.

Report-2

SDK has  2 levels. The first level allows us to get Real-Time Video, taking photos, videos, gimbal control, retrieving battery info, getting drone sensors data, etc.(for more proceed to the developer’s site) To get the DJI SDK level 1 you need to register at https://dev.dji.com/ and request an access to SDK. Approval may take up to 8 business days. By the way, if you need SDK level 2 for non-profit purposes you can request it for free, but you will need to specify your projects goals and detailed implementation plan, give some IDs, like driving license, etc.

DJI SDK Setup

You can find an example in the SDK archive how to import the SDK into the Eclipse and  Android Studio. The brief plan is:

  • Download archive which contains SDK, docs, and sample.

  • Unzip and import into IDE SDK project

  • Setup ~meta-data API KEY and Internet permission into Manifest

    <meta-data
      android: name="com.dji.sdk.API_KEY"
      android:value="YOUR_API_KEY" />

    NOTE: Internet permission is needed at least for the first app launch to SDK activation.

  • Init SDK in your Application class

    private void onInitSDK() {
      DJIDrone.initWithType(getApplicationContext(),
         DJIDroneTypeDef.DJIDroneType.DJIDrone_Inspire1);
      DJIDrone.connectToDrone();
      }

  • heck SDK permissions

    Example wrap this code into a separate thread, so you should do the same. Response code 0 means that it is all fine – we got an access. And if not – check developer’s site.

    DJIDrone.checkPermission(getApplicationContext(), new DJIGerneralListener() {
      @Override
      public void onGetPermissionResult(int i) {
        Log.e(LOG_TAG, "onGetPermissionResult" + i);
        ...
      }
    });

    At this point, we can already have some output/input from our drone. We have here 2 attentions points:

    • Real-time video
    • Drone altitude
    • Camera attitude

Getting Real-time Video

DJI SDK has own view class for video displaying – DjiGLSurfaceView. Apparently, we need to transmit raw frames there, which we a receiving from DJIReceivedVideoDataCallBack. And DJIReceivedVideoDataCallBack had to be setted into DJI Camera as a callback.

mDjiGLSurfaceView = (DjiGLSurfaceView) findViewById(R.id.surface);
mDjiGLSurfaceView.start();
DJIReceivedVideoDataCallBack mReceivedVideoDataCallBack = new DJIReceivedVideoDataCallBack(){
@Override
public void onResult(byte[] videoBuffer, int size){
    mDjiGLSurfaceView.setDataToDecoder(videoBuffer, size);
}
};
DJIDrone.getDjiCamera().setReceivedVideoDataCallBack(mReceivedVideoDataCallBack);

You can check if camera is connected by DJIDrone.getDjiCamera().getCameraConnectIsOk() method; And in onDestroy method not forget to clean the mess.

@Override protected void onDestroy() {
mDjiGLSurfaceView.destroy();
DJIDrone.getDjiCamera().setReceivedVideoDataCallBack(null);
super.onDestroy()

}

Drone Attitude

To get drone altitude we need to connect to the DJI Main Controller class, which can give you a lot of telemetry at this point, like remain power, velocity, attitude, etc. But all we need is altitude. We need to have in mind that in SDK v 2.0 MC altitude measurements unit is decimeters, which means that if a callback returns us altitude 50 – it actually means 5 meters.

mMcuUpdateStateCallBack = new DJIMcuUpdateStateCallBack() {
@Override
public void onResult(final DJIMainControllerSystemState state) {
Log.d(LOG_TAG, "altitude " + state.altitude);
//Storing altitude somewhere
}
};
DJIDrone.getDjiMC().setMcuUpdateStateCallBack(mMcuUpdateStateCallBack);
DJIDrone.getDjiMC().startUpdateTimer(UPDATE_INTERVAL_MS);
//Don't forget to stop updates in the end
DJIDrone.getDjiMC().stopUpdateTimer();

Camera Attitude

To get camera attitude we need to connect to the gimbal class. It is similar to MC connection. But pitch at DJI SDK v 2.0 has its own weird measurement units – 0  means we are looking at nadir and 750 means that pitch is 90°. In DJI SDK v2.1 they changed the output to degrees.

mGimbalUpdateAttitudeCallBack = new DJIGimbalUpdateAttitudeCallBack() {
@Override
public void onResult(final DJIGimbalAttitude attitude) {
Log.d(LOG_TAG, "pitch " + attitude.pitch);
}
};
DJIDrone.getDjiGimbal().setGimbalUpdateAttitudeCallBack(mGimbalUpdateAttitudeCallBack);
DJIDrone.getDjiGimbal().startUpdateTimer(UPDATE_INTERVAL_MS);
//Cleaning the mess
DJIDrone.getDjiGimbal().stopUpdateTimer();

We have all data collected at this point. So let’s implement the math:

The Math Implementation

Well, after theoretical background from The Math implementation we can easily implement a method, that calculates the distance.

public static double getDistance(double alt, double pitch) throws Exception {
double cameraAngle = (90d / 750d) * pitch;  //Converting camera pitch from parrots to degrees
if(cameraAngle >= 90){
  throw new Exception("Location unavail");
}
alt = alt * .1; //Converting decimeters to meters
final double distance = alt * Math.tan(Math.toRadians(cameraAngle));
return distance;
}

Final Thoughts

Unfortunately, this is not so accurate method, because Earth’s surface has different heights on different points, thus calculated distance will be different from real.Drone’s altitude value related to the start point, and if it flies away somewhere – the real altitude may change. We can go forward to request heights from some API, or cache .hgt files of our region, but it will lead us to more complicated math.

Nevertheless, this is just one example of how drones may be used. This may be utilized as the basis to create a drone Android app or drone iOS app. Just mind that not all drone software is available for tweaking and that makes the overall task a bit complicated.

Got a project idea?

Master of Code designs, builds, and launches exceptional mobile, web, and conversational experiences.

















    By continuing, you're agreeing to the Master of Code
    Terms of Use and
    Privacy Policy and Google’s
    Terms and
    Privacy Policy

    Also Read

    All articles