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

Node not able to get its namespace parameters

asked 2017-06-05 09:13:02 -0500

gnurf gravatar image

My code looks like:

rospy.init_node('images_feeder', anonymous=True)
params = rospy.get_param('~')

I'm pretty sure it used to work "before", but now it gives:

KeyError: '~'

The parameters server seems to be running because I can for example successfully:

print rospy.get_param('/rosdistro')

(which is indigo by the way). Between "before" and now, I performed several ros (system) packages updates, and maybe changed the way I run things (currently "roscore" in one terminal, "rosrun x y" in another).

Could this be a regression, or is there something more to do, so that the node can "get its namespace" ? Thanks.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-06-05 09:41:20 -0500

updated 2017-06-05 09:51:11 -0500

Are you sure that code worked previously? I'm not aware of any recent changes to rospy that would have changed this behavior, but I suppose I could have missed something. Also note that the KeyError being thrown is exactly what should happen if the parameter you are trying to get ("~") doesn't exist and you don't provide a default value (documentation here).

Regardless, there are a variety of functions specifically for getting names from the ROS graph in the rospy.names module. You could instead be using rospy.get_namespace() or perhaps rospy.get_name() (depending on what you are trying to do). Additionally rospy is slightly different from C++ in that you don't need a "private node handle" to be able to access private parameters. In other words, depending on what you are doing, you might not even need to get the namespace directly. You might be able to use something like rospy.get_param("~private_param1").

edit flag offensive delete link more


Actually, despite the wording of the question, I am not trying to get the namespace, but to later do:

x = params['x']
y = params['y']

My problem was, I was not longer providing these parameters (via command line or launch file). If I provide them, everything is fine.

gnurf gravatar image gnurf  ( 2017-06-05 13:27:40 -0500 )edit

So the confusing thing is: if the node does not have any parameter, it will fail on get_param('~'). I guess it's an implementation quirk.

gnurf gravatar image gnurf  ( 2017-06-05 13:28:35 -0500 )edit

Perhaps you'd actually want rospy.get_param_names?

jarvisschultz gravatar image jarvisschultz  ( 2017-06-05 16:02:33 -0500 )edit

That would have helped me debug the problem, but I really want it the way I wrote it, it's just the error that is confusing.

gnurf gravatar image gnurf  ( 2017-06-06 00:52:09 -0500 )edit

Question Tools



Asked: 2017-06-05 09:13:02 -0500

Seen: 1,503 times

Last updated: Jun 05 '17