AeroCoder Logo

Cheatsheets

Pymavlink snippets cheatsheet

Copy-paste recipes for connect, send, receive, and request-message-rate.

Cheatsheet — Pymavlink Snippets

Copy-paste recipes for the most common Pymavlink operations. All examples assume `conn` is an active mavutil.mavlink_connection.

python

# ── Connect ────────────────────────────────────────────────
from pymavlink import mavutil
conn = mavutil.mavlink_connection("udpin:0.0.0.0:14550")
conn.wait_heartbeat()
print(f"System {conn.target_system} component {conn.target_component}")

python

# ── Receive a specific message ─────────────────────────────
msg = conn.recv_match(type="ATTITUDE", blocking=True, timeout=5)
if msg:
    print(f"Roll={msg.roll:.2f} Pitch={msg.pitch:.2f} Yaw={msg.yaw:.2f}")

python

# ── Request message at rate (e.g. ATTITUDE at 10 Hz) ──────
conn.mav.command_long_send(
    conn.target_system, conn.target_component,
    mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL, 0,
    30,        # message ID for ATTITUDE
    100000,    # interval in µs (100000 = 10 Hz)
    0, 0, 0, 0, 0
)

python

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

python

# ── Takeoff ────────────────────────────────────────────────
conn.mav.command_long_send(
    conn.target_system, conn.target_component,
    mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0,
    0, 0, 0, 0, 0, 0,
    10    # altitude in meters
)

python

# ── Read a parameter ───────────────────────────────────────
conn.mav.param_request_read_send(
    conn.target_system, conn.target_component,
    b"ARMING_CHECK", -1
)
msg = conn.recv_match(type="PARAM_VALUE", blocking=True, timeout=3)
print(f"{msg.param_id} = {msg.param_value}")

python

# ── Set a parameter ────────────────────────────────────────
conn.mav.param_set_send(
    conn.target_system, conn.target_component,
    b"ARMING_CHECK", 0,
    mavutil.mavlink.MAV_PARAM_TYPE_INT32
)

python

# ── Send a raw MAVLink message ─────────────────────────────
conn.mav.statustext_send(
    mavutil.mavlink.MAV_SEVERITY_INFO,
    b"Hello from Python!"
)

← Previous

MAV_CMD cheatsheet

Next →

Connection strings cheatsheet

Edit this page on mavlink.io ↗

On This Page

Cheatsheet — Pymavlink Snippets

© 2024 AeroCoder. All rights reserved

TwitterYouTubeInstagram