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

Revision history [back]

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/10493/programmatic-way-to-stop-roslaunch/)

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

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/10493/programmatic-way-to-stop-roslaunch/)

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:

https://answers.ros.org/question/10493/programmatic-way-to-stop-roslaunch/ (already mentioned above)

https://answers.ros.org/question/42849/how-to-launch-a-launch-file-from-python-code/

https://answers.ros.org/question/198910/how-to-set-args-with-python-roslaunch/

https://answers.ros.org/question/263862/if-it-possible-to-launch-a-launch-file-from-python/

https://answers.ros.org/question/265796/parameters-roslaunch-from-python-script/

Also programmatic rosrun questions:

https://answers.ros.org/question/41848/start-a-node-from-python-code-rospy-equivalence-rosrun-rosgui-qt/

TBD add some others

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/10493/programmatic-way-to-stop-roslaunch/)

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:

https://answers.ros.org/question/10493/programmatic-way-to-stop-roslaunch/ programmatic-way-to-stop-roslaunch (already mentioned above)

https://answers.ros.org/question/42849/how-to-launch-a-launch-file-from-python-code/

https://answers.ros.org/question/198910/how-to-set-args-with-python-roslaunch/

https://answers.ros.org/question/263862/if-it-possible-to-launch-a-launch-file-from-python/

https://answers.ros.org/question/265796/parameters-roslaunch-from-python-script/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:

https://answers.ros.org/question/41848/start-a-node-from-python-code-rospy-equivalence-rosrun-rosgui-qt/start-a-node-from-python-code-rospy-equivalence-rosrun-rosgui-qt

TBD add some others