[C++/CLI] Type conversion for rosparams in ROS 1
Note: I'm not using launch files (I should, but I'm not)
$ rosrun pkg node _param_name:=45
Clearly, param_name
is integer as per the rules and 45.0 would make it a double. But
$ rosrun pkg node _param_name:='56'
Here, param_name
is still an integer instead of a string. Using '"56"'
or "'56'"
obviously converts it to string. The same is observed when I use the following
rosparam set node/param_name VALUE
How can I
- Either set a numerical parameter as string from CLI
- OR manage to store the parameter into a string from C++ (because it is a string most of the time, it being a number is a special corner case)
It'd be great if there's a method without resorting to
if(!nodeHandle.getParam(param_name, str)){if(nodeHandle.getParam(param_name, num)) str=std::to_string(num);}
if(str.size()) { do smthg }
OR
if(nodeHandle.getParam(param_name, XmlRpcValue)) { do smthg }
As a starting point, I think the issue is automatic conversion for XML RPC
because the TypeString
is checked in ros::param::getImpl
where the error is initiated.
One quick question. You seem to know that launch files are a very simple solution to this problem. Is there a reason why you are not using one here?
Creating a launch file when the parameter changes frequently is a little bothersome
You may be interested in #q249061
@jayess That question doesn't help because the issue lies in the way node parameter assignment is done during remapping process. The remapping process converts "45" into integer 45 instead of string with contents '4', '5' and '\0'
A launch file can however pull values from command line arguments or environment variables and define their types at the same time thereby solving your problem.
@PeteBlackerThe3rd I didn't consider that at all. Thanks for reminding me about this. I went with this and also found a weird fact that the ROS api somewhere manages to drop the quotation marks before passing them to boost::lexical_cast