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

How to pass multiple arguments to rospy node through command line?

asked 2019-06-17 07:31:22 -0600

kump gravatar image

updated 2019-06-17 09:31:11 -0600

I'm looking for a notation like this

 rosrun my_package my_node.py arg1 arg2 arg3

That I can cun in the terminal, that will enable me to pass arguments to a ROS node which is implemented in python. So something like:

rosrun my_package read_topics.py arg1:='/topic1' arg2:='/topic2'

And be able to access those inside the python code like for example

subscriber = rospy.Subscriber(arg1, Bool, callback)
publisher = rospy.Publisher(arg2, Bool, queue_size=1)

How can I do that?


EDIT:

Based on the answers, the right approach is to pass arguments in a format

rosrun my_package my_node.py _one_topic:="/topic1" _another_topic:="/topic2" __name:="my_node_name"

and access the arguments inside the script as

publisher = rospy.Publisher(rospy.get_param('~one_topic'), Bool, queue_size=1)
subscriber = rospy.Subscriber(rospy.get_param('~another_topic'), Bool, callback)

The node name can be passed using a special parameter __name, because we need to know the node name prior to initializing the rospy node.

This works for me. Thank you both.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-06-17 07:54:59 -0600

gvdhoorn gravatar image

updated 2019-06-17 07:58:39 -0600

The rosrun documentation should help here:

It's also possible to pass a ~parameter using the following syntax (replace the ~ with an _):

rosrun package node _parameter:=value

Example:

rosrun my_package my_node _my_param:=value

Note:

So something like:

rosrun my_package read_topics.py arg1:='/topic1' arg2:='/topic2'

The exact syntax you're using in your example is actually the syntax you'd use for remapping topics from the command line with rosrun.

And be able to access those inside the python code like for example

subscriber = rospy.Subscriber(arg1, Bool, callback)
publisher = rospy.Publisher(arg2, Bool, queue_size=1)

that's not how this would work.

You'd access the parameters as private parameters (Using Parameters in rospy).

edit flag offensive delete link more

Comments

@gvdhoorn So this approach works when I access the arguments after node initialization. But how can you pass the name of the node? You need to know the name before rospy node initialization.

kump gravatar image kump  ( 2019-06-17 08:53:02 -0600 )edit
1

You can set the node name via parameter with the __name:=node_name param (note the two underscores!) Then use rospy.init("some_node_name") After you initialize, you can get the node name with rospy.get_name() The value of get_name() depends on whether or not you set the __name from the commandline.

ChuiV gravatar image ChuiV  ( 2019-06-17 09:16:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-06-17 07:31:22 -0600

Seen: 3,081 times

Last updated: Jun 17 '19