An Unmanned Aerial Vehicle (UAV), commonly known as a drone, is an aircraft without a human pilot on board. The flight of a drone is either controlled autonomously by computers in the vehicle or remotely controlled by a pilot on the ground.
Irrespective of categories, most of the drones are programmable based on the flight controller present onboard. Nowadays, the usage of drones extends from complex to very complex applications such as drone delivery applications to drone light shows.
Advertisement
As mentioned above, one of the complex applications of programmable drones is the Drone Light show. Imagine the fact, that hundreds of drones are being controlled by a single computer or couple of computers and performing a show in an orchestrated manner but there are many complicated factors involved in the process.
Adding, one of the longest animations performed by unmanned aerial vehicles (UAVs) is 26 min 19 sec and was achieved by EFYI Group (China) and supported by Tianjin University (China) in Tianjin, China, on 18 December 2020, for reference.
Video Credits: Guinness World Records
For programming a drone, we need to have a basic understanding of the following things:
Flight Control Software
The Flight Control Software is the firmware that powers the flight controllers. For example, it’s similar to the operating system that powers the Central Processing Unit (CPU).
Two major open-source Flight Control Softwares (Flight Stacks) which are preferred by developers are:
ArduPilot
Image Credits: ArduPilot
ArduPilot provides a comprehensive suite of tools suitable for almost any vehicle such as Multirotor drones, Fixed-wing and VTOL aircraft, Helicopters, Ground rovers, Boats, Submarines, Antenna trackers.
PX4
Image Credits: PX4
PX4 is an open-source flight control software for drones and other unmanned vehicles. The project provides a flexible set of tools for drone developers to share technologies to create tailored solutions for drone applications.
MAVLink
The above-mentioned flight stacks (ArduPilot, PX4) communicates to the ground and other companion computer using MAVLink messaging protocol.
Image Credits: MAVLink
MAVLink follows a modern hybrid publish-subscribe and point-to-point design pattern. MAVLink was first released in early 2009 by Lorenz Meier and has now a significant number of contributors.
DroneKit-Python
The DroneKit-Python is an Application Programming Interface (API) that allows developers to create Python apps that communicate 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 the user to check the code and behaviour of the drones for executed commands without involving any physical hardware (drones).
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.
The user manual of Ardupilot SITL is available on ArduPilot Official Documentation Page.
Note
If you’re looking for setting up the ArduPilot’s SITL on your Linux machine, then you can have a look at the following article:
Python scripts to control a drone
Let’s hope the above section helped a lot in understanding the fundamental things involved in programming a drone. This part comprises actual python scripts based on 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""
Simple Program which gets ARM/DISARM command from the user and
ARM or DISARM the vehicle based on inputs entered
Inputs:
1. ARM - Arm all the motors of the drone
2. DISARM - Disarm all the motors of the drone
"""
# Importing necessary Packages
from dronekit import connect, VehicleMode
# Connecting the Vehicle
vehicle = connect('udpin:127.0.0.1:14551', baud=115200)
# Initially Changing the Vehicle to GUIDED mode
vehicle.mode = VehicleMode("GUIDED")
while True:
armCommand = input()
if armCommand == "ARM":
vehicle.armed = True
if armCommand == "DISARM":
vehicle.armed = False
Source: Link
Advertisement
Execution
The program needs to be executed as below,
1
$ python armDisarm.py
Output
Video Credits: Dhulkarnayn, Elucidate Drones
Code 2
A simple python script named simpleMission.py
performs basic autonomous takeoff and landing missions on execution.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Import Necessary Packages
from dronekit import connect, VehicleMode,LocationGlobalRelative
import time
def basicTakeOff(altitude):
"""
Inputs:
1. altitude - TakeOff Altitude
"""
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True
time.sleep(2)
vehicle.simple_takeoff(altitude)
while True:
print("Reached Height = ", vehicle.location.global_relative_frame.alt)
if vehicle.location.global_relative_frame.alt >= (altitude - 1.5):
break
time.sleep(1)
# Connecting the Vehicle
vehicle = connect('udpin:127.0.0.1:14551', baud=115200)
# Takeoff the Vehicle
takeOffAltitude = 10
basicTakeOff(takeOffAltitude)
print("Reached:", takeOffAltitude, " m")
# Landing the Vehicle
vehicle.mode=VehicleMode('LAND')
print("Landing the Vehicle!!!")
time.sleep(1)
print("Exiting the Code!!!")
time.sleep(1)
Source: Link
Advertisement
Execution
The program needs to be executed as below,
1
$ python simpleMission.py
Output
Video Credits: Dhulkarnayn, Elucidate Drones
Conclusion
I’ve tried to explain how to Control a Drone Using Python, with the acquired information from various sources. I’m expectantly waiting for your valuable feedback and suggestions regarding this topic.
At last, Sharing is Caring, feel free to share with your friends if you’ve liked this article. Thank you!
Comments
Pixhawk programing help me
Very very thanks
Abraham, I’m pleased you found this helpful. Maybe you should also read my post on the python program to calculate the distance and bearing between two geo-locations.
I appreciate your hard work, I will keep visiting it. https://aditidigitalsolutions.com/data-science-training-hyderabad/
This comment was posted by the commenter on [04-OCT-2021]. Reposted by the author after migration of site.
Thank you!!!