
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()
Originally posted by yue.z on ROS Answers with karma: 1 on 2017-06-14
Post score: 0
Original comments
Comment by ufr3c_tjc on 2017-06-14:
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).
Comment by ufr3c_tjc on 2017-06-14:
NameError: name 'rospy' is not defined
Add import rospy
Comment by yue.z on 2017-06-14:
Thank you!
But now the error become
rospy.on_shutdown(self.shutdown)
NameError: name 'self' is not defined
Comment by yue.z on 2017-06-14:
the line 3 error is fixed by adding #!/usr/bin/env python. stupid missing.
Comment by ufr3c_tjc on 2017-06-14:
Can you edit the question with the file's code you are trying to run? That will make it much easier to help you.
Comment by yue.z on 2017-06-14:
sure. It's basically the sample II from the roslaunch API page.
Comment by ufr3c_tjc on 2017-06-14:
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.
Comment by yue.z on 2017-06-14:
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
Comment by yue.z on 2017-06-14:
thank you for you patient! :)
Comment by ufr3c_tjc on 2017-06-14:
Yes, the rospy.on_shutdown(self.shutdown) will run the shutdown function if your have one defined, when running inside a class. It's just weird that their example doesn't have a class, so it errors. Remove the launch.shutdown(), this is what's causing it to stop.
Comment by yue.z on 2017-06-15:
It happened after I removed launch.shutdown statement. Actually the terminal looks the same with or without it. That's weird. I'm looking into the documents.
Comment by ufr3c_tjc on 2017-06-15:
It because the script ends. Put in a rospy.spin() line at the end, and it will wait and not exit until told to do so by the ros master.
Comment by yue.z on 2017-06-16:
Turns out the rospy.on_shutdown(self.shutdown) is quiet important. otherwise, the node wont stop by ctrl -c any more. So I modify the code into one with class. The attribute 'shutdown' also must be writen. I would update my detailed solution so far for future newbies like me.
Comment by yue.z on 2017-06-16:
thank you for your kindness help
Comment by gvdhoorn on 2017-06-17:
Please post your final update as an answer and then accept your own answer. That way there is a clear answer to this question, and it's also visible from the main question listing. Thanks.