在ROS2中,launcher 文件是通过Python构建的,它们的功能是声明用哪些选项或参数来执行哪些程序,可以通过 launcher 文件快速同时启动多个节点。一个 launcher 文件内可以引用另一个 launcher 文件。
使用 launcher 文件 ros2 launch 可以代替 ros2 run 启动包内的程序。
在下面的例子中,我们在一个Python程序中,使用 generate_launch_description() 函数,返回 LaunchDescription 对象。
每一个LaunchDescription 对象包含了一些 action
Nodeaction:运行程序IncludeLaunchDescriptionaction:包含其他launcher文件DeclareLaunchArgumentaction:声明启动器参数SetEnvironmentVariableaction:设置环境变量
使用 launcher 时,需要在 CMakeLists.txt 中添加 install(DIRECTORY launch DESTINATION share/${PROJECT_NAME})
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
pub_cmd=Node(
package='br2_basics',
executable='publisher',
output='screen'
)
sub_cmd=Node(
package='br2_basics',
executable='subscriber_class',
output='screen'
)
ld=LaunchDescription()
ld.add_action(pub_cmd)
ld.add_action(sub_cmd)
还有另一种 launcher 的写法:
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
Node(
package='br2_basics',
executable='publisher',
output='screen'
),
Node(
package='br2_basics',
executable='subscriber_class',
output='screen'
)
])



















