๐Ÿ•ฐ๏ธRobots With Schedules

gayatri kumargayatri kumar
6 min read

๐Ÿ™๏ธ Welcome to a Timely Town

In Robot Town, not all bots act on commands.

Some bots operate on daily schedules โ€” like:

  • WaterBot watering gardens at sunrise โ˜€๏ธ

  • TrashBot collecting garbage every evening ๐Ÿ—‘๏ธ

  • DanceBot performing hourly groove sessions ๐Ÿ•บ

These bots donโ€™t wait for messages or user input โ€” they use timers to trigger actions periodically.

Today, weโ€™ll learn to give our bots their own alarm clocks using ROS2 Timers.


โฐ Timers in ROS2: What Are They?

In simple terms, a timer tells your bot:

โ€œHey! Every x seconds, do this thing.โ€

Timers are perfect for:

  • Repeating actions at intervals

  • Scheduling routines

  • Looping without using while True


๐Ÿง  How ROS2 Timers Work (Simple Mental Model)

Think of it like this:

Timer = A robotโ€™s digital alarm clock.
Every few seconds โ†’ โ€œRing!โ€ โ†’ Run a task.

You set:

  • The interval (e.g., every 2 seconds)

  • The callback function (what to do when the alarm rings)


๐Ÿค– Introducing WaterBot

Letโ€™s build WaterBot, who waters plants every 5 seconds.

๐Ÿ“ Folder Prep (Inside robot_town_ws/src/water_bot/)

water_bot/
โ”œโ”€โ”€ water_bot/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ water_node.py
โ”œโ”€โ”€ package.xml
โ”œโ”€โ”€ setup.py
โ””โ”€โ”€ resource/
    โ””โ”€โ”€ water_bot

Make sure CMakeLists.txt and setup.cfg are set up (like in previous bots).


๐Ÿ’ง water_node.py

Hereโ€™s a simple timer-powered ROS2 node.

import rclpy
from rclpy.node import Node

class WaterBot(Node):
    def __init__(self):
        super().__init__('water_bot')  # Initialize the node with the name 'water_bot'
        self.counter = 0  # Keeps track of how many times the plants have been watered

        # ๐Ÿ•’ Create a timer that triggers the water_plants method every 5 seconds
        # - 5.0 is the interval in seconds
        # - self.water_plants is the callback function to call every interval
        self.timer = self.create_timer(5.0, self.water_plants)

        # ๐Ÿ“ Log startup message to indicate the bot is active
        self.get_logger().info('WaterBot is on duty!')

    def water_plants(self):
        # ๐Ÿงฎ Increment the watering counter
        self.counter += 1

        # ๐Ÿ“ฃ Log each watering action with round number
        self.get_logger().info(f'๐ŸŒฑ Watering plants... round {self.counter}')

def main(args=None):
    rclpy.init(args=args)     # ๐Ÿ”Œ Initialize the ROS 2 system
    node = WaterBot()         # ๐Ÿค– Create an instance of WaterBot
    rclpy.spin(node)          # ๐Ÿ”„ Keep the node alive and processing timer callbacks
    rclpy.shutdown()          # ๐Ÿ“ด Shutdown the ROS 2 system when done

if __name__ == '__main__':
    main()  # ๐Ÿ Run the main function if this file is executed directly

๐Ÿ”จ Build & Run WaterBot

Build your workspace:

colcon build
source install/setup.bash

Then run:

ros2 run water_bot water_node

Expected output:

[INFO] [water_bot]: WaterBot is on duty!
[INFO] [water_bot]: ๐ŸŒฑ Watering plants... round 1
[INFO] [water_bot]: ๐ŸŒฑ Watering plants... round 2
...

๐Ÿงญ Whatโ€™s Happening Behind the Scenes?

create_timer(interval, callback)

  • Calls callback() every interval seconds.

rclpy.spin(node)

  • Keeps the bot running and waiting for timer events.

  • Like a power grid that lets timers tick.


๐Ÿ—‚๏ธ Folder Structure Recap

Hereโ€™s how your WaterBot package should now look:

robot_town_ws/
โ””โ”€โ”€ src/
    โ”œโ”€โ”€ water_bot/
    โ”‚   โ”œโ”€โ”€ water_bot/
    โ”‚   โ”‚   โ””โ”€โ”€ water_node.py
    โ”‚   โ”œโ”€โ”€ package.xml
    โ”‚   โ”œโ”€โ”€ setup.py
    โ”‚   โ”œโ”€โ”€ setup.cfg
    โ”‚   โ””โ”€โ”€ resource/
    โ”‚       โ””โ”€โ”€ water_bot

If you've built mayor_bot, door_bot, chatter_bot, etc., your workspace may now look like:

src/
โ”œโ”€โ”€ mayor_bot/
โ”œโ”€โ”€ door_bot/
โ”œโ”€โ”€ chatter_bot/
โ”œโ”€โ”€ weather_bot/
โ”œโ”€โ”€ umbrella_bot/
โ”œโ”€โ”€ chef_bot/
โ”œโ”€โ”€ water_bot/

One big bustling bot community!


๐Ÿงน Enter TrashBot: Evening Cleanup Duty

Letโ€™s build a second bot: TrashBot, who takes out the trash every 3 seconds.


๐Ÿ› ๏ธ Folder Setup (Inside robot_town_ws/src/trash_bot/)

trash_bot/
โ”œโ”€โ”€ trash_bot/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ trash_node.py
โ”œโ”€โ”€ package.xml
โ”œโ”€โ”€ setup.py
โ””โ”€โ”€ resource/
    โ””โ”€โ”€ trash_bot

โ™ป๏ธ trash_node.py

import rclpy
from rclpy.node import Node

class TrashBot(Node):
    def __init__(self):
        super().__init__('trash_bot')
        self.counter = 0

        # Timer: every 3 seconds
        self.timer = self.create_timer(3.0, self.take_out_trash)
        self.get_logger().info('TrashBot reporting for duty!')

    def take_out_trash(self):
        self.counter += 1
        self.get_logger().info(f'๐Ÿ—‘๏ธ Taking out trash... trip {self.counter}')

def main(args=None):
    rclpy.init(args=args)
    node = TrashBot()
    rclpy.spin(node)
    rclpy.shutdown()

if __name__ == '__main__':
    main()

๐Ÿ”€ Launching WaterBot + TrashBot Together

Now letโ€™s create a launch file to wake both bots up together, like setting multiple alarms in one clock.


๐Ÿ“‚ Create a New Package: daily_schedule

tcd ~/robot_town_ws/src
ros2 pkg create --build-type ament_python daily_schedule

Inside daily_schedule/launch/, create:

daily_schedule/
โ”œโ”€โ”€ daily_schedule/
โ”‚   โ””โ”€โ”€ __init__.py
โ”œโ”€โ”€ launch/
โ”‚   โ””โ”€โ”€ morning_routine.launch.py
โ”œโ”€โ”€ package.xml
โ”œโ”€โ”€ setup.py
โ””โ”€โ”€ resource/
    โ””โ”€โ”€ daily_schedule

๐ŸŒ„ morning_routine.launch.py

from launch import LaunchDescription               # ๐Ÿ“ฆ Import the LaunchDescription object from the 'launch' module
from launch_ros.actions import Node                # ๐Ÿš€ Import the Node action from the ROS 2 launch system

def generate_launch_description():
    return LaunchDescription([                     # ๐Ÿ“œ Create and return a list of launch actions

        # ๐ŸŒŠ Launch WaterBot
        Node(
            package='water_bot',                   # The ROS 2 package containing the node
            executable='water_node',               # The executable name defined in setup.py or entry_points
            name='water_bot'                       # The name the node will use in the ROS graph
        ),

        # ๐Ÿ—‘๏ธ Launch TrashBot
        Node(
            package='trash_bot',                   # Package where the trash bot's code lives
            executable='trash_node',               # The actual ROS 2 executable for TrashBot
            name='trash_bot'                       # Node name used for logging, namespace, etc.
        ),
    ])

๐Ÿงช Build and Launch It

Step 1: Build Everything

cd ~/robot_town_ws
colcon build
source install/setup.bash

Step 2: Run the Routine

ros2 launch daily_schedule morning_routine.launch.py

๐ŸŒ‡ Expected Output

[INFO] [water_bot]: WaterBot is on duty!
[INFO] [trash_bot]: TrashBot reporting for duty!

[INFO] [water_bot]: ๐ŸŒฑ Watering plants... round 1
[INFO] [trash_bot]: ๐Ÿ—‘๏ธ Taking out trash... trip 1
[INFO] [trash_bot]: ๐Ÿ—‘๏ธ Taking out trash... trip 2
[INFO] [water_bot]: ๐ŸŒฑ Watering plants... round 2
...

๐Ÿ—‚๏ธ Robot Town So Far

Hereโ€™s how your workspace might look now:

robot_town_ws/
โ””โ”€โ”€ src/
    โ”œโ”€โ”€ mayor_bot/
    โ”œโ”€โ”€ door_bot/
    โ”œโ”€โ”€ chatter_bot/
    โ”œโ”€โ”€ weather_bot/
    โ”œโ”€โ”€ umbrella_bot/
    โ”œโ”€โ”€ chef_bot/
    โ”œโ”€โ”€ water_bot/
    โ”œโ”€โ”€ trash_bot/
    โ””โ”€โ”€ daily_schedule/

Each bot lives in its own home, and daily_schedule acts like the Town Planner, coordinating routines.


๐Ÿง  What You Learned

  • ROS2 Timers let bots perform tasks at scheduled intervals.

  • create_timer() is used to register periodic callbacks.

  • Launch files can activate multiple bots together like synchronized clocks.


โœ… Mini Project: Build a Chore Scheduler

Create more bots with timers:

  • ๐Ÿงฝ CleanBot: cleans every 10 sec

  • ๐Ÿš— PatrolBot: patrols every 7 sec

  • ๐Ÿฝ๏ธ ServeBot: serves food every 4 sec

Add them to your morning_routine.launch.py!


๐Ÿ“ฐ Coming Up Next:

Silent Conversations (Actions in ROS2)

Bots will now make requests that take time, and wait for replies โ€” like ordering parts or requesting repairs.


Ready to turn bots into helpful collaborators instead of just time-based task machines?
Letโ€™s build silent action bots in the next issue of Robot Town! ๐Ÿ› ๏ธ๐Ÿค–

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