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

If it possible to launch a launch file from python?

asked 2017-06-14 14:00:43 -0600

yue.z gravatar image

updated 2017-06-16 16:26:15 -0600

Final update: The solution so far is modified from here with help from ufr3c_tjc. (Thank you loudly in my heart) So the original codes from the link above is erroneous and won't work. The main part is remained:

class open_launch_file(): 
    def __init__(self):
        rospy.init_node('tester', anonymous=True)
        rospy.on_shutdown(self.shutdown)

        uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
        roslaunch.configure_logging(uuid)
        launch = roslaunch.parent.ROSLaunchParent(uuid,["/path/to/your/launch/file"])
        launch.start()

    def shutdown(self):
        rospy.loginfo("Stopping the robot...")
        destroy() #my own cleaning stuff function, you dont have to have it. but if there is anything you need to do before close the ros node, do it here.
        rospy.sleep(1)

===============================================================================

I have a .launch file that basically just open the webcam. And I would like to call it from my python code when needed. I searched online and found something like this: I have test other normal commands like "ls -l", "echo Hello world". And they are working fine. However, it would not work with the launch file

import subprocess
p = subprocess.Popen(["roslaunch ~/path/to/cam.launch "],stdout=subprocess.PIPE,stderr = subprocess.PIPE,shell=True)
print p.communicate()

the error message is:

'/bin/sh: 1: roslaunch: not found\n

I am using python 2.7

update:

the output of printenv | grep PY is:

INSIDE_CAJA_PYTHON=
PYTHONPATH=/opt/ros/kinetic/lib/python2.7/dist-packages

When using the API example provide by ROS, there were errors that confuse me. I have set the environment correctly I believe. When I rosrun tester_pkg tester.py , it gives error NameError: name 'rospy' is not defined

So I tried sudo python tester.py directly, it gives ImportError: No module named roslaunch

Thank you all for the replying. I am new to ROS and really need some helps.

update codes: tester.py

#!/usr/bin/env python
import roslaunch
import rospy

rospy.init_node('tester', anonymous=True)
rospy.on_shutdown(self.shutdown)

uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
roslaunch.configure_logging(uuid)
launch = roslaunch.parent.ROSLaunchParent(uuid,["/home/yue/catkin_ws/src/webcam_car/launch/cam.launch"])

launch.start()

launch.shutdown()
edit retag flag offensive close merge delete

Comments

Don't use sudo. That will run it as root, which will never work unless ROS is installed as root. line 3: syntax error near unexpected token is telling you the issue. Look at line 3 of the file and see what could be wrong (or post the file for us to see).

ufr3c_tjc gravatar image ufr3c_tjc  ( 2017-06-14 21:10:22 -0600 )edit

NameError: name 'rospy' is not defined

Add import rospy

ufr3c_tjc gravatar image ufr3c_tjc  ( 2017-06-14 21:15:10 -0600 )edit

Thank you! But now the error become

rospy.on_shutdown(self.shutdown)
NameError: name 'self' is not defined
yue.z gravatar image yue.z  ( 2017-06-14 21:25:51 -0600 )edit

the line 3 error is fixed by adding #!/usr/bin/env python. stupid missing.

yue.z gravatar image yue.z  ( 2017-06-14 21:36:37 -0600 )edit

Can you edit the question with the file's code you are trying to run? That will make it much easier to help you.

ufr3c_tjc gravatar image ufr3c_tjc  ( 2017-06-14 21:38:44 -0600 )edit

sure. It's basically the sample II from the roslaunch API page.

yue.z gravatar image yue.z  ( 2017-06-14 21:46:45 -0600 )edit

The self.shutdown part is weird. The self keyword is only used in a class context. Just remove that line completely. Also, this script will immediately start then shutdown the launch file, so maybe just remove the launch.shutdown() part until you decide how/when you want to shut it down.

ufr3c_tjc gravatar image ufr3c_tjc  ( 2017-06-14 21:52:13 -0600 )edit

I found comment about the self.shutdown # Set rospy to exectute a shutdown function when terminating the script Remove this line and the launch.shutdown() works as I can see the process being created! But still the launch file shutdown itself. I guess I can take over from here now! Thank you so muc

yue.z gravatar image yue.z  ( 2017-06-14 22:00:23 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-06-14 14:09:20 -0600

gvdhoorn gravatar image

updated 2017-06-14 14:09:42 -0600

Please use the search first in the future, this has been asked multiple times already. That sounds harsh, and we do want to help you, but with almost 35000 questions on this site it gets increasingly important to avoid duplicates.

As to your question: yes, this is possible, you'll just have to make sure that the environment in which you run your Python script has already been setup with the appropriate environment variables. In other words: run it from a terminal in which you have sourced the correct setup.bash, and make sure your Popen call inherits the environment from its caller.

As an alternative: do not start roslaunch .. directly in your Popen call, but use a small wrapper script that first sources the setup.bash and then executes roslaunch.

edit flag offensive delete link more
1

answered 2017-06-14 18:01:14 -0600

ufr3c_tjc gravatar image

updated 2017-06-14 18:43:39 -0600

The correct way to do this is to use the actual roslaunch Python API. Do not use calls to subprocess etc. It gets way too messy to control environment variables and makes stopping the processes much harder. Using the proper API allows you to start, but also to stop, the launch file nodes easily. See here for examples.

Again though, that API page is the second Google result for 'roslaunch python'. Please do some searching before posting next time :)

edit flag offensive delete link more

Comments

actually this was the first thing I tried. It gave error of cannot find module roslaunch.

yue.z gravatar image yue.z  ( 2017-06-14 20:25:18 -0600 )edit

Then your path must be set up incorrectly. Can you edit the question to show the output of printenv | grep PY. It should include something like /opt/ros/kinetic/lib/python2.7/dist-packages

ufr3c_tjc gravatar image ufr3c_tjc  ( 2017-06-14 20:29:27 -0600 )edit

Also make sure that you're running the python script from a shell that has the correct environment variables set (ROS_PACKAGE_PATH etc.), otherwise it won't work.

ufr3c_tjc gravatar image ufr3c_tjc  ( 2017-06-14 20:35:36 -0600 )edit

the output is:

    INSIDE_CAJA_PYTHON=
    PYTHONPATH=/opt/ros/kinetic/lib/python2.7/dist-packages
yue.z gravatar image yue.z  ( 2017-06-14 21:03:44 -0600 )edit

That part is at least correct, but it doesn't have your own workspace in the path. I assume that you always source devel/setup.bash before running your code from within your own workspace? Also, what is the output of printenv | grep ROS?

ufr3c_tjc gravatar image ufr3c_tjc  ( 2017-06-14 21:07:54 -0600 )edit

ROS_ROOT=/opt/ros/kinetic/share/ros ROS_PACKAGE_PATH=/home/yue/catkin_ws/src:/opt/ros/kinetic/share ROS_MASTER_URI=http://localhost:11311 ROSLISP_PACKAGE_DIRECTORIES=/home/yue/catkin_ws/devel/share/common-lisp ROS_DISTRO=kinetic ROS_ETC_DIR=/opt/ros/kinetic/etc/ros

yue.z gravatar image yue.z  ( 2017-06-14 21:21:13 -0600 )edit
1

@ufr3c_tjc: using the roslaunch Python API is indeed a better approach, but I did not want to complicate things too much for @yue.z, seeing as he already states that he is a beginner.

gvdhoorn gravatar image gvdhoorn  ( 2017-06-15 04:46:57 -0600 )edit

"she". I will try both ways to see which one fit my need if I could make them work.

yue.z gravatar image yue.z  ( 2017-06-15 07:18:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-06-14 14:00:43 -0600

Seen: 6,582 times

Last updated: Jun 16 '17