Listening to MAVlink SITL

I run SITL using the following command:
sim_vehicle.py -v plane --console --map -w
As a result, I get the following output:


I want to listen to SITL messages so I use the following pymavlink script:

from pymavlink import mavutil
# Start a connection listening on a UDP port
the_connection = mavutil.mavlink_connection('udpin:localhost:14550')
# Wait for the first heartbeat
#   This sets the system and component ID of remote system for the link
the_connection.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % (the_connection.target_system, the_connection.target_component))

But even through the simulation is running, nothing is printed.

Try to use the exact IP in your script.

the_connection = mavutil.mavlink_connection('udpin:172.19.112.1:14550')

Or launch SITL with an additional --out argument.

sim_vehicle.py --out udp:localhost:14550
1 Like

Thanks a lot for your input!
Changing ‘localhost’ to the explicit address:
the_connection = mavutil.mavlink_connection('udpin:172.19.112.1:14550')
Resulted in an error:


Online examples recommend adding another UDP port (because one is already taken by SITL)
So I added 14560 using your suggested command.
sim_vehicle.py --out udp:localhost:14560
with
the_connection = mavutil.mavlink_connection('udpin:172.19.112.1:14560')
And this resulted in a similar error:

What worked is using:
sim_vehicle.py --out udp:localhost:14560
with
the_connection = mavutil.mavlink_connection('udpin:localhost:14560')

Why using the address explicitly didn’t work ?

I’d assume it’s some kind of network misconfiguration from your OS but I can offer no further help on that subject.
When I launch SITL in WSL it uses the localhost IP (127.0.0.1).

@Echo
After making it work I modified the script to listen to attitude messages outgoing from SITL.

from pymavlink import mavutil
# Start a connection listening on a UDP port
sitl_outputs = mavutil.mavlink_connection('udpin:localhost:14560')
while 1:
    msg_sitl_outputs = sitl_outputs.recv_match(type='ATTITUDE', blocking=True)
    print (msg_sitl_outputs)

It also works and I periodically get ATTITDUTE messages printed out.

Now want to send a message from the Mavproxy console to the SITL ( for example : ‘SET_ATTITUDE_TARGET’ ) and have it captured and printed.
How would you modify the Python script to achieve that ?

I’m not really sure what you mean by that but there are several ways of sending MAVLink messages to the Autopilot. If you list the methods of sitl_outputs.mav you can see there’s a set_attitude_target_encode and set_attitude_target_send function.

Here are some examples of the last function being used:

https://www.ardusub.com/developers/pymavlink.html

1 Like

@Echo,

What I meant is that you can send messages from the Mavproxy console to the SITL (not through pymavlink)
For example: entering attitude 1 0 0 0 0.5 at the Mavproxy console will send the SET_ATTITUDE_TARGET Mavlink message to the SITL.
image
This is explained at the bottom of the following documentation page:
https://ardupilot.org/dev/docs/copter-commands-in-guided-mode.html#copter-commands-in-guided-mode-set-attitude-target
Now, my pymavlink code listens to messages sent from the SITL.
But I want to add the capability to capture messages sent to the SITL.
In my particular example, I want the code to capture the SET_ATTITUDE_TARGET message sent from the Mavproxy console.