0
0
Pcb-designHow-ToBeginner · 3 min read

How to Change Flight Mode Using DroneKit: Simple Guide

To change the flight mode using dronekit, set the vehicle.mode property to a new VehicleMode object with the desired mode name, then wait until the mode change is confirmed. For example, use vehicle.mode = VehicleMode('GUIDED') to switch to GUIDED mode.
📐

Syntax

The basic syntax to change the flight mode in DroneKit is:

  • vehicle.mode = VehicleMode('MODE_NAME'): Assigns the new flight mode.
  • VehicleMode: A class that takes the mode name as a string.
  • vehicle.mode.name: Property to check the current mode name.

You should wait until the mode change is confirmed by checking vehicle.mode.name.

python
vehicle.mode = VehicleMode('GUIDED')
while vehicle.mode.name != 'GUIDED':
    time.sleep(1)
💻

Example

This example connects to a drone, changes its flight mode to GUIDED, and waits until the mode is confirmed.

python
from dronekit import connect, VehicleMode
import time

# Connect to the vehicle (replace with your connection string)
vehicle = connect('127.0.0.1:14550', wait_ready=True)

# Change mode to GUIDED
print(f'Current mode: {vehicle.mode.name}')
vehicle.mode = VehicleMode('GUIDED')

# Wait until mode is changed
while vehicle.mode.name != 'GUIDED':
    print('Waiting for mode change...')
    time.sleep(1)

print(f'Mode changed to: {vehicle.mode.name}')

# Close vehicle object
vehicle.close()
Output
Current mode: STABILIZE Waiting for mode change... Mode changed to: GUIDED
⚠️

Common Pitfalls

Common mistakes when changing flight mode with DroneKit include:

  • Not waiting for the mode change to complete before proceeding.
  • Using an invalid mode name that the vehicle does not support.
  • Trying to change mode before the vehicle is fully connected and ready.

Always check vehicle.mode.name after setting the mode to confirm the change.

python
from dronekit import VehicleMode
import time

# Wrong: Not waiting for confirmation
vehicle.mode = VehicleMode('GUIDED')
print(vehicle.mode.name)  # Might still show old mode

# Right: Wait for confirmation
vehicle.mode = VehicleMode('GUIDED')
while vehicle.mode.name != 'GUIDED':
    time.sleep(1)
print(vehicle.mode.name)
📊

Quick Reference

ActionCode ExampleNotes
Set flight modevehicle.mode = VehicleMode('MODE_NAME')MODE_NAME like 'GUIDED', 'LOITER', 'RTL'
Check current modeprint(vehicle.mode.name)Returns current mode as string
Wait for mode changewhile vehicle.mode.name != 'MODE_NAME': time.sleep(1)Ensures mode change is complete
Connect to vehiclevehicle = connect('connection_string', wait_ready=True)Must connect before changing mode

Key Takeaways

Use vehicle.mode = VehicleMode('MODE_NAME') to change flight mode in DroneKit.
Always wait and check vehicle.mode.name to confirm the mode has changed.
Ensure the vehicle is connected and ready before changing modes.
Use valid mode names supported by your drone firmware.
Avoid proceeding with commands until the mode change is confirmed.