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.
Solution
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
my_mission=automission('copter')
import the module and create an instance 'my_mission'. It will represent our mission.
lat,lon=41.0824324,29.0501797alt=50
The coordinates and altitude that I will use at the beginning.
my_mission.takeoff(angle=10,alt=15)
As it is seen, my first command is takeoff, it will takeoff to a 15 meters.
my_mission.waypoint(lat,lon,alt)
Next command is that I want it to go to the specified location and altitude.
my_mission.do_set_roi(lat-0.5,lon-0.5)
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.waypoint(lat,lon,alt)
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.
my_mission.rtl()
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):
my_mission.spline_waypoint(lat,lon,alt)
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
increase-=0.00005
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-planner.git
This post can also be accessed from my site, http://talhakorkmaz.com/drone/preparing_auto_mission/
Comments
I guess you can make a script for 360 panorama photos as well? :)
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...
MP does support python scripts, but does not implement dronekit. Check inside the MP install directory, then Scripts, for a few examples.
@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.
Delete Comment
Firstly thank you all for your kind words! I will contact Mission Planner team and let you know the process.
Moreover, I just put it as an example but it seems it is nice feature; I will put crop circle generator that took center and radius of your field as well as clearance as an input and generate a vortex.
@earthpatrol, I've not heard GSA till you mentioned it, it looks quite interesting, I will study it, and will contact you, thanks. As for the auto mission, (correct me if I'm wrong) only choices in current mission planner are grid for mapping, create circle or spline circle, and create text ie write your name during flight. Even with these missions, if you want to add-subtract anything (like adding delay and shot a photo of you at every 10sec) you should manually modify all the related commands.
Not to create auto mission, but to interact with APM family during flight, you might want to look at http://python.dronekit.io/ (I've not tested yet) there is scripting interface (ie control the controller) for both on-board and off-board companion computers.
Is this approach novel for APM/Mission Planner development? It's something that's inherent in PaparazziUAV architecture for at least a decade. Just curious. This seems like a first order feature that must exist already prior to this post? Is this really the first time someone has built a scripting interface to interact with APM?
nice script mission planning!
I have always thought the lawnmower pattern was inefficient due to all the 90 deg turns. I hope to see a gain in efficiency using a spiral. Though I am not sure how efficient a constant turn with be.
Also, I assume the MP auto way point algorithm finds the minimum time to cover the polygon. But I don't think this is true??? Would be a nice feature/option to build into your script as well.
This should be integrated directly into mission planner, in the same way that the mapping feature is.