AeroCoder Logo

Implementation Guide

Using Pymavlink

Install, connect, send, receive, and request message rates with Python.

Pymavlink is the official Python MAVLink library. It provides connection management, message encoding/decoding, and convenience methods for common operations.

bash

# Install Pymavlink
pip install pymavlink

python

from pymavlink import mavutil

# Connect (SITL over UDP)
conn = mavutil.mavlink_connection("udpin:0.0.0.0:14550")
conn.wait_heartbeat()
print(f"Connected to system {conn.target_system}")

# Request a specific message at 10 Hz
conn.mav.command_long_send(
    conn.target_system, conn.target_component,
    mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL,
    0,
    33,       # GLOBAL_POSITION_INT message ID
    100000,   # interval in µs (10 Hz)
    0, 0, 0, 0, 0
)

# Receive messages
while True:
    msg = conn.recv_match(type="GLOBAL_POSITION_INT", blocking=True, timeout=5)
    if msg:
        print(f"Lat: {msg.lat/1e7:.6f}, Lon: {msg.lon/1e7:.6f}, Alt: {msg.alt/1e3:.1f}m")

Connection strings

udpin:host:port — listen for UDP. udpout:host:port — send UDP. tcp:host:port — TCP client. /dev/ttyUSB0 or COM3 — serial. udpin:0.0.0.0:14550 is the most common for SITL.

python

# Arm the vehicle
conn.mav.command_long_send(
    conn.target_system, conn.target_component,
    mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
    0,
    1,    # 1 = arm
    0, 0, 0, 0, 0, 0
)
ack = conn.recv_match(type="COMMAND_ACK", blocking=True, timeout=3)
print(f"Arm result: {ack.result}")

← Previous

Language and generator options

Next →

SITL connection examples

Edit this page on mavlink.io ↗

On This Page

Connection strings

© 2024 AeroCoder. All rights reserved

TwitterYouTubeInstagram