How to Create Waypoint Mission Using DroneKit Python
To create a waypoint mission using
DroneKit Python, you connect to the drone, define waypoints as LocationGlobalRelative objects, add them to a CommandSequence, upload the mission, and then start the mission by changing the drone's mode to AUTO. This process lets the drone fly through the specified GPS points automatically.Syntax
Here is the basic syntax to create and upload a waypoint mission using DroneKit Python:
connect(): Connect to the drone.CommandSequence(): Create a list to hold mission commands.LocationGlobalRelative(lat, lon, alt): Define each waypoint with latitude, longitude, and altitude.add(): Add waypoint commands to the mission list.vehicle.commands.upload(): Upload the mission to the drone.vehicle.mode = VehicleMode('AUTO'): Start the mission.
python
from dronekit import connect, VehicleMode, LocationGlobalRelative, Command # Connect to the vehicle vehicle = connect('127.0.0.1:14550', wait_ready=True) # Create a new mission command list cmds = vehicle.commands cmds.clear() # Add waypoints cmds.add(Command(0, 0, 0, 16, 0, 0, 0, 0, 47.397742, 8.545594, 10)) # Waypoint 1 cmds.add(Command(0, 0, 0, 16, 0, 0, 0, 0, 47.397825, 8.545600, 10)) # Waypoint 2 # Upload mission cmds.upload() # Set mode to AUTO to start mission vehicle.mode = VehicleMode('AUTO')
Example
This example connects to a simulated drone, creates a simple two-waypoint mission, uploads it, and starts the mission in AUTO mode.
python
from dronekit import connect, VehicleMode, Command import time # Connect to the vehicle vehicle = connect('127.0.0.1:14550', wait_ready=True) # Clear existing mission cmds = vehicle.commands cmds.clear() # Define waypoints (latitude, longitude, altitude) waypoints = [ (47.397742, 8.545594, 10), (47.397825, 8.545600, 10) ] # Add waypoints to mission for wp in waypoints: lat, lon, alt = wp cmds.add(Command(0, 0, 0, 16, 0, 0, 0, 0, lat, lon, alt)) # Upload mission cmds.upload() print("Mission uploaded") # Set mode to AUTO to start mission vehicle.mode = VehicleMode('AUTO') print("Mission started in AUTO mode") # Wait for mission to complete or timeout time.sleep(30) # Close vehicle connection vehicle.close() print("Mission finished and vehicle disconnected")
Output
Mission uploaded
Mission started in AUTO mode
Mission finished and vehicle disconnected
Common Pitfalls
Common mistakes when creating waypoint missions include:
- Not clearing existing missions before uploading new ones, causing conflicts.
- Forgetting to upload the mission after adding waypoints.
- Not setting the vehicle mode to
AUTOto start the mission. - Using incorrect coordinate formats or altitudes that the drone cannot reach.
- Not waiting for the vehicle to be ready before sending commands.
python
from dronekit import connect, VehicleMode, Command # Wrong: Not clearing old mission vehicle = connect('127.0.0.1:14550', wait_ready=True) cmds = vehicle.commands # cmds.clear() # Missing clear causes old mission to remain cmds.add(Command(0, 0, 0, 16, 0, 0, 0, 0, 47.397742, 8.545594, 10)) cmds.upload() vehicle.mode = VehicleMode('AUTO') # Right: Clear before adding cmds.clear() cmds.add(Command(0, 0, 0, 16, 0, 0, 0, 0, 47.397742, 8.545594, 10)) cmds.upload() vehicle.mode = VehicleMode('AUTO')
Quick Reference
Summary tips for creating waypoint missions with DroneKit Python:
- Always connect and wait for the vehicle to be ready.
- Clear existing missions before adding new waypoints.
- Use
Commandobjects with correct parameters for waypoints. - Upload the mission before starting.
- Set vehicle mode to
AUTOto begin the mission.
Key Takeaways
Connect to the drone and wait until it is ready before sending commands.
Clear existing missions before adding new waypoint commands to avoid conflicts.
Use
Command objects with latitude, longitude, and altitude to define waypoints.Upload the mission commands to the drone before starting the mission.
Set the drone's mode to
AUTO to execute the waypoint mission.