【launch】

【launch】

※前置代码:https://www.cnblogs.com/whiteink/articles/18613483
可使用Python、XML、YAML编写
本教材为Python

使用launch启动多个节点

launch文件夹->demo.launch.py文件

import launch
import launch_ros

"""
launch工具在运行脚本时,会搜索名称为generate_launch_description的函数
->内置函数但是要编写
"""

def generate_launch_description():
                               #创建launch_ros.actions.Node类
    action_node_turtle_control = launch_ros.actions.Node(
	#package 指定功能包名称
	#executable 可执行文件名称
	#output 指定日志输出位置
        package='demo_cpp_service',
        executable='turtle_control',
        output='screen', # 屏幕
    )
    action_node_patrol_client = launch_ros.actions.Node(
        package='demo_cpp_service',
        executable='patrol_client',
        output='log', # 日志
    )
    action_node_turtlesim_node = launch_ros.actions.Node(
        package='turtlesim',
        executable='turtlesim_node',
        output='both', # 前面两者(好智能啊(感慨
    )
	                    #创建启动描述对象launch_description
    launch_description = launch.LaunchDescription({
        action_node_turtle_control,
        action_node_patrol_client,
        action_node_turtlesim_node
    })
    return launch_description
	#launch工具在拿到启动描述对象后,根据其内容完成启动

CMakeList.txt添加内容

install(DIRECTORY launch
    	DESTINATION share/${PROJECT_NAME}
)

【运行】 launch 不是run!

ros2 launch demo_cpp_service demo.launch.py

使用launch传递参数

【demo.launch.py内】

#创建参数声明action,用于解析launch命令后的参数
action_declare_arg_max_speed=launch.actions.DeclareLaunchArgument('launch_max_speed',default_value='2.0')

在action_node_turtle_control中添加

#使用launch中参数launch_max_speed值替换节点中的max_speed参数值
parameters=[{'max_speed':launch.substitutions.LaunchConfiguration('launch_max_speed',default=2.0)}],

使用launch中的max_speed值替换节点中的max_speed参数值

launch_description = launch.LaunchDescription({
	//添加参数声明动作
        action_declare_arg_max_speed,
        ...
    })

启动时指定launch参数值

ros2 launch demo_cpp_service demo.launch.py launch_max_speed:=3.0

launch_max_speed:=3.0
被动作action_declare_arg_max_speed解析
->启动脚本中有launch_max_speed参数
->传递参数给节点

launch组件

动作

launch_ros.actions.Node属于动作
动作对象:输出、终端命令、另一个launch文件
使用方法:包含其他launch、执行进程、输出日志、组合和定时启动

import launch
import launch_ros
from ament_index_python.packages import get_package_share_directory

def generate_launch_description():
    multisim_launch_path = [get_package_share_directory("turtlesim"),"/launch/","/multisim.launch.py"]
    #动作1-启动其他launch-包含其他launch文件
    action_include_launch = launch.actions.IncludeLaunchDescription(
        launch.launch_description_sources.PythonLaunchDescriptionSource(
            multisim_launch_path
        )
    )

    #动作2-打印数据
    action_log_info = launch.actions.LogInfo(msg='使用launch来调用服务生成海龟')

    #动作3-执行进程->执行命令行:生成新海龟
    action_executeprocess=launch.actions.ExecuteProcess(
        cmd=['ros2','service','call','/turtlesim1/spawn','turtlesim/srv/Spawn','{x: 1,y: 1}']
    )

    #动作4-组织动作成组,把多个动作放到一组
    action_group = launch.actions.GroupAction(
        [
            #动作5-定时器来制造顺序
            launch.actions.TimerAction(period=2.0,actions=[action_log_info]),
            launch.actions.TimerAction(period=2.0,actions=[action_executeprocess]),
        ]
    )
    return launch.LaunchDescription([
        action_include_launch,
        action_group
    ])

替换

使用launch传递参数即为替换
声明launch文件的参数->使用launch参数替换节点参数值

条件:决定哪些动作启动/不启动

结合替换进行条件:在原有动作中加入condition即可使用条件

import launch
import launch_ros
from launch.conditions import IfCondition

def generate_launch_description():
    #声明参数:是否创建新的海龟
    declare_spawn_turtle = launch.actions.DeclareLaunchArgument(
        'spawn_turtle',default_value='False',description='是否生成新的海龟'
    )
    spawn_turtle = launch.substitutions.LaunchConfiguration("spawn_turtle")
    #在运行时,追加参数 spawn_turtle:=true 或 spawn_turtle:=false就可控制是否运行生成海龟

    action_turtlesim = launch_ros.actions.Node(
        package="turtlesim",
        executable="turtlesim_node",
        output="screen",
    )

    #给日志输出和服务调用添加条件
    action_executeprocess = launch.actions.ExecuteProcess(
        condition=IfCondition(spawn_turtle),
        cmd=['ros2','service','call','/spawn','turtlesim/srv/Spawn','{x: 1, y: 1}']
    )
    action_log_info = launch.actions.LogInfo(
        condition=IfCondition(spawn_turtle),
        msg="使用executeprocess来调用服务生成海龟"
    )
    #利用定时器动作实现依次启动
    action_group=launch.actions.GroupAction([
        launch.actions.TimerAction(period=2.0,actions=[action_log_info]),
        launch.actions.TimerAction(period=3.0,actions=[action_executeprocess]),
    ])
    #合成启动描述并返回
    launch_description=launch.LaunchDescription([
        declare_spawn_turtle,
        action_turtlesim,
        action_group
    ])
    return launch_description

【控制】 运行时

ros2 launch demo_cpp_service conditions.launch.py spawn_turtle:=true
posted @ 2024-12-19 21:44  White_ink  阅读(138)  评论(0)    收藏  举报