Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).

How to control DJI Tello Mini-Drone using Python and Docker

4 min read

If you want to take your drone programming skills to the next level, DJI Tello is the right product to buy. Tello is $99 mini-drone that can be programmed using Python, Scratch and Swift. It is rightly called as an indoor quadcopter that comes equipped with an HD camera, giving you a bird’s-eye view of the world. It’s damn easy to fly. Start flying by simply tossing Tello into the air. Slide on your mobile screen to perform 8D flips cool aerial stunts. It’s quite lightweight and dimensions are 3.8 x 3.6 x 1.6 inches, weighing only 2.8 ounces. One of the amazing feature is its VR headset compatibility. You can fly it with a breathtaking first-person view.

Tello is a small quadcopter that features a Vision Positioning System and an onboard camera. Tello is a great choice if you want to learn AI analytics at the Edge. Imagine you’re building an application that captures video streaming from the drone and sends it to AI computers like Jetson Xavier or Nano for AI analytics, storing the time-series data in Redis running over Cloud and visualizing it over Grafana. There’s ample amount of learning opportunity using these affordable drones for researchers, engineers and students.

Notable Features of Tello Drone

  • DJI Tello has an excellent flight time of 13 minutes. (enough for your indoor testing!)
  • It comes with a 5MP camera. It can shoot 720p videos, and has digital image stabilization!
  • Approximately 80 g (Propellers and Battery Included) in weight.
  • This small drone has a maximum flight distance of 100 meters and you can fly it in various flight modes using your smartphone or via the Bluetooth controller!
  • It has two antennas that make video transmission extra stable and a high-capacity battery that offers impressively long flight times.
  • It comes with a Micro USB Charging Port
  • Equipped with a high-quality image processor, Tello shoots incredible photos and videos. Even if you don’t know how to fly, you can record pro-level videos with EZ Shots and share them on social media from your smartphone.
Photo Quality Setting in Tello App
  • Tello comes with sensors that helps it finding obstacles and during the landing
DJI Tello Calibration Settings
  • Tello height can be hacked . Check this out: http://protello.com/tello-hacking-height-limit/
  • DJI Tello has a brushed motor, which is cheaper than the brushless motor, but it’s also less efficient. (Sadly, brushed motors are known to burn out sometimes due to low quality or poor implementation. They’re also susceptible to rough impacts).
  • Being a Non-GPS drone, it is very stable. Video quality is quite decent and landing is also accurate. Also fly it during calm winds or no winds conditions otherwise it’ll sway away with he wind. Good for indoors as well and to click some good selfies.
  • DJI Tello is controlled using an application on an iOS or Android mobile phone. It is also possible to control via Bluetooth joystick connected via application.

Getting Started

Hardware Required:

Unboxing DJI Tello Drone
  • DJI Tello Drone (Buy)
  • Charging Cable
  • Battery (Buy)

DJI Tello comes with a detachable Battery of 1.1Ah/3.8V. Insert the 26g battery into the aircraft and charge the battery by connecting the Micro USB port on the aircraft to a charger.

Ways of controlling Your DJI Tello

There are 2 ways you can control your DJI Tello. The first one is using your mobile device, you will need to download Tello or Tello EDU App first. You can also control your Tello via Python or Scratch programming. In this blog, we will see how to control Tello using Python.

Pre-requisite:

  • Linux System( Desktop or Edge device)
  • Python3
  • Tello Mobile app

Press the “Power” button of Tello once. Once it start blinking, open up Tello Android app to discover Tello drone. Open settings and configure WiFi settings like username and password. Connect your laptop to the Tello WiFI network. Follow the below steps to connect via Python script.

Install using pip

pip install djitellopy

For Linux distributions with both python2 and python3 (e.g. Debian, Ubuntu, …) you need to run

pip3 install djitellopy

API Reference

See djitellopy.readthedocs.io for a full reference of all classes and methods available.

Step 1. Connect, TakeOff, Move and Land

The below Python script allows you to connect to the drone, take off, make some movement – Left and Right and then Land smoothly.

from djitellopy import Tello

tello = Tello()

tello.connect()
tello.takeoff()

tello.move_left(100)
tello.rotate_counter_clockwise(90)
tello.move_forward(100)

tello.land()

Step 2. Take a Picture

import cv2
from djitellopy import Tello

tello = Tello()
tello.connect()

tello.streamon()
frame_read = tello.get_frame_read()

tello.takeoff()
cv2.imwrite("picture.png", frame_read.frame)

tello.land()

Step 3. Recording a Video


# source https://github.com/damiafuentes/DJITelloPy
import time, cv2
from threading import Thread
from djitellopy import Tello

tello = Tello()

tello.connect()

keepRecording = True
tello.streamon()
frame_read = tello.get_frame_read()

def videoRecorder():
    # create a VideoWrite object, recoring to ./video.avi

    height, width, _ = frame_read.frame.shape
    video = cv2.VideoWriter('video.avi', cv2.VideoWriter_fourcc(*'XVID'), 30, (width, height))

    while keepRecording:
        video.write(frame_read.frame)
        time.sleep(1 / 30)

    video.release()

# we need to run the recorder in a seperate thread, otherwise blocking options
#  would prevent frames from getting added to the video
recorder = Thread(target=videoRecorder)
recorder.start()

tello.takeoff()
tello.move_up(100)
tello.rotate_counter_clockwise(360)
tello.land()

keepRecording = False
recorder.join()

Step 4. Control the drone using Keyboard

# source https://github.com/damiafuentes/DJITelloPy
from djitellopy import Tello
import cv2, math, time

tello = Tello()
tello.connect()

tello.streamon()
frame_read = tello.get_frame_read()

tello.takeoff()

while True:
    # In reality you want to display frames in a seperate thread. Otherwise
    #  they will freeze while the drone moves.

    img = frame_read.frame
    cv2.imshow("drone", img)

    key = cv2.waitKey(1) & 0xff
    if key == 27: # ESC
        break
    elif key == ord('w'):
        tello.move_forward(30)
    elif key == ord('s'):
        tello.move_back(30)
    elif key == ord('a'):
        tello.move_left(30)
    elif key == ord('d'):
        tello.move_right(30)
    elif key == ord('e'):
        tello.rotate_clockwise(30)
    elif key == ord('q'):
        tello.rotate_counter_clockwise(30)
    elif key == ord('r'):
        tello.move_up(30)
    elif key == ord('f'):
        tello.move_down(30)

tello.land()

In my next blog post, I will showcase how to implement object detection and analytics using Deep Learning, DJI Tello, Jetson Nano and DeepStream.

Controlling Drone using Docker Container

Assuming the following Python Script

import socket

TELLO_IP = '192.168.10.1'
TELLO_PORT = 8889

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', TELLO_PORT))

# Configure the camera settings
sock.sendto(b'command', (TELLO_IP, TELLO_PORT))
sock.sendto(b'set camera 0', (TELLO_IP, TELLO_PORT))

# Fetch the camera values
sock.sendto(b'camera?', (TELLO_IP, TELLO_PORT))

# Receive the values from the drone
data, server = sock.recvfrom(1518)

# Print the received data
print(data.decode(encoding='utf-8'))

Writing the Dockerfile

# Use an official Python runtime as the base image
FROM python:3.8-slim-buster

# Set the working directory
WORKDIR /app

# Copy the script to the image
COPY tello.py .

# Install the necessary dependencies
RUN pip install python-tello

# Set the command to run when the container starts
CMD ["python", "tello.py"]

Building the Docker Container

docker build -t ajeetraina/tello-drone .
docker run --privileged -d -p 8889:8889 ajeetraina/tello-drone

References:

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server
Index