Unable to establish a connection to SITL/Pixhawk via UDP/TCP/Serial

Hello all,

I’m not sure if this is the correct forum to be posting this question to. I apologize beforehand if it is not.

I’m running into an issue when I attempt to connect to the Gazebo simulator using pymavlink. Specifically, the issue is this line of code:

vehicleConnection = mavutil.mavlink_connection() 

For TCP, I set up SITL (Gazebo) using these instructions. This is what it looks like when it is running:

(not sure what “link 1 down” means)

Here is the connection I use:

vehicleConnection = mavutil.mavlink_connection('tcp:127.0.0.1:5760') 

When I run this line within a Python script, I get the following output:

[Errno 111] Connection refused sleeping
[Errno 111] Connection refused sleeping
Unable to establish connection with PixHawk.

For UDP, I ran the headless Gazebo simulator found on px4-gazebo-headless. Here is the connection I use:

vehicleConnection = mavutil.mavlink_connection('udpin:localhost:14540') 

When I run the Python script, it never gets past this line. I have to Ctrl+C to kill the process.

I know this port is active because I’m able to connect to it via mavsdk_server. I was also able to retrieve data from the simulation with MAVSDK.

For serial, I plug in a Pixhawk into my machine with a USB cord and then attach a GPS unit to the Pixhawk.

This was originally “working”. The building I’m in makes it impossible for the GPS unit to connect to satellites, but I was at least able to get 0’s. But now when I run the script with this line of code:

vehicleConnection = mavutil.mavlink_connection('/dev/ttyACM0')

I only get BAD DATA responses. Updating the firmware with QGC did not fix this issue.

My system is running Ubuntu 20.04 LTS.

My main goal is to get an SITL running and figure out the serial connection later. I’ve searched this forum, the pymavlink gitter channel, and the Ardupilot repo for answers but I’m coming up short.

Any help would be greatly appreciated.

-Matt

Update in case anybody comes across this topic with the same issue. Hopefully it helps them get unstuck.

For TCP: I am still unable to connect. Our project will be using UDP. Not being able to connect via TCP was to showcase the issue.

For UDP: Changing the IP from localhost (or 127.0.0.0) to 0.0.0.0 allows me to connect to the headless Gazebo. It does not let me connect to the Gazebo is setup using these instructions.

Setting the IP to 0.0.0.0 is essentially telling MAVLink that you are not sure what the IP address is. However, I’m not sure how it finds the correct IP. This was also the question within this topic regarding a MAVLink tool.

It’s odd that localhost did not work, but the issue could be that the SITL is running within a docker container. I attempted to locate the UDP port using netstat/ss. The only thing I found was the TCP connection from the docker container. I did not explore this further.

For serial: The issue was that I was attempting to print out parameters associated with the mavlink_connection prior to recieiving a heartbeat. I had done this to debug the code but didn’t realize that it was a bug in my code. Classic.

i.e. if I have

connection = mavutil.mavlink_connection('/dev/ttyACM0')

and I attempted to print out parameters associated with connection prior to establishing a heartbeat,

print(connection.recv_match(blocking=True))

I get BAD_DATA.

But if I do

connection = mavutil.mavlink_connection('/dev/ttyACM0')
connection.wait_heartbeat()
print(connection.recv_match(blocking=True))

or

connection = mavutil.mavlink_connection('/dev/ttyACM0')
connection.recv_match(type='HEARTBEAT', blocking=True)
print(connection.recv_match(blocking=True))

I get the correct output.

If anybody knows how pymavlink finds the IP address associated with the port, let me know. I couldn’t find it in the source code on github.

-Matt