π§ Talking Bots!

Table of contents
- π Welcome back to Robot Town!
- π¨οΈ What is a Node?
- ποΈ Project Setup: Letβs Build the Gossip System
- π§± Step 1: Create a New Package for ChatterBot
- π Step 2: Write the Gossip Node
- π§ Code Breakdown (Plain English)
- βοΈ Step 3: Add an Entry Point
- π¨ Step 4: Build the Package
- π§ͺ Step 5: Source and Run
- π§ Folder Recap
- ποΈ Building the Second House: GreeterBot
- π Create the GreeterBot Node
- π Add Entry Point in setup.py
- π¨ Rebuild Everything
- π§ͺ Run Both Bots Together
- π§ What Did We Just Learn?
- ποΈ Final Workspace Recap
- ποΈ Up Next: The Town Radio System (Topics & Publishing)

π Welcome back to Robot Town!
Weβve built homes (packages) for our robots, and we gave them basic functions. But so far, our robots have been silent.
Itβs time they got a voice!
In todayβs adventure, weβll install the walkie-talkie system in our robot homes.
π¨οΈ What is a Node?
Think of a Node as a robotβs communication center β like a walkie-talkie that lets it say things (and later, listen too).
In ROS2:
A node is a unit of execution.
Itβs a Python program that runs inside a package.
It has a name (like
chatter_bot
) and it can publish, subscribe, and more.Today we only focus on talking, not listening.
ποΈ Project Setup: Letβs Build the Gossip System
π¬ Objective:
We will make one robot named ChatterBot who keeps saying something every second β just like a town gossip.
π§± Step 1: Create a New Package for ChatterBot
Navigate to your workspace:
cd ~/robot_town_ws/src
Create a new ROS2 Python package:
ros2 pkg create --build-type ament_python chatter_bot --dependencies rclpy
This creates:
robot_town_ws/
βββ src/
βββ chatter_bot/ β New package
βββ chatter_bot/ β Python module folder
β βββ __init__.py
βββ package.xml
βββ setup.py
π¦ Why two chatter_bot folders?
The outer one is the package.
The inner one is the Python module where our code lives.
Think of the outer one as the house and the inner one as the rooms inside.
π Step 2: Write the Gossip Node
Create a new file inside the inner chatter_bot/
folder:
touch ~/robot_town_ws/src/chatter_bot/chatter_bot/gossip.py
Paste this code:
import rclpy
from rclpy.node import Node
# π£οΈ ChatterBot is a friendly node that keeps talking every second,
# counting up its "gossip" messages like a lively townsperson.
class ChatterBot(Node):
def __init__(self):
# Initialize the node with the name 'chatter_bot'
super().__init__('chatter_bot')
# Initialize a counter to keep track of how many times we've gossiped
self.counter = 0
# π Create a timer that triggers the gossip method every 1 second
# create_timer(interval_sec, callback) sets up a periodic callback
self.timer = self.create_timer(1.0, self.gossip)
def gossip(self):
# π Use the node's built-in logger to print an informational message
# get_logger() provides the logger interface for this node
# info() logs a message at the "info" severity level,
# which is typically shown on the console by default
logger = self.get_logger().info(f"ChatterBot says: Gossip number {self.counter}")
# Increment the counter for the next gossip message
self.counter += 1
# π Program entry point
def main(args=None):
# Initialize ROS2 communication for this Python program
rclpy.init(args=args)
# Create an instance of ChatterBot
node = ChatterBot()
# Keep the node alive, listening and processing callbacks like timers
rclpy.spin(node)
# Clean up after the node is stopped
node.destroy_node()
rclpy.shutdown()
# Only run main() if this script is executed directly (not imported)
if __name__ == '__main__':
main()
π§ Code Breakdown (Plain English)
Code Line | Meaning |
class ChatterBot(Node) | We are creating a robot with communication powers. |
create_timer(1.0, self.gossip) | This schedules the robot to speak every 1 second. |
self.get_logger().info(...) | This prints to the terminal β like a robot speaking out loud. |
rclpy.spin() | Keeps the node alive forever β like switching the bot βonβ. |
ποΈ Think of the
gossip()
function as your robot speaking into a walkie-talkie.
βοΈ Step 3: Add an Entry Point
Open your setup.py
file and edit the entry_points
section:
entry_points={
'console_scripts': [
'gossip = chatter_bot.gossip:main',
],
},
Now ROS2 knows that when you run gossip
, it should run the main()
in gossip.py
.
π¨ Step 4: Build the Package
Navigate to the root of your workspace:
cd ~/robot_town_ws
colcon build
π§ͺ Step 5: Source and Run
Every time you open a new terminal, you need to run:
source install/setup.bash
This sets up the entry points and environment for ROS2.
Then run your robot:
ros2 run chatter_bot gossip
You should see output like:
[INFO] [chatter_bot]: ChatterBot says: Gossip number 0
[INFO] [chatter_bot]: ChatterBot says: Gossip number 1
[INFO] [chatter_bot]: ChatterBot says: Gossip number 2
...
π Congratulations! Your robot is officially TALKING!
π§ Folder Recap
robot_town_ws/
βββ src/
β βββ chatter_bot/
β βββ chatter_bot/
β β βββ gossip.py β Our talking bot
β βββ setup.py
β βββ package.xml
β βββ ...
βββ install/
βββ ... (generated after build)
ποΈ Building the Second House: GreeterBot
Weβll keep our previous packages:
mayor_bot
: The robot mayor from Article 1door_bot
: The door-opening bot from Article 2chatter_bot
: Our talkative friend from Part 1
Letβs now add a fourth house:
cd ~/robot_town_ws/src
ros2 pkg create --build-type ament_python greeter_bot --dependencies rclpy
Your updated workspace structure:
robot_town_ws/
βββ src/
βββ mayor_bot/
βββ door_bot/
βββ chatter_bot/
β βββ chatter_bot/
β βββ gossip.py
βββ greeter_bot/
βββ greeter_bot/
π Create the GreeterBot Node
Letβs give greeter_bot
a voice!
Create a file:
touch ~/robot_town_ws/src/greeter_bot/greeter_bot/greet.py
Add this code:
import rclpy
from rclpy.node import Node
class GreeterBot(Node):
def __init__(self):
super().__init__('greeter_bot')
self.timer = self.create_timer(2.0, self.say_hi) # Speak every 2 seconds
def say_hi(self):
self.get_logger().info("GreeterBot waves: Welcome to Robot Town!")
def main(args=None):
rclpy.init(args=args)
node = GreeterBot()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
π Add Entry Point in setup.py
In greeter_bot/
setup.py
, update the entry points:
entry_points={
'console_scripts': [
'greet = greeter_bot.greet:main',
],
},
This tells ROS2: βHey, when someone runs
ros2 run greeter_bot greet
, launch thegreet.py
file!β
π¨ Rebuild Everything
Youβve added a new package. Time to build the entire town again:
cd ~/robot_town_ws
colcon build
After the build completes:
source install/setup.bash
π§ͺ Run Both Bots Together
Open two terminals, and in each:
- Source the workspace:
cd ~/robot_town_ws
source install/setup.bash
- In terminal 1:
ros2 run chatter_bot gossip
- In terminal 2:
ros2 run greeter_bot greet
You should now see:
[INFO] [chatter_bot]: ChatterBot says: βI heard DoorBot opened again!β
[INFO] [chatter_bot]: ChatterBot says: βMayorBot tripped on his welcome mat!β
[INFO] [greeter_bot]: GreeterBot waves: Welcome to Robot Town!
[INFO] [chatter_bot]: ChatterBot says: βDoorBot and GreeterBot are up to something.β
...
π Success! Two bots in different homes, speaking independently!
π§ What Did We Just Learn?
Concept | What It Means |
Package | A robotβs home β contains its code |
Node | A communication unit β like a walkie-talkie |
Entry point | A shortcut command to run a Python script |
Multiple Nodes | Each bot is its own process β can speak in parallel |
Timer | Allows repeating actions at a fixed interval |
ποΈ Final Workspace Recap
robot_town_ws/
βββ src/
βββ mayor_bot/
β βββ mayor_bot/
β βββ greet.py
βββ door_bot/
β βββ door_bot/
β βββ open.py
βββ chatter_bot/
β βββ chatter_bot/
β βββ gossip.py
βββ greeter_bot/
βββ greeter_bot/
βββ greet.py
π Each of these packages has a bot with its own role in the town.
ποΈ Up Next: The Town Radio System (Topics & Publishing)
Imagine the town square filled with lively announcements that everyone can hear! In this next article, we'll set up our own public radio frequency using ROS2 Topics and Publishing.
Meet WeatherBot, your friendly weather announcer, broadcasting temperature updates every few seconds. Get ready to bring Robot Townβs news to life! π€οΈπ‘
But before we dive into that, itβs worth pausing and letting the idea of independent talkative nodes sink in.
Subscribe to my newsletter
Read articles from gayatri kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by