Skip to main content

Command Palette

Search for a command to run...

๐Ÿ—๏ธBehind the Scenes in Robot Town โ€“ Understanding CMake, Ament, and XML

Published
โ€ข4 min read
๐Ÿ—๏ธ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

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. ๐Ÿ““๐Ÿค–โœ๏ธ

S
Saanvi Kumar11mo ago

Loved how this made CMake, ament, and colcon feel less intimidating. The comparison to a construction crew and planner really works.

More from this blog

GeeKee's Odyssey

113 posts