What is best practice for setting a QoS profile in a rclcpp publisher or subscriber?
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 =
{
RMW_QOS_POLICY_HISTORY_KEEP_LAST,
5,
RMW_QOS_POLICY_RELIABILITY_RELIABLE,
RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL,
RMW_QOS_DEADLINE_DEFAULT,
RMW_QOS_LIFESPAN_DEFAULT,
RMW_QOS_POLICY_LIVELINESS_SYSTEM_DEFAULT,
RMW_QOS_LIVELINESS_LEASE_DURATION_DEFAULT,
false
};
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);
broadcaster->sendTransform(...);
}
}
From the code in qos.hpp I think this works:
auto qos = rclcpp::QoS(
rclcpp::QoSInitialization::from_rmw(my_custom_qos_profile),
my_custom_qos_profile);
There is plenty of documentation on the QoS policies, but the documentation for this code is very minimal. So my questions are:
- Does this code do what I think it does and copy across the full contents of my_custom_qos_profile?
- What method is best practice? E.g. should I even be using the
rmw_qos_profile_t
type to hold this information? - Why does
QoSInitialization
exist? And what is its purpose beyond aiding the initalization ofrclcpp::QoS
?
Thanks in advance for any advice or explanation.
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.