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

Check if ROS2 Parameters Exist with RCLCPP

asked 2018-10-29 15:10:45 -0500

David Lu gravatar image

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?

edit retag flag offensive close merge delete


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.

William gravatar image William  ( 2018-10-29 18:55:57 -0500 )edit

Here's the pull request: , 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.

William gravatar image William  ( 2018-10-29 18:57:51 -0500 )edit

@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.

David Lu gravatar image David Lu  ( 2018-10-30 07:52:01 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-10-29 16:16:41 -0500

tfoote gravatar image

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:

Or get even more information in describe_parameters:

The third is to ask for the parameter and check the return code:

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.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-10-29 15:10:45 -0500

Seen: 1,488 times

Last updated: Oct 29 '18