In this video, we are going to answer a question found at ROS answers (https://answers.ros.org/question/291235/roslaunch-cant-locate-node-but-rosrun-works-fine/). The person is asking why he can run a Python script with rosrun, but not when using roslaunch.
Let’s try this out and solve this problem in ROS Development Studio. You can register a free account and follow the tutorial below!
Step1: create a package
After creating a new project in RDS, open a new shell from the tools tab and type the following command into the shell to create a ROS package.
cd catkin_ws/src/ catkin_create_pkg video_qa rospy
Step2: create a test file for the code.
It’s easier to do this with an IDE. You can find IDE in the tools tab.
Now right click and select the new file to open a new file test under the folder /video_qa/src
Paste the following code into the test file.
#! /usr/bin/env python import rospy rospy.init_node('test') rate = rospy.Rate(1) while not rospy.is_shutdown(): print "Hello there" rate.sleep()
Step3: create a launch file
According to ROS’s file structure, the launch file should be created under the launch directory. Let’s create it with the IDE and add a test.launch file under the /video_qa/launch folder.
Copy and paste the following code for testing.
<launch> <node pkg="video_qa" type="test" name="test" output="screen"> </node> </launch>
Step4: start roscore
To run the code with roslaunch, we need to start roscore. You can either do that by typing roscore into the shell or start a simulation from the Simulations tab.
Step5: rosrun and roslaunch
Firstly, we try to directly execute the test file using rosrun video_qa test, but we got some error message.
[rosrun] Couldn’t find executable named test below /home/user/catkin_ws/src/video_qa
…
That’s because of the lack of permission for this file. We can give the file permission by typing:
chmod +x video_qa/src/test
then we do rosrun video_qa test again. It works!
Then we try roslaunch video_qa test.launch. Strangely, it also works…WHY?
It turns out if we name the file test.py instead of test, we will have the same problem. To solve it, we rename the file to test.py and also in the launch file we use test.py instead of test.
TAKEAWAY:
Always name the file with extension(e.g. test.py). However, the type in the launch file takes the executable during the compiling process. Since there is no need for compiling .py file. If you use python, just put the file name with the extension(e.g. test.py) in the type section.
I hope you enjoy the ROS Q&A video today. If you are interested in learning ROS, please check the links below for the Robot ignite academy.
// RELATED LINKS
▸ ROS answers question: https://answers.ros.org/question/291235/roslaunch-cant-locate-node-but-rosrun-works-fine/
▸ ROS Development Studio: https://goo.gl/273g12
▸ Robot Ignite Academy: https://goo.gl/LsMMqh
▸ ROS Basics in 5 days online course: https://goo.gl/TDVG1j
Please disable the smooth scrolling…
Thank you for this!!!!
This does not follow common ROS guidelines which state that python executables should NOT end with .py to make it transparent to the user what kind of node it is.
See: http://docs.ros.org/jade/api/catkin/html/howto/format2/installing_python.html
On modern Linux systems, file extensions are part of the file name and are not recognized as file extensions by the kernel itself.
So test and test.py are simply different file names, test will never return test.py.
Also note that roslaunch CAN be used without building a package, though it MAY require some hard coded paths to avoid file not found errors.
In addition, rosrun and roslaunch can run both python nodes and scripts, scripts require a shebang.
The shebang may need to match the major python version (2 or 3) if the system default is different.
#! /usr/bin/env python3