0
$\begingroup$

Rosanswers logo

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


Originally posted by anuppari on ROS Answers with karma: 63 on 2013-06-07

Post score: 4

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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.


Originally posted by fergs with karma: 13902 on 2013-06-07

This answer was ACCEPTED on the original site

Post score: 12


Original comments

Comment by jarvisschultz on 2013-06-08:
For more advanced arguments, check out the argparse module

Comment by anuppari on 2013-06-10:
Thanks for your help!

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by adamconkey with karma: 642 on 2018-04-02

This answer was NOT ACCEPTED on the original site

Post score: 6


Original comments

Comment by shrini96 on 2023-06-13:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.