Creating a ROS package
The ROS packages are the basic unit of the ROS system. We can create a ROS package, build it, and release it to the public. The current distribution of ROS we are using is kinetic. We are using the catkin build system to build ROS packages. A build system is responsible for generating 'targets' (executable/libraries) from a raw source code that can be used by an end user. In older distributions, such as Electric and Fuerte, rosbuild was the build system. Because of the various flaws of rosbuild, catkin came into existence, which is basically based on Cross Platform Make (CMake). This has a lot of advantages, such as porting the package into another operating system, such as Windows. If an OS supports CMake and Python, catkin -based packages can be easily ported into it.
The first requirement work with ROS packages is to create a ROS catkin workspace. After installed ROS, we can create and build a catkin_workspace called catkin_ws:
$ mkdir -p ~/catkin_ws/src
To compile the workspace, we should source the ROS environment, in order to get access to ROS functions:
$ source /opt/ros/kinetic/setup.bash
Switch to the source, src folder previously created.
$ cd ~/catkin_ws/src
Initialize a new catkin workspace:
$ catkin_init_workspace
We can build the workspace even if there are no packages. We can use the following command to switch to the workspace folder:
$ cd ~/catkin_ws
The catkin_make command will build the following workspace:
$ catkin_make
This last command will create a devel and a build directory in the catkin workspace. Inside the devel folder different setup files are located. To add the created ROS workspace to the ROS environment, we should source one of this file. In addition, we can source the setup file of this workspace every time that a new bash session starts with the following command:
$ echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc $ source ~/.bashrc
After setting the catkin workspace, we can create our own package that has sample nodes to demonstrate the working of ROS topics, messages, services, and actionlib. The catkin_create_pkg command is used to create a ROS package. This command is used to create our package, in which we are going to create demos of various ROS concepts.
Switch to the catkin workspace src folder and create the package, using the following command:
$ catkin_create_pkg package_name [dependency1] [dependency2]
Here is the command to create the sample ROS package:
$ catkin_create_pkg mastering_ros_demo_pkg roscpp std_msgs actionlib actionlib_msgs
The dependencies in the packages are as follows:
- roscpp: This is the C++ implementation of ROS. It is a ROS client library which provides APIs to C++ developers to make ROS nodes with ROS topics, services, parameters, and so on. We are including this dependency because we are going to write a ROS C++ node. Any ROS package which uses the C++ node must add this dependency.
- std_msgs: This package contains basic ROS primitive data types, such as integer, float, string, array, and so on. We can directly use these data types in our nodes without defining a new ROS message.
- actionlib: The actionlib metapackage provides interfaces to create preemptible tasks in ROS nodes. We are creating actionlib -based nodes in this package. So we should include this package to build the ROS nodes.
- actionlib_msgs: This package contains standard message definitions needed to interact with the action server and action client.
After package creation, additional dependencies can be added manually by editing the CMakeLists.txt and package.xml files. We will get the following message if the package has been successfully created:
After creating this package, build the package without adding any nodes, using the catkin_make command. This command must be executed from the catkin workspace path. The following command shows you how to build our empty ROS package:
~/catkin_ws $ catkin_make
After a successful build, we can start adding nodes to the src folder of this package.
The build folder in the CMake build files mainly contains executables of the nodes that are placed inside the catkin workspace src folder. The devel folder contains bash script, header files, and executables in different folders generated during the build process. We can see how to make ROS nodes and build using catkin_make.