Introduction
Hello, this is Prompt Rabbit.
I’ve been steadily studying ROS2, and this time I finally tried my hand at Launch files.
Launching nodes one by one is a hassle, so I thought:
“Wouldn’t it be nice to start them all at once?”
That’s exactly what Launch files are for.
A Handy Environment Trick
Up until now, every time I ran a node I typed:
cd ~/ros2_ws
source install/setup.bash
But ChatGPT suggested:
“Just add it to your .bashrc to make life easier.”
Run this once:
echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc
Then reload:
source ~/.bashrc
Now, every time I open a terminal, ros2 run works right away.
It’s a small thing, but it really makes daily work smoother.
Launch File: One Command to Start Them All
This time, the goal was to launch these three at once:
My custom nodes (circle_motion, mixer)
Official node (turtlesim_node)
👉 Note: nodes that need keyboard input should be run separately.
Target Directory Structure
ros2_ws/
└── src/
└── my_robot_teleop/
├── package.xml
├── setup.py
├── resource/
│ └── my_robot_teleop ← empty file
├── my_robot_teleop/
│ ├── __init__.py
│ ├── teleop_keyboard.py
│ ├── circle_motion.py
│ └── mixer.py
├── launch/
│ └── turtlesim_spiral.launch.py
└── setup.cfg (if exists)
Steps
1. Create a launch folder
cd ~/ros2_ws/src/my_robot_teleop/
mkdir launch
2. Create a new file
~/ros2_ws/src/my_robot_teleop/launch/turtlesim_spiral.launch.py
File contents:
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
Node(
package='my_robot_teleop',
executable='circle_motion',
name='circle'
),
Node(
package='my_robot_teleop',
executable='mixer',
name='mixer'
),
Node(
package='turtlesim',
executable='turtlesim_node',
name='sim'
),
])
3. Edit setup.py
entry_points={
'console_scripts': [
'teleop_keyboard = my_robot_teleop.teleop_keyboard:main',
'circle_motion = my_robot_teleop.circle_motion:main',
'mixer = my_robot_teleop.mixer:main',
],
}
Add launch files to data_files:
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
(os.path.join('share', package_name, 'launch'), glob('launch/*.launch.py')),
],
4. Add required empty resource file
cd ~/ros2_ws/src/my_robot_teleop
mkdir -p resource
touch resource/my_robot_teleop
5. Rebuild
cd ~/ros2_ws
colcon build
source install/setup.bash
6. Run the launch file
ros2 launch my_robot_teleop turtlesim_spiral.launch.py
7. Confirm
ls install/my_robot_teleop/share/my_robot_teleop/launch
If you see turtlesim_spiral.launch.py, it worked!
8. Keyboard input is launched separately (Repeated for emphasis 😉)
source ~/ros2_ws/install/setup.bash
ros2 run my_robot_teleop teleop_keyboard
And that’s it—you’re done! 🎉
Thoughts After Trying It
Honestly… this was pretty tough for a beginner 😅
But being able to manage multiple nodes together is definitely going to be useful later.
Using this TurtleSim practice as a stepping stone, I’ll try another simulation next.
Summary
Add setup sourcing to .bashrc → much more convenient
Launch files → start multiple nodes at once
Hard for beginners, but a skill that pays off
Studying ROS2 is like moving at a turtle’s pace. 🐢
But before I know it—I’ve made progress! ✨