A workspace in ROS2 serves as the central repository for organizing all development artifacts, including source code, configuration parameters, and scripts, for robot functionality. This structured directory facilitates systematic project management.
A canonical ROS2 workspace contains four primary subdierctories:
- src (Source Space): Contains manually placed user-developed code, scripts, and packages.
- build (Build Space): Holds intermediate files generated during the compilation process.
- install (Install Space): Stores executables and launch scripts produced after a successful build.
- log (Log Space): Records informational, warning, and error logs from compilation and execution. Most development activities occur within the
srcdirectory, with successful builds making results available ininstall.
Initializing a New Workspace
To create a new workspace named dev_ws and initialize it with tutorial code:
mkdir -p ~/dev_ws/src
cd ~/dev_ws/src
git clone https://gitee.com/guyuehome/ros2_21_tutorials.git
Resolving Package Dependencies Automatically
Dependencies for code fetched from the community can be installed automatically using the rosdep tool.
sudo apt install -y python3-pip
sudo pip3 install rosdepc
sudo rosdepc init
rosdepc update
cd ~/dev_ws/
rosdepc install -i --from-path src --rosdistro humble -y
This command, executed from the workspace root, scans the src directory for all package dependencies and installs any missing ones for the specified ROS 2 distribution.
Building the Workspace
Compilation must be performed from the workspace's root directory. Errors will indicate missing dependencies or code issues.
sudo apt install python3-colcon-ros
cd ~/dev_ws/
colcon build
Upon successful compilation, the build, log, and install directories are automatically generated.
Configuring the Environment
After building, enviroment variables must be set so the system can locate the new packages and executables.
source install/local_setup.sh # Effective for current terminal only
# To apply automatically for all new terminals, add the source command to .bashrc
echo "source ~/dev_ws/install/local_setup.sh" >> ~/.bashrc
ROS2 Packages
Folders within the src directory are not ordinary folders but packages. A package groups the code, libraries, and configuration files for a specific robot capability (e.g., motion control, perception). This modular design reduces code coupling and is fundamental to software reuse in ROS2.
Generating a New Package
Use the ros2 pkg create command to generate a package skeleton.
ros2 pkg create --build-type <build-type> <package_name>
For example, to create packages for C++ and Python:
cd ~/dev_ws/src
ros2 pkg create --build-type ament_cmake demo_pkg_cpp # C++ package
ros2 pkg create --build-type ament_python demo_pkg_py # Python package
Compiling Individual or All Packages
New or modifeid packages must be compiled and their environment sourced.
cd ~/dev_ws
colcon build # Builds all packages in the workspace
source install/local_setup.bash