Ask Your Question
1

How to get an array of parameters with rclcpp

asked 2018-06-27 16:32:15 -0500

David Lu gravatar image

In ROS1, you could retrieve a list of parameters

The documentation shows get_parameter returning an ParameterVariant but ParameterVariant doesn't seem to have an as_vector option.

edit retag flag offensive close merge delete

Comments

Do you want a list of parameters in a certain namespace, like /ns/* -> ['value of /ns/foo', 'value of /ns/bar', 'value of /ns/baz'], or do you want one parameter as a vector (e.g. parameter /position -> [1.0, 1.0, 0.0])?

William gravatar image William  ( 2018-06-27 17:18:30 -0500 )edit

The latter, one parameter name, which is why I figured get_parameters wouldn't work.

David Lu gravatar image David Lu  ( 2018-06-27 17:51:05 -0500 )edit

We may not have that API yet, but we can certainly add it or whatever the appropriate alternative is. There was some work done on lists in parameters this cycle, but I don't know the state off-hand, let me look it up.

William gravatar image William  ( 2018-06-27 17:59:12 -0500 )edit

4 Answers

Sort by ยป oldest newest most voted
1

answered 2018-06-27 18:08:33 -0500

William gravatar image

For the slightly different case than what @tfoote linked to, here's how to get a single parameter as an array of values.


We recently merged this feature (made by @ayrton04), see:

Looks like there's no documentation yet, but there is an example of setting:

https://github.com/ros2/demos/blob/03...

And you can use get_parameter(s) to get the rclcpp::Parameter for it, and on that class there are as_*_array() methods:

https://github.com/ros2/rclcpp/blob/a...

Which return vector<T>.

edit flag offensive delete link more

Comments

Is there a way to achieve this by just using this->declare_parameter<float>("foo", 15.0); this->get_parameter("foo", foo); syntax? Or in this example how come that the parameter doesn't need to be declared? From reading a long issue involving some 2 booleans with undeclared_auto_param_something_something and another boolean I just got that you always should declare your params, but this example doesn't do that? And is the documentation 2years later still missing?

Hakaishin gravatar image Hakaishin  ( 2020-12-02 03:41:07 -0500 )edit
1

answered 2018-06-27 16:59:23 -0500

tfoote gravatar image

updated 2018-06-27 18:07:52 -0500

There's get_parameters() one line above which takes a vector of names and returns a vector of ParameterVariants

Edit:

Sorry I thought you wanted to list multiple parameters. We also support arrays of primatives. It looks like the documentation is not complete.

The ParameterVarient includes ARRAY types natively

The get_value method on the variant will return the vector value. We don't have it in the demos but there are tests covering it you can use as an example, here

edit flag offensive delete link more
0

answered 2022-04-16 17:28:16 -0500

clyde gravatar image

The syntax has been getting easier over time. Here is a simple way to declare and get a vector of integers in Galactic:

auto foo = declare_parameter<std::vector<int64_t>>("param", std::vector<int64_t>({1, 2, 3}));
edit flag offensive delete link more
0

answered 2020-12-02 03:59:14 -0500

Hakaishin gravatar image

Inspired by williams answer and checking out the newest commit on master here: https://github.com/ros2/demos/blob/ma... I found below code to work, not sure if this is the recommended way, but it works.

this->declare_parameter("foo");
rclcpp::Parameter foo_param("foo", std::vector<double>({}));
this->get_parameter("foo", foo_param);
std::vector<double> foo = foo_param.as_double_array();
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

4 followers

Stats

Asked: 2018-06-27 16:32:15 -0500

Seen: 2,536 times

Last updated: Apr 16