B4A Library DJI Drones

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jan 15, 2017.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    DJI Drones are very popular and advanced drones. DJI provides an Android app that is used during flights to control the drone.
    The Android device connects to the remote controller with a USB cable (USB debug mode should be enabled).

    DJI are also providing a feature-rich SDK that allows us, developers, to create alternative applications to control the drone. It is quite amazing to be able to easily program a drone to do all kinds of sophisticated tasks.

    The following video is an example created with B4A and the DJI library. On the left side you can see the drone camera feed and various parameters. On the right side there is a standard Google Map. Touching on a point in the map starts a hotspot mission. The drone will rotate automatically around the spot.



    How to use

    1. Register with DJI: https://developer.dji.com/ and create an app in the User Center page. You will see an App Key that you need to add to the manifest editor.
    The bundle identifier must match your app package name.
    2. Add this code to the manifest editor:
    Code:
    '************ DJI SDK '***********
    SetApplicationAttribute(android:name, "anywheresoftware.b4a.objects.BaseApplication")
    SetApplicationAttribute(android:largeHeap, 
    "true")
    AddPermission(android.permission.BLUETOOTH)
    AddPermission(android.permission.BLUETOOTH_ADMIN)
    AddPermission(android.permission.VIBRATE)
    AddPermission(android.permission.INTERNET)
    AddPermission(android.permission.ACCESS_WIFI_STATE)
    AddPermission(android.permission.WAKE_LOCK)
    AddPermission(android.permission.ACCESS_COARSE_LOCATION)
    AddPermission(android.permission.ACCESS_NETWORK_STATE)
    AddPermission(android.permission.ACCESS_FINE_LOCATION)
    AddPermission(android.permission.CHANGE_WIFI_STATE)
    AddPermission(android.permission.MOUNT_UNMOUNT_FILESYSTEMS)
    AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
    AddPermission(android.permission.READ_EXTERNAL_STORAGE)
    AddPermission(android.permission.SYSTEM_ALERT_WINDOW)
    AddPermission(android.permission.READ_PHONE_STATE)
    AddManifestText(

    <uses-feature android:name=
    "android.hardware.camera" />
    <uses-feature android:name=
    "android.hardware.camera.autofocus" />
    <uses-feature
      android:name=
    "android.hardware.usb.host"
      android:required=
    "false" />
    <uses-feature
      android:name=
    "android.hardware.usb.accessory"
      android:required=
    "true" />
    <!-- Permissions 
    and features -->
    )
    AddApplicationText(
      <uses-library android:name=
    "com.android.future.usb.accessory" />
      <meta-data
      android:name=
    "com.dji.sdk.API_KEY"
      android:value=
    "XXXXXXXXXXXXXXX" />
      <
    activity
      android:name=
    "dji.sdk.sdkmanager.DJIAoaControllerActivity"
      android:theme=
    "@android:style/Theme.Translucent" >
      <
    intent-filter>
      <action android:name=
    "android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
      </
    intent-filter>
      <meta-data
      android:name=
    "android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
      android:resource=
    "@xml/accessory_filter" />
      </
    activity>
      <
    service android:name="dji.sdk.sdkmanager.DJIGlobalService" >
      </
    service>
    )
    Replace XXXXXXXXXX with your App key.
    3. Add to the main activity code:
    Code:
    #BridgeLogger: true
    #MultiDex: true
    #AdditionalJar: dji-sdk-provided-4.4.1, ReferenceOnly
    4. Initialize the SDK when the application starts. The RegisteredResult event will be raised. If successful then call sdk.StartConnectionToProduct to start a connection.
    4. The ProductChanged event will be raised. If AircraftData is not Null then you are good to go and can initialize the DJIAircraft object and the other features (see how it is done in the example).
    5. Most of the operations are asynchronous. They are built to work with the new Wait For keyword.
    6. Expect disconnections and other errors.

    See the attached example.

    Tips & Notes

    - Make sure that you are able to connect to the drone with the DJI app. Not all Android devices have proper support for USB connected devices.
    - You will see a dialog that asks you whether you always want to start the DJI app when the USB is connected. Click on "This time only". Otherwise you will need to uninstall the DJI app as it will get an exclusive permission to use the USB device.
    - Use B4A-Bridge to connect the IDE to the Android device.
    - Supported drones: https://developer.dji.com/mobile-sd.../product_introduction.html#supported-products
    - Use the simulator to test your app. It works well.
    https://developer.dji.com/mobile-sd...on-development-workflow/workflow-testing.html

    - Don't fly the drone indoors.
    - Check the GPS status. All the nice automatic features will fail without the GPS. Should be 4 or 5.
    - Make sure that the home location is set before flight.
    - Always be prepared to manually control the drone.

    Download the native libraries and copy them to the additional libraries folder:
    www.b4x.com/android/files/dji.zip
    Download the attached library and copy it as well.

    Version 4.41 is released. It is based on DJI SDK v4.41. It supports waypoint missions
    It requires B4A v8+.
    The minimum Android version is 5.0.

    Two examples are attached:
    DroneFirst - A basic example for generic flights.
    DroneMission - Example of using WaypointMissionOperator to create a waypoint mission.
    The DroneMission demonstrates how to request the permissions when setting targetSdkVersion to 26.

    The library source code is attached.
     

    Attached Files:

    Last edited: May 2, 2018
  2. susu

    susu Well-Known Member Licensed User

    It's amazing! Thanks Erel.
     
  3. freedom2000

    freedom2000 Well-Known Member Licensed User

    Really Great !
     
  4. wonder

    wonder Expert Licensed User

    MarcoRome, freedom2000 and Erel like this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    Another example is attached. This one adds a MJPEG server to the app. It allows other clients to connect and see the camera feed.
    The url is displayed in the app screen.



    The compiled APK is available here: www.b4x.com/android/files/DroneMJPEG.apk
    Consider it a beta app and always be prepared to manually control the drone.
     

    Attached Files:

    Johan Hormaza, GMan and DonManfred like this.
  6. Widget

    Widget Well-Known Member Licensed User

    That's a very impressive video. If I had one of these drones. I'd never get any programming done. :D

    Is there any way in Android to improve the readability of the text that is superimposed on top of the background video? In the first demo you have black text on top of the video which looks OK if the background is bright. But if the background is dark the black text is hard to read. When operating a drone you really need to be able to read the text clearly.

    The first solution is to put a white border around the black text, or a back border around white text. This will help the text to stand out on any type of background it is superimposed on. See http://stackoverflow.com/questions/8960105/how-to-add-border-to-a-text-in-textview-android
    Is this possible in B4A?

    If that isn't possible, then a quick "solution" would be to create a new Label class (or add the properties to the existing Label) that has a 2 new properties:
    ShadowColor and ShadowOffset. The result would be more readable text on an image or video. See sample image.
     

    Attached Files:

  7. Erel

    Erel Administrator Staff Member Licensed User

    The shadow can be added with this code:
    Code:
    Sub SetTextShadow(lbl As Label)
       
    Dim jo As JavaObject = lbl
       
    Dim radius = 2dip, dx = 0dip, dy = 0dip As Float
       jo.RunMethod(
    "setShadowLayer"Array(radius, dx, dy, Colors.Yellow))
    End Sub
     
  8. Widget

    Widget Well-Known Member Licensed User

    This is an improvement. (See image)

    But there are 2 problems.
    1. Doesn't "setShadowLayer" require hardware acceleration to be turned on? And it is turned on for targetSDK >= 14 but not for Android 6?
    2. The shadow it produces uses a blur, which is fine on large text but is barely noticeable on smaller text. Is there a way to eliminate the blur so it works better on smaller text?
    TIA

    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        'Activity.LoadLayout("Layout1")
        Activity.Color = Colors.Black
      Label1.Initialize(
    "")
        Label1.Text      = 
    "Testing"
        Label1.TextSize  = 
    100
        Label1.TextColor = 
    Colors.Blue
        
    Activity.AddView(Label1, 20Dip20Dip500dip500dip)
       
        
    Private jo As JavaObject = Label1
    '    Private Radius = 4dip, dx=0dip, dy=0dip As Float
        Private Radius = 10dip, dx=0dip, dy=0dip As Float
        jo.RunMethod(
    "setShadowLayer"Array(Radius, dx,dy, Colors.Yellow))
    End Sub
     

    Attached Files:

  9. omarruben

    omarruben Member Licensed User

    ** my mistake *** i had a old b4a version
    i did get the keys from DJI and google maps...
    al works fine now AWESOME

    Hi, i have downloaded the demo files , i ahve error on 1.bal not found and unknow typoe googlge map, can you please check i am so exited about this library, thank you
     
    Last edited: Jan 18, 2017
    Erel likes this.
  10. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this.

    Which drone are you using?
     
  11. gvoulg

    gvoulg Member Licensed User

    Great Library Erel.
    I have a Phantom 3 advanced and I will try to test it.
    What about waypointmission? Are you planning to wrap the rest of the functions in DJI's SDK?
    George
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    I was waiting for demand :)

    It should be very simple to add support for other types of missions. I can also show you how it can be done with JavaObject.

    Start with this example and once you get it working (the USB exclusive permission can be a bit annoying) we can extend it.
     
    gvoulg likes this.
  13. DonManfred

    DonManfred Expert Licensed User

    If there is anyone who want to spend a Phantom 3 to me i would be glad to extend the lib :D

    PD: okok, i need to win the lottery i guess ;-)
     
    Erel and MarcoRome like this.
  14. gvoulg

    gvoulg Member Licensed User

    first impressions... Fantastic!!!!!!
    I manage to get it to work on the dji emulator. (drone connected to the pc)
    Bad weather so its difficult to test on field but all process is very promising.
    Most difficult thing was to install DJI's USB driver to set the emulator to work (has to do with signature of driver)
    Thank you Erel!!
    Waiting for more;)
    George
     
    Erel likes this.
  15. TomDuncan

    TomDuncan Active Member Licensed User

    I have a phantom 3 standard which does not use the USB connection from the transmitter to the tablet.
    Will this still work? I will test today and check.
    What a great addition this will make, my love of filming, drones and b4x, yummy.
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    I think that it will work.
     
  17. TomDuncan

    TomDuncan Active Member Licensed User

    Just did a test using your compiled apk.
    It just sits with a blank screen on left and right (google)
    What might be happening is the lack of usb connection with the Phantom 3 standard
    All I do is connect the Android device using the dji wifi PHANTOM3_2fd145

    Any thoughts.

    Also while I was generating my own using B4A it would go through and look as though it sent the apk via Bridge but nothing would happen.
    Maybe this has something to do with file size or Tom finger errors.

    Tom
     
    Last edited: Jan 23, 2017
  18. Erel

    Erel Administrator Staff Member Licensed User

    It can take a minute for the app to be installed due to the library size.

    Connect the Android device with a USB cable to the PC and enable USB debug mode. It will allow you to see the logs.
     
  19. PABLO2013

    PABLO2013 Active Member Licensed User

    regards I have a standard phantom 3, but the connection is via wifi (produced by the control).
    I think the phantom advanced and pro if they are connected by usb cable as these controls if you have the port, in my case as you could do ....
    on the other hand I did some tests, for now ask me to be the first time is due Register the application ... but being using the wifi with the control can not ... some help .... previous examples about which phantom version were made, thanks
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    What happens when you run the example from the first post?
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice