ποΈBehind the Scenes in Robot Town β Understanding CMake, Ament, and XML


π Packages are Robot Houses β But Who Builds Them?
Imagine youβre building a new robot house (package). For that, you need:
π Blueprints (
CMakeLists.txt
) β what to build and how.π§Ύ Permit documents (
package.xml
) β whatβs inside and what it depends on.ποΈ Construction crew (
ament
) β the build system that follows the blueprints.π§ City planner (
colcon
) β oversees and builds the whole neighborhood.
Letβs break down each component.
π CMakeLists.txt
: The Blueprint
This file tells ament
how to build your package.
Hereβs a simplified version:
cmake_minimum_required(VERSION 3.5)
project(mood_bot)
# Find required ROS2 packages
find_package(ament_cmake REQUIRED)
find_package(rclpy REQUIRED)
find_package(std_msgs REQUIRED)
# Install Python code
install(
PROGRAMS
mood_bot/mood_publisher.py
DESTINATION lib/${PROJECT_NAME}
)
# Add messages (if any)
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Mood.msg"
)
ament_export_dependencies(rosidl_default_runtime)
ament_package()
π Key Concepts
Line | What It Means |
cmake_minimum_required(...) | Required version of CMake |
project(...) | Name of the package |
find_package(...) | Imports dependencies (like rclpy ) |
install(...) | Tells where to copy scripts during install |
rosidl_generate_interfaces(...) | Builds custom messages from .msg files |
ament_package() | Finalizes the blueprint so ament can use it |
π§Ύ package.xml
: The Permit Document
This XML file tells ROS2 the metadata about your package: its name, version, description, and dependencies.
<?xml version="1.0"?>
<package format="3">
<name>mood_bot</name>
<version>0.0.1</version>
<description>Bot that shares moods with emoji</description>
<maintainer email="you@town.com">Town Engineer</maintainer>
<license>Apache License 2.0</license>
<exec_depend>rclpy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<buildtool_depend>ament_cmake</buildtool_depend>
<build_depend>rosidl_default_generators</build_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
</package>
π Important Tags
Tag | What It Does |
<name> , <version> | Identifies the package |
<maintainer> | Whoβs responsible for it |
<license> | Legal use |
<exec_depend> | Run-time dependencies |
<build_depend> | Build-time dependencies |
<buildtool_depend> | Tools needed to build (like ament_cmake ) |
βοΈ What is ament
?
ament
is the build system used by ROS2. Itβs like a robot construction crew that reads CMakeLists.txt
and builds your code correctly.
Why not just use CMake directly?
Because ROS2 is complex. It needs:
message generation
dependency management
Python/C++ builds
Soament
adds ROS-specific intelligence on top of CMake.
There are two types:
ament_cmake
β for CMake-based projectsament_python
β for Python-only packages
ποΈ What is colcon
?
colcon
is like the general contractor. It:
Builds entire workspaces
Respects package dependencies
Offers parallel builds and logs
π§ When you run:
colcon build
Hereβs what happens:
It finds every package in your workspace
Builds them in the correct order
Installs scripts and message files
Prepares a
setup.bash
file to βactivateβ the workspace
π§ Behind the setup.bash
Spell
When you run:
source install/setup.bash
Youβre doing two magical things:
Updating your environment so that ROS knows:
Where your packages are installed
Where to find your custom messages
What nodes/scripts are available
Enabling ros2 CLI tools to discover new packages
Think of it like registering a newly built house in Robot Townβs map. Without sourcing, your robot houses donβt βexistβ to the rest of the town!
ποΈ Where Does It All Fit In?
Letβs look at the real Robot Town structure:
robot_town_ws/
βββ build/ β temporary build files
βββ install/ β installed scripts, libraries, msg headers
βββ log/ β logs of builds
βββ src/ β all your packages
βββ mood_bot/
β βββ mood_bot/
β β βββ mood_publisher.py
β βββ msg/
β β βββ Mood.msg
β βββ CMakeLists.txt
β βββ package.xml
β βββ setup.py (for Python packages)
π¬ TL;DR: Who Does What?
Part | Role |
CMakeLists.txt | Construction blueprint (how to build) |
package.xml | Metadata and dependencies (what is being built) |
ament | The builder that understands ROS |
colcon | Workspace manager and multi-package builder |
setup.bash | Magic spell that makes your packages discoverable |
π§ Extra Resources
ROS2 Build System Docs
ROS2 Message Generation
π° Whatβs Next: The Town Newspaper (Logging in ROS2)
In Robot Town, every bot keeps a diary of its adventures! Next up, weβll explore logging in ROS2 β how bots like GuardBot record everything they see and do.
Join us to learn how to make your robots write their own stories, and try the mini project to log a full day of events in Robot Town. ππ€βοΈ
Subscribe to my newsletter
Read articles from gayatri kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by