0
$\begingroup$

Rosanswers logo

In ROS1 there was the hasParam method.

In ROS2, there doesn't seem to be a way to check if a parameter exists directly without attempting to get the value of the parameter.

A) Is there a way to check if a parameter exists that I'm missing B) If not, why not?


Originally posted by David Lu on ROS Answers with karma: 10932 on 2018-10-29

Post score: 0


Original comments

Comment by William on 2018-10-29:
There's no reason I'm aware of that we don't have that function, probably just haven't needed it yet. I am also working on some improvements right now so that you can have read-only parameters. Part of that is an explicit declare step for parameters, which will make "exists" make more sense.

Comment by William on 2018-10-29:
Here's the pull request: https://github.com/ros2/rclcpp/pull/495, it was originally started by Shane, but I'm trying to resurrect it right now. Maybe I can add an "exists" or "has" or "is_declared" like function at the same time.

Comment by David Lu on 2018-10-30:
@william That's what I wanted to know. If you don't end up rolling it into that PR, please ping me and I'll try to make one.

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

We didn't provide the specific API to check if a parameter exists. There are many ways to get that information.

First is to list the parameters and check if your parameter is in the list: http://docs.ros2.org/bouncy/api/rclcpp/classrclcpp_1_1_node.html#a37aa95886a706c174db77c2b160f6d7d

Or get even more information in describe_parameters: http://docs.ros2.org/bouncy/api/rclcpp/classrclcpp_1_1_node.html#adbfb47c0983e14482c39159b274f6308

The third is to ask for the parameter and check the return code: http://docs.ros2.org/bouncy/api/rclcpp/classrclcpp_1_1_node.html#a1fe80d17fb6789dec1ecf74c4d135aad

Depending on your use case each different one has tradeoffs. If you're going to be checking more than one parameter, the listing is likely valuable. If you want to do more the descriptions are valuable. And lastly if you just use the return code you can integrate the fallback behavior inside your accessing logic.


Originally posted by tfoote with karma: 58457 on 2018-10-29

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$
1
$\begingroup$

For future questions I would like to add that onwards from dashing there exists function has_parameter() within rclcpp::Node for this purpose.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.