ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

What is best practice for setting a QoS profile in a rclcpp publisher or subscriber?

asked 2021-04-20 00:21:34 -0500

M@t gravatar image

updated 2021-04-21 22:09:08 -0500

The Problem (in short)

I want to create a variety of rclcpp publishers and subscribers with a particular Quality of Service profile. I have found code and explanations that show how to create a QoS struct from the rmw library. I have also found some code that I _think_ works to convert this to a rclcpp::QoS, so that it can be input to a publisher or subscriber object. But does this code do what I think it's doing? And what is best practice here?

I'm using ROS2 Foxy for reference.

The Problem (in full)

Let's say I have some QoS profile (below) that follows the examples in qos_profiles.hpp. I know I need to convert this to a rclcpp::QoS object to input it to any ROS2 publisher or subscriber (in this example I'm using a Tf broadcaster).

#include "rclcpp/rclcpp.hpp"
#include "rmw/types.h"
#include "rclcpp/qos.hpp"
#include "tf2_ros/transform_broadcaster.h"

static const rmw_qos_profile_t my_custom_qos_profile =

class myNode: public rclcpp::Node
    std::shared_ptr<tf2_ros::TransformBroadcaster> broadcaster;

    void publish_transform (...)
        // what goes here? rclcpp::QoS qos;?
        broadcaster = make_shared<tf2_ros::TransformBroadcaster>(this, qos);


From the code in qos.hpp I think this works:

auto qos = rclcpp::QoS(

There is plenty of documentation on the QoS policies, but the documentation for this code is very minimal. So my questions are:

  1. Does this code do what I think it does and copy across the full contents of my_custom_qos_profile?
  2. What method is best practice? E.g. should I even be using the rmw_qos_profile_t type to hold this information?
  3. Why does QoSInitialization exist? And what is its purpose beyond aiding the initalization of rclcpp::QoS?

Thanks in advance for any advice or explanation.

edit retag flag offensive close merge delete


Partial answer to Q1 at least:

As per this ROS answer, you can see the QoS profile of any topic by adding --verbose. E.g.

$ ros2 topic info /topic_name --verbose
M@t gravatar image M@t  ( 2021-05-06 20:21:31 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2022-06-28 08:01:48 -0500

Fellfalla gravatar image

I think the intened way is something like this:

#include <rmw/qos_profiles.h>
#include <rclcpp/qos.hpp>

// [...]
auto qos = rclcpp::QoS(rclcpp::KeepLast(1), rmw_qos_profile_sensor_data);

Where all the standard profiles are defined in here

You may also use the convenience constructor for rclcpp::QoS( and simply modify the policy via the setting modification functions:

auto qos = rclcpp::QoS(1); // or rclcpp::KeepAll();


edit flag offensive delete link more

Question Tools



Asked: 2021-04-20 00:21:34 -0500

Seen: 4,828 times

Last updated: Jun 28 '22