In this tutorial I’ll show you how to create multiple catkin workspaces with ROS.
Sometimes you want to create different projects for different robots. The easiest way to manage that is to create several catkin workspaces, one for each robot project. But it’s not as simple as that. There are a few things you need to understand before you can actually manage multiple catkin workspaces.
Your first catkin workspace: behind the hood
As a reminder, you did something like that to create your first catkin workspace:
$ mkdir -p ~/catkin_ws/src $ cd ~/catkin_ws/ $ catkin_make
The catkin_make command generates a devel/ folder and a build/ folder, using the content inside the src/ folder.
Then, you added the line
source ~/catkin_ws/devel/setup.bash into your ~/.bashrc file.
Your .bashrc now surely contains this:
source /opt/ros/melodic/setup.bash source ~/catkin_ws/devel/setup.bash
(Actually you can run those “source” commands every time you open a new terminal, but it’s far more convenient to just put them on your .bashrc.)
The first line finds where ROS is installed on your computer. You need it to be able to use the ROS core functionalities (such as roscore, rosrun, …), and use all the ROS packages that you installed from binary source using apt-get.
Note: the order for those two lines is very important. Always source the global ROS install before your catkin workspace.
All in all, you can see that creating a catkin workspace is a two-step process:
1. You create a specific folder and use catkin_make.
2. You source this catkin workspace so you can use it.
Create another catkin workspace
Let’s now create another catkin workspace in addition to the one you already have:
$ mkdir -p ~/test_directory/another_catkin_ws/src $ cd ~/test_directory/another_catkin_ws/ $ catkin_make
Basically you can name your catkin workspace as you want, and you can create it anywhere you want, not necessarily on the root of your home folder.
Now, you can add the line
source ~/test_directory/another_catkin_ws/devel.setup.bash in your .bashrc.
Let’s say you now have the following lines into your .bashrc:
source /opt/ros/melodic/setup.bash source ~/catkin_ws/devel/setup.bash source ~/test_directory/another_catkin_ws/devel/setup.bash
What will happen?
When creating a new session (ex: open a terminal), the content of your .bashrc will be executed line by line.
The first line will find where ROS is installed on your computer, as we already saw before.
The second line will “activate” your first catkin workspace.
And the third line will “activate” your second catkin workspace, while “deactivating” your first catkin workspace.
Now, if you want to start a node from your first workspace, you’ll get an error because it won’t be found.
You can’t run multiple catkin workspaces on the same session. The last to be sourced is the one that is activated.
So, you have to be careful which workspace you activate.
Switch between multiple catkin workspaces
To switch from a catkin workspace to another, you simply have to run the “source” command associated with the workspace you want to activate.
Let’s say you opened a terminal, and the last line on your .bashrc activates your second workspace. All you have to do is to run
source ~/catkin_ws/devel/setup.bash inside your terminal to activate your first catkin workspace (and deactivate the second one by the way).
You can also simply change the order in your .bashrc, or comment the lines you don’t need.
source /opt/ros/melodic/setup.bash source ~/catkin_ws/devel/setup.bash # As the second catkin workspace is commented, only the first one will be activated. # source ~/test_directory/another_catkin_ws/devel/setup.bash
I personally use this technique to manage multiple catkin workspaces on my computer. When you have more than 3 workspaces, you’d better stay organized!