Launching multiple nodes lets you start several parts of a robot program at once. This helps different robot functions work together smoothly.
Launching multiple nodes in ROS
ros2 launch package_name file.launch.py
The ros2 launch command runs a launch file that can start many nodes.
Launch files are Python scripts that list nodes and their settings.
<launch> <node pkg="turtlesim" type="turtlesim_node" name="sim"/> <node pkg="turtlesim" type="turtle_teleop_key" name="teleop"/> </launch>
my_robot package, showing their output on the screen.<launch> <node pkg="my_robot" type="sensor_node" name="sensor" output="screen"/> <node pkg="my_robot" type="motor_node" name="motor" output="screen"/> </launch>
This launch file starts two demo nodes: one that sends messages (talker) and one that receives them (listener). Both print output to the screen.
<launch> <node pkg="demo_nodes_cpp" type="talker" name="talker" output="screen"/> <node pkg="demo_nodes_cpp" type="listener" name="listener" output="screen"/> </launch>
Make sure all nodes you launch are installed and built correctly.
You can add parameters and remappings inside the launch file for more control.
The output="screen" attribute shows node outputs in your terminal.
Launch files let you start many ROS nodes together easily.
Use ros2 launch with a launch file to run multiple nodes.
Launch files are XML and list nodes with their package, type, and name.
Practice
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 CQuick Check:
Launch file = start multiple nodes [OK]
- Thinking launch files write node code
- Confusing launch files with debugging tools
- Assuming launch files stop nodes
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 AQuick Check:
Node tag = <node> [OK]
- Using <launch> or <start> instead of <node>
- Forgetting to close the tag properly
- Mixing up attribute names
<launch> <node pkg="turtlesim" type="turtlesim_node" name="sim1" /> <node pkg="turtlesim" type="turtlesim_node" name="sim2" /> </launch>
What happens when you run
roslaunch with this file?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 DQuick Check:
Unique node names = both run [OK]
- Assuming same package means one node only
- Thinking duplicate names cause silent errors
- Believing launch files always cause syntax errors
<launch> <node pkg="demo_pkg" type="demo_node" name="node1"> </launch>
What is the error and how to fix it?
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 AQuick Check:
Unclosed node tag = add </node> [OK]
- Confusing 'type' with 'exec' attribute
- Ignoring missing closing tags
- Assuming launch tag is not closed
<launch> <node pkg="pkg1" type="node1" name="node1" /> <node pkg="pkg2" type="node2" name="node2" /> <node pkg="pkg3" type="node3" name="node3" /> </launch>
How can you modify this launch file to add a parameter
use_sim_time set to true for all nodes?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 BQuick Check:
Global param inside <launch> = all nodes get it [OK]
- Adding param inside each node causing repetition
- Using <arg> or <env> instead of <param>
- Placing param outside launch tag
Ready to prove it?
Pass the test and this pattern moves unknown
