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 bycolcon.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:
-
Create a directory for your workspace:
mkdir -p ~/ros2_ws/src
cd ~/ros2_wsThe
-pflag ensures that parent directories are created if they don't exist.ros2_wsis a common naming convention, but you can choose any name. -
Initialize the workspace (optional but good practice): While
colcon buildwill implicitly create thebuild/andinstall/directories, some tutorials suggest explicitly callingcolcon buildin an empty workspace to ensure all necessary setup files are present.cd ~/ros2_ws
colcon buildThis command will build any packages found in the
src/directory (if any) and also prepare theinstall/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(forament_cmakepackages) orsetup.py(forament_pythonpackages): Build configuration files that tellcolconhow 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.
-
Navigate to your workspace's
srcdirectory:cd ~/ros2_ws/src -
Create the package: The
--build-typeargument specifies how the package will be built (ament_pythonfor Python packages,ament_cmakefor C++ packages). The--dependenciesargument 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_msgsThis creates a package named
my_first_packagewith Python build support and declares dependencies onrclpy(ROS 2 Python client library) andstd_msgs(standard message types).For a C++ package:
ros2 pkg create --build-type ament_cmake my_cpp_package --dependencies rclcpp std_msgsThis creates a C++ package named
my_cpp_packagewith dependencies onrclcpp(ROS 2 C++ client library) andstd_msgs.After running these commands, you will find a new directory (
my_first_packageormy_cpp_package) within your~/ros2_ws/srcfolder, containing thepackage.xmland the respective build files (setup.pyorCMakeLists.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.
-
Build the workspace: Navigate to the root of your workspace and run
colcon build.cd ~/ros2_ws
colcon buildcolcon 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).
-
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'sinstall/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.,
~/.bashrcor~/.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.