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

How to pass private parameters to multiple instances of node?

asked 2019-11-15 10:56:15 -0500

LukeAI gravatar image

updated 2019-11-19 06:18:42 -0500

gvdhoorn gravatar image

I have created a package containing a rospy node that I wish to be able to launch from arbitrary other packages with custom arguments with each launch

eg. myex.py

#!/usr/bin/env python
import rospy
foo = rospy.get_param("foo")          
print str(foo)

In a launch file somewhere else:

<launch>
<node pkg="min_ex" name="node_name" type="myex.py" output="screen">
    <param name="foo" type="int" value="5"/>
</node>
</launch>

This always returns KeyError: 'foo'

Adding "~" to the beginning of the param name in both or either of the launch or .py does not make a difference.

rosparam list shows me that foo is being published on mylib1/foo

So How can I get the node to access the parameter without having to know about the namespace in which it has been launched? I want to be able to create many instantiations of the same node using different parameters.

Code example to show my problem:

#!/usr/bin/env python
import rospy
foo = rospy.get_param("~foo")
print str(foo)

launch file:

<launch>
<node pkg="min_ex" name="node_name" type="myex.py" output="screen">
    <param name="~foo" type="int" value="5"/>
</node>
</launch>
edit retag flag offensive close merge delete

Comments

re: MWE in 'mini workspace': we don't need a workspace. The two files are sufficient.

gvdhoorn gravatar image gvdhoorn  ( 2019-11-19 06:20:08 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2019-11-19 06:27:43 -0500

LukeAI gravatar image

The thing I was missing was that it is necessary to init the node in order to access private params. eg.

#!/usr/bin/env python
import rospy
rospy.init_node("some_name")
foo = rospy.get_param("~foo")
print str(foo)
edit flag offensive delete link more
2

answered 2019-11-15 11:10:09 -0500

In rospy , private parameters use the ~ symbol.

http://wiki.ros.org/rospy/Overview/Pa...

Ei.

  foo = rospy.get_param("~foo")
edit flag offensive delete link more

Comments

Thanks for response, I have read that page, unfortunately it doesn't work. I have produced a minimal working example that illustrates the point

LukeAI gravatar image LukeAI  ( 2019-11-19 05:36:49 -0500 )edit

Adding "~" to the beginning of the param name in both or either of the launch or .py does not make a difference.

you are only supposed to prefix ~ to the name of the parameter in your rospy.get_param(..) call. Not to the name in the .launch file.

The fact that you make the param element a child of the node element already makes it a private parameter.

gvdhoorn gravatar image gvdhoorn  ( 2019-11-19 06:17:10 -0500 )edit
1

You show this in your MWE:

#!/usr/bin/env python
import rospy
foo = rospy.get_param("~foo")
print str(foo)

this is not a node (as you don't call rospy.init_node(..)), so rospy.get_param(..) will also not be relative to a node. Hence the problem accessing the parameter.

gvdhoorn gravatar image gvdhoorn  ( 2019-11-19 06:19:39 -0500 )edit

yeah that got it!

LukeAI gravatar image LukeAI  ( 2019-11-19 06:26:43 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-11-15 10:56:15 -0500

Seen: 946 times

Last updated: Nov 19 '19