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

parameter management in ROS2

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

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.

Thanks!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

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

ChuiV gravatar image

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

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;
}

ClassN:

class ClassN
{
public:
  ClassN(std::shared_ptr<ClassNConfiguration> configuration) :
      configuration_(configuration)
  {
    ...
  }
protected:
  std::shared_ptr<ClassNConfiguration> configuration_;
}

Node Class:

class MyNode : rclcpp::Node
{
public:
  MyNode() :
      rclcpp::Node("my_node"),
      class_n_config_(std::make_shared<ClassNConfiguration>())
  {
    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_);
  }
protected:
  std::shared_ptr<ClassNConfiguration> class_n_config_;
  std::shared_ptr<ClassN> class_n_;
}
edit flag offensive delete link more

Comments

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 -0500 )edit

Thanks a lot!

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

Question Tools

1 follower

Stats

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

Seen: 248 times

Last updated: Dec 19 '22