For basic ROSPY usage, we do not need to build ROSPY into our Catkin Package.
Simply importing the rospy library into Python is sufficient for basic usage.
For more advanced ROSPY usage, it is advised to have a proper setup in your Catkin Package to build and compile your python code with any libraries, messages, services, and resources you may have in your package.
This section requires the catkin_ws to be initialized and the turtlebot_dabit package created.
Please click here to learn how to initialize the catkin workspace
For this tutorial, we will be using the turtlebot_dabit catkin package we created. In order to enable building ROSPY files in our package, we need to edit the CMakeLists.txt and package.xml to build with our dependencies.
cd ~/catkin_ws/src/turtlebot_dabit
gedit package.xml
Near the bottom of the file, find the <buildtool_depend>catkin</buildtool_depend>
line:
...
<buildtool_depend>catkin</buildtool_depend>
...
Add the build_depend and run_depend tags for roscpp underneath the buildtool_depend line:
<build_depend>rospy</build_depend>
<run_depend>rospy</run_depend>
<?xml version="1.0"?>
<package>
<name>turtlebot_dabit</name>
<version>0.0.0</version>
<description>The turtlebot_dabit package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
<maintainer email="user@todo.todo">user</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<!-- ... -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>rospy</build_depend>
<run_depend>rospy</run_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
gedit CMakeLists.txt
Near the top of the file, find the find_package(catkin REQUIRED)
line:
...
find_package(catkin REQUIRED)
...
Replace that line with:
find_package(catkin REQUIRED COMPONENTS
rospy
)
Near the top of the file, find the # catkin_python_setup()
line:
...
# catkin_python_setup()
...
Uncomment that line:
catkin_python_setup()
Near the middle of the file, find the catkin_package(
line:
...
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES turtlebot_dabit
# CATKIN_DEPENDS other_catkin_pkg
# DEPENDS system_lib
)
...
Replace that line with:
catkin_package(
INCLUDE_DIRS
CATKIN_DEPENDS rospy
)
Near the middle of the file, find the # include_directories(include)
line:
...
# include_directories(include)
...
Replace that line with:
include_directories(
${catkin_INCLUDE_DIRS}
)
cmake_minimum_required(VERSION 2.8.3)
project(turtlebot_dabit)
## Add support for C++11, supported in ROS Kinetic and newer
# add_definitions(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
rospy
)
# ...
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
catkin_python_setup()
# ...
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if you package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
INCLUDE_DIRS
CATKIN_DEPENDS rospy
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
${catkin_INCLUDE_DIRS}
)
## ...
catkin_make --directory ~/catkin_ws --pkg turtlebot_dabit
Upon success, you should see:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/catkin_ws/build
####
#### Running command: "make -j4 -l4" in "/home/user/catkin_ws/build/turtlebot_dabit"
####
Now we get to write our first Python code to communicate with ROS.
gedit ~/catkin_ws/src/turtlebot_dabit/scriptsrospy_hello_world.py
Start with the following Hello World code in gedit:
#!/usr/bin/env python2.7
# Import ROS libraries and messages
import rospy
# Print "Hello!" to terminal
print "Hello!"
# Initialize the ROS Node named 'opencv_example', allow multiple nodes to be run with this name
rospy.init_node('opencv_example', anonymous=True)
# Print "Hello ROS!" to the Terminal and ROSLOG
rospy.loginfo("Hello ROS!")
chmod +x ~/catkin_ws/src/turtlebot_dabit/scripts/rospy_hello_world.py"
source ~/catkin_ws/devel/setup.sh
rosrun turtlebot_dabit rospy_hello_world.py
[ INFO] [1492726164.127098818]: Hello ROS!