ποΈHome Directory Magic

Table of contents
- π§± Why Do We Need a Workspace?
- ποΈ Real-Life Analogy
- ποΈ ROS2 Workspace Layout
- π¦ Creating a Clean Workspace
- π§ What's in a Package?
- π οΈ colcon build β Your Town Assembler
- π§ββοΈ setup.bash β The Town Spellbook
- π¦ Multiple Packages in One Workspace
- π Can I Have Multiple Workspaces?
- π§ TL;DR Summary
- π¦ Recap: Where We Left Off
- π οΈ Step 1: TimeBot Code (Logging Current Time)
- π Step 2: Update setup.py
- ποΈ Step 3: Build the Entire Town
- π€ Step 4: Run TimeBot!
- π Folder Review: What Happened After Build?
- π Adding More Bots
- π§ͺ Mini Project Challenge: Master Workspace Organizer
- π§ TL;DR Recap
- π Whatβs Next: Powering Bots Up Together (Launch Files)

π§± Why Do We Need a Workspace?
Think of our robotsβMayorBot, DoorBot, ChatterBot... Imagine they each live somewhere in Robot Town. But where do their files live?
Just like City Hall maintains a master cabinet where each robot has its folder, ROS2 uses a workspace to group, build, and run these bots as a team.
ποΈ Real-Life Analogy
Workspace = Filing Cabinet in City Hall ποΈ
Package = One folder for a citizen robot (like a record for MayorBot)
Source Folder = A shelf inside the cabinet where all robots' files are kept
Build & Install Folders = Assembly and action stations for robot teams
ποΈ ROS2 Workspace Layout
Here's how our current town is laid out in your system:
robot_town_ws/ β Filing cabinet (Workspace)
βββ src/ β Blueprints and designs (Source code of packages)
β βββ mayor_bot/
β βββ door_bot/
β βββ chatter_bot/
β βββ weather_bot/
β βββ umbrella_bot/
β βββ mood_bot/
β βββ guard_bot/
βββ build/ β Workshop where bots are assembled
βββ install/ β Deployment shelf (ready-to-run bots)
βββ log/ β All logs and construction records
This folder is created automatically when you run
colcon build
. You only have to create thesrc/
manually!
π¦ Creating a Clean Workspace
Letβs walk through building a brand-new City Hall!
β Step 1: Create the cabinet (workspace)
mkdir -p ~/robot_town_ws/src
cd ~/robot_town_ws
β Step 2: Add a citizen (package)
cd ~/robot_town_ws/src
ros2 pkg create --build-type ament_python --node-name time_bot time_bot
You now have a folder
time_bot
insidesrc/
.
π§ What's in a Package?
Letβs open the time_bot folder and peek inside:
time_bot/
βββ package.xml β Declaration: What this bot/package is
βββ setup.py β Installation script
βββ resource/
β βββ time_bot β Empty file needed for ament indexing
βββ time_bot/
βββ __init__.py β Where weβll write the botβs code
Letβs rename __init__.py
to something meaningful, like clock.py
, and write a quick script in Part 2.
π οΈ colcon build β Your Town Assembler
When you run this:
cd ~/robot_town_ws
colcon build
ROS2 will:
Visit each package in
src/
Build and assemble them into
build/
andinstall/
Update launch paths and executables
This is like printing official ID cards and building instructions for each bot!
π§ββοΈ setup.bash β The Town Spellbook
Running:
source install/setup.bash
...tells the terminal:
βHey! I live in Robot Town now. I know the paths to all my bots.β
If you skip this, your shell will forget where to find ros2 run chatter_bot speak
.
Always source
setup.bash
after building or when opening a new terminal window.
π¦ Multiple Packages in One Workspace
You can have as many bots/packages as you want inside one workspace:
robot_town_ws/
βββ src/
βββ mayor_bot/
βββ door_bot/
βββ new_bot/
This keeps everything tidy and lets you build or run bots together.
π Can I Have Multiple Workspaces?
Yes! Each workspace is a separate cabinet.
Example:
~/robot_town_ws/ β Your main workspace
~/robot_lab_ws/ β For experiments
~/robot_sandbox_ws/ β For messy temporary bots
Each time, just:
cd ~/robot_lab_ws
source install/setup.bash
That activates the workspace youβre in. You can chain them too β weβll explore that later!
π§ TL;DR Summary
Concept | Meaning (Robot Town) |
Workspace | City Hall's filing cabinet |
src/ | Where each robot's files live |
build/ | Workshop for compiling stuff |
install/ | Deployment zone (ready-to-run) |
setup.bash | The spell that tells terminal where bots are |
π¦ Recap: Where We Left Off
We created a workspace: robot_town_ws/
, and added several bots (packages) into its src/
folder:
robot_town_ws/
βββ src/
βββ mayor_bot/
βββ door_bot/
βββ chatter_bot/
βββ weather_bot/
βββ umbrella_bot/
βββ mood_bot/
βββ guard_bot/
βββ time_bot/ β Our new addition
Letβs now:
Write the TimeBot code
Build the entire town again
Test everything is in place
Understand how this scales with more bots
π οΈ Step 1: TimeBot Code (Logging Current Time)
Go to the file robot_town_ws/src/time_bot/time_bot/
clock.py
touch robot_town_ws/src/time_bot/time_bot/clock.py
chmod +x robot_town_ws/src/time_bot/time_bot/clock.py
Paste this into clock.py
:
import rclpy
from rclpy.node import Node
from datetime import datetime
class TimeBot(Node):
def __init__(self):
super().__init__('time_bot')
self.create_timer(2.0, self.tell_time) # Every 2 seconds
def tell_time(self):
now = datetime.now().strftime("%H:%M:%S")
self.get_logger().info(f"The time is {now}")
def main(args=None):
rclpy.init(args=args)
node = TimeBot()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
β° This bot announces the current time every 2 seconds. Think of it as the town's official clock tower ringing!
π Step 2: Update setup.py
Go to: robot_town_ws/src/time_bot/
setup.py
Update the entry_points
section like this:
entry_points={
'console_scripts': [
'clock = time_bot.clock:main',
],
},
This tells ROS2: "If someone runs
ros2 run time_bot clock
, start themain()
function inclock.py
."
ποΈ Step 3: Build the Entire Town
Now weβll use colcon to build all bots at once!
cd ~/robot_town_ws
colcon build
Youβll see something like:
Starting >>> time_bot
Finished <<< time_bot
...
Summary: 8 packages finished
Now activate the workspace:
source install/setup.bash
β Don't forget: every new terminal needs this line or your bots wonβt be found.
π€ Step 4: Run TimeBot!
ros2 run time_bot clock
Output:
[INFO] [time_bot]: The time is 15:12:03
[INFO] [time_bot]: The time is 15:12:05
π Your City Hall clock is ticking!
π Folder Review: What Happened After Build?
After colcon build
, 3 new folders appeared:
robot_town_ws/
βββ build/ β Temporary compiled files
βββ install/ β Installed packages, executables, paths
βββ log/ β Logs of build actions
These are like:
π οΈ build/ β The robot parts
π¦ install/ β Fully assembled, ready-to-run bots
π log/ β Diary of the assembly process
π Adding More Bots
Letβs say you want to add a new bot: music_bot
You just:
cd ~/robot_town_ws/src
ros2 pkg create --build-type ament_python --node-name sing music_bot
...write the code, update setup.py
, then:
cd ~/robot_town_ws
colcon build
source install/setup.bash
No need to create a new workspace β just drop it in
src/
and build again!
π§ͺ Mini Project Challenge: Master Workspace Organizer
Your task:
Organize all bots (MayorBot, ChatterBot, etc.) inside one workspace
Run any bot from this unified setup using
ros2 run
Ensure each has a clean folder and setup
Bonus:
- Create a bot like
cleaning_bot
that logs every 10 seconds:"Checking cabinet integrity..."
π§ TL;DR Recap
Concept | What it Means in Robot Town |
src/ | All robot folders (packages) |
colcon build | Assembles every robot |
setup.bash | Activates your workspace |
entry_points | Defines run commands for each bot |
One workspace | Can hold many packages |
π Whatβs Next: Powering Bots Up Together (Launch Files)
Imagine waking up all the bots in Robot Town with a single sunrise alarm! Next, weβll learn how to start multiple bots at once using launch files.
With one command, youβll launch bots like TrafficBot and LightBot togetherβmaking Robot Town buzz with activity! π π€β‘
Get ready to make your robots work together
Subscribe to my newsletter
Read articles from gayatri kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by