December 3, 2024

ROS Tutorial: Create Catkin Workspace and Packages in ROS

In this Robot Operating System (ROS) tutorial, we explain how to create a workspace and packages in ROS. Note that this tutorial is based on the Noetic ROS distribution. The YouTube tutorial accompanying this post is given below.

STEP 1: Create a Workspace

In order to create a ROS application or a package, we first need to create and setup our workspace. A workspace is a set of directories, files, and additional documents that are used to create a ROS package. More information about the ROS Catkin workspace can be found here.

Before creating the workspace, we need to ensure that we have “sourced” our environment. The word construction “sourcing the environment” means that in our current terminal or bash shell, we have executed a ROS script that sets up the basic paths, and other internal stuff related to ROS proper operation. There are two ways to do that. We can open the terminal window, and we can type

source /opt/ros/noetic/setup.bash

The “source” command is a built-in shell command that will read and execute the content of the file in the currently active shell. This command will execute the script “setup.bash”. We can display the content of this script by typing

cat /opt/ros/noetic/setup.bash

More information about the command “source”, can be found here and here. The second method for sourcing our ROS environment is to add the sourcing command to “.bashrc” file. The “.bashrc” file is a configuration script file for the Bash shell environment. When the shell or terminal starts, the file “.bashrc” is automatically executed. This file contains a number of comments, system configurations, and functions that are used to automate tasks and customize the Bash shell. More information about the file “.bashrc” can be found here and here. To check if the “setup.bash” file is added to the “.bashrc” file, type in the terminal

gedit ~/.bashrc

This command will open the file “.bashrc”. In this file (usually somewhere at the end), there should be the following line “source /opt/ros/noetic/setup.bash“. If there is such a line, this means that the file “setup.bash” will be automatically sourced when the shell starts. If there is no such a line, then type in the terminal:

echo “source /opt/ros/noetic/setup.bash” >> ~/.bashrc

This will add the line “source /opt/ros/noetic/setup.bash” at the end of the “.bashrc”. For readers who are not familiar with the Linux command line, the tilde symbol “~” is an alias or a shortcut for the home folder. More information about this alias can be found here.

Finally, after these initial checks, we are ready to create a workspace. We first need to create a directory. We do that by opening a terminal and by typing

mkdir -p ~/my_workspace/src

This command will create the directory “my_workspace” in the home directory of the current user, and at the same time it will create the “src” sub-directory. The command “mkdir -p” will create directories and sub-directories at the same time. This command is explained here.

Next, let us change the current working directory by typing

cd ~/my_workspace/

In this directory, we execute the following command:

catkin_make

This command will create the catkin workspace, and it will create three directories: “build”, “devel”, and “src”, as well as a CMakeLists.txt link in the “src” folder. Briefly speaking, the “src” directory contains the source code of the Catkin packages. This is where we can extract/clone the source code for the packages we want to build. The “devel” directory is the place where built targets are placed before they are installed. The “build” directory corresponds to the build space.

Inside of the “devel” directory, there are several setup files that start with “setup.” We need to overlay our current workspace over the current ROS environment. We can do that by typing

source ~/my_workspace/devel/setup.bash

Next, we need to verify that our workspace overlays the ROS workspace. We can do that by typing

echo $ROS_PACKAGE_PATH

The output should look like this:

/home/aleksandar/my_workspace/src:/opt/ros/noetic/share

In your case, instead of “aleksandar”, your current username should be written. Note that any time you start a terminal, you will need to run “source ~/my_workspace/devel/setup.bash“. Instead of doing that you can add this command to your “~/.bashrc” file. We can do that by typing

echo “source ~/my_workspace/devel/setup.bash” >> ~/.bashrc

However, if you want to erase the created workspace and packages that we will create, then make sure that you also remove this command from the “~/.bashrc” file.

STEP 2: Create a Catkin package

Before we explain how to create a package, let us first explain the basic structure of the package. The simplest possible ROS Catkin package contains the following file structure:

my_workspace/ —- Workspace
src/ —- Source space
CMakeLists.txt —- Top-level CMake file that is provided by catkin
new_package/
CMakeLists.txt —- CMake file for the package
package.xml —- Package manifest for “new_package”

The name of the package is “new_package”. The file “package.xml” provides meta-information about the package. The file “CMakeLists.txt” is the CMake file of the package.

We use the following procedure to create a catkin package. First, we type

cd ~/my_workspace/src

Then, we type

catkin_create_pkg new_package

The name of the created package is “new_package” and “catkin_create_pkg” is the ROS command used to create the package. This command will create a directory called “new_package” with the previously described file structure.

Next, we need to build the created package. We do that by typing

cd ~/my_workspace/
catkin_make

By analyzing the structure of the “devel” folder, we can conclude that this folder has a similar structure of the structure of the /opt/ros/noetic directory.

The final step is to add the workspace to the ROS environment. We can do that by typing

. ~/my_workspace/devel/setup.bash

In this way, we created our first ROS package. More information about ROS package creation can be found here. In the future tutorial we will edit and play with this package.