0
0
Pcb-designHow-ToBeginner · 4 min read

How to Send MAVLink Command to Drone: Simple Guide

To send a MAVLink command to a drone, you create a command message using the mavutil library and send it over a communication link like serial or UDP. Use mav.send() with a properly constructed command message such as command_long_encode to control the drone.
📐

Syntax

Sending a MAVLink command involves creating a command message and sending it through a connection to the drone.

  • mav.command_long_encode(): Builds the command message with parameters.
  • mav.send(): Sends the encoded command to the drone.
  • Parameters: Include target system, component, command ID, confirmation, and command-specific arguments.
python
command_message = mav.command_long_encode(
    target_system,       # Drone system ID
    target_component,    # Component ID (usually 0 or 1)
    command,             # MAVLink command ID (e.g., MAV_CMD_NAV_TAKEOFF)
    confirmation,        # 0 for no confirmation
    param1, param2, param3, param4, param5, param6, param7  # Command parameters
)
mav.send(command_message)
💻

Example

This example shows how to connect to a drone via UDP and send a takeoff command using MAVLink in Python.

python
from pymavlink import mavutil

# Connect to the drone on UDP port 14550
mav = mavutil.mavlink_connection('udp:127.0.0.1:14550')

# Wait for the heartbeat message to find the system ID
mav.wait_heartbeat()
print(f"Heartbeat from system {mav.target_system} component {mav.target_component}")

# Create a takeoff command message
command = mav.command_long_encode(
    mav.target_system,    # target system
    mav.target_component, # target component
    mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, # command ID
    0,                    # confirmation
    0, 0, 0, 0,          # params 1-4 (unused here)
    0, 0, 10             # params 5-7: latitude, longitude, altitude (altitude=10m)
)

# Send the command
mav.send(command)

print("Takeoff command sent.")
Output
Heartbeat from system 1 component 1 Takeoff command sent.
⚠️

Common Pitfalls

  • Wrong target system/component: Make sure you use the correct system and component IDs from the heartbeat.
  • Incorrect parameters: Each MAVLink command requires specific parameters; check the official MAVLink docs.
  • Not waiting for heartbeat: Sending commands before connection is ready causes failure.
  • Ignoring confirmation: Some commands need confirmation; set the confirmation parameter accordingly.
python
from pymavlink import mavutil

# Wrong: Sending command before heartbeat
mav = mavutil.mavlink_connection('udp:127.0.0.1:14550')
command = mav.command_long_encode(
    1, 1, mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, 0, 1, 0, 0, 0, 0, 0, 0
)
mav.send(command)  # May fail because heartbeat not received

# Right: Wait for heartbeat first
mav.wait_heartbeat()
mav.send(command)  # Now it works
📊

Quick Reference

StepDescription
1Connect to drone using mavutil.mavlink_connection()
2Wait for heartbeat with wait_heartbeat() to get system IDs
3Create command with command_long_encode() using correct parameters
4Send command using send() method
5Check for command acknowledgment if needed

Key Takeaways

Always wait for the drone heartbeat before sending commands to ensure connection.
Use mavutil.command_long_encode() with correct parameters to build MAVLink commands.
Send commands through the established connection using mav.send().
Verify target system and component IDs match the drone you want to control.
Check MAVLink documentation for command-specific parameter details.