Wiki
Launch
Existing English translations were reused. 13 current source blocks remain in Simplified Chinese, without triggering paid translation.
1.Overview

The role of rosbag2 is to serialize and store data; it is a database.



ros2 launch 功能包名 launch文件名





ros2 pkg create cpp01_launch --build-type ament_cmake --dependencies rclcpp
ros2 pkg create cpp01_launch --build-type ament_cmake --dependencies rclcpp

from launch import LaunchDescription
from launch_ros.actions import Node
封装终端指令相关类
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
参数声明与获取
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
分组相关
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
事件相关
from launch.event_handlers import OnProcessStart,OnProcessExit
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
获取功能包下share目录或路径
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
return LaunchDescription([])
2.Basic usage workflow of launch(C++)









After configuring this, no matter how many launch files there are in my launch directory, I only need to configure it once.



This indicates that our CMake configuration is correct.


from launch import LaunchDescription
from launch_ros.actions import Node
封装终端指令相关类
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
参数声明与获取
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
分组相关
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
事件相关
from launch.event_handlers import OnProcessStart,OnProcessExit
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
获取功能包下share目录或路径
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
return LaunchDescription([])




<launch>
<node pkg = "turtlesim" exec = "turtlesim_node" name = "t1" />
<node pkg = "turtlesim" exec = "turtlesim_node" name = "t2" />
</launch>


launch:
node:
pkg: "turtlesim"
exec: "turtlesim_node"
name: "t1"
node:
pkg: "turtlesim"
exec: "turtlesim_node"
name: "t2"


It is best to add a dependency.


3.Launch_Python_Node




这个是标签exec_name

The difference between these two parameter passing methods lies in the difference of --ros-args



It is simpler to write in launch.

This is equivalent


The content inside {} is in YAML format.

There is another more commonly used method, which is to read a YAML file at the start, store all data in YAML, and read it directly when needed.


Place the yaml file in config.

ros2 param dump haha --output-dir src/cpp01_launch/config/


CMakeLists also needs to be configured.




If we are going to read, we should read the one under the install directory.


Copy path directly


The code can be further optimized


This is used to obtain the path to the share directory of a certain feature package.



from launch import LaunchDescription
from launch_ros.actions import Node
封装终端指令相关类
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
参数声明与获取
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
分组相关
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
事件相关
from launch.event_handlers import OnProcessStart,OnProcessExit
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
获取功能包下share目录或路径
from ament_index_python.packages import get_package_share_directory
import os
def generate_launch_description():
# turtle1 = Node(
# package="turtlesim",
# executable="turtlesim_node",
# exec_name="my_label",
# ros_arguments=["--remap","__ns:=/t2"]
# )
turtle2 = Node(
package="turtlesim",
executable="turtlesim_node",
name="haha",
parameters=[os.path.join(get_package_share_directory("cpp01_launch"),"config","haha.yaml")]
)
return LaunchDescription([turtle2])


It is recommended to use the third method: dynamically obtain the path.

respawn means automatic restart. In this way, after the node is run, if you use the mouse to close the turtle window, the node will also automatically restart and then open a new turtle window.
Press Ctrl+C to terminate.
from launch import LaunchDescription
from launch_ros.actions import Node
封装终端指令相关类
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
参数声明与获取
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
分组相关
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
事件相关
from launch.event_handlers import OnProcessStart,OnProcessExit
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
获取功能包下share目录或路径
from ament_index_python.packages import get_package_share_directory
import os
def generate_launch_description():
# turtle1 = Node(
# package="turtlesim",
# executable="turtlesim_node",
# exec_name="my_label",
# ros_arguments=["--remap","__ns:=/t2"]
# )
turtle2 = Node(
package="turtlesim",
executable="turtlesim_node",
name="haha",
# parameters=[{"background_r": 255,"background_g": 0,"background_b": 0}],
# parameters=["/home/tungchiahui/mysource/ros2src/4.ws02_tools/install/cpp01_launch/share/cpp01_launch/config/haha.yaml"],
parameters=[os.path.join(get_package_share_directory("cpp01_launch"),"config","haha.yaml")],
respawn=True
)
return LaunchDescription([turtle2])

The first is the original topic name, and the second is the new topic name.
4.Launch_Python_执行命令




This is a list, and the instructions are written inside the list.

This outputs the log both to the terminal and to the log file; if omitted, it will only be output to the log file.

This executes the command as a terminal instruction.


from launch import LaunchDescription
from launch_ros.actions import Node
封装终端指令相关类
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
参数声明与获取
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
分组相关
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
事件相关
from launch.event_handlers import OnProcessStart,OnProcessExit
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
获取功能包下share目录或路径
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
turtle = Node(
package="turtlesim",
executable="turtlesim_node"
)
cmd = ExecuteProcess(
cmd = ["ros2 topic echo /turtle1/pose"],
output = "both",
shell = True
)
return LaunchDescription([turtle,cmd])

If the instruction is too long, it can be split into multiple strings for execution.



This can also run.
from launch import LaunchDescription
from launch_ros.actions import Node
封装终端指令相关类
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
参数声明与获取
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
分组相关
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
事件相关
from launch.event_handlers import OnProcessStart,OnProcessExit
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
获取功能包下share目录或路径
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
turtle = Node(
package="turtlesim",
executable="turtlesim_node"
)
cmd = ExecuteProcess(
cmd = [FindExecutable(name="ros2"),"topic","echo","/turtle1/pose"],
output = "both",
shell = True
)
return LaunchDescription([turtle,cmd])

5.Launch_Python_参数设置








If no value is passed, the turtle's red is at full, so that's a pink background

You can also pass a value, for example, pass backg_r:=0
This makes the background color shift more toward blue-green.



from launch import LaunchDescription
from launch_ros.actions import Node
封装终端指令相关类
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
参数声明与获取
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
分组相关
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
事件相关
from launch.event_handlers import OnProcessStart,OnProcessExit
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
获取功能包下share目录或路径
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
bg_r = DeclareLaunchArgument(name="backg_r",default_value="255")
bg_g = DeclareLaunchArgument(name="backg_g",default_value="255")
bg_b = DeclareLaunchArgument(name="backg_b",default_value="255")
turtle = Node(
package="turtlesim",
executable="turtlesim_node",
parameters=[{"background_r" : LaunchConfiguration("backg_r"),"background_g" : LaunchConfiguration("backg_g"),"background_b" : LaunchConfiguration("backg_b")}]
)
return LaunchDescription([bg_r,bg_g,bg_b,turtle])
6.Launch_Python_文件包含

Suppose I want to write a launch file related to robot startup. In this launch file, I may need to start the lidar, start the IMU, start the chassis, and so on. We need to include all these launch files in the launch file for robot startup.



This value is an object encapsulated by the included launch file.
The class corresponding to this object is PythonLaunchDescriptionSource,
A parameter also needs to be set within the class.

This parameter is launch_file_path the file path.


It is recommended to use this to obtain the path.





You can also pass arguments to it



from launch import LaunchDescription
from launch_ros.actions import Node
封装终端指令相关类
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
参数声明与获取
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
分组相关
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
事件相关
from launch.event_handlers import OnProcessStart,OnProcessExit
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
获取功能包下share目录或路径
from ament_index_python.packages import get_package_share_directory
import os
def generate_launch_description():
include = IncludeLaunchDescription(
launch_description_source=PythonLaunchDescriptionSource(
launch_file_path=os.path.join(
get_package_share_directory("cpp01_launch"),
"launch/py",
"py04_args_launch.py"
)
),
launch_arguments=[("backg_r","80"),("backg_g","10"),("backg_b","200")]
)
return LaunchDescription([include])
7.Launch_Python_分组设置







from launch import LaunchDescription
from launch_ros.actions import Node
封装终端指令相关类
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
参数声明与获取
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
分组相关
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
事件相关
from launch.event_handlers import OnProcessStart,OnProcessExit
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
获取功能包下share目录或路径
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
turtle1 = Node(
package="turtlesim",
executable="turtlesim_node",
name="t1"
)
turtle2 = Node(
package="turtlesim",
executable="turtlesim_node",
name="t2"
)
turtle3 = Node(
package="turtlesim",
executable="turtlesim_node",
name="t3"
)
group1 = GroupAction(actions=[PushRosNamespace("g1"),turtle1,turtle2])
group2 = GroupAction(actions=[PushRosNamespace("g2"),turtle3])
return LaunchDescription([group1,group2])
8.Launch_Python_事件设置


The first is mainly used for registering events, the second is the start event, and the third is node exit.




This is the command to generate a new small turtle in the terminal




The first parameter specifies which event to register for.

target_action is the event source; which node do you want to register the event for?
on_start是等到事件被触发,你要做哪些操作?




from launch import LaunchDescription
from launch_ros.actions import Node
封装终端指令相关类
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
参数声明与获取
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
分组相关
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
事件相关
from launch.event_handlers import OnProcessStart,OnProcessExit
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
获取功能包下share目录或路径
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
turtle = Node(
package="turtlesim",
executable="turtlesim_node",
)
spawn = ExecuteProcess(
cmd=["ros2 service call /spawn turtlesim/srv/Spawn \"{'x': 8.0,'y': 3.0}\""],
output="both",
shell=True
)
event_start = RegisterEventHandler(
event_handler=OnProcessStart(
target_action=turtle,
on_start=spawn
)
)
event_exit = RegisterEventHandler(
event_handler=OnProcessExit(
target_action=turtle,
on_exit=[LogInfo(msg="turtlesim_node:退出!")]
)
)
return LaunchDescription([turtle,event_start,event_exit])

on_exit可以创建一个对象,也可以放在列表里。
You can learn only the Python version of Launch. It is enough to just understand the XML and YAML versions of Launch. When writing your own, write the Python version. When you need to use the Launch of someone else's open-source feature package, if it is in XML or YAML version, it generally does not affect normal use.