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

Passing arguments to launch file from Python API

asked 2016-06-15 12:34:40 -0600

juesato gravatar image

I'd like to do the equivalent of

roslaunch a.launch foo:=bar

in Python. Right now I'm using the ROSLaunchRunner, but that's not important to me if there's another way to do this.

edit retag flag offensive close merge delete

6 Answers

Sort by ยป oldest newest most voted
4

answered 2018-03-13 09:39:00 -0600

Cerin gravatar image

It's not well, documented, but the roslaunch.core.Node class supports all the same parameters that the rosrun and 'roslaunch' commands support.

So, if you were to launch a node with:

rosrun mypackage mynode.py _param1:=123 _param2:=abc

you could do the same thing from Python with:

import roslaunch

node = roslaunch.core.Node(
    package='mypackage',
    node_type='mynode.py',
    name='mynode',
    args='_param1:=123 _param2:=abc',
    output='screen')
launch = roslaunch.scriptapi.ROSLaunch()
launch.start()
process = launch.launch(node)
# do stuff
process.stop()
edit flag offensive delete link more

Comments

3

this doesn't work with launch files.

mugenzebra gravatar image mugenzebra  ( 2019-03-11 14:29:19 -0600 )edit
2

answered 2018-06-19 16:41:40 -0600

lucasw gravatar image

updated 2018-06-19 16:51:23 -0600

I found a hacky method that works at least in kinetic and possibly a few versions earlier- the roslaunch api for lunar looks to support arguments properly so that ought to be used (and hopefully it gets backported).

(I also described this partially and probably confusingly in https://answers.ros.org/question/1049... )

import sys
...
# override the sys.argv in roslaunch with our own,
# make sure there are no blank '' entries
sys.argv = ['roslaunch', 'foo', 'bar.launch', 'arg1:=234', etc.]
import roslaunch
roslaunch.main()

(In the api it looks like roslaunch.main(argv) ought to work without the sys.argv overriding, but I think the call to main in __init__ keeps that from working?)

It is then possible to put the above into a node and then launch that node like @Cerin describes, and furthermore make the list of args set by args from the scriptapi call (though making that generic to any arbitrary arguments to arbitrary launch files is more work again).

Some similar questions:

programmatic-way-to-stop-roslaunch (already mentioned above)

how-to-launch-a-launch-file-from-python-code

how-to-set-args-with-python-roslaunch

if-it-possible-to-launch-a-launch-file-from-python

parameters-roslaunch-from-python-script

Also programmatic rosrun questions:

start-a-node-from-python-code-rospy-equivalence-rosrun-rosgui-qt

TBD add some others

edit flag offensive delete link more
1

answered 2016-06-16 07:55:51 -0600

I've never had much luck digging into ROSLaunchRunner.... would it be okay if you run the roslaunch file as a subprocess of the Python script? You could use os.system or subprocess.Popen with the exact roslaunch command that you want run. I've done that quite a few times successfully.

edit flag offensive delete link more
0

answered 2019-07-18 11:16:06 -0600

Wang Shengyu gravatar image

updated 2019-07-18 11:47:11 -0600

Just follow the code below:

import roslaunch

cli_args = ['/home/mosaic/catkin_ws/src/robot/launch/id.launch','vel:=2.19']
roslaunch_args = cli_args[1:]
roslaunch_file = [(roslaunch.rlutil.resolve_launch_arguments(cli_args)[0], roslaunch_args)]

parent = roslaunch.parent.ROSLaunchParent(uuid, roslaunch_file)

parent.start()

reference: http://wiki.ros.org/roslaunch/API%20U...

edit flag offensive delete link more

Comments

Code only answers should be discouraged. Can you please update your question with an explanation

jayess gravatar image jayess  ( 2019-07-18 11:32:56 -0600 )edit

I think code only answer is enough for this question. And you can click the reference to get further info.

Wang Shengyu gravatar image Wang Shengyu  ( 2019-07-18 11:46:00 -0600 )edit
0

answered 2020-10-10 11:46:20 -0600

This answer is for Melodic. You'll need to pass the arguments as individual entries in a Python list. To better understand this, take a look at the sample code in the API documentation (roslaunch/API Usage):

import roslaunch

cli_args = ['/home/mosaic/catkin_ws/src/robot/launch/id.launch','vel:=2.19']
roslaunch_args = cli_args[1:]
roslaunch_file = [(roslaunch.rlutil.resolve_launch_arguments(cli_args)[0], roslaunch_args)]

parent = roslaunch.parent.ROSLaunchParent(uuid, roslaunch_file)

parent.start()

cli_args is simply a Python list containing the launch file as list item 0 and arguments after 0, hence cli_args[1:]. Here's a modified version of their example to illustrate:

import roslaunch

arg0 = '/home/mosaic/catkin_ws/src/robot/launch/id.launch'
arg1 = 'vel:=2.19'
arg2 = 'udp_dest:=192.168.2.100'
arg3 = 'mode:=2048'
cli_args = [arg0, arg1, arg2, arg3]
roslaunch_args = cli_args[1:]
roslaunch_file = [(roslaunch.rlutil.resolve_launch_arguments(cli_args)[0], roslaunch_args)]

parent = roslaunch.parent.ROSLaunchParent(uuid, roslaunch_file)

parent.start()

(Note that you'll have to set the uuid for this to work. Just look at how they do it earlier in the document.)

edit flag offensive delete link more
0

answered 2016-12-21 17:35:21 -0600

vinaykumarhs2020 gravatar image

This might be a too late. You can use roslaunch api to do this. See this example for basic usage: http://wiki.ros.org/roslaunch/API%20U...

To remap arguments, as you have asked, you can use args and remap_args as mentioned here: http://docs.ros.org/kinetic/api/rosla...

edit flag offensive delete link more

Comments

That does not show how to pass in arguments...

Cerin gravatar image Cerin  ( 2018-03-13 09:20:27 -0600 )edit
2

It does now, but arguments only work in lunar and after.

lucasw gravatar image lucasw  ( 2018-06-19 16:17:25 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-06-15 12:34:40 -0600

Seen: 4,610 times

Last updated: Oct 10 '20