🏗️Welcome to Robot Town!

gayatri kumargayatri kumar
8 min read

🚀 Welcome, City Planner!

Imagine you’re tasked with building Robot Town, a magical city where robots live, work, and talk to each other. But before we see flying drones and wheeled helpers roaming around — we need to:

  • Clear the land (prepare Windows)

  • Lay power lines (install ROS 2)

  • Build the town’s control center (create a workspace)

  • Appoint a mayor (our first robot node: MayorBot)

Let’s get the construction started!


🪟 Step 1: Setting Up Ubuntu in Windows

Before we can build robots, we need a space to work. On Windows, that space is WSL — think of it like building a tiny Linux island inside your Windows computer.

🧠 WSL (Windows Subsystem for Linux) allows you to run Linux programs directly inside Windows — super helpful since ROS 2 works best on Linux.

🛠️ 1.1 Install WSL and Ubuntu 22.04

Open PowerShell as Administrator, then run:

wsl --install -d Ubuntu-22.04

📝 You might need to restart your computer afterward.

Once restarted, launch Ubuntu from your Start Menu.

You’ll be asked to create a username and password — this is for your Linux environment, not Windows.


🧼 Step 2: Clean and Update the System

Inside Ubuntu:

sudo apt update && sudo apt upgrade -y

🧠 Think of this as sweeping the construction site — we want to be sure everything is ready and up to date before we build.

Also install some helpful tools:

sudo apt install curl gnupg lsb-release -y

🤖 Step 3: Installing ROS 2 — Our Power Grid

Now it’s time to install ROS 2 Humble, the framework that lets robots talk, move, sense, and collaborate.

💡 ROS = Robot Operating System
Even though it says "Operating System," it's actually a bunch of tools and libraries that make robot software easier to build.

🔌 3.1 Add the Official ROS 2 Repository

This is like telling our system “Hey! Here’s where to find robot software.”

sudo apt install software-properties-common -y

Then:

sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg

Next, add the repository:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] \
https://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

Update again so Ubuntu knows about the new source:

sudo apt update

📦 3.2 Install ROS 2 Humble Desktop

This will install everything we need — tools, visualizations, simulators, and core libraries:

sudo apt install ros-humble-desktop python3-argcomplete -y

🧠 What’s in the “desktop” version?

  • rclpy (for Python robots!)

  • Visual tools like Rviz2

  • Simulation tools like Gazebo and Turtlesim

  • Core ROS tools and developer support


🧪 Step 4: Test the ROS 2 Installation

You need to “turn on” the ROS environment in each terminal:

source /opt/ros/humble/setup.bash

🧠 This command is like powering up Robot Town. It sets environment variables so commands like ros2 run will work.

Now try:

ros2 --version

You should see something like:

ros2 0.11.x

✅ If so — congrats! Robot Town now has electricity ⚡


📂 Step 5: Creating Your First Workspace

Now that the power grid is live, we need a place to build buildings — that’s your workspace.

📌 5.1 What Is a Workspace?

Think of it as your construction yard.
Every robot, tool, sensor, or simulation you build will live here as a package.

Let’s create it:

mkdir -p ~/robot_town_ws/src
cd ~/robot_town_ws

Here’s how it looks:

robot_town_ws/
└── src/     ← all packages will go here

🛠️ Step 6: Install colcon — Your Construction Manager

sudo apt install python3-colcon-common-extensions python3-pip -y
pip3 install -U argcomplete

🧠 What is colcon?
It’s the tool that builds your workspace — like a construction company that goes through each building blueprint (package) and assembles it.

You’ll run colcon build every time you change your code and want to “rebuild the city.”


🧠 Recap So Far

You’ve:

✅ Installed Linux inside Windows
✅ Installed the full ROS 2 framework
✅ Created a workspace for robot development
✅ Prepared a build system

We’re just about ready to welcome our first robot: MayorBot!


🏗️ Step 7: Creating the MayorBot Package

In Robot Town, packages are like buildings. Each building (package) can contain workers (nodes) that perform tasks.

We’ll now build our first building inside the workspace: the Mayor’s Office — mayor_bot.

Navigate to your workspace's src folder:

cd ~/robot_town_ws/src

Then create the package:

ros2 pkg create --build-type ament_python mayor_bot

You’ll see something like:

creating mayor_bot
creating folders
creating files
...

📁 Now your folder structure looks like this:

javaCopyEditrobot_town_ws/
└── src/
    └── mayor_bot/
        ├── mayor_bot/          ← Python module (code lives here)
        │   └── __init__.py
        ├── package.xml         ← Metadata about the package
        ├── setup.cfg
        ├── setup.py            ← The "entry gate" to your node
        └── resource/
            └── mayor_bot

🧠 Why is there another mayor_bot/ inside the outer one?
The outer folder is the package (think: building), and the inner is the Python module (think: the office space where the mayor sits).


🧠 Let’s Talk About ROS 2 Nodes

A node in ROS 2 is like a single robot worker with a job — like greeting, navigating, or lifting.

We'll create a node inside mayor_bot named greet.py, and this node will simply say hello to anyone who enters Robot Town.


📝 Step 8: Writing Your First ROS 2 Node

Navigate to the Python module folder:

cd ~/robot_town_ws/src/mayor_bot/mayor_bot

Create a file called greet.py:

touch greet.py

Open and edit it with your editor. Here’s what to paste:

# greet.py

# Import the core ROS2 Python library
import rclpy

# Import the Node class to create our own custom ROS2 nodes
from rclpy.node import Node

# 🏛️ Meet the Mayor! This class defines a simple ROS2 node named 'mayor_bot'
class MayorBot(Node):
    def __init__(self):
        # Initialize the Node with the name 'mayor_bot'
        super().__init__('mayor_bot')

        # Use the built-in ROS2 logger to print a greeting message
        self.get_logger().info('🤖 MayorBot: Welcome to Robot Town!')

# 🏁 This is where our ROS2 program begins
def main(args=None):
    # Initialize the ROS2 communication layer
    rclpy.init(args=args)

    # Create an instance of our custom node — the MayorBot is now alive!
    node = MayorBot()

    # Keep the node running, waiting for events (like a mayor attending duties!)
    rclpy.spin(node)

    # Once done (e.g., program interrupted), cleanly shut down the node
    node.destroy_node()

    # Shut down the ROS2 client library
    rclpy.shutdown()

🔍 Let's Break That Down

LineWhat It Means
rclpy.init()Powers on the ROS system for this node
MayorBot(Node)We’re creating a robot personality — a subclass of Node
get_logger().info(...)Prints a message to the screen, robot-style
rclpy.spin(node)Keeps the node running like a heartbeat until you stop it
rclpy.shutdown()Gracefully shuts down ROS when the node is stopped

🧠 This is our MayorBot greeting visitors. He's not doing much yet — but he’s alive, and that’s a big deal!


🔗 Step 9: Declare the Entry Point

ROS2 doesn’t just “know” about your Python files. You must tell it which file can be run as an executable.

Open setup.py in mayor_bot and edit it like this:

from setuptools import setup

package_name = 'mayor_bot'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='yourname',
    maintainer_email='you@example.com',
    description='MayorBot greets visitors in Robot Town.',
    license='MIT',
    entry_points={
        'console_scripts': [
            'greet = mayor_bot.greet:main'
        ],
    },
)

🧠 This entry_points thing is like registering a robot with the town’s electricity grid. Now ros2 run mayor_bot greet will know where to look.


🧱 Step 10: Building the City (colcon build)

Now go back to the root of your workspace:

cd ~/robot_town_ws

Then:

colcon build

🧠 What is colcon build doing?
It walks through every package in src/, checks its blueprint (like setup.py), and compiles or links things so they can run properly.

Once done, source your workspace:

source install/setup.bash

🧠 Why source?
It tells your terminal, "Hey! Use the freshly built robot packages in this workspace now."

Without it, the terminal won’t know where to find your greet executable.


👋 Step 11: Run Your First Node!

You're ready:

ros2 run mayor_bot greet

You should see:

[INFO] [mayor_bot]: 🤖 MayorBot: Welcome to Robot Town!

🎉 That’s it! You’ve built your first robot program in Python with ROS 2.


🧩 Bonus: What If You Have Multiple Packages?

Let’s say you add pizza_bot and cleaner_bot. You’ll just:

  • Add them inside the same src/ folder

  • Run colcon build again

  • Source again

  • Run each with ros2 run pizza_bot deliver, etc.

All packages live together peacefully in the same workspace. But if you're working on unrelated robot projects, you might keep separate workspaces to avoid clutter.


🧠 Quick Recap

ConceptReal-World Analogy
NodeA worker robot in the city
PackageA building housing robot workers
WorkspaceThe entire Robot Town
colcon buildConstruction company building everything
sourcePowering on the town after construction
Entry PointRegistering the robot with city hall

🎯 Mini Project: Add a GreeterBot

  • Clone your mayor_bot package and rename it greeter_bot.

  • Make it say something new every time.

  • Bonus: Use Python’s random.choice() to greet with different messages!

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