Page 2: Drone Control with Python (Tello Example)

2. Drone Control with Python (Tello Example)

Python is excellent for direct command-line control of drones like the DJI Tello. The Tello uses UDP communication on port 8889 for commands and port 8890 for status.

2.1. Connecting to Tello and Sending Basic Commands

First, connect your computer to the Tello's Wi-Fi network. Then, you can use Python's `socket` module to send commands.

import socket
import time

tello_ip = '192.168.10.1'
tello_port = 8889
local_port = 9000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', local_port))

def send_command(command):
    sock.sendto(command.encode('utf-8'), (tello_ip, tello_port))
    response, _ = sock.recvfrom(128)
    print(f"Command: {command}, Response: {response.decode('utf-8')}")

# Enter SDK mode
send_command('command') 
time.sleep(1)

# Basic flight
send_command('takeoff')
time.sleep(5)
send_command('land')
time.sleep(3)

sock.close()

2.2. Programming a Simple Flight Path

You can sequence commands to create flight paths, such as flying a square.

# ... (previous setup code) ...

send_command('command')
time.sleep(1)
send_command('takeoff')
time.sleep(5)

send_command('up 50')
time.sleep(3)
send_command('forward 100')
time.sleep(3)
send_command('right 100')
time.sleep(3)
send_command('back 100')
time.sleep(3)
send_command('left 100')
time.sleep(3)
send_command('land')

sock.close()

Remember: Always fly in a safe, open environment and ensure your drone's battery is charged.