Ask Your Question
0

Adding arguments via command line during roslaunch

asked 2018-09-13 18:33:55 -0500

surabhi96 gravatar image

updated 2018-09-14 01:49:37 -0500

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!

edit retag flag offensive close merge delete

Comments

Well, obviously your launchfile is not correct at line 3, column 90. If you share it, we could help instead of only guess....

mgruhler gravatar image mgruhler  ( 2018-09-14 00:53:36 -0500 )edit

@mgruhler it is the same as given in the link.. if you see in the answer given by Leonid.

surabhi96 gravatar image surabhi96  ( 2018-09-14 01:19:11 -0500 )edit

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

mgruhler gravatar image mgruhler  ( 2018-09-14 01:24:05 -0500 )edit

@mgruhler Just updated

surabhi96 gravatar image surabhi96  ( 2018-09-14 01:36:18 -0500 )edit

Thanks, this is just way easier to figure out.

mgruhler gravatar image mgruhler  ( 2018-09-14 02:00:09 -0500 )edit

@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?

surabhi96 gravatar image surabhi96  ( 2018-09-14 02:04:14 -0500 )edit

just saw you actually answered your main question yourself :-)

mgruhler gravatar image mgruhler  ( 2018-09-14 02:27:05 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-09-14 02:26:28 -0500

mgruhler gravatar image

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...

edit flag offensive delete link more

Comments

Thanks! That worked.

surabhi96 gravatar image surabhi96  ( 2018-09-16 18:39:36 -0500 )edit
0

answered 2018-09-16 18:46:04 -0500

surabhi96 gravatar image

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"
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-09-13 18:33:55 -0500

Seen: 3,546 times

Last updated: Sep 16 '18