0
0
Pcb-designHow-ToBeginner · 4 min read

How to Fly to Location Using DroneKit Python

To fly to a location using dronekit in Python, connect to your drone, arm it, take off to a safe altitude, then use vehicle.simple_goto(LocationGlobalRelative) with the target GPS coordinates. This commands the drone to fly to the specified latitude, longitude, and altitude.
📐

Syntax

The main steps to fly to a location using DroneKit Python are:

  • Connect to the vehicle: Use connect() with your drone's connection string.
  • Arm and takeoff: Arm the drone and take off to a target altitude using a custom function.
  • Fly to location: Use vehicle.simple_goto() with a LocationGlobalRelative object specifying latitude, longitude, and altitude.
python
from dronekit import connect, VehicleMode, LocationGlobalRelative

# Connect to the vehicle
vehicle = connect('127.0.0.1:14550', wait_ready=True)

# Arm and takeoff function
def arm_and_takeoff(aTargetAltitude):
    vehicle.mode = VehicleMode('GUIDED')
    vehicle.armed = True
    while not vehicle.armed:
        pass
    vehicle.simple_takeoff(aTargetAltitude)
    while True:
        if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
            break

# Fly to location
latitude = 47.397742
longitude = 8.545594
altitude = 10
point = LocationGlobalRelative(latitude, longitude, altitude)
vehicle.simple_goto(point)
💻

Example

This example connects to a drone, arms it, takes off to 10 meters altitude, then flies to a specific GPS location.

python
from dronekit import connect, VehicleMode, LocationGlobalRelative
import time

# Connect to the vehicle
vehicle = connect('127.0.0.1:14550', wait_ready=True)

def arm_and_takeoff(aTargetAltitude):
    print('Arming motors')
    vehicle.mode = VehicleMode('GUIDED')
    vehicle.armed = True
    while not vehicle.armed:
        time.sleep(1)
    print('Taking off!')
    vehicle.simple_takeoff(aTargetAltitude)
    while True:
        print(f'Altitude: {vehicle.location.global_relative_frame.alt:.1f} m')
        if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
            print('Reached target altitude')
            break
        time.sleep(1)

arm_and_takeoff(10)

# Define target location
target_location = LocationGlobalRelative(47.397742, 8.545594, 10)
print('Flying to target location')
vehicle.simple_goto(target_location)

# Let the drone fly for 30 seconds
time.sleep(30)

print('Landing')
vehicle.mode = VehicleMode('LAND')

# Close vehicle object
vehicle.close()
Output
Arming motors Taking off! Altitude: 0.0 m Altitude: 3.2 m Altitude: 7.8 m Altitude: 9.8 m Reached target altitude Flying to target location Landing
⚠️

Common Pitfalls

  • Not waiting for the drone to arm: Trying to take off before the drone is armed causes failure.
  • Incorrect vehicle mode: The drone must be in GUIDED mode to accept commands.
  • Ignoring altitude checks: Not confirming takeoff altitude can cause unsafe flight.
  • Using wrong coordinate types: Use LocationGlobalRelative for relative altitude, not LocationGlobal.
python
from dronekit import VehicleMode

# Wrong: Not setting mode to GUIDED
vehicle.mode = VehicleMode('STABILIZE')
vehicle.armed = True
# This will fail to take off

# Right: Set mode to GUIDED before arming
vehicle.mode = VehicleMode('GUIDED')
vehicle.armed = True
📊

Quick Reference

  • Connect: vehicle = connect('connection_string', wait_ready=True)
  • Arm and takeoff: Set mode to GUIDED, arm, then simple_takeoff(altitude)
  • Fly to location: Use simple_goto(LocationGlobalRelative(lat, lon, alt))
  • Land: Set mode to LAND

Key Takeaways

Always set the vehicle mode to GUIDED before arming and flying.
Use vehicle.simple_goto() with LocationGlobalRelative to fly to GPS coordinates.
Wait until the drone reaches the target altitude before flying to the location.
Check that the drone is armed before sending flight commands.
Switch to LAND mode to safely land the drone after the mission.