Skip to main content

ROS 2 Workspaces and Package Creation

Learning Objectives​

By the end of this chapter, you will be able to:

  • Understand the fundamental concept and purpose of a ROS 2 workspace.
  • Create and initialize a new ROS 2 workspace.
  • Explain the basic structure and components of a ROS 2 package.
  • Create a new ROS 2 package using ros2 pkg create.
  • Build and source a ROS 2 workspace to make packages available.

What is a ROS 2 Workspace?​

A ROS 2 workspace is a directory on your filesystem where you can organize, build, and install ROS 2 packages. It's essentially a managed collection of source code packages that colcon (the ROS 2 build tool) can process. By using workspaces, you can easily work on multiple ROS 2 projects or different versions of packages without conflicts.

The typical structure of a workspace includes:

  • src/: Contains the source code of your ROS 2 packages.
  • build/: Stores intermediate build files generated by colcon.
  • install/: Contains the installed files of your packages (executables, libraries, Python modules, etc.). This is where ROS 2 looks for your packages.
  • log/: Stores logging information from the build process.

Creating a Workspace

To create a new ROS 2 workspace, you typically follow these steps:

  1. Create a directory for your workspace:

    mkdir -p ~/ros2_ws/src
    cd ~/ros2_ws

    The -p flag ensures that parent directories are created if they don't exist. ros2_ws is a common naming convention, but you can choose any name.

  2. Initialize the workspace (optional but good practice): While colcon build will implicitly create the build/ and install/ directories, some tutorials suggest explicitly calling colcon build in an empty workspace to ensure all necessary setup files are present.

    cd ~/ros2_ws
    colcon build

    This command will build any packages found in the src/ directory (if any) and also prepare the install/ directory.

Understanding ROS 2 Packages

A ROS 2 package is the fundamental unit of software organization in ROS 2. It contains all the necessary files for a specific functionality, such as code (Python or C++), build scripts, configuration files, message/service/action definitions, and other resources.

Every ROS 2 package must contain:

  • package.xml: Provides metadata about the package (name, version, description, maintainers, license, dependencies).
  • CMakeLists.txt (for ament_cmake packages) or setup.py (for ament_python packages): Build configuration files that tell colcon how to build and install the package.

Creating a ROS 2 Package

You can create a new ROS 2 package using the ros2 pkg create command. This command generates the basic directory structure and necessary configuration files for your new package.

  1. Navigate to your workspace's src directory:

    cd ~/ros2_ws/src
  2. Create the package: The --build-type argument specifies how the package will be built (ament_python for Python packages, ament_cmake for C++ packages). The --dependencies argument lists other ROS 2 packages that your new package will rely on.

    For a Python package:

    ros2 pkg create --build-type ament_python my_first_package --dependencies rclpy std_msgs

    This creates a package named my_first_package with Python build support and declares dependencies on rclpy (ROS 2 Python client library) and std_msgs (standard message types).

    For a C++ package:

    ros2 pkg create --build-type ament_cmake my_cpp_package --dependencies rclcpp std_msgs

    This creates a C++ package named my_cpp_package with dependencies on rclcpp (ROS 2 C++ client library) and std_msgs.

    After running these commands, you will find a new directory (my_first_package or my_cpp_package) within your ~/ros2_ws/src folder, containing the package.xml and the respective build files (setup.py or CMakeLists.txt).

Building and Sourcing Your Workspace

After creating or modifying packages, you need to build your workspace to compile the code and make the executables available.

  1. Build the workspace: Navigate to the root of your workspace and run colcon build.

    cd ~/ros2_ws
    colcon build
    • colcon build: Builds all packages in your workspace.
    • colcon build --packages-select <package_name>: Builds only a specific package and its dependencies.
    • colcon build --symlink-install: Creates symlinks to installed files instead of copying them, which can be useful for development (changes in Python files are immediately reflected without rebuilding).
  2. Source the setup files: After building, the executables and libraries are placed in the install/ directory. To make ROS 2 aware of these new packages, you must "source" the setup file in your current shell.

    source install/setup.bash

    (If you are using Zsh, it might be source install/setup.zsh. For Windows PowerShell, it's install/setup.ps1.)

    You will need to run this command in every new terminal you open where you want to use your workspace packages. To avoid typing this command every time, you can add it to your shell's startup script (e.g., ~/.bashrc or ~/.zshrc).

Interactive Quiz​

What is the primary purpose of a ROS 2 workspace?

Answer: To organize, build, and install ROS 2 packages.

Which directory within a workspace typically contains the source code of your ROS 2 packages?

Answer: src/

True or False: After creating a new package, you must always run ros2 build to compile it.

Answer: False. You use colcon build to build packages in a ROS 2 workspace, not ros2 build.

Further Reading​