πŸ—οΈBehind the Scenes in Robot Town – Understanding CMake, Ament, and XML

gayatri kumargayatri kumar
4 min read

🏠 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

LineWhat 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

TagWhat 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
    So ament adds ROS-specific intelligence on top of CMake.

There are two types:

  • ament_cmake β†’ for CMake-based projects

  • ament_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:

  1. It finds every package in your workspace

  2. Builds them in the correct order

  3. Installs scripts and message files

  4. 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:

  1. Updating your environment so that ROS knows:

    • Where your packages are installed

    • Where to find your custom messages

    • What nodes/scripts are available

  2. 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?

PartRole
CMakeLists.txtConstruction blueprint (how to build)
package.xmlMetadata and dependencies (what is being built)
amentThe builder that understands ROS
colconWorkspace manager and multi-package builder
setup.bashMagic spell that makes your packages discoverable

🧠 Extra Resources


πŸ“° 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. πŸ““πŸ€–βœοΈ

10
Subscribe to my newsletter

Read articles from gayatri kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

gayatri kumar
gayatri kumar