Ask Your Question

ROS 2 Bouncy get_parameter

asked 2018-09-27 21:50:47 -0500

Marc Testier gravatar image

updated 2018-09-27 22:19:21 -0500

Hi, I'm using ROS 2 Bouncy and I'm trying to get parameters from a yaml file. I checked a few ways to get parameters and I found one that works but takes 3 lines for each parameter :

auto ip_port_param = rclcpp::Parameter("ip_port", 10940);
nh_->get_parameter("ip_port", ip_port_param);
ip_port_ = ip_port_param.as_int();

Is there a nicer way to get parameters in one line like in ROS 1 ?

pnh_.param<int>("ip_port", ip_port_, 10940);

I could change ip_port_ into a rclcpp::Parameter instead of an int but then I would have to modify the whole code. Would that be the "proper" way to do it ?

I also tried to use rclcpp::SyncParametersClient but I can't use it from within a node, I have to be outside of it (ie: in the main) because the class constructor takes a rclcpp::Node::SharedPtr node. But when I'm in the node if I use this I get a rclcpp::Node* or if I use this->shared_from_this() then it compiles and run but I don't get the parameters, they take the default value.

auto parameters_client = std::make_shared<rclcpp::SyncParametersClient>(this->shared_from_this());
ip_port_ = parameters_client->get_parameter("ip_port", 10940);

All the examples from the demos pkg that use rclcpp::SyncParametersClient, do it from the main and not inside the node.

Whereas for rclcpp::AsyncParametersClient, you can use it inside your node with this but there is no T get_parameter (const std::string &parameter_name, const T &default_value) function, you have to use a get_parameters(...) and then you don't even get the direct values but a vector of Parameter().

Am I missing something or is it just the way we have to get parameters for now ?


Edit : I can do something like :

template <class T>
rclcpp::Parameter get_param (rclcpp::Node* node, std::string param_name, T default_value) {
  auto param = rclcpp::Parameter(param_name, default_value);
  node->get_parameter(param_name, param);
  return param;

And then :

ip_port_ = get_param(this, "ip_port", "").as_int();

But I feel like it should be something that's already there.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-09-28 23:56:15 -0500

William gravatar image

updated 2018-10-08 10:54:26 -0500

There's the get_parameter_or method:

Which would allow you to do something like this:

int ip_port_;
node->get_parameter_or("ip_port", ip_port_, 80);

And recently the get_parameter_or_set which will get it if set, and if not it will set the parameter to the "default" you gave and then return it.

Edit: remove & because it's pass by reference not a pointer.

edit flag offensive delete link more


Both get_parameter_or and get_parameter_or_set take a Parameter() in and not an int or string, so we can't do this. We would need to do :

Parameter ip_port_param;
this->get_parameter_or("ip_port", ip_port_param, Parameter("", 80));
ip_port_ = ip_port_param.as_int();
Marc Testier gravatar imageMarc Testier ( 2018-09-30 19:52:29 -0500 )edit

Are you sure? The one I'm looking at takes ParameterT which is a template argument.

William gravatar imageWilliam ( 2018-10-05 09:51:35 -0500 )edit

Ok, I had some errors and didn't look much further, my bad, it does work with:

int ip_port_;
this->get_parameter_or("ip_port", ip_port_, 80);

Need to remove the & since we pass by reference, otherwise we get an error.

Marc Testier gravatar imageMarc Testier ( 2018-10-07 20:05:33 -0500 )edit

Awesome, glad you got it to work.

William gravatar imageWilliam ( 2018-10-08 10:54:36 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2018-09-27 21:50:47 -0500

Seen: 389 times

Last updated: Oct 08 '18