A sample mission created using python

Too long, didn't read: For complex autonomous flights, create missions using python instead of mouse clicking each command on Mission Planner. For instance, go 1km straight and at each 10 meter, stop and took panoramic shoot. Source code is here.

Note to DIYDrones community: This is my first blog post, and although this is pretty simple and not complicated topic, I decided to start from low level - basic stuff. Forgive me since it is my first attempt :).

Before you begin reading this article, if you want to have a better understanding about drones, what they are, how they work, please refer to this excellent site and from there move on to the suggested pages.

Drones are (in this context) aerial vehicles that fly according to the operators' input or fly autonomously. We will focus on the latter. In autonomous flights, drone follows specific instructions that are put in order, combination of which are called a mission. Missions are prepared before the flight and uploaded to the flight controller. When activated, drone starts to execute the mission.

What is a Mission?


Missions are composed of discrete commands. Each command dictates a specific action.

When the autonomous mode is activated, the drone starts to execute the commands, beginning from the first command and after completing it, starts to execute the next command till all commands are executed or auto mode deactivated. Please have a look at http://copter.ardupilot.com/wiki/mission-command-list/ for detailed description about missions.

Some commands are:

  • Takeoff: tells the drone to takeoff to specific altitude
  • Waypoint: moves the drone to the specified position at specified altitude.
  • Loiter-time: Drone waits at specific location for the given time
  • Return-to-launch: Drone goes back to its launching position and lands there.

When you combine these commands, it becomes a mission. For instance, takeoff, then go to the this point (point you clicked on a map), look at the stadium, take a photo, go to my house, and land.

The inefficiency

It is all cool and fun to fly the drone autonomously. The mission based flights are precise, you tell the drone where to go at which speed, how much to wait, drop the cargo at specified point etc., and it will follow the orders as precise as the sensors allow. However, unfortunately these commands are supplied by hand. For instance, if you will prepare a mission consists of 100 waypoint (ie 100 point to visit), you have to supply those 100 points manually, modify each points altitude, delay time etc., which is pretty time consuming and there is an increased chance of making error during the preparation process. The exceptions are mapping, creating text, and create circular motion missions, which are automatically prepared by the mission planner based on your input parameters. But if you need to prepare another mission, you have to do the whole job.


The mission files are saved as text files and each line in the file represents a command: an ID of the action and its parameters. A sample line (one command) in a mission is shown below:

16 41.112469 27.740479 100

16 indicates it is 'waypoint' command, following 2 numbers are latitude and longitude (position), and final one is altitude.

I should say that it turns out the process is extremely easy, but the motivation for me to write and share the code is to make this even easier and make it ready to use whenever someone needs.

We produce the command in python file, save it in txt file which mission planner directly opens it as if it is planned using mission planners own user interface. The good thing is that you will see the visualization of your mission on the map and will access each command, so any error you made will be easy to catch.

Lets move to the sample code:

from automission import automission

import the module and create an instance 'my_mission'. It will represent our mission.


The coordinates and altitude that I will use at the beginning.


As it is seen, my first command is takeoff, it will takeoff to a 15 meters.


Next command is that I want it to go to the specified location and altitude.


Roi stands for region of interest, from now on drone the front of the drone (or camera) will look at this point regardless of where the drone is going.

   for i in range(15):  
my_mission.do_digicam_control() alt= alt+10
lat+=1 if i%4==0 else 0
lon+=1 if i%4==1 else 0

lat+=1 if i%4==2 else 0
lon-=1 if i%4==3 else 0

There is a loop with 15 cycle, at each cycle the altitude is increased 10 meters, and drone moves square-wave pattern. Moreover, at the each corner it takes photograph with the digicam command.


Finally we tell her to return to the home and land.

my_mission.write() saves the mission in as txt file. When we open it in Mission Planner (flight plan tab, load WP file button) here is our mission:


We've just created a mission! When we upload it to the drone and activate AUTO mode, it will follow these commands.

Here is another example. Slightly modifying the for loop, one can easily obtain a vortex;


increase=0.001for i in range(20):  
lat+=increase if i in range(0,20,4) else 0
lon+=increase if i in range(1,20,4) else 0
lat-=increase if i in range(2,20,4) else 0
lon-=increase if i in range(3,20,4) else 0

What is next, and possible applications

  • First of all, at this stage one can design its own mission easily, like go 1 km straight, at each 10 meter stop and took panoramic shoot.
  • Manuel supplying GPS coordinates are impractical. It can be improved, like directly importing coordinates when you clicked on map etc. Moreover, preparing the mission with distance units can also implemented, like go 10 meters north, etc.
  • Auto mission preparation can be integrated into industrial drone applications. For example, using image processing, buildings would be detected automatically and again automatically a mission is created such that every roof in specific area is inspected for damage assessment or roof surveying.

  The automission script and example codes can be accessed from GitHub: https://github.com/unnamed-idea/auto-mission-prepare-for-mission-pl...

This post can also be accessed from my site, http://talhakorkmaz.com/drone/preparing_auto_mission/

Views: 3895

Comment by earthpatrol on January 28, 2016 at 8:59am

@unamed_idea I am surprised that Mission Planner/APM relies on dronekit.io for scripting and doesn't have it's own internal state machine for scripting behavior. Beyond surprised. I just assumed it had it. I have all the scripting control I need with PaparazziUAV, no reason to use dronekit.io stuff. I can program in any language and have it interact at different flight abstractions. Still cant' believe that there is not a scripting facility/approach for APM/Mission Planner. I would have lost that bet.

Comment by Michael Oborne on January 28, 2016 at 6:37pm

MP does support python scripts, but does not implement dronekit. Check inside the MP install directory, then Scripts, for a few examples.

Comment by Alpo Hassinen on January 29, 2016 at 1:48am

It is quite easy to use also meters in formulas.

Here is some simple mathematics:

- Distance from equator to North pole is 10 000 km
- Distance is also 90 degrees
- So 1 degree is 10000 / 90 = about 111 km.
- 1 meter = 90 / 10 000 000 = 0.000009 degrees
- The rest you can calculate...

East-west direction is a little bit different, it depends on latitude.

- 1 longitude degree = cosini(latitude) x 111 km
- For example 1 meter in 45 degree latitude = cosini(45) x (90 / 10 000 000) = 0.7071 X (90 / 10 000 000) = 0.000006 degrees
- The rest you can calculate...

Comment by Tommy Larsen on February 4, 2016 at 6:58am

I guess you can make a script for 360 panorama photos as well? :)


You need to be a member of DIY Drones to add comments!

Join DIY Drones


Season Two of the Trust Time Trial (T3) Contest 
A list of all T3 contests is here. The current round, the Vertical Horizontal one, is here

© 2019   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service