Utility headers to make life easier for ROS2 C++ development.
parameters
: declare/read/write nested data structures as ROS2 parameters viaboost::hana
introspectionclock_traits_ros
: use ROS clocks with theclock_traits
header fromcbr_utils
msg_utils
: conversions between ROS2 interfaces andstd::chrono
andEigen
types
Including cbr_ros/parameters.hpp
makes it possible to declare whole data structures as ROS parameters.
Handle nested data structures of
- Regular ROS parameter types
std::array
std::tuple
boost::hana
-registeredstruct
std::vector
, with the exception that multiple levels ofstd::vector
is not supported
For example, consider the following C++ struct:
struct MyParameters
{
double d{1};
int i{2};
std::string s{"3"};
};
We wish to declare the members of the struct as ROS parameters. Using the regular rclcpp API would require three calls to declare and three calls to read read those parameters.
MyParameters prm{};
node->declare_parameter<double>("myprm.d", prm.d);
node->declare_parameter<int>("myprm.i", prm.i);
node->declare_parameter<std::string>("myprm.s", prm.s);
prm.d = node->get_parameter("myprm.d").as_double();
prm.i = node->get_parameter("myprm.i").as_int();
prm.s = node->get_parameter("myprm.s").as_string();
This becomes very verbose for large, nested structures.
As a remedy, declareParams
in cbr_ros/parameter.hpp
makes it possible to handle MyParameters
as a single object. The caveat is that it must first be registered with boost::hana
to enable data structure introspection.
#include <boost/hana/define_struct.hpp>
BOOST_HANA_ADAPT_STRUCT(MyParameters, d, i, s); // option: register just a subset of the members
Now parameter handling becomes much more succinct:
#include <cbr_ros/parameters.hpp>
MyParameters prm{};
cbr::declareParams(*node, "myprm", prm);
cbr::getParams(*node, "myprm", prm);
Even shorter is cbr::initParams(*node, "myprm", prm);
which both declares and reads values.
See test/test_parameters.cpp
for more complex examples.
There is no support in these methods for declaring parameter descriptors.