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

How to pass arguments to python node

asked 2013-06-07 08:17:09 -0500

anuppari gravatar image

updated 2014-01-28 17:16:48 -0500

ngrennan gravatar image

I would like pass arguments to a node like so:

rosrun my_package my_node.py myArg1 myArg2

Is this possible? How do I define my python function? Currently it is:

#!/usr/bin/env python
import roslib; roslib.load_manifest('my_package')
import rospy

def my_node(myArg1,myArg2)
...

but when I use the command line and run rosrun I get errors saying the my function takes two arguments and 0 were passed.

Using topics to pass in information seems over complicated and I don't want to use parameters because I don't need to make myArg1 and myArg2 available to any other nodes. Any help would be appreciated.

Edit: Running fuerte

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
12

answered 2013-06-07 21:03:18 -0500

fergs gravatar image

updated 2013-06-07 21:06:01 -0500

If you aren't going to use ROS parameters, this basically a generic Python question. You should use the 'sys' module:

#!/usr/bin/env python
from __future__ import print_function
import roslib; roslib.load_manifest('my_package')
import rospy
import sys

def my_node(myArg1,myArg2)
    ...

if __name__=="__main__":
    if len(sys.argv) < 3:
        print("usage: my_node.py arg1 arg2")
    else:
        my_node(sys.argv[1], sys.argv[2])

It's also good practice (but not required) to use the <if __name__="="__main_""> stuff so that if you import this file into another one it does not run. Finally, the above example uses __future__'s print_function so that you are Python 3 compatible.

edit flag offensive delete link more

Comments

For more advanced arguments, check out the argparse module

jarvisschultz gravatar image jarvisschultz  ( 2013-06-08 08:14:06 -0500 )edit

Thanks for your help!

anuppari gravatar image anuppari  ( 2013-06-10 06:05:36 -0500 )edit
6

answered 2018-04-02 17:50:29 -0500

While fergs answer is correct, you can run into some bugs error checking the length of sys.argv because ROS will pass in additional arguments beyond what you give, as described here. One annoying bug I've had is I was passing a namespace into a node, but ROS was passing in the name of a log file of the form _log_foo, and that leading underscore is disallowed as a namespace. It's a hard bug to track down. You should do

myargv = rospy.myargv(argv=sys.argv)

and then proceed as normal with myargv, as that function will strip out the arguments appended by ROS.

edit flag offensive delete link more

Comments

I agree. It is hard to track the fact that ROS is passing a ton of other things. I had simple length checking conditions, and took me a while to realise this is the reason why my nodes were not working. Thanks a lot for this alternative way of using passed arguments. Works like a charm.

shrini96 gravatar image shrini96  ( 2023-06-13 16:05:32 -0500 )edit

Question Tools

Stats

Asked: 2013-06-07 08:17:09 -0500

Seen: 22,467 times

Last updated: Apr 02 '18