Skip to content

yamaha-bps/cbr_ros

Repository files navigation

cbr_ros

foxy ci galactic ci coverage

Utility headers to make life easier for ROS2 C++ development.

  • parameters: declare/read/write nested data structures as ROS2 parameters via boost::hana introspection
  • clock_traits_ros: use ROS clocks with the clock_traits header from cbr_utils
  • msg_utils: conversions between ROS2 interfaces and std::chrono and Eigen types

Parameters

Including cbr_ros/parameters.hpp makes it possible to declare whole data structures as ROS parameters.

Supported data format

Handle nested data structures of

  • Regular ROS parameter types
  • std::array
  • std::tuple
  • boost::hana-registered struct
  • std::vector, with the exception that multiple levels of std::vector is not supported

Example

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.

Nested structures

See test/test_parameters.cpp for more complex examples.

Shortcomings

There is no support in these methods for declaring parameter descriptors.