As the drone industry evolves from simple RC robots in the sky to actual industries, one thing is certain: drone programming will be crucial in fulfilling the realization of truly autonomous drones. Think about it, whether we’re talking about drone delivery or surveillance drones, human pilots will not suffice. 

Up to this point, the main problem in learning about drone programming was, well, the lack of structured learning resources. This article is going to cover a ton of material, and if you are truly interested in learning about drone coding, digest this information and run with it!

We are going to start out learning the basics of drone software, then we’ll build up to actual python drone programming.

Here is a breakdown of what is contained in this article:

  • Open source software that structures a drone (ardupilot flight stack)
  • Python drone programming
  • Control a simulated drone with python dronekit
  • Control an actual drone with python dronekit 

To help juice your motivation and gain an understanding for what is possible with python drone coding, here is a 100% autonomous drone delivery mission running off a simple 200 line dronekit python script.

Autonomous Drone Delivery | Taco Copters and Precision Landing
Autonomous Drone Delivery with Python

Open Source Drone Software Stack

A useful analogy to understanding the flight stack is computers. There are various layers to a computer:

  1. Hardware:
  • CPUs
  • Storage
  • RAM
  1. Firmware (low level code that commands the hardware):
  • Windows
  • Max
  • Linux
  1. Applications:
  • Solitaire
  • Microsoft Word
  • Paint

Let’s say you’re a developer wanting to write a new online poker app. In order to work, the software will have to communicate with the physical hardware. Thankfully, the developer can simply write high level code and depend on the operating system (firmware) to communicate with the hardware on the high level code’s behalf. 

Well, a similar relationship exists with drone software as well. The hardware layer on a drone consists of motors, escs, batteries, etc, but what about the firmware layer? Enter ArduPilot, the Linux of drones.

ArduPilot Firmware

Just like how Windows firmware is responsible for communicating with the computer hardware, ArduPilot is responsible for commanding a drone’s hardware. Actually without ArduPilot, or some flight control firmware, it would be impossible to fly multirotor uavs. That is because ArduPilot sends around 400 commands a second to the drone’s motors, which translates into smooth and steady flight. 

ArduPilot Introduction | Open Source Drones (2019)
Introduction to ArduPilot

Ardupilot is crucial to the drone programmer, because it allows them to focus on the high level missions or applications. For example, Let’s say you’re trying to develop a drone delivery mission. The last thing you’d want to worry about is what PWM values are being written to your motors 400 times a second! ArduPilot abstracts the low level duties of a drone away from the programmer.

So great news, we don’t have to write a drone’s firmware from scratch! Thanks ArduPilot. But what software is there for application level drone programming?

Dronekit Python

Python and Dronekit Intro | Drone Programming (2019)

Dronekit python is an open source python library that provides high level functions to command the drones movement, check vehicle status and many other things. 

Essentially, this unlocks the application layer to drone programmers. 

Want to perform an autonomous drone delivery mission in 200 lines of code? You can do that with dronekit python. Want to combine dronekit and openCV to implement computer vision into your drone programming? You can do that too. 

But how does it work? To understand this, we must investigate the MAVLink Protocol

MAVLink Protocol

When you try and call someone’s phone number, there is structure to this. You must dial the country code, then the local area code, and then the individuals number. This is a standardized system. If I want to call my Uncle Ron, I must use this standard system AND know the numbers which represent him. 

In essence, this is what MAVLink is for drone communication. The MAVLink protocol consists of two things:

Mavlink Logo
  1. Packet Structure: Just like how all phone numbers have the same (country)-(local)-(personal) structure, MAVLink offers a standard packet structure so it is easy to receive and transmit data. MAVLink 1 data consists of just 8 bytes, and MAVLink 2 data consists of 14 bytes.
  2. Standard Messages: If I want to call my Uncle Ron, I simply dial his number 1-999-9999 and he picks up (hopefully). Similarly, the MAVLink protocol offers specific messages with specific functions behind them. For example, MAVLink message 78 and command 22 is a takeoff command that would launch the drone into the air, with the target height contained within the message. In addition to commands, MAVLink messages can be strictly informational as well. For example, MAVLink message 25 holds the current GPS status of the vehicle. 

How is the MAVLink Protocol Implemented?

Think of MAVLink as the glue that allows Dronekit Python and ArduPilot to communicate. It offers standard messages which can be accepted and understood by the ArduPilot firmware. 

The neat thing about MAVLink is that it works with any drone firmware which is MAVLink enabled, not just Ardupilot. There are many different types of MAVLink enabled drone firmware, another one being PX4.

MAVLink Introduction | Middleware for Open Source Drones
Intro to MAVLink

Since MAVLink is just a standard packet/messaging protocol, it can be structured in libraries for basically any programming language. This includes:

  • Java
  • C++
  • Go
  • Python
  • Many others!

Once the library is represented in the desired programming language, it can be used to receive information from the drone, or command the drone. Remember how the example MAVLink message 78 and command 22 from above could launch the drone into the air? Well, that message could be created and sent to the drone right from a python script!

In python, the pymavlink library defines the MAVLink messages in python form. The dronekit python library uses pymavlink and establishes a connection with the drone. This allows direct control of the drone right from a python script, so any MAVLink drone is therefore a programmable drone.

Python Drone Programming

Now that we have a basic understanding of the open source drone software stack, let’s actually start drone coding with python dronekit! As noted previously, we can even begin drone programming without an actual drone! We’ll do this with the ArduPilot SITL simulator.

ArduPilot SITL

SITL stands for ‘Software-In-The-Loop’. There are many cool aspects about SITL, a few being:

  • The same source code can compile for a real autopilot board or the simulated drone
  • We can test the real firmware right from our computer. For example, what would happen to the drone if it all of the sudden lost GPS signal? That is testable on SITL.
  • It is also possible to test high level dronekit python scripts against the simulated ardupilot before trying the code out in the field. 
  • We can use the native ArduPilot SITL simulator (pictured below), or use SITL with a more advanced simulator like Gazebo.
SITL Drone Running on a Computer

In order to run the simulated ArduPilot drone, we need to download some dependencies. This quick tutorial will be from a Ubuntu Linux perspective.

Download the ArduPilot Source Code
  1. git clone https://github.com/ardupilot/ardupilot
  2. cd ardupilot
  3. git submodule update – init – recursive
  4. cd ArduCopter
  5. ../Tools/autotest/sim_vehicle.py – console – map

Now the ArduPilot SITL drone is up and running. The SITL drone can be controlled by MAVProxy, dronekit python, or some other ground control station. Check out this video for a walk through of the SITL vehicle you just launched.

Control a Simulated Drone SITL with ArduPilot using MAVProxy
Launching ArduPilot SITL

Now that we have a way to launch a simulated MAVLink quadcopter, we need to download dronekit python. 

Download Dronekit Python
  • pip install dronekit==2.9.2

Writing Our First Dronekit Python Script

Let’s write a basic dronekit python script that will command the drone into the air at some target altitude, and then simply land. Again, this will all be from a python script. You can choose to either implement this on your SITL vehicle, or on an actual programmable drone.

Simple Arm and Takeoff Python Dronekit Script

#############DEPENDENCIES#######################


from dronekit import connect, VehicleMode,LocationGlobalRelative,APIException
import time
import socket
import exceptions
import math

##############FUNCTIONS##########################



##Function to arm the drone props and takeoff at targetHeight (m)


def arm_and_takeoff(targetHeight):

	while vehicle.is_armable!=True:
		print("Waiting for vehicle to become armable.")
		time.sleep(1)
	print("Vehicle is now armable")

	vehicle.mode = VehicleMode("GUIDED")

	while vehicle.mode!='GUIDED':
		print("Waiting for drone to enter GUIDED flight mode")
		time.sleep(1)
	print("Vehicle now in GUIDED MODE. Have fun!!")

	vehicle.armed = True
	while vehicle.armed==False:
		print("Waiting for vehicle to become armed.")
		time.sleep(1)
	print("Look out! Virtual props are spinning!!")

	vehicle.simple_takeoff(targetHeight)

	while True:
		print("Current Altitude: %d"%vehicle.location.global_relative_frame.alt)
		if vehicle.location.global_relative_frame.alt>=.95*targetHeight:
			break
		time.sleep(1)
	print("Target altitude reached!!")

	return None


############MAIN EXECUTABLE#############


####sim_vehicle.py opens up port on localhost:14550


vehicle = connect('127.0.0.1:14550',wait_ready=True)


####Arm the drone and takeoff into the air at 5 meters


arm_and_takeoff(5)
print("Vehicle reached target altitude")


####Once drone reaches target altitude, change mode to LAND 



vehicle.mode=VehicleMode('LAND')
while vehicle.mode!='LAND':
	print("Waiting for drone to enter LAND mode")
	time.sleep(1)
print("Vehicle now in LAND mode. Will touch ground shortly.")

Testing the Code on a Real Programmable Drone

If you are looking to build a real drone that can be controlled by dronekit python scripts, checkout my raspberry pi programmable drone kit.

Code a Drone to Fly with only a Python Dronekit Script
Dronekit Python Controlling a Real Drone

Drone Programming Course

Obviously, we are just touching the surface of drone programming and what is possible with dronekit python. If this information interested you and you want to learn more about drone programming, check out this comprehensive drone programming course!

We take a deep dive into dronekit python scripting, the ardupilot flight stack, and much more. No drone required. However, the knowledge you learn can be applied on a real drone!

Drone Programming Course