0
$\begingroup$

Rosanswers logo

Hi! I want to add command line arguments while calling my launch file in the bash, just like how I add it while running the node via rosrun. For eg. rosrun pkg_name executable_name /home/user/Documents/my_file.yml /home/user/Pictures/ The closest answer I found to solve my problem was this. My launch file looks like this

<launch>
<arg name="my_args"/>
<node pkg="pkg" type="node.py" name="executable" output="screen" args=$(arg my_args)/>
</launch>

and the command looks like this roslaunch launch_file.launch my_args:="/home/user/Documents/my_file.yml" However, I get the following 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. I also wanted to know how can I access these command line arguments in my python script. Is it just sys.argv[0]? I also have another c++ node which needs command line arguments. So, in short, I want to launch these two nodes with the respective command line arguments. I would be really thankful if someone could provide an example launch file to solve the above problem. Thanks!


Originally posted by surabhi96 on ROS Answers with karma: 41 on 2018-09-13

Post score: 0


Original comments

Comment by mgruhler on 2018-09-14:
Well, obviously your launchfile is not correct at line 3, column 90. If you share it, we could help instead of only guess....

Comment by surabhi96 on 2018-09-14:
@mgruhler it is the same as given in the link.. if you see in the answer given by Leonid.

Comment by mgruhler on 2018-09-14:
first or second one? just copy'n'paste? Any adaptations? still many open questions. I'd like to help, but I'm not going to dig around if I'm not sure what you are actually using

--> please just edit your question with an unchanged c'n'p of your launch file. Format it with the button 1010101

Comment by surabhi96 on 2018-09-14:
@mgruhler Just updated

Comment by mgruhler on 2018-09-14:
Thanks, this is just way easier to figure out.

Comment by surabhi96 on 2018-09-14:
@mgruhler I did args="$(arg my_args)" and it worked.. however, I am getting a problem in accessing this argument. Is sys.argv[0] the right way?

Comment by mgruhler on 2018-09-14:
just saw you actually answered your main question yourself :-)

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

You are missing some quotation marks around "$(arg my_args)". (Actually, this is wrong in the answer you cite as well...)

About retrieving the arguments: sys.argv[0] returns the name of the script itself. In python, best use sys.argv[1:] which returns all other args as a list and then you can iterate through that.

Another option is to check the number of arguments (python: len(sys.argv), C++: check argc) and manually retrieve the respective arguments from argv starting at index 1.

For ROS, depending on your usecase, another option would be to use ROS parameters...


Originally posted by mgruhler with karma: 12390 on 2018-09-14

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by surabhi96 on 2018-09-16:
Thanks! That worked.

$\endgroup$
0
$\begingroup$

Rosanswers logo

In order to completely answer the question(to launch two nodes with the respective command line arguments), This is how my launch file looks now,

<launch>
 <arg name="args1"/>
 <arg name="args2"/>
 <node pkg="pkg" type="node.py" name="executable" output="screen" args="$(arg args1)"/>
 <node pkg="pkg" type="node1" name="executable1" output="screen" args="$(arg args2)"/> 
</launch>

and the command I enter in bash

rosrun pkg launch_file.launch args1:="a b c" args2:="a1 b1"

Originally posted by surabhi96 with karma: 41 on 2018-09-16

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

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