0
0
Pcb-designHow-ToBeginner · 4 min read

How to Land a Drone Using DroneKit Python

To land a drone using dronekit in Python, connect to the vehicle, set the mode to LAND, and wait until the drone finishes landing. Use vehicle.mode = VehicleMode('LAND') to command the drone to land safely.
📐

Syntax

The basic syntax to land a drone with DroneKit Python involves setting the vehicle's mode to LAND. This tells the drone to start descending and land automatically.

  • vehicle.mode = VehicleMode('LAND'): Changes the drone's mode to land.
  • vehicle.is_armable: Checks if the drone is ready to be controlled.
  • vehicle.armed: Indicates if the drone's motors are armed.
python
from dronekit import connect, VehicleMode

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

# Set mode to LAND to start landing
vehicle.mode = VehicleMode('LAND')
💻

Example

This example connects to a drone, checks if it is armable, arms it, takes off to 10 meters, then commands it to land safely.

python
from dronekit import connect, VehicleMode, LocationGlobalRelative
import time

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

# Wait until the vehicle is armable
while not vehicle.is_armable:
    print('Waiting for vehicle to become armable...')
    time.sleep(1)

# Arm the vehicle
vehicle.mode = VehicleMode('GUIDED')
vehicle.armed = True

while not vehicle.armed:
    print('Waiting for arming...')
    time.sleep(1)

# Take off to 10 meters
target_altitude = 10
vehicle.simple_takeoff(target_altitude)

# Wait until the vehicle reaches a safe height
while True:
    print(f'Altitude: {vehicle.location.global_relative_frame.alt:.1f} m')
    if vehicle.location.global_relative_frame.alt >= target_altitude * 0.95:
        print('Reached target altitude')
        break
    time.sleep(1)

# Command the vehicle to land
print('Landing...')
vehicle.mode = VehicleMode('LAND')

# Wait until the vehicle lands and disarms
while vehicle.armed:
    print(f'Altitude: {vehicle.location.global_relative_frame.alt:.1f} m')
    time.sleep(1)

print('Landed and disarmed')

# Close vehicle object
vehicle.close()
Output
Waiting for vehicle to become armable... Waiting for vehicle to become armable... ... Waiting for arming... Altitude: 0.0 m Altitude: 2.3 m Altitude: 5.1 m Altitude: 9.8 m Reached target altitude Landing... Altitude: 9.5 m Altitude: 6.2 m Altitude: 3.0 m Altitude: 0.5 m Landed and disarmed
⚠️

Common Pitfalls

Common mistakes when landing a drone with DroneKit Python include:

  • Not checking if the vehicle is armable before arming.
  • Forgetting to set the mode to GUIDED before arming and takeoff.
  • Not waiting for the drone to reach the target altitude before landing.
  • Not monitoring the vehicle's armed state to confirm landing completion.

Always ensure proper mode changes and state checks to avoid unsafe commands.

python
from dronekit import VehicleMode

# Wrong: Trying to land without setting mode to GUIDED or arming
vehicle.mode = VehicleMode('LAND')  # This may fail if not armed or guided

# Right: Proper sequence
vehicle.mode = VehicleMode('GUIDED')
vehicle.armed = True
# Wait for arming
while not vehicle.armed:
    pass
vehicle.mode = VehicleMode('LAND')
📊

Quick Reference

Summary tips for landing a drone using DroneKit Python:

  • Always connect and wait for vehicle readiness.
  • Set mode to GUIDED before arming and takeoff.
  • Use simple_takeoff() to reach a safe altitude.
  • Set mode to LAND to start landing.
  • Monitor vehicle.armed to confirm landing completion.

Key Takeaways

Set the vehicle mode to 'LAND' using vehicle.mode = VehicleMode('LAND') to command landing.
Always check if the drone is armable and set mode to 'GUIDED' before arming and takeoff.
Use vehicle.simple_takeoff() to reach a safe altitude before landing.
Monitor vehicle.armed to confirm the drone has landed and disarmed.
Proper mode changes and state checks prevent unsafe drone commands.