Welcome to Jetson AGX Orin repository
当製品の日本語版マニュアル資料は、こちらからご取得ください。
19.ROS2 Launch startup file configuration
19. ROS2 Launch startup file configuration
1. Introduction to Launch
Until now, every time we launched a ROS node, we had to open a new terminal and run a command. With so many nodes in a robotic system, doing this every time is cumbersome. Is there a way to launch all nodes at once? The answer is, of course, a launch file, a script that launches and configures multiple nodes in the ROS system.
In ROS2, launch is used to launch multiple nodes and configure program parameters. ROS2 launch files are available in XML, YAML, and Python formats. This lesson uses a Python launch file as an example. Compared to the other two formats, the Python format is more flexible:
- Python has numerous function libraries that can be used in launch files;
- ROS2 general and specific launch features are written in Python, allowing access to launch features that may not be exposed in XML and YAML;
The key to writing ROS2 launch files in Python is to abstract each node, file, script, etc. into an action, launching them using a unified interface.
References:
- Launch System Design Document: ROS 2 Launch System
- Official Launch API Documentation: Architecture of launch — launch 0.4.0 documentation
- Preparation: Create a package to store program files
xxxxxxxxxxros2 pkg create learn_launch --build-type ament_python2. Writing a Single Node Launch Program
2.1. Creating a Launch File
Create a launch folder under the package, then create a file called [single_node_launch.py] within the launch folder. Copy the following content into the file:
xxxxxxxxxxfrom launch import LaunchDescriptionfrom launch_ros.actions import Nodedef generate_launch_description(): node = Node( package='pkg_helloworld_py', executable='helloworld', output='screen' ) return LaunchDescription([node])
2.2 Configuring the setup.py File
The launch file is often named "LaunchName_launch.py." LaunchName is customizable, while _launch.py is considered fixed. You need to modify the setup.py file in the package to add the files in the launch path and compile to generate the executable .py file.
xxxxxxxxxx#1. Import related header filesimport osfrom glob import glob#2. In the data_files list, add the launch path and the launch.py ••file under the path(os.path.join('share',package_name,'launch'),glob(os.path.join('launch','*launch.py')))
2.3. Compile the package
xxxxxxxxxxcolcon build --packages-select learn_launch
2.4. Run the program
- Refresh the environment variables and run the launch file
xxxxxxxxxxros2 launch learn_launch single_node_launch.py
2.5. Source Code Analysis
- Import related libraries
xxxxxxxxxxfrom launch import LaunchDescriptionfrom launch_ros.actions import Node
- Define a function called generate_launch_description and return a launch_description.
xxxxxxxxxxdef generate_launch_description(): node = Node( package='pkg_helloworld_py', executable='helloworld', ) return LaunchDescription([node])We define a variable called node as the return value of a node startup. We then call the Node function with two important parameters: package and executable.
- package: represents the package name.
- executable: represents the executable program name.
Finally, we call the LaunchDescription function, passing in the node parameter, and execute the function.
xxxxxxxxxxreturn LaunchDescription([node])
3. Writing a Launch Program for Multiple Nodes
3.1. Creating a Launch File
Create a new file called [multi_node_launch.py] and add the following content:
xxxxxxxxxxfrom launch import LaunchDescriptionfrom launch_ros.actions import Nodedef generate_launch_description(): publisher_node = Node( package='pkg_topic', executable='publisher_demo', output='screen' ) subscriber_node = Node( package='pkg_topic', executable='subscriber_demo', output='screen' ) return LaunchDescription([ publisher_node, subscriber_node ])
3.2. Compile the package
xxxxxxxxxxcolcon build --packages-select learn_launch
3.3. Run the program
- Refresh the environment variables and run the launch file.
xxxxxxxxxxros2 launch learn_launch multi_nod