Ask Your Question
1

How to pass a parameter of type double as an input to a roscpp node?

asked 2017-10-10 21:39:37 -0500

M@t gravatar image

updated 2017-10-10 21:44:04 -0500

I'm trying to pass a variable of type "double" to a roscpp node as a command-line variable. E.g.

$ rosrun package_name node_name parameter:=0.1

And then access the parameter within the node like so:

ros::NodeHandle nh;
double parameter;
nh.param("parameter", parameter, 0.5);

However this doesn't work, and instead I get the following error:

terminate called after throwing an instance of 'ros::InvalidNameException'
    what():  Character [0] is not valid as the first character in Graph Resource Name [0.1].  Valid characters are a-z, A-Z, / and in some cases ~.

I'm sure I've just got the wrong syntax, either within the node or when entering the parameter. I've thoroughly read the wiki pages on NodeHandle and the Parameter Server, but I haven't found a solution. Everything I've tried either produces the error above, or my ignores the input and sets the parameter to it's default value (0.5). Is anyone able to correct my syntax?

Thanks in advance.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2017-10-10 22:09:32 -0500

ufr3c_tjc gravatar image

updated 2017-10-11 15:41:40 -0500

M@t gravatar image

The rosrun docs seems to suggest a _ must be prepended to the variable name. Maybe try that? That is, rosrun package_name node_name _parameter:=0.1

Edit: In order to receive the parameter properly, the node handle must also be initalized to access private parameters, e.g.

ros::NodeHandle nh("~");

This also works with multiple "double" parameters

edit flag offensive delete link more

Comments

2

This is (almost certainly) the answer, so I converted it to an answer.

@M@t: without the underscore parameter:=0.1 is a remapping.

gvdhoorn gravatar image gvdhoorn  ( 2017-10-11 03:15:54 -0500 )edit

That's part of the answer. The underscore isn't enough by itself. It looks like the node handle must be set up to access private parameters. I.e. you also need to add ~ to the initialization:

ros::NodeHandle nh("~");

Then, the parameter is received properly. Thanks gvdhoorn and ufr3c_tjc!

M@t gravatar image M@t  ( 2017-10-11 15:37:33 -0500 )edit

Yeah it was a guess on my end so wasn't confident enough in it to put it as an answer, but glad you figured it out!

ufr3c_tjc gravatar image ufr3c_tjc  ( 2017-10-11 17:44:46 -0500 )edit
1

answered 2020-12-20 21:46:23 -0500

htanwar922 gravatar image

updated 2020-12-20 21:50:17 -0500

Another way for this is using ros::param::param instead of nh.param. This way, only the desired param is rendered private, rest aren't affected, unlike with the Nodehandle nh("~").

using namespace ros;
NodeHandle nh;
double param;
ros::param::param <double> ("~param", param, 0.5);
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-10-10 21:39:37 -0500

Seen: 1,414 times

Last updated: Dec 20 '20