DJI Drones library for B4A

Peter Simpson

Expert
Licensed User
Longtime User
Nice @ilan, but you should try one of these whilst flying 3D :p
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Hi Erel, how is this going to work? Bluetooth? WiFi? both are either outside the range or outside the LAN router.
The Android device connects to the remote controller with a USB cable. It doesn't communicate with the aircraft directly, only through the remote controller.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
This is the best use of drones harnessed with the power of Xbox Kinect that I've seen. Well worth watching till near the end when he has three independently controlled drones catching a ball and actually throwing it back at him.
 

ilan

Expert
Licensed User
Longtime User
wow this is really cool erel. so all states on the left you can get from the device with the sdk?

is it possible to control it through an b4a app like telling him set speed to xm/sec and the drone will fly that speed?
or give him other commands instead of using the pads on the control?

btw, what device are you using?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
On the right side you see a standard Google Map.
On the left side you see the drone camera feed that is coming from the DJI SDK and all the data that is also coming from the SDK.

You can really do many things, both high level and low level. That's include controlling the speed.

btw, what device are you using?
Nexus 5X

Complete code for this example:
B4X:
Sub Process_Globals
   Private sdk As DJISDKManager
   Private timer1 As Timer
   Private aircraft As DJIAircraft
   Private controller As DJIFlightController
   Private camera As DJICamera
   Private csu As CallSubUtils
   Private lastLocation As DJILocation3D
   Private aircraftName As String
   Private missionManager As DJIMissionManager
End Sub

Sub Globals
   Private map As GoogleMap
   Private MapFragment1 As MapFragment
   Private pnlCamera As Panel
   Private airplaneMarker As Marker
   Private lblText As Label
   Private lblIMU As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   If FirstTime Then
     sdk.Initialize("sdk")
     timer1.Initialize("timer1", 100)
     csu.Initialize
   End If
End Sub

Sub MapFragment1_Ready
   map = MapFragment1.GetMap
   Dim cp As CameraPosition
   cp.Initialize(0, 0, 16)
   map.MoveCamera(cp)
   airplaneMarker = map.AddMarker(0, 0, "airplane")
End Sub


Sub SDK_RegisteredResult (Success As Boolean, ErrorMessage As String)
   If Success Then
     Log("Registered successfully!")
     sdk.StartConnectionToProduct
   Else
     Log(ErrorMessage)
   End If
End Sub

Sub SDK_ProductChanged (AircraftData As Object)
   If AircraftData <> Null Then
     Log("Product connected")
     aircraft.Initialize("aircraft", AircraftData)
     If aircraft.Connected Then
       AfterAircraftConnected
     End If
   Else
     Log("No connected product")
   End If
End Sub

Sub Aircraft_ProductConnectivityChanged (Connected As Boolean)
   Log($"Aircraft_ProductConnectivityChanged: ${Connected}"$)
   If Connected Then
     AfterAircraftConnected
   Else
     Log("Aircraft disconnected!")
     lblText.Text = $"Disconnected!!!"$
     If lastLocation.IsInitialized Then
       lblText.Text = lblText.Text & $"
LastLoction: $1.4{lastLocation.Latitude} / $1.4{lastLocation.Longitude}"$
     End If
     timer1.Enabled = False
   End If
End Sub

Sub AfterAircraftConnected
   If aircraft.CameraReady = False Then
     Log("Camera not ready")
     If aircraft.Connected Then csu.CallSubPlus(Me, "AfterAircraftConnected", 500) ' try again in 500 ms
     Return
   End If
   controller.Initialize("controller", aircraft)
   Log($"simulator: ${controller.SimulatorStarted)}"$
   camera.Initialize("camera", aircraft)
   pnlCamera.AddView(camera.CreateVideoView, 0, 0, pnlCamera.Width, pnlCamera.Height)
   timer1.Enabled = True
   aircraftName = "N/A"
   aircraft.GetName
   missionManager.Initialize("missionManager", aircraft)
End Sub

Sub Aircraft_ResultWithValue (ActionName As String, Success As Boolean, ErrorMessage As String, Value As Object)
   Log($"Aircraft_ResultWithValue: ${ActionName}, Success: ${Success}, Error: ${ErrorMessage}, Value: ${Value}"$)
   If Success Then
     Select ActionName
       Case "GetName"
         aircraftName = Value
     End Select
   End If
   
End Sub


Sub Activity_Resume
   If sdk.Registered Then sdk.StartConnectionToProduct
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   timer1.Enabled = False
   pnlCamera.RemoveAllViews 'remove the video feed view (if it was added)
End Sub


Sub Timer1_Tick
   If controller.IsInitialized Then
     Dim St As DJIFlightControllerCurrentState = controller.CurrentState
     Dim cp As CameraPosition
     cp.Initialize(St.AircraftLocation.Latitude, St.AircraftLocation.Longitude, map.CameraPosition.Zoom)
     map.MoveCamera(cp)
     Dim ll As LatLng
     ll.Initialize(St.AircraftLocation.Latitude, St.AircraftLocation.Longitude)
     Dim distanceToMyLocation As String = "N/A"
     If map.MyLocation.IsInitialized Then
       Dim loc1, loc2 As Location
       loc1.Initialize2(St.AircraftLocation.Latitude, St.AircraftLocation.Longitude)
       loc2.Initialize2(map.MyLocation.Latitude, map.MyLocation.Longitude)
       distanceToMyLocation = $"$1.0{loc1.DistanceTo(loc2)}"$
     End If
     airplaneMarker.Position = ll
     Dim MarkerJO As JavaObject = airplaneMarker
     Dim rotate As Float = controller.Heading
     MarkerJO.RunMethod("setRotation", Array(rotate))
     Dim speed As Double = Sqrt(Power(St.VelocityX, 2) + Power(St.VelocityY, 2))
     lblText.Text = $"Aircraft: ${aircraftName}
Simulator: ${controller.SimulatorStarted}
IMUPreheating: ${St.IMUPreheating}
GPSStatus (5 = good): ${St.GPSStatus}
FlightMode: ${St.FlightMode}
Battery: ${St.RemainingBattery}
Distance to me: ${distanceToMyLocation} m
Altitude: $1.0{St.AircraftLocation.Altitude} m
Speed: $1.0{speed} m/s
VelocityZ: $1.1{-St.VelocityZ} m/s
"$
     lastLocation = St.AircraftLocation
   End If
   
End Sub

Sub MapFragment1_Click (Point As LatLng)
   If missionManager.IsInitialized And missionManager.Connected Then
     If Msgbox2("Start hotspot mission?", "", "Yes", "Cancel", "No", Null) = DialogResponse.POSITIVE Then
       Dim hsm As DJIHotSpotMission
       hsm.Initialize(Point.Latitude, Point.Longitude, 30, 5)
       missionManager.PrepareMission(hsm)
     End If
   End If
End Sub


Sub MissionManager_Result (ActionName As String, Success As Boolean, ErrorMessage As String)
   Log($"MissionManager_Result Action: ${ActionName}, Success: ${Success}, Error: ${ErrorMessage}"$)
   If Success Then
     Select ActionName
       Case "PrepareMission"
         missionManager.StartMission
     End Select
   Else
     ToastMessageShow(ErrorMessage, True)
   End If
End Sub

Sub Controller_Result (ActionName As String, Success As Boolean, ErrorMessage As String)
   Log($"Controller_Result Action: ${ActionName}, Success: ${Success}, Error: ${ErrorMessage}"$)
End Sub

Sub Controller_IMUStateChanged (State As DJIIMUState)
   If State.AccelerometerStatus = "" Then Return
   lblIMU.Text = $"Acceleromter: ${State.AccelerometerStatus}
Calibration: ${State.CalibrationStatus}
Gyroscope: ${State.GyroscopeState}"$
End Sub

Sub Controller_ResultWithValue (ActionName As String, Success As Boolean, ErrorMessage As String, Value As Object)
   Log($"Controller_ResultWithValue Action: ${ActionName}, Success: ${Success}, Error: ${ErrorMessage}, Value: ${Value}"$)
End Sub

Sub btnStopMission_Click
   If missionManager.IsInitialized And missionManager.Connected Then
     missionManager.StopMission
   End If
End Sub
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Fantastic @Erel

In early 2010 I started creating my own Quadricopter using inertial system scavaged from a Wii Motion PLus
Then came the first MEMs at really cheap prices, and we went on having a lot of fun with these pieces of hardware + embeded software.

Then came the moment where chineese started to sell cheap fully working quadcopters and I stopped this stuff !

Seeing what is possible with this sdk almost pushes me to buy a new quadcopter. Will it work with the Mavik ?

Here is a video of my quadcopter fully "handmade" !

It was five years ago !

Another question : could you send "postionning orders" to the drone with the sdk ? I mean send a lat/lon coordinate a let the drone fly to this point ?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
I mean send a lat/lon coordinate a let the drone fly to this point ?
as far as i understand the postings here it is just working like that. In this case it is a mission

B4X:
Sub MapFragment1_Click (Point As LatLng)
   If missionManager.IsInitialized And missionManager.Connected Then
     If Msgbox2("Start hotspot mission?", "", "Yes", "Cancel", "No", Null) = DialogResponse.POSITIVE Then
       Dim hsm As DJIHotSpotMission
       hsm.Initialize(Point.Latitude, Point.Longitude, 30, 5)
       missionManager.PrepareMission(hsm)
     End If
   End If
End Sub
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
@Erel has Amazon been in touch yet? Automated parcel delivery? I heard rumour that they were looking into this, although I suspected it was just a hoax (now I'm not so sure!) ;)
 

freedom2000

Well-Known Member
Licensed User
Longtime User
as far as i understand the postings here it is just working like that. In this case it is a mission

B4X:
Sub MapFragment1_Click (Point As LatLng)
   If missionManager.IsInitialized And missionManager.Connected Then
     If Msgbox2("Start hotspot mission?", "", "Yes", "Cancel", "No", Null) = DialogResponse.POSITIVE Then
       Dim hsm As DJIHotSpotMission
       hsm.Initialize(Point.Latitude, Point.Longitude, 30, 5)
       missionManager.PrepareMission(hsm)
     End If
   End If
End Sub

Oh yes you are right it seems to work !
Then should be very easy to navigate from waypoint to waypoint
 

aaronk

Well-Known Member
Licensed User
Longtime User
Google/Apple/Whoever has been working on a self driving cars.. Maybe they should of been working on self flying drones.

How cool would it be to jump in one of these and set a location and it fly's you there.

Soon we won't be going to get a car license we will all be going to get a drone license.

upload_2017-1-13_10-2-10.png


 

Beja

Expert
Licensed User
Longtime User
This is the best use of drones harnessed with the power of Xbox Kinect that I've seen. Well worth watching till near the end when he has three independently controlled drones catching a ball and actually throwing it back at him.

The collective maneuvering is achieved by using the controlling server and indoor GPS..
http://www.robotshop.com/en/indoor-navigation-positioning-beacon-433mhz.html
Outdoor or satellite GPS can not achieve these results due to high rate of error more than a meter (unless military or the new European GPS system)
The GPS can cameras are connected to the server, not directly to the drones, and it's the server that's controlling the drones.
(source: presenter)
 

ilan

Expert
Licensed User
Longtime User
Sounds really cool.
We could define an area where the drone may not cross and also limit the speed with the app and like this let kids play with it.
 
Top