[C++/CLI] Type conversion for rosparams in ROS 1

asked 2018-06-26 02:06:52 -0500

kunaltyagi gravatar image

updated 2018-06-27 01:26:56 -0500

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 }


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.

edit retag flag offensive close merge delete


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?

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-06-26 09:31:17 -0500 )edit

Creating a launch file when the parameter changes frequently is a little bothersome

kunaltyagi gravatar image kunaltyagi  ( 2018-06-26 20:20:19 -0500 )edit

You may be interested in #q249061

jayess gravatar image jayess  ( 2018-06-27 01:18:25 -0500 )edit

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

kunaltyagi gravatar image kunaltyagi  ( 2018-06-27 01:29:30 -0500 )edit

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 gravatar image PeteBlackerThe3rd  ( 2018-06-27 10:46:42 -0500 )edit

@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

kunaltyagi gravatar image kunaltyagi  ( 2018-07-04 03:18:17 -0500 )edit