Manually sending and receiving MAVLink Messages to/from Gazebo

Hi all,

I am trying to manually send HIL_ACTUATOR_CONTROLS MAVLink messages directly to Gazebo through a Python script to control a drone. I am sending MAVLink messages through the pymavlink library.

When I start Gazebo, through ROS, it starts, and hangs with a black screen until I start a TCP MAVLink connection with

connection = mavutil.mavtcp("localhost:4560")'

At which point Gazebo will finish starting up, and run for 5 seconds, until it hangs again.

According to the simulation documentation, because Gazebo is running in Lockstep mode, Gazebo should be freewheeling HIL_SENSOR messages to me until it receives its first HIL_ACTUATOR_CONTROLS message. However, when I check the_connection.messages (as is suggested to check the messages received), the only messages that I see Iā€™ve gotten are a ā€˜MAVā€™ message and a ā€˜HOMEā€™ message, neither of which are valid messages. I have tried sending HIL_ACTUATOR_CONTROLS messages nonetheless, but Gazebo does not respond.

Note that I have been able to connect and control a drone in Microsoft AirSim this way (by connecting with connection.mavtcp(ā€œlocalhost:4560ā€) and manually sending HIL_ACTUATOR_CONTROLS messages.)

To summarize, my questions are as follows:

  1. Why isnā€™t Gazebo sending me any HIL_SENSOR messages?
  2. Why isnā€™t Gazebo listening to my HIL_ACTUATOR_CONTROLS messages?

EDIT: Iā€™ve found where the HIL_SENSOR messages are. Instead of calling connection.messages, I need to call connection.recv_match() or connection.recv_msg(). Not sure why connection.messages doesnā€™t work, though.

Hi!

Can you explain what youā€™re actually trying to achieve? Iā€™m not sure what youā€™re trying to do makes much sense.

Hi! I figured it out.

I was trying to manually send and receive MAVLink messages to/from a simulator via pymavlink.

After running connection = mavutil.mavtcp("localhost:4560"), I was able to call msg = connection.recv_msg() to access the HIL_SENSOR mavlink message.

Furthermore, I was able to send HIL_ACTUATOR_COMMANDS messages by calling
connection.mav.hil_actuator_controls_send(*args...*).

Now, I am able to intercept messages from the PX4 and forward them to Gazebo, and vice versa!

2 Likes

Hello chungma

I am trying to do something very similar but with jMAVsim. Did you figure out the difference between the two connection strings, from the look of it, it seems like a try - except handler (intercept messages as they arrive otherwise do nothing). Anyhow I am using

the_connection = mavutil.mavlink_connection('udpin:localhost:14540') for jMAVsim.

But instead of HIL_SENSOR, I am accessing state information via MAVLINK messages such as LOCAL_POSITION_NED and ATTITUDE_QUATERNION. I am able to successfully obtain these state information.

I am having trouble with sending the HIL_ACTUATOR_CONTROL message. Can you showcase how you send those messages and what simulated hardware you are using, specifically the channel assignment for the CONTROLS field name?