📖 Scenario: You are building a simple robot system using ROS (Robot Operating System). You want to start two nodes: one node to publish sensor data and another node to process that data.
🎯 Goal: Create a ROS launch file that starts two nodes: sensor_publisher and data_processor. This will help you run both parts of your robot system together easily.
📋 What You'll Learn
Create a launch file named multi_node.launch.py
Define two nodes inside the launch file: sensor_publisher and data_processor
Use the launch_ros.actions.Node class to define each node
Set the package name to my_robot_package for both nodes
Set the executable names exactly as sensor_publisher and data_processor
Launch both nodes together when running the launch file
💡 Why This Matters
🌍 Real World
Robots often have many parts working together. Launch files help start all these parts easily with one command.
💼 Career
Knowing how to write ROS launch files is essential for robotics developers to manage complex robot systems efficiently.
Progress0 / 4 steps
1
Create the launch file and import modules
Create a new Python launch file named multi_node.launch.py. Import launch and launch_ros.actions.Node at the top.
ROS
Hint
Use import launch and from launch_ros.actions import Node to prepare your launch file.
2
Define the launch description function
Define a function named generate_launch_description() that returns a launch.LaunchDescription object. Inside it, create an empty list named nodes.
ROS
Hint
Define generate_launch_description() and return launch.LaunchDescription(nodes).
3
Add the sensor_publisher node to the launch description
Inside generate_launch_description(), add a Node for sensor_publisher to the nodes list. Use package='my_robot_package' and executable='sensor_publisher'.
ROS
Hint
Create a Node for sensor_publisher and add it to nodes.
4
Add the data_processor node and complete the launch file
Add another Node for data_processor with package='my_robot_package' and executable='data_processor'. Append it to nodes before returning the launch description.
ROS
Hint
Create and append the data_processor node just like the sensor_publisher node.
Practice
(1/5)
1. What is the main purpose of a ROS launch file when working with multiple nodes?
easy
A. To debug a single ROS node
B. To write the source code of ROS nodes
C. To start several ROS nodes together with one command
D. To stop all running ROS nodes automatically
Solution
Step 1: Understand the role of launch files and compare options
Launch files are used to start multiple ROS nodes at once, simplifying the process. Only "To start several ROS nodes together with one command" correctly describes starting multiple nodes together; others describe unrelated tasks.
Final Answer:
To start several ROS nodes together with one command -> Option C
Quick Check:
Launch file = start multiple nodes [OK]
Hint: Launch files group nodes to run with one command [OK]
Common Mistakes:
Thinking launch files write node code
Confusing launch files with debugging tools
Assuming launch files stop nodes
2. Which of the following is the correct XML tag to define a node in a ROS launch file?
easy
A.
B.
C.
D.
Solution
Step 1: Recall ROS launch file syntax and check options
The correct tag to start a node is <node> with attributes pkg, type, and name. Only <node pkg="package_name" type="node_type" name="node_name" /> uses the correct <node> tag; others use invalid tags like <launch>, <start>, <run>.
Final Answer:
<node pkg="package_name" type="node_type" name="node_name" /> -> Option A
Quick Check:
Node tag = <node> [OK]
Hint: Use <node> tag with pkg, type, and name attributes [OK]
What happens when you run roslaunch with this file?
medium
A. The launch file causes a syntax error and no nodes start
B. Only one turtlesim node starts named sim1
C. Two nodes start but both have the same name sim1 causing a conflict
D. Two turtlesim nodes named sim1 and sim2 start simultaneously
Solution
Step 1: Analyze node definitions and ROS naming rules
Two nodes are defined with different names: sim1 and sim2, both from turtlesim package. Nodes with unique names can run simultaneously without conflict.
Final Answer:
Two turtlesim nodes named sim1 and sim2 start simultaneously -> Option D
Quick Check:
Unique node names = both run [OK]
Hint: Different node names allow multiple nodes to run [OK]
A. Missing closing tag for <node>, fix by adding </node>
B. Wrong attribute name 'type', fix by changing to 'exec'
C. Missing package name, fix by adding pkg attribute
D. Extra closing tag needed for <launch>, fix by adding </launch>
Solution
Step 1: Check XML tag completeness and verify attributes
The <node> tag is opened but not closed properly; it needs a closing </node> tag. Attributes pkg and type are correct; <launch> tag is closed properly.
Final Answer:
Missing closing tag for <node>, fix by adding </node> -> Option A
Quick Check:
Unclosed node tag = add </node> [OK]
Hint: Every opened tag must be closed properly [OK]
Common Mistakes:
Confusing 'type' with 'exec' attribute
Ignoring missing closing tags
Assuming launch tag is not closed
5. You want to launch three nodes from different packages in one launch file:
How can you modify this launch file to add a parameter use_sim_time set to true for all nodes?
hard
A. Add <param name="use_sim_time" value="true" /> inside each <node> tag
B. Add <param name="use_sim_time" value="true" /> directly inside <launch> tag
C. Add <arg name="use_sim_time" default="true" /> inside each <node> tag
D. Add <env name="use_sim_time" value="true" /> inside each <node> tag
Solution
Step 1: Understand parameter scope in launch files and compare options
Parameters placed directly inside <launch> apply globally to all nodes. Adding <param name="use_sim_time" value="true" /> directly inside <launch> sets it once for all; inside each <node> repeats unnecessarily; options B and C use wrong tags (<env>, <arg>).
Final Answer:
Add <param name="use_sim_time" value="true" /> directly inside <launch> tag -> Option B
Quick Check:
Global param inside <launch> = all nodes get it [OK]
Hint: Put common params inside <launch> for all nodes [OK]
Common Mistakes:
Adding param inside each node causing repetition
Using <arg> or <env> instead of <param>
Placing param outside launch tag
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.