Tech Arcanist
Get in touch

05Chapter 5 of 51

ROS 2 Workspace & Package Creation

Creating a ROS 2 Workspace, Building It, and Creating Python & C++ Packages

What is a ROS 2 Workspace?

A ROS 2 workspace is simply a folder on your PC that contains:

  • All ROS 2 packages
  • All source code for your robot
  • Build and install artifacts generated by ROS 2 tools

Think of it as a container for all robot software.

This workspace acts as a ROS 2 overlay, meaning:

  • You can use all default ROS 2 packages

Creating the Workspace Structure

Step 1: Open a New Terminal

Use Terminator (recommended).

Step 2: Create Workspace and src Folder

mkdir -p bumperbot_ws/src
  • mkdir = make directory
  • -p → creates parent folders if they don’t exist
  • bumperbot_ws → workspace name
  • src → source folder (mandatory for ROS 2)

Step 3: Verify Creation

ls

You should see bumperbot_ws

Exploring the Workspace

Move into Workspace

cd bumperbot_ws

List Contents

ls
  • Workspace only contains source folder
  • No ROS 2 structure yet

Initializing the Workspace with colcon

Build the Workspace

colcon build
  • colcon is the ROS 2 build tool
  • Converts source code (Python / C++) into executable files
  • Generates required ROS 2 structure

Check Generated Folders

ls

ROS 2 Workspace Folder Structure

FolderPurpose
src/All source code & packages
build/Intermediate build files
install/Final executables & setup files
log/Build logs

Creating ROS 2 Packages

Packages must always be created inside src/.

Python Package

cd src/
ros2 pkg create --build-type ament_python bumperbot_py_examples

ros2 pkg create, build-type <BUILD_SYSTEM> <PACKAGE_NAME>

  • ros2 → ROS 2 command-line tool
  • pkg → work with packages
  • create → create a new package
  • --build-type → defines how the package is built (most important)
  • ament_python → Python-based package
    • ament → ROS 2 build system
    • python → Python-based package

C++ Package

ros2 pkg create --build-type ament_cmake bumperbot_cpp_examples

Rebuilding the Workspace

Go back to workspace root:

cd ..
colcon build

Now colcon:

  • Detects 2 new packages
  • Builds them
  • Installs executables
    Build output confirms:
    • Packages found
    • Packages successfully compiled

Activating (Sourcing) the Workspace

ROS 2 must be explicitly informed about your workspace.

Open another Terminal (Important)

cd install
ls
. setup.bash

This makes the current terminal:

  • Aware of bumperbot_ws
  • Able to run its packages
  • . = source (source setup.bash)

Verifying Package Recognition

ros2 pkg list

Open a New Terminal (Without Sourcing)

ros2 pkg list

In First bumperbot_* packages is visible and in second bumperbot_* packages is NOT visible