Skip to main content

Setup Your ROS2 Workspace

Creating a Workspace

To compensate some colcon build drawbacks and allow you to use cb command anywhere in the workspace, get the following aliases first.

cd ~/git

git clone git@github.com:ctu-mrs/mrs_uav_development.git

cd mrs_uav_development

git checkout ros2

Prepare your workspace

mkdir -p ~/ros2_ws/src

Add the following to your ~/.bashrc (or ~/.zshrc):

# workspace to be sourced
export ROS_WORKSPACE="$HOME/ros2_ws"

# ROS DEVELOPMENT
# * source this after exporting $ROS_WORKSPACE="<path to your workspace>"
# * workspace is automatically sourced and the sourcing is cached
# * to force-source a workspace after adding new packages, call `presource_ros`
source $HOME/git/mrs_uav_development/shell_additions/shell_additions.sh

These shell additions allow for faster compilation, as it cashes parts that take long to build (mostly python parts). This cache is then being presourced, but adding new packages that require some of these components to be rebuild might require to call presource_ros in the workspace.

Clone an existing ROS2 package and link it to your workspace.

cd ~/git

git clone git@github.com:ctu-mrs/mrs_core_examples.git

cd ~/ros2_ws/src

ln -sf ~/git/mrs_core_examples/cpp/example_waypoint_flier

Building a Workspace

Now you can build your workspace and check, if all the configuration works.

cd ~/ros2_ws/

colcon init

colcon build

You should be able to call just cb out of any subdirectory of your workspace.

Since you've already put the path to your workspace to your ~/.bashrc (or ~/.zshrc), it should source install/setup.bash (or source install/setup,ths) automatically when you open a new terminal.

Adding a new Workspace

In ROS2, if you source a new workspace, it will be automatically linked with the previous one and it will adopt its dependencies, but will not refresh them if you make any changes. That can very well compromise your new workspace and you might be forced to clean the workspace often. Therefore we encourage you to modify the path to your new workspace in ~/.bashrc (or ~/.zshrc) if you create a new one. The shell_additions will take care of the sourcing for you.

# workspace to be sourced
export ROS_WORKSPACE="$HOME/new_ros2_ws"