#Coding4Fun – How to control your #drone with 20 lines of code! (6/N)

Buy Me A Coffee

Hi!

Today is code time also! And a continuation from my previous sample.

Yesterday I show how to read a static value: the battery. And, when you work with a device like a drone there are other important values to analyze in order to send commands to the drone. Like altitude, position, time of flight etc.

So, based on yesterday sample, I’ll show how to create a simple Python app, that will display the accelerator X value. In the following video you can see the how the value start “static” during the 1st couple of seconds, until I pickup the device and I moved it around.

Important: don’t blame me about the low battery. Playing with the drone drains the battery very fast!

Once again, the code is very straight forward. It runs a loop furing 10 seconds, showing the battery and accelX information.

Just as a reminder, this is the information we get from the drone:

pitch:0;roll:1;yaw:0;vgx:0;vgy:0;vgz:0;templ:79;temph:82;tof:10;h:0;bat:39;baro:50.42;time:0;agx:-8.00;agy:-17.00;agz:-999.00

As you can read, all the information is condensed in a single line and we can split and get:

  • pitch
  • roll
  • yaw
  • vgx
  • vgv
  • vgz
  • templ (temperature low)
  • temph (temperature high)
  • tof (time of flight)
  • h (height)
  • b (battery)
  • baro (barometer)
  • time
  • agx
  • agy
  • agz

Happy coding!

Greetings

El Bruno

References

#Coding4Fun – How to control your #drone with 20 lines of code! (5/N)

Buy Me A Coffee

Hi!

Today is code time! In my previous post I share some code to send commands to the drone. Today I’ll show how to read the information from the drone. Before I start, someone asked if my kids are having fun with the drone. A image will be enough to answer this.

Reading drone information

So, as far as I understand, the drone is constantly sending information to the connected client. That’s why we have the following function running all the time in a separate thread:

def receiveData():
    global response
    while True:
        try:
            response, _ = clientSocket.recvfrom(1024)
        except:
            break
# ... more code
response = None
recThread = threading.Thread(target=receiveData)
recThread.daemon = True
recThread.start()

The response is stored in a global variable named response. And it’s very easy to understand the information that we the drone sends back. This is a sample received data.

pitch:0;roll:1;yaw:0;vgx:0;vgy:0;vgz:0;templ:79;temph:82;tof:10;h:0;bat:39;baro:50.42;time:0;agx:-8.00;agy:-17.00;agz:-999.00

As you can read, all the information is condensed in a single line and we can split and get:

  • pitch
  • roll
  • yaw
  • vgx
  • vgv
  • vgz
  • templ (temperature low)
  • temph (temperature high)
  • tof (time of flight)
  • h (height)
  • b (battery)
  • baro (barometer)
  • time
  • agx
  • agy
  • agz

In python this is a simple routine, and after split this into a list, the battery value is on index 21:

ls = 'pitch:0;roll:1;yaw:0;vgx:0;vgy:0;vgz:0;templ:79;temph:82;tof:10;h:0;bat:39;baro:50.42;time:0;agx:-8.00;agy:-17.00;agz:-999.00'
>>> ls1 = ls.replace(';', ':').split(':')
>>> ls1
['pitch', '0', 'roll', '1', 'yaw', '0', 'vgx', '0', 'vgy', '0', 'vgz', '0', 'templ', '79', 'temph', '82', 'tof', '10', 
'h', '0', 'bat', '39', 'baro', '50.42', 'time', '0', 'agx', '-8.00', 'agy', '-17.00', 'agz', '-999.00']
>>> ls1[21]
'39'
>>>

So with this, to get the battery level of the drone will require this code:

The output is not very amazing, but it works!

powershell console displaying the drone battery level.

Happy coding!

Greetings

El Bruno

References

#Coding4Fun – How to control your #drone with 20 lines of code! (4/N)

Buy Me A Coffee

Hi!

The DJI Drone is a great device, you can have a lot of fun and also learn a lot. However, because the main way to interact with the device is via WIFI and UDP, you need to connect to the device WIFI. And, connecting and disconnecting to the is some times not a very happy moment.

So, there are 2 main tips to work in a more agile way with this.

Connect to the Drone WiFi via Command Line

You probably already know the command

netsh wlan connect name=<YOUR WIRELESS NAME> 

This is the quickest way to connect to a wireless. Once you have your DJI Tello name and your home / work / whatever network name in your Windows Terminal, just key up until you find it.

Bonus: If you want to know the names of the network, you may want to use the command

netsh wlan show profiles

As you can see in the following image, in my home computer I got several IoT Devices connections, my phone, the DJI Tello WiFi and my home one

The command to connect to my drone will be

netsh wlan connect name=TELLO-589327 

Once I want to connect to my default network, I simply disconnect from the drone and wait until my machine connect to the default Wireless Network. The command to disconnect is

netsh wlan disconnect

So, as a recap the only 2 commands you need are:

netsh wlan connect name=<TELLO-Your device number>
netsh wlan disconnect

Super easy!

Ping your drone to check when you are connected

Once you connect to your drone WIFI, it may take a couple of seconds or more to establish the connection. If during this time, you launch an app to interact with the device, be prepared to some funny results.

The way that I use to see when the Wireless connection is ready is as simple as to use a ping -t command with

ping 192.168.10.1 -t

This is the default drone IP, and you can run this in a separate window and you will know when the drone is ready.

Happy coding!

Greetings

El Bruno

References

#Coding4Fun – How to control your #drone with 20 lines of code! (3/N)

Buy Me A Coffee

Hi!

Today I’ll write the equivalent of a Hello World in the drone ecosystem. This is a very complex app which:

  • take off the drone
  • wait a couple of seconds
  • lands the drone

I’ve followed the Python code sample from the SDK and the final code is very complex (see below). And it deserves some remarks

  • Line 11. The function recv() run in a separated thread to receive messages from the drone. The thread is started on line 44
  • Line 19. The function sendMessage() sends messages to the drone. Important, the messages must be UTF-8 encoded. This took me some time, until I figure it out. This time implements a timeout with 5 second until the response is processed in a separated thread
  • Lines 31-41. Connection information and sockets to communicate with the drone
  • Line 48. Main code for the app. Start the SDK mode, Wait 5 seconds, send the take off message, and then send the land message. A very simple exception catch is implemented here.

Important: When using the SDK, the 1st command should be “command”, as I did in line 49.

Here is the code:

Here is the app running at 3X speed so you don’t spend all day watching boring drones videos:

Drone Hello World, take off and land

Happy coding!

Greetings

El Bruno

#Coding4Fun – How to control your #drone with 20 lines of code! (2/N)

Buy Me A Coffee

Hi!

I my previous posts I shared some links about the DJI Tello drone. One of them is the SDK 1.3.0.0. In this document we can find the main commands and descriptions of the specifics commands to use to communicate with the drone.

dji tello drone sdk architecture

The document also links a Python sample file with the following code:

The code is very easy to read:

  • Lines 10-14. Defines the main libraries to be used. I never used sockets and threads in Python, so this is an excellent chance for me to learn about this
  • Lines 16-26. Implements the basic communication via UDP described in the architecture from the SDK. Accessing the drone via IP 192.168.10.1 and port 8889, and bind to localhost with post 9000
  • Lines 28-48. Function to receive data from the drone. It’s executed in an different thread, so here are my 1st multi-threading python app.
  • Lines 50-74. Main App Loop, where it waits for user type command and send the command to the drone. It also checks for Python version, and close the socket before end.

An amazing way to start playing with the drone. Just 5 minutes to connect and have something up and running.

Happy coding!

Greetings

El Bruno

References

#Coding4Fun – How to control your #drone with 20 lines of code! (1/N)

Buy Me A Coffee

Hi !

I’ll start to write a couple of posts about controlling a drone using Python. This all started at the end of 2019 when my friend Daniel (@danielcauser) told me that he was playing around with the DJI Tello Drone (see references). This drone is a small quadcopter that features a Vision Positioning System and an onboard camera.

The drone is a perfect balance between a toy and also a capable device to play around with some code. The price is around U$D100, so it’s not very expensive. And the main specifications are very cool for a device of this price

DJI Tello Specs

  • 720p Videos
  • 5MP Photos
  • Takeoff and Landing from Your Hand
  • Intel Processor
  • Programmable via Scratch SDK
  • Free Tello App with User-Friendly UI
  • Compatible with Bluetooth Controllers

DJI Tello Features

  • Collision detection system
  • Auto Takeoff
  • Auto Landing
  • Low Battery protection features
  • DJI’s flight stabilization technology

This is not a new device, so you may find several reviews online for the device. I’ll keep this mostly focused on the programming side.

One of the key features of the device is focused on the programming capabilities for this drone. DJI provides a couple of applications to interact with the drone and also a Tello EDU app focused on education and introduction to programming and controlling the drone using Scratch.

As part of this Edu package there is a Tello SDK PDF document (see references) where we can find the description for all the commands to control the drone. The communication is via UDP, so we only need to connect to the device Wifi, and … start to have fun!

Happy coding!

Greetings

El Bruno

References