The guidance of a drone from one place to another relies on the navigation systems present onboard. The navigation system consists of multiple sensors, and these sensors aid in the navigation and guidance of the drone.
Global Positioning System (GPS) receiver is one of the sensors present onboard in all the drones intended for outdoor operations. GPS receiver (User Segment) helps to identify the location of that receiver by receiving its location from the GPS Satellites (Space Segment) in terms of latitude and longitude values.
Image Credits: Dhulkarnayn, Elucidate Drones
The latitude and longitude of a specific location are unitedly called geographical coordinates or GPS coordinates. Using the geographical coordinates values, we can able to identify the current location of the drone easily.
Note
Kindly have a look at the following article for understanding how the Global Positioning System (GPS) works and how it helps in drone navigation:
How to retrieve the Geographical Coordinates of your drone?
If you’re using a MAVLink supported drone, it’s possible to control your drone with DroneKit-Python.
To retrieve the geographical coordinates of your drone, you need to invoke the lat
or lon
element from the vehicle.location.global_relative_frame
object.
The object name vehicle
differs based on the object name you’ve used for connecting the drone.
Advertisement
For example, the following statement invoked the vehicle
object using the name drone
:
1
drone = dronekit.connect('udpin:127.0.0.1:14551', baud=115200)
The following script is an example to retrieve the geographical coordinates using dronekit
. The script geo_coords.py
will print the drone’s current geographical coordinates (latitude
and longitude
) after the execution.
Cloning the Repository
You can directly download (clone) the repository of the following script to your device by executing the following git
command on your terminal:
1
$ git clone https://github.com/Dhulkarnayn/geo-coords-dronekit/
Note
If
git
is not already installed on your device means, execute the following command on your terminal to installgit
:
1 2 $ sudo apt-get update $ sudo apt-get install git
Or, if you want to copy and paste the script, you can copy the following script and paste it to a file named geo_coords.py
on your device.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
#..................................................................................
# Author : Saiffullah Sabir Mohamed
# Github : https://github.com/TechnicalVillager
# Website : http://technicalvillager.github.io/
# Source : https://github.com/TechnicalVillager/geo-coords-dronekit/
#..................................................................................
# Import Necessary Packages
from dronekit import connect
# Connecting the Vehicle
vehicle = connect('udpin:127.0.0.1:14551', baud=115200)
# Printing Vehicle's Latitude
print("Vehicle's Latitude = ", vehicle.location.global_relative_frame.lat)
# Printing Vehicle's Longitude
print("Vehicle's Longitude = ", vehicle.location.global_relative_frame.lon)
Source: Link
Execution
To run the script geo_coords.py
, execute the following command on your terminal:
1
$ python geo_coords.py
Output
Image Credits: Dhulkarnayn, Elucidate Drones
Note
Instead of using an actual drone, I’ve used ArduPilot’s Software In The Loop (SITL) simulation and Mission Planner as Ground Control Station (GCS) software. Mission Planner is one of the MAVLink supported Ground Control Station (GCS) software that runs natively on the Windows operating system. Also, you can able run Mission Planner on Linux and Mac OS using Mono.
How to retrieve the Altitude of your drone?
To retrieve the drone’s current altitude, you need to invoke the alt
element from the vehicle.location.global_relative_frame
object.
As mentioned earlier, The object name vehicle
differs based on the object name you’ve used for connecting the drone.
The following script is an example to retrieve the drone’s current altitude using dronekit
. The script geo_coords_alt.py
will print the drone’s current altitude, along with the geographical coordinates (latitude
and longitude
) after the execution.
Advertisement
If you’ve cloned the repository already by following the commands mentioned in the previous section, there you can find the geo_coords_alt.py
script.
Or, if you want to copy and paste the script, you can copy the following script and paste it to a file named geo_coords_alt.py
on your device.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
#..................................................................................
# Author : Saiffullah Sabir Mohamed
# Github : https://github.com/TechnicalVillager
# Website : http://technicalvillager.github.io/
# Source : https://github.com/TechnicalVillager/geo-coords-dronekit/
#..................................................................................
# Import Necessary Packages
from dronekit import connect
# Connecting the Vehicle
vehicle = connect('udpin:127.0.0.1:14551', baud=115200)
# Printing Vehicle's Latitude
print("Vehicle's Latitude = ", vehicle.location.global_relative_frame.lat)
# Printing Vehicle's Longitude
print("Vehicle's Longitude = ", vehicle.location.global_relative_frame.lon)
# Printing Vehicle's Altitude
print("Vehicle's Altitude (in meters) = ", vehicle.location.global_relative_frame.alt)
Source: Link
Execution
To run the script geo_coords_alt.py
, execute the following command on your terminal:
1
$ python geo_coords_alt.py
Output
Image Credits: Dhulkarnayn, Elucidate Drones
Note
Instead of using an actual drone, I’ve used ArduPilot’s Software In The Loop (SITL) simulation and Mission Planner as Ground Control Station (GCS) software.
How to retrieve the Heading angle of your drone?
To retrieve the heading angle of your drone, you need to invoke the heading
element from the vehicle
object.
As mentioned earlier, The object name vehicle
differs based on the object name you’ve used for connecting the drone.
Advertisement
The following script is an example to retrieve the drone’s heading angle using dronekit
. The script geo_coords_w_heading.py
will print the drone’s current heading angle in degrees along with the altitude and geographical coordinates (latitude
and longitude
) after the execution.
If you’ve cloned the repository already by following the commands mentioned in the previous section, there you can find the geo_coords_w_heading.py
script.
Or, if you want to copy and paste the script, you can copy the following script and paste it to a file named geo_coords_w_heading.py
on your device.
Code
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
#!/usr/bin/env python
#..................................................................................
# Author : Saiffullah Sabir Mohamed
# Github : https://github.com/TechnicalVillager
# Website : http://technicalvillager.github.io/
# Source : https://github.com/TechnicalVillager/geo-coords-dronekit/
#..................................................................................
# Import Necessary Packages
from dronekit import connect
# Connecting the Vehicle
vehicle = connect('udpin:127.0.0.1:14551', baud=115200)
# Printing Vehicle's Latitude
print("Vehicle's Latitude = ", vehicle.location.global_relative_frame.lat)
# Printing Vehicle's Longitude
print("Vehicle's Longitude = ", vehicle.location.global_relative_frame.lon)
# Printing Vehicle's Altitude
print("Vehicle's Altitude (in meters) = ", vehicle.location.global_relative_frame.alt)
# Printing Vehicle's Heading Angle
print("Vehicle's Heading = ", vehicle.heading)
Source: Link
Execution
To run the script geo_coords_w_heading.py
, execute the following command on your terminal:
1
$ python geo_coords_w_heading.py
Advertisement
Output
Image Credits: Dhulkarnayn, Elucidate Drones
Note
Instead of using an actual drone, I’ve used ArduPilot’s Software In The Loop (SITL) simulation and Mission Planner as Ground Control Station (GCS) software. 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:
Conclusion
In this article, I’ve tried to explain how to get GPS Coordinates of a drone using dronekit-python. Also, I’ve included the guide to retrieve the altitude and heading angle of a drone using dronekit-python. I’m expectantly waiting for your valuable feedback and suggestions regarding this article.
At last, Sharing is Caring, feel free to share with your friends if you’ve liked this article. Thank you!