-
Notifications
You must be signed in to change notification settings - Fork 1
Creating a ROS Package that uses Python Scripts
Written by: Federico Pizarro Bejarano
Here we will be creating a ROS package that depends on rospy. Our package will print out a message to the screen. This is based off this tutorial, although it has been simplified: http://wiki.ros.org/rospy_tutorials/Tutorials/Makefile
Run these commands in your workspace to create the package and create the necessary structure.
$ cd ~/catkin_ws/src
$ catkin_create_pkg my_pkg rospy #add other dependencies here
$ cd my_pkg # new catkin package, in the workspace
$ mkdir bin
$ mkdir src
$ mkdir src/tutorial_package #this will be our python package
$ touch src/tutorial_package/__init__.pyThe __init__.py is empty but it is necessary as it makes python recognize the folder as a python package.
Now lets create our python function. Make a file in tutorial_package called hello.py and type in the following:
def say(name):
print('Hello ' + name)To create the executable first create the file hello (no extension) in the bin folder and put in:
#! /usr/bin/env python
import tutorial_package.hello
if __name__ == '__main__':
tutorial_package.hello.say('my friend!')The first line is the shebang needed for the executable to be recognized as python. Next is the main statement. Make this file an executable by running chmod u+x bin/hello.
Now in ~/catkin_ws/src/my_pkg create the file setup.py and put in:
## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup
# fetch values from package.xml
setup_args = generate_distutils_setup(
packages=['tutorial_package'],
package_dir={'': 'src'},
)
setup(**setup_args)This file is needed to for the ROS package to work.
Now, replace your CMakeLists.txt with:
cmake_minimum_required(VERSION 2.8.3)
project(my_pkg)
find_package(catkin REQUIRED COMPONENTS
rospy
)
catkin_python_setup()
catkin_install_python(PROGRAMS bin/hello DESTINATION ${PROJECT_NAME}/bin)
If there are any different dependencies than the ones used in this tutorial, they will need to be here inside of find_package.
Now return to the workspace and enter:
$ cd ~/catkin_ws
$ catkin_make
$ . devel/setup.bash # only needed when the directory structure changesIf everything works well, you can run:
$ rosrun my_pkg helloNow you should see Hello my friend!
General ROS and Technical Help
- Creating a rospy package
- Model the Robot
- Odometry - General Information
- Odometry - Configuring with ROS
Creating and Using Our Packages