ROS2 Gazebo with px4_ros_com


I am working towards setting up a ROS2 Eloquent, PX4 SITL, Gazebo simulation so I can fly an iris quad-copter. I have read a lot of the PX4 documentation and built much of the code including px4_ros_com.

The part I’m struggling with is working out how to connect the FastRTPS client to a drone in Gazebo. The old way of doing this in ROS was to use the MAVROS package and the Gazebo drone model included a library to exchange messages. Is there something similar for ROS2 and the FastRTPS client?

Many thanks.

Let me ping @TSC21 here.

Thanks for passing on my request.

@AndyBlight I am not sure what you are trying to achieve, but the examples that you have available in use SITL with Gazebo.

@TSC21. Thanks for getting back to me. Apologies for not being clear in my first post. I am relatively new to ROS2, PX4 and Gazebo and it is difficult to know what to ask.

What I am trying to do is to use ROS2 to fly a drone in Gazebo. Once I understand the basics, I then want to modify the drone to add several custom parts to the simulation. This may involve changing the behaviour of the drone so I need to understand the how everything fits together.

From what I have read and tried, there seem to be two well trodden paths, 1. ROS2 and Gazebo and 2. PX4, SITL and Gazebo. My guess was that I should be able to use ROS2 to talk to PX4, SITL and Gazebo as was done for ROS using MAVROS. I found px4_ros_com and it seemed to be a replacement for MAVROS but I could not a clear example of how to put it all together. I worked through all the examples in and, after spending a few hours getting it to build using ROS2 only, I was able to run the examples. However, I still could not figure out how to get a drone to fly using ROS2 to command it.

From what I understand of Gazebo, the model of the simulated drone includes a library that tells Gazebo how the model should behave. This library is connected somehow (SITL?) to px4_ros_com so that ROS2 nodes can control the done. So this comes down to me not fully understanding how the px4_ros_com client connects to Gazebo.

Shortly after posting the original question, I started searching again for possible solutions and revisited the OSRF Drone Demo project. After several hours spent picking through the code, I eventually found a test program that did what I wanted: use a ROS2 node to control a drone using the ros2_serial_example package to talk to a SITL PX4 drone in Gazebo. This program commands a drone to take off, fly to a few way points and land. Almost exactly what I want to do to start with.

So my questions are:

  1. Is there any documentation that shows me how ROS2 uses px4_ros_com with SITL and Gazebo? I want to understand how it all fits together, what bits of software are used to make it all work, which processes use what libraries, ideally referring to code so I can work out which bits I need to modify in the future.
  2. What are the pros and cons of using px4_ros_com and ros2_serial_example? From my beginners point of view, they both do the same job but it different ways.
  1. The examples in use SITL and Gazebo. What you are looking for is a ROS2 node that sends setpoints to the vehicle and closes the control loop on the node itself, similar to what you do with MAVROS when using Offboard. There is no example of that yet and it does require some time to set one up, besides a clear understanding of how the PX4 internals work, since as you might have understood, the micro-RTPS bridge directly interfaces with uORB topics - there’s no abstraction layer over it, similar to what you find in Mavlink;

  2. I am not sure of the specifics of the ros2_serial_example, so I can’t actually answer that question.


I spent most of yesterday working through the examples in micrortps.html again. Whilst I was able to build and run the examples, the sensor_combined_listener example prints the Starting ... line but then does nothing. Also, the micrortps_agent seg faults when Ctrl+c is pressed. I’ll raise bugs on the px4_ros_com repo.

I really can’t spend much more time on getting px4_ros_com to work so I’m going to use the osrf/drone_demo approach for now.

Thanks for your help.

@AndyBlight you are using the wrong Fast-RTPS-Gen version, according to what I could see on the issues you opened. On the documentation we clearly identify that the version that needs to be installed is 1.0.4, not 1.0.2.

You are free to use the drone_demo, but keep in mind that that repo is an OSRF demo, while what we are providing here is an official PX4 supported bridge with DDS/ROS2. If you want our help to make it work, we can help, but it is relevant that people follow the documentation correctly, which would for sure remove all the cumbersome of hours of debug from the user.

@TSC21 Thanks for supporting me while I try to get a build together that works.

Now that I have built and installed fast-rtps-gen v1.0.4, I have tried to rebuild PX4/Firmware using the command make px4_sitl_rtps. This fails with the error:

[ 22%] Building C object src/modules/micrortps_bridge/micro-CDR/CMakeFiles/microcdr.dir/src/c/types/basic.c.o
Error: Unable to access jarfile /usr/local/bin/../share/fastrtpsgen/java/fastrtpsgen.jar

It appears that the install of fast-rtps-gen places the file here /usr/local/share/fastrtps/fastrtpsgen.jar so is not where the Firmware build is looking for it. I am using today’s master branch of Should I be using a tagged version of the repo?

Having copied the fastrtpsgen.jar file to the correct place, I now face the next problem with the build.

Scanning dependencies of target micrortps_agent
[  0%] Building CXX object CMakeFiles/micrortps_agent.dir/RtpsTopics.cpp.o
In file included from /home/andy/git/PX4/Firmware/build/px4_sitl_rtps/src/modules/micrortps_bridge/micrortps_client/micrortps_agent/RtpsTopics.h:38:0,
                 from /home/andy/git/PX4/Firmware/build/px4_sitl_rtps/src/modules/micrortps_bridge/micrortps_client/micrortps_agent/RtpsTopics.cpp:34:
/home/andy/git/PX4/Firmware/build/px4_sitl_rtps/src/modules/micrortps_bridge/micrortps_client/micrortps_agent/microRTPS_timesync.h:46:10: fatal error: Timesync_Publisher.h: No such file or directory
 #include "Timesync_Publisher.h"
compilation terminated.

I seem to remember that I had this last time, but don’t have record of how I fixed it. I remember something about copying files from another repo but I may be wrong.

Did you clean the build before trying to build again?

@TSC21 Sorry. I just noticed that make px4_sitl_rtps builds correctly.

The previous error message comes when I try to build micrortps_agent in the Firmware repo using:

# Build the ROS independent agent.
cd ${PX4_FIRMWARE_GIT_DIR}/build/px4_sitl_rtps/src/modules/micrortps_bridge/micrortps_client/micrortps_agent
mkdir -p build && cd build
cmake ..

This is not important for what I’m trying to do, so I’ll ignore it for now.

I have now rebuilt the Firmware using make px4_sitl_rtps gazebo and the agent using fastrtpsgen v1.0.4 and I can now successfully run the sensor combined listener example program.

Thanks again for your patience and expertise in guiding me through this problem.

1 Like