Ask Your Question
3

Passing arguments to launch file from Python API

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

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

5 Answers

Sort by ยป oldest newest most voted
3

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

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

this doesn't work with launch files.

mugenzebra gravatar imagemugenzebra ( 2019-03-11 14:29:19 -0500 )edit
2

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

lucasw gravatar image

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

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

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

Wang Shengyu gravatar image

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

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 imagejayess ( 2019-07-18 11:32:56 -0500 )edit

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

Wang Shengyu gravatar imageWang Shengyu ( 2019-07-18 11:46:00 -0500 )edit
0

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

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 imageCerin ( 2018-03-13 09:20:27 -0500 )edit
2

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

lucasw gravatar imagelucasw ( 2018-06-19 16:17:25 -0500 )edit

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: 2016-06-15 12:34:40 -0500

Seen: 1,971 times

Last updated: Jul 18