This tutorial explains how to create your workspace and create packages inside a workspace.
Before getting started with this, it is assumed that you have a working ROS installation and have gone through the following files
- I0_Basic_Terminology.md: Basic terminology in ROS
- T0_GS_Turtlesim.md: A codeless tutorial using TurtleSim
You may check if you have correctly sourced the ROS installation using
echo $ROS_DISTROAnd if everything is right, you must see noetic (or whatever distro you installed).
As explained earlier, a workspace is a directory on your system which contains all the ROS related, user developed content. This repository is created as a workspace on a local machine and pushed to the cloud.
More about workspaces here
To create your own workspace, follow the following instructions
-
Create a folder and a
srcfolder inside itThis can be done using the
mkdircommandmkdir -p ~/ros_workspaces/learning_ws/srcI prefer making a parent folder which contains different workspaces (sometimes I feel the need to work on completely different projects in ROS which need to be separate). The
ros_workspacesfolder acts as this directory. In roswiki tutorials, you may find that instead ofros_workspaces/learning_wsthe workspace folder is mentioned ascatkin_ws. You can use whatever name you want to, just make sure you remember it. -
Go to your workspace folder
For us, the workspace folder is
ros_workspaces/learning_ws. It will be whatever you chose in step 1. You need tocdinto it.cd ~/ros_workspaces/learning_ws
For the rest of this repository, it is assumed that unless mentioned otherwise, you're running all commands from this folder.
-
Build your workspace for the first time
After navigating into the workspace folder, run the
catkin_makecommandcatkin_make
This command will build all the packages inside an existing workspace. However, if an empty workspace is used, it will create the files necessary for the workspace.
roswiki on Creating a ROS workspace using Catkin here
After the first build, a few things can be noted
-
In the
srcfolder inside your workspace, a file calledCMakeLists.txtwould have appeared. This file is actually a soft copy of a template build file. Under no ordinary circumstances, you are to modify this file. All the build configurations that packages require are done throughCMakeLists.txtfile inside the package folders and not this one. -
Two new folders named
buildanddevelwould have formed.- The
buildfolder is used by CMake and catkin to keep their cache information. This folder aids in building everything in the package.More about the build space here
- The
develfolder is used to store built targets (executables, library headers, etc.). This is useful for ROS to locate them.More about devel space here
- In case you want to rebuild everything from ground up, you can delete the
buildanddevelfolders (you may userm -r) and then again runcatkin_makein the workspace folder.
- The
-
Source your workspace: Inside the
develfolder, there must be files namedsetup.*. In order for ROS to know where the workspace is, you need to source the workspace. Without this, packages inside your workspace shall not be discoverable.To confirm this, you may first run
echo $ROS_PACKAGE_PATH
If you do not have any other workspace sourced, you must see only
/opt/ros/noetic/sharein the output (installation path). Let us source the workspace that we just createdsource ~/ros_workspaces/learning_ws/devel/setup.bash
And now run
echo $ROS_PACKAGE_PATH
Now, in the output, we can also see the
srcfolder of the workspace that we created. If you use the defaultbashshell, then you must sourcesetup.bash. If you're using another shell, sayzsh, sourcingsetup.zshmay be a better idea.-
The above
sourcecommand will have to be executed every time you open a new terminal. Otherwise, the$ROS_PACKAGE_PATHvariable would not be updated. Therefore, it is a good idea to have the source line in the~/.bashrcfile, so that every time you open a new terminal, the workspace is sourced also (similar to what you may have done by sourcing the ROS installation file when installing ROS).Run the following command
echo "source ~/ros_workspaces/learning_ws/devel/setup.bash" >> ~/.bashrc
Note that you may use
~/.bashrcif you use bash. If you're using, say zsh, you might want to use the commandecho "source ~/ros_workspaces/learning_ws/devel/setup.zsh" >> ~/.zshrc
-
A package is used to contain all the things made for a particular purpose inside a single folder. These folders are stored inside the src folder of the workspace.
More about packages here
To create your own package, you must first have the following information in mind
- Package name: An appropriate name for your package. For the purpose of this tutorial, let it be
cpp_basic_nodes. - Dependencies: They are other packages and libraries on which your package depends to build and run everything inside it. You can even add or remove them after creating the package. However, it's better to have the known ones set up with creation. For this tutorial, we'll use the following dependencies
roscpp: Since this is a C++ package, it uses the C++ client library.
To create a package, follow these steps
-
Make sure that you are in the
srcfolder of your workspace and that the workspace and ROS is properly sourced.cd ~/ros_workspaces/learning_ws/src
-
Run the following command to create a package using
catkin_create_pkgcatkin_create_pkg cpp_basic_nodes roscpp
This must have create a folder named cpp_basic_nodes and the folder must have
CMakeLists.txtfile: This is to manage how things in the package are builtMore about the CMakeLists.txt file here
package.xmlfile: For managing the manifesto and information about the packageMore about the package.xml file here
After the package is created, you might want to build it (even if its empty, just to see if everything is fine). To do that, first cd into the workspace folder
cd ~/ros_workspaces/learning_ws/Now run the following
catkin_makeYou must see a message in the output mentioning that your package cpp_basic_nodes was found (under traversing N packages in topological order).
To selectively build packages, you may use the --pkg tag with catkin_make.
Some tools that are useful in handling packages
The rospack tool can be used to get information about packages. For example, to get the basic dependencies of a package, you may run
rospack depends1 cpp_basic_nodesTo get all dependencies, you may use
rospack depends cpp_basic_nodesYou can use rospack help to get more information about the tool.
More about the rospack tool here
The rosls tool is used to list the contents of a package (irrespective of the directory you're in).
rosls cpp_basic_nodesWill list the contents of the cpp_basic_nodes package.
The roscd tool is used to cd into a package directory
roscd cpp_basic_nodesWill get you into the directory ~/ros_workspaces/learning_ws/src/cpp_basic_nodes (if you did everything according to this tutorial)
In this tutorial, you learned how to make your own workspace and create packages inside the workspace. You also learned how to build the packages in a workspace.