How to get a bool from the param server in a C++ class?
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.
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?
Does the code which instantiates
myClass
also callros::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.@Solrac3589 Thanks for the tip! Yes, using
rosparam get my_bool
I can confirm that it is 'true'Found the issue, in my yaml file I had the boolean as "true" not true. I.e. I incorrectly added the quotation marks.