πŸ“˜The Robot Town Ledger

gayatri kumargayatri kumar
4 min read

🧾 Introduction: YAML Is Your Town's Ledger πŸ“š

Imagine every bot in Robot Town has a personal diary in City Hall. It’s neat, human-readable, and stores all settings like favorite food, alarm times, and security levels.

That’s what YAML files are in ROS 2 β€” clear, structured, and powerful.

And just like the Mayor gives out a Morning Bulletin to activate all bots, launch files let us load those settings at startup.

In this article, we’ll:

  • Understand the YAML structure in ROS2

  • Create reusable config files

  • Pass them into launch files

  • Bonus: Launch multiple bots, each with their own config!


🧩 Anatomy of a ROS 2 YAML File

A YAML file stores parameters per node.

Let’s say we have a bot named guard_bot with a sensitivity level.

guard_bot:
  ros__parameters:
    alert_level: 5
    watch_mode: "motion"

🧠 Breakdown:

  • guard_bot: This is the node's name.

  • ros__parameters: A magic key that tells ROS these are parameters.

  • The inner keys like alert_level and watch_mode are the actual parameters used in your node code.


πŸ“ Example: ChefBot’s YAML Configuration

In chef_bot/chef_bot/chef_config.yaml:

chef_bot:
  ros__parameters:
    flavor: sweet
    spice_level: 3

Then in your chef_node.py, you'd fetch these parameters like so:

self.flavor = self.get_parameter("flavor").get_parameter_value().string_value
self.spice = self.get_parameter("spice_level").get_parameter_value().integer_value

Run with:

ros2 run chef_bot chef_node --ros-args --params-file src/chef_bot/chef_bot/chef_config.yaml

πŸš€ Launch Files with Parameters

Let’s build a launch file that powers on ChefBot with settings loaded from YAML.

πŸ—‚ File: chef_bot/launch/chef_launch.py

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='chef_bot',
            executable='chef_node',
            name='chef_bot',
            parameters=['../chef_bot/chef_config.yaml']
        )
    ])

Run with:

ros2 launch chef_bot chef_launch.py

πŸ“ Make sure the relative path to chef_config.yaml is correct or use os.path.join for safer access.


🍱 Multi-Bot Launch with YAMLs

Let’s launch ChefBot and GuardBot, each with their own YAML:

1. YAML for GuardBot – guard_bot/guard_config.yaml

guard_bot:
  ros__parameters:
    alert_level: 7
    watch_mode: "thermal"

2. Launch File – launch/all_bots_launch.py

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='chef_bot',
            executable='chef_node',
            name='chef_bot',
            parameters=['../chef_bot/chef_config.yaml']
        ),
        Node(
            package='guard_bot',
            executable='guard_node',
            name='guard_bot',
            parameters=['../guard_bot/guard_config.yaml']
        )
    ])

πŸš€ Run all bots:

ros2 launch your_package all_bots_launch.py

Now both bots boot up, read their personality profiles from YAML, and run accordingly.


πŸ€– Dynamic Tip: YAML + Command Line = Override

You can override a YAML setting live:

ros2 run chef_bot chef_node --ros-args \
  --params-file src/chef_bot/chef_bot/chef_config.yaml \
  -p flavor:=bitter

ROS2 will read the YAML but use bitter for flavor.


πŸ§ͺ Testing and Debugging

βœ… To check what parameters a node is using:

ros2 param list
ros2 param get /chef_bot flavor

βœ… YAML Format Checker:

Use VS Code or online YAML linters to avoid indentation errors.


🏁 Recap: What You’ve Learned

  • βœ… YAML stores bot preferences in a clean, editable format.

  • βœ… Launch files can pass YAML to bots for easy config.

  • βœ… You can override YAML on the command line.

  • βœ… Every bot in Robot Town can have a personal profile, loaded at start!


🧱 Folder Structure Snapshot

robot_town_ws/
└── src/
    β”œβ”€β”€ chef_bot/
    β”‚   β”œβ”€β”€ chef_bot/
    β”‚   β”‚   β”œβ”€β”€ chef_node.py
    β”‚   β”‚   └── chef_config.yaml
    β”‚   └── launch/
    β”‚       └── chef_launch.py
    β”œβ”€β”€ guard_bot/
    β”‚   β”œβ”€β”€ guard_bot/
    β”‚   β”‚   β”œβ”€β”€ guard_node.py
    β”‚   β”‚   └── guard_config.yaml
    β”‚   └── launch/
    β”‚       └── guard_launch.py
    └── launch/
        └── all_bots_launch.py

πŸ’‘ Bonus Ideas for Exploration

  • Create a Mood YAML for MayorBot with tone and message frequency

  • Use YAML to store DanceBot’s routine steps

  • Pass coordinates in YAML to NavigationBot


YAML Cheatsheet for Robot Town πŸš€πŸ“’

Basic Syntax

# Comments start with a hash
key: value          # Key-value pair

Data Types

string: "Hello Robot Town"   # Strings (quotes optional unless special chars)
integer: 42                  # Integers
float: 3.14                  # Floats
boolean_true: true           # Booleans: true / false
boolean_false: false
null_value: null             # Null / None

Collections

t# List (sequence)
bots:
  - MayorBot
  - TrafficBot
  - GuardBot

# Inline list
colors: [red, green, blue]

# Dictionary (mapping)
robot:
  name: GuardBot
  role: Patrol
  active: true

# Nested dictionary
launch:
  nodes:
    mayor:
      name: mayor_bot
      executable: mayor
    door:
      name: door_bot
      executable: door

Indentation

  • Use spaces only (no tabs)

  • Consistent indentation (2 or 4 spaces)

  • Indentation shows hierarchy and nesting

Multiline Strings

description: |
  This is a
  multiline string
  preserving line breaks.

note: >
  This is a
  multiline string
  folded into one line.

Common YAML in ROS2 Launch Files

launch:
  - node:
      package: robot_package
      executable: robot_node
      name: robot1
      output: screen
      parameters:
        - param1: value1
        - param2: value2
      remappings:
        - from: /old_topic
          to: /new_topic

πŸŽ‰ What’s Next: Practice Day β€” First ROS2 Simulation Day

Time for the big Robot Town festival! Next, your bots will practice their routines in a fun ROS2 simulation.
Watch DanceBot groove through moves, and learn to coordinate a group act using pub/sub messaging β€” the perfect team performance! πŸ€–πŸ’ƒπŸŽΆ

0
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