Initially, most of the Unmanned Aerial Vehicles (UAVs) were designed solely for military purposes, but now they are serving many applications. Drones are almost everywhere and, most industries are beneficiaries of drone technology because of its availability and ease of operation.
Image Credits: Technology illustrations by Storyset
Irrespective of the categories of the drones, most of the drones available are programmable based on the flight controller present onboard. If you’re using the DIY drone or community-supported flight controllers like pixhawk or navio, you can use DroneKit-Python to control your drone.
The DroneKit-Python is an Application Programming Interface (API) that allows developers to create Python apps that communicate with vehicles over MAVLink protocol.
Need for Autonomous Take-off and Landings in Drones
There are many applications, which require autonomous take-off and landings of drones. Moreover, in most cases, the complete mission is entirely automated, for ease of operation.
Video Credits: AirHub
One of the evident technology that uses autonomous missions (including autonomous take-off and landings) is Drone-in-a-box. These boxes or containers will be placed in multiple places of cities or refineries, where there’s a frequent need for inspection of the sites.
The drones used in the box are designed in a way to intelligently return to one of the deployed boxes after completing the intended mission and recharge themselves in the box.
Most of the drones used in the drone-in-a-box technology will land on the box using the aid of visual navigation powered by artificially intelligent vision systems.
Like the above, many applications require autonomous take-off and landing operations. This will reduce most of the manual tasks and increase the overall effectiveness of the system.
Advertisement
Code - Autonomous Take-off and Landing
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
Execution
The program needs to be executed as below,
1
$ python simpleMission.py
Output
Video Credits: Dhulkarnayn, Elucidate Drones
Code Explanation
The above code is written in Python utilizing the DroneKit-Python package for controlling the drone. I’ll try to explain the above code procedural-wise instead of explaining functional-wise (line-by-line) for a better understanding of the code flow.
The flow of the code will be something like below:
- Import Necessary Packages
- Connect the Vehicle
- Change the Mode to GUIDED
- Take-off the Vehicle
- Check the Vehicle’s Altitude
- Land the Vehicle
Import Necessary Packages
Like in other programming languages, in python also, we need to import the required packages similar to the header files in C and C++ languages. In the above code, the necessary packages for our operation are dronekit
and time
.
1
2
3
# Import Necessary Packages
from dronekit import connect, VehicleMode,LocationGlobalRelative
import time
From the dronekit
package, the following methods will be utilized in the code,
connect()
- For establishing the connection between the drone and the computer/companion computer.VehicleMode()
- For changing the flight mode of the drone.LocationGlobalRelative()
- For retrieving the global position of the drone, which includes latitude, longitude, and altitude.
The purpose of the time
package is to make the main process to sleep()
for a known amount of time.
Connect the Vehicle
As mentioned earlier, the connect()
method is used for establishing the connection between the vehicle and the computer.1
1
2
# Connecting the Vehicle
vehicle = connect('udpin:127.0.0.1:14551', baud=115200)
The connect()
method syntax is as follows:
1
2
3
4
5
connect(ip, _initialize=True, wait_ready=None, timeout=30,
still_waiting_callback=<function default_still_waiting_callback>,
still_waiting_interval=1, status_printer=None, vehicle_class=None,
rate=4, baud=115200, heartbeat_timeout=30, source_system=255,
source_component=0, use_native=False)
The following table is the explanation of the parameters that can be passed on the connect()
method:
Parameter | Type | Explanation |
---|---|---|
ip | String | Connection string for target address - e.g. 127.0.0.1:14550 |
wait_ready | Bool/Array | If True , wait until all default attributes have been downloaded before the method returns. You can also specify a named set of parameters to wait on (e.g. wait_ready=['system_status', 'mode'] ) |
status_printer | deprecated | By default, the status information is handled by the autopilot logger. |
vehicle_class | Vehicle | The class will be instantiated by the connect() method. |
rate | int | Data stream refresh rate. The default is 4Hz (4 updates per second). |
baud | int | The baud rate for the connection. The default is 115200. |
heartbeat_timeout | int | Connection timeout value in seconds (default is 30s). If a heartbeat is not detected within this time an exception will be raised. |
source_system | int | The MAVLink ID of the Vehicle object is returned by this method (by default 255). |
source_component | int | The MAVLink Component ID of the Vehicle object is returned by this method (by default 0). |
use_native | bool | Use precompiled MAVLink parser. |
Advertisement
Change the Mode to GUIDED
To control the drone via a program, firstly, you need to change the mode to GUIDED
, an armable mode used to guide the vehicle.
1
vehicle.mode = VehicleMode("GUIDED")
You can change the vehicle mode to GUIDED
using the VehicleMode()
method.
Take-off the Vehicle
Before taking off the vehicle, we need to arm the motors of the vehicle. You can arm the vehicle using the following commands. After executing the arming command, wait for at least 2 seconds for the completion of the execution.
1
2
vehicle.armed = True
time.sleep(2)
Note
Try to avoid the usage of the
time.sleep()
method by looping over the condition, if possible. In real-time,time.sleep()
may cause some issues during critical operations.
You can take off the drone using the simple_takeoff()
method. While calling the simple_takeoff()
method, pass the desired take-off altitude (in meters) as an argument.
1
2
3
# Takeoff the Vehicle
takeOffAltitude = 10
vehicle.simple_takeoff(takeOffAltitude)
Check the Vehicle’s Altitude
It is possible to check the altitude of the vehicle in the current instance using the command vehicle.location.global_relative_frame.alt
. This command returns the current altitude of the vehicle in meters.
You can able to check whether the vehicle reached the desired location or not by looping around the condition that checks the vehicle’s altitude, as shown below.
1
2
3
4
5
6
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)
Also, you need to add the condition for breaking(break
) the loop once after satisfying the required condition.
Note
time.sleep(1)
is used in the loop to check the condition in the loop for every second.
Advertisement
Land the Vehicle
Once after breaking the loop, the landing of the vehicle is done by changing the mode of the vehicle to LAND
.
1
vehicle.mode=VehicleMode('LAND')
Autonomous Execution
I hope the above section helped you to understand how the simpleMission.py
program works. But the above program is a semi-autonomous script, which means you need to execute the script every time in the companion computer.
Executing the script every time after turning on the companion computer makes the script autonomous, but this operation cannot be achieved using python scripts. For this, we need to add the script details in the .bashrc
file to execute the script every time during the startup automatically.
The .bashrc
file is a script file that’s executed when a user logs in. The file itself contains a series of configurations for the terminal session. This includes setting up or enabling: coloring, completion, shell history, command aliases, and more.2
Conclusion
In this article, I’ve tried to explain how to program a drone to perform autonomous take-off and landing using DroneKit-Python. 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!
References
What is .bashrc file in Linux?, JournalDev. ↩