For a little test, I’d like to read the RC_CHANNELS data from MavLink using a python script in the OpenVSCode extension.
I’ve installed the pymavlink using pip install, and I tried to make a script to connect to udp 127.0.0.1:14550 and udp 0.0.0.0:14550. I thought by looking at the MAVLink Endpoints in BlueOS, that the GCS Server Link would be at 0.0.0.0:14550, so that reading the RC_CHANNELS could be readed through a UDP Client 127.0.0.1:14550 Endpoint aswell.
Here is my python script:
from pymavlink import mavutil
import time
# This is the endpoint BlueOS uses by default for companion apps:
connection_string = 'udpin:0.0.0.0:14550'
print(f"Connecting to MAVLink on {connection_string} ...")
mav = mavutil.mavlink_connection(connection_string)
print("Waiting for vehicle heartbeat...")
mav.wait_heartbeat()
print("Connected! Reading RC channels (press Ctrl+C to stop)")
try:
while True:
msg = mav.recv_match(type='RC_CHANNELS', blocking=True, timeout=1)
if msg:
# Print first 8 RC channels
print(" ".join([f"RC{i}: {getattr(msg, f'chan{i}_raw', None)}" for i in range(1,9)]))
else:
print("No RC_CHANNELS message received.")
time.sleep(0.2)
except KeyboardInterrupt:
print("\nExiting.")
Am I missing something, or maybe using the wrong command to actually connect to the UDP port?
By the way, are you using radio communication, or are you actually wanting to check the MAVLink-based pilot inputs (e.g. with RC_CHANNELS_OVERRIDE), or the servo rail outputs (via SERVO_OUTPUT_RAW)?
I’ve set up a new UDP Client specifically for pymavlink communication on 127.0.0.1:14575.
At this point I’m only trying to receive a heartbeat, just as a little test if I’m actually able to read any Mavlink messages with pymavlink in this setup.
This is my code to get the heartbeat:
from pymavlink import mavutil
connection_str = 'udp:0.0.0.0:14575' # Listen on all interfaces, port 14575 (adjust port if needed)
print(f"Connecting to MAVLink at {connection_str}...")
master = mavutil.mavlink_connection(connection_str)
print("Waiting for vehicle heartbeat...")
master.wait_heartbeat()
print(f"Heartbeat received from system {master.target_system}, component {master.target_component}")
from pymavlink.dialects.v20 import common as mavlink2
AUTOPILOT_COMPONENT_ID = mavutil.mavlink.MAV_COMP_ID_AUTOPILOT1
if master.target_component != AUTOPILOT_COMPONENT_ID:
print("Waiting specifically for autopilot heartbeat...")
# Wait for a heartbeat from the autopilot component
while True:
hb = master.recv_match(type='HEARTBEAT', blocking=True, timeout=10)
if hb and hb.get_srcComponent() == AUTOPILOT_COMPONENT_ID:
master.target_component = AUTOPILOT_COMPONENT_ID
print(f"Autopilot heartbeat received from system {hb.get_srcSystem()}, component {hb.get_srcComponent()}")
break
In my final use-case, I’d like to read the joystick axis (HID Device) to automate some extra things. So I guess I need to read the RC_CHANNELS messages?