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

How to get a bool from the param server in a C++ class?

asked 2020-08-25 23:57:25 -0500

M@t gravatar image

updated 2020-08-26 17:50:50 -0500

I'm trying to retrieve a bool from the ROS param server, inside a class with the private node handle as a member variable. However every method I've tried fails. Why? Below is my code, and I've set the value of "my_bool" on the param server to True, which I can confirm when viewing the roslaunch output.

class myClass
{
  ros::NodeHandle nh;
  bool classBool;

  myClass 
  {
    nh = ros::NodeHandle("~");
  }

  getBool
  {
    classBool = false;
                                     // Returns:
    nh.hasParam("my_bool");          //    1
    ros::param::has("~my_bool");     //    1

                                                               // Value of classBool:
    ros::param::get("~my_bool", classBool);                    //    0
    ros::param::param<bool>("~my_bool", classBool, false);     //    0
    classBool = ros::param::param<bool>("~my_bool", false);    //    0
    nh.param("my_bool", classBool, classBool);                 //    0
    nh.getParam("my_bool", classBool);                         //    0
    nh.param<bool>("my_bool", false);                          //    0
  }

};

The params are present in a yaml file:

# in my_params.yaml
my_bool: "true"
param_2: "string"
param_3: 2.0
# etc...

Which is loaded via a roslaunch file command:

<rosparam command="load" file="$(arg my_params.yaml)"

I'm thoroughly confused, because the calls to has and hasParam show that this parameter is indeed being found on the server. Yet nothing seems to return the correct value of the bool (true/1), and I would have expected each of the six commands I've tried to work. This leads me to believe that something else about this setup is interfering with the retrieval of the bool, but I don't know what.

I can use nh.param to read strings and doubles just fine. Only bools seem to fail to read.

For reference, I'm using ROS Melodic inside WSL2.

edit retag flag offensive close merge delete

Comments

can you check with the command:

rosparam list

that you find the param? and after:

rosparam get my_bool

To see if the value is actually true?

Solrac3589 gravatar image Solrac3589  ( 2020-08-26 00:31:50 -0500 )edit

Does the code which instantiates myClass also call ros::init(..)?

In general it would be good to add the missing information here: how is myClass used and instantiated, how are you starting a node using it, where is the parameter set (.launch file perhaps), etc.

gvdhoorn gravatar image gvdhoorn  ( 2020-08-26 02:34:04 -0500 )edit

@Solrac3589 Thanks for the tip! Yes, using rosparam get my_bool I can confirm that it is 'true'

M@t gravatar image M@t  ( 2020-08-26 15:05:46 -0500 )edit

Found the issue, in my yaml file I had the boolean as "true" not true. I.e. I incorrectly added the quotation marks.

M@t gravatar image M@t  ( 2020-08-26 17:43:51 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-08-26 18:17:41 -0500

M@t gravatar image

The problem was that I entered the boolean in the yaml file as a string:

# in my_params.yaml
my_bool1: "true"        # String, fails
my_bool2: true          # Boolean, succeeds

I should have picked up on this earlier as the error is also apparent when using rosparam get:

$ rosparam get my_bool1
'true'
$ rosparam get my_bool2
true

When correctly entered as a boolean rather than string, all of the methods I tried in the question will successfully return a true state.

As an aside, entering booleans as 1 or 0 in the yaml file doesn't work.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-08-25 23:57:25 -0500

Seen: 3,249 times

Last updated: Aug 26 '20