0

Rosanswers logo

Read through all the tutorials, but couldn't find the answer to this one. I have a ROS node executable, which I can run like this:

./my_node -arg1 -arg2

I can also start it using roslaunch and a my_launch.launch file:

roslaunch my_launch.launch

How do I specify arg1 and arg2 when starting using roslaunch? I.e. how do I get arg1 and arg2 to show up in argc and argv?


Originally posted by Leonid on ROS Answers with karma: 163 on 2017-12-08

Post score: 4

2 Answers2

1

Rosanswers logo

If you using raw command line arguments then you'll need to use the args attribute of the node tag in your launch file are described in the node tag documentation.

The example below will work in your case:

<launch>
  <node pkg="my_package" type="my_node" name="node_instance" args="-arg1 -arg2" />
</launch>

Originally posted by PeteBlackerThe3rd with karma: 9529 on 2017-12-08

This answer was ACCEPTED on the original site

Post score: 9

1

Rosanswers logo

Thanks for the responses. I'd just like to document what I found. Like PeteBlackerThe3rd said, you can hardcode command line args in your launch file:

<launch>
   <node pkg="my_package" type="my_node" name="node_instance" args="-arg1 -arg2" />
</launch>

You can also specify arguments on the actual command line (this is what I was trying to get at with my question), but that requires an extra step. The launch file would look like this:

<launch>
   <arg name="my_args"/>
   <node pkg="my_package" type="my_node" name="node_instance" args="$(arg my_args)"/>
</launch>

And your roslaunch command would look like this:

> roslaunch my_launch.launch my_args:="-arg1 -arg2"

Originally posted by Leonid with karma: 163 on 2017-12-08

This answer was NOT ACCEPTED on the original site

Post score: 11


Original comments

Comment by surabhi96 on 2018-09-13:
@Leonid how did you use these arguments in your node? Like args[0], args[1] etc. ?

Comment by Leonid on 2018-09-13:
I used getopt to parse them, but yes, you can access them through argc and argv in your main().

Comment by surabhi96 on 2018-09-13:
@Leonid I am using a python script and am accessing them through sys.argv[0]. But before that, I'm getting this error: Invalid roslaunch XML syntax: not well-formed (invalid token): line 3, column 90 The traceback for the exception was written to the log file

Comment by ahendrix on 2018-09-14:
I've edited this answer to add the appropriate quote marks, as noted in https://answers.ros.org/question/303339/adding-arguments-via-command-line-during-roslaunch/

Comment by surabhi96 on 2018-09-16:
@ahendrix Thanks!