Advertisement

Drone Programming - How to Control a Drone Using Python?

August 20, 2021 in Aerospace, Programming by DhulkarnaynDhulkarnayn4 minutes

Unmanned Aerial Vehicles (UAVs), commonly known as drones, have become integral to various industries. Programming drones using Python opens up new possibilities, from drone delivery to intricate drone light shows.

Advertisement

One of the fascinating applications of programmable drones is the Drone Light show. Imagine hundreds of drones being controlled by a single computer, performing a show in an orchestrated manner. Achievements like the 26-minute animation by EFYI Group and Tianjin University showcase the complexity involved in drone light shows.

video-cover

Video Credits: Guinness World Records

Programming a Drone: Essential Concepts

To program a drone effectively, understanding the following concepts is crucial:

  1. Flight Control Software
  2. MAVLink
  3. DroneKit-Python
  4. Simulation - Software In the Loop (SITL)

Flight Control Software

Flight Control Software acts as the firmware powering drone flight controllers, similar to an operating system for a Central Processing Unit (CPU). Two major open-source Flight Control Softwares preferred by developers are:

ArduPilot

ArduPilot offers a comprehensive suite of tools suitable for various vehicles, including multirotor drones, fixed-wing and VTOL aircraft, helicopters, ground rovers, boats, and submarines.

ArduPilot Project

Image Credits: ArduPilot dev team, Public domain, via Wikimedia Commons

PX4

PX4 is an open-source flight control software providing a flexible set of tools for drone developers to create tailored solutions for drone applications.

PX4 Logo

Image Credits: PX4

MAVLink is the messaging protocol enabling communication between flight stacks (ArduPilot, PX4) and ground or companion computers.

MAVLink - Micro Air Vehicle Communication Protocol

Image Credits: MAVLink

MAVLink follows a modern hybrid publish-subscribe and point-to-point design pattern, initially released in 2009 by Lorenz Meier.

DroneKit-Python

DroneKit-Python is an Application Programming Interface (API) allowing developers to create Python apps communicating with vehicles over MAVLink protocol.

The DroneKit API provides classes and methods to:

  • Connect to a drone from a script.
  • Get and set drone state/telemetry and parameter information.
  • Receive asynchronous notification of state changes.
  • Guide a drone to a specified position.
  • Send arbitrary custom messages to control drone movement and other hardware.
  • Create and manage waypoint missions.
  • Override RC channel settings.

Simulation - Software In the Loop (SITL)

SITL enables users to check code and behavior of drones without physical hardware involvement. ArduPilot’s SITL allows running ArduPilot software on a PC, providing access to the full range of development tools.

ArduPilot’s SITL directly allows the user to run ArduPilot software on the PC, the sensor data comes from a flight dynamics model in a flight simulator. A big advantage of ArduPilot on SITL is it gives you access to the full range of development tools.

Refer to the ArduPilot SITL user manual for setup instructions.

Note

For setting up ArduPilot’s SITL on a Linux machine, refer to the article:

Python scripts to control a drone

Let’s delve into actual Python scripts using DroneKit-Python.

Code 1

A simple python script named armDisarm.py for arming and disarming the motors of a drone, connected to the same network

armDisarm.py
 1"""
 2Simple Program which gets ARM/DISARM command from the user and 
 3ARM or DISARM the vehicle based on inputs entered
 4Inputs:
 5    1. ARM     -  Arm all the motors of the drone
 6    2. DISARM  -  Disarm all the motors of the drone
 7"""
 8
 9# Importing necessary Packages
10from dronekit import connect, VehicleMode
11
12# Connecting the Vehicle
13vehicle = connect('udpin:127.0.0.1:14551', baud=115200)
14
15# Initially Changing the Vehicle to GUIDED mode
16vehicle.mode = VehicleMode("GUIDED")
17
18while True:
19    armCommand = input()
20
21    if armCommand == "ARM":
22        vehicle.armed = True
23
24    if armCommand == "DISARM":
25        vehicle.armed = False

Source: Link

Advertisement

Execution

The program needs to be executed as below,

python armDisarm.py

Output

video-cover

Video Credits: Dhulkarnayn, Elucidate Drones

Code 2

A simple python script named simpleMission.py performs basic autonomous takeoff and landing missions on execution.

simpleMission.py
 1# Import Necessary Packages
 2from dronekit import connect, VehicleMode,LocationGlobalRelative
 3import time
 4
 5def basicTakeOff(altitude):
 6
 7    """
 8    Inputs:
 9        1.  altitude            -   TakeOff Altitude
10    """
11
12    vehicle.mode = VehicleMode("GUIDED")
13    vehicle.armed = True
14    time.sleep(2)
15    vehicle.simple_takeoff(altitude)
16
17    while True:
18        print("Reached Height = ", vehicle.location.global_relative_frame.alt)
19
20        if vehicle.location.global_relative_frame.alt >= (altitude - 1.5):
21            break
22        time.sleep(1)
23
24
25# Connecting the Vehicle
26vehicle = connect('udpin:127.0.0.1:14551', baud=115200)
27
28# Takeoff the Vehicle 
29takeOffAltitude = 10
30basicTakeOff(takeOffAltitude)
31print("Reached:", takeOffAltitude, " m")
32
33# Landing the Vehicle
34vehicle.mode=VehicleMode('LAND')
35print("Landing the Vehicle!!!")
36time.sleep(1)
37
38print("Exiting the Code!!!")
39time.sleep(1)

Source: Link

Advertisement

Execution

The program needs to be executed as below,

python simpleMission.py

Output

video-cover

Video Credits: Dhulkarnayn, Elucidate Drones

Conclusion

In conclusion, this comprehensive guide delves into the intricate realm of drone programming using Python. By comprehending essential concepts such as flight control software, MAVLink, and leveraging DroneKit-Python for scripting, you’ve gained a robust foundation for controlling drones through code. Your valuable feedback and suggestions on this topic are eagerly awaited in the comments section below.

Remember, the essence of sharing knowledge. If you found this article valuable, do share it with your friends. Thank you for exploring the exciting world of drone programming with us!


Please consider supporting this project!

If this article has been of help to you, and you feel generous at the moment, don’t hesitate to buy us a coffee. It's an easy, fun and direct way to show your support — any amount of coffee is highly appreciated.
Buy me a Coffee


Comments