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

parameter management in ROS2

asked 2022-12-15 20:22:41 -0600

xibeisiber gravatar image

Hi all,

I want to pass a bunch of parameters to certain C++ class which is indirectly called in a ros2 node: image description

Some parameters can be modified in runtime via HMI or other ways?

Since ros2 can only load parameters in yaml file from a node(if I understand correctively), is there a convenient way to pass these parameters around? I saw elsewhere that somebody suggests using database db file to store these parameters and use orocos rtt::service to load them.


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-12-16 11:26:55 -0600

ChuiV gravatar image

updated 2022-12-19 06:07:30 -0600

There may be better ways, but we (at my company) tend to gravitate towards passing around the rclcpp::Node object and parameter namespaces. Then anything that needs to have some configuration can use the parameter namespace and node object to just declare it's own parameters and register callbacks for those parameter changes.

In cases where these classes are specifically ros-agnostic, we'll pass in a shared_ptr configuration struct into the non-ros classes. Then from the ros node, we can still declare parameters, and callbacks that change values in that shared configuration struct.

Added Examples:

Configuration Struct:

struct ClassNConfiguration
  int64_t field_a;
  std::string field_b;


class ClassN
  ClassN(std::shared_ptr<ClassNConfiguration> configuration) :
  std::shared_ptr<ClassNConfiguration> configuration_;

Node Class:

class MyNode : rclcpp::Node
  MyNode() :
    class_n_config_.field_a = declare_parameter("class_n.field_a", 42);
    class_n_config_.field_b = declare_parameter("class_n.field_b", "hello ros2");
    class_n_ = std::make_shared<ClassN>(class_n_config_);
  std::shared_ptr<ClassNConfiguration> class_n_config_;
  std::shared_ptr<ClassN> class_n_;
edit flag offensive delete link more


Thanks very much for your reply.

Could you please provide an example of the shared_ptr configuration struct? I am not sure how to implement it.

Thanks for your kind help!

xibeisiber gravatar image xibeisiber  ( 2022-12-18 03:30:29 -0600 )edit

Thanks a lot!

xibeisiber gravatar image xibeisiber  ( 2022-12-21 00:26:28 -0600 )edit

Question Tools

1 follower


Asked: 2022-12-15 20:22:41 -0600

Seen: 202 times

Last updated: Dec 19 '22