If it possible to launch a launch file from python?
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()
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).NameError: name 'rospy' is not defined
Add
import rospy
Thank you! But now the error become
the line 3 error is fixed by adding
#!/usr/bin/env python
. stupid missing.Can you edit the question with the file's code you are trying to run? That will make it much easier to help you.
sure. It's basically the sample II from the roslaunch API page.
The
self.shutdown
part is weird. Theself
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 thelaunch.shutdown()
part until you decide how/when you want to shut it down.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