ROS:launch文件演示

文章介绍了如何在ROS中创建和使用launch文件来优化节点启动过程。通过新建launch文件夹,创建launch文件如start_turtle.launch,编辑内容来添加需要启动的节点,包括turtlesim_node和turtle_teleop_key,以及自定义的hello_vscode_c节点。然后在终端中进行编译并设置环境变量,最后通过roslaunch命令运行launch文件,实现一次性启动多个ROS节点。

前言

一个程序中可能需要启动多个节点,比如:ROS 内置的小乌龟案例,如果要控制乌龟运动,要启动多个窗口,分别启动 roscore、乌龟界面节点、键盘控制节点。如果每次都调用 rosrun 逐一启动,显然效率低下,如何优化?官方给出的优化策略是使用 launch 文件,可以一次性启动多个 ROS 节点。

一、添加launch文件夹

hello_vscode —> 右键新建文件夹 —> launch文件夹
在这里插入图片描述

二、新建launch文件

aunch文件夹下新建文件,文件名:start_turtle.launch
在这里插入图片描述

三、编辑launch内容

<launch>
    <!--添加被执行的节点-->
    <!--乌龟GUI-->
    <node pkg="turtlesim" type="turtlesim_node" name="turtle_GUI" />
    <node pkg="turtlesim" type="turtle_teleop_key" name="turtle_key" />
    <node pkg="hello_vscode" type="hello_vscode_c" name="hello" output="screen"/>

</launch>

其中
在这里插入图片描述

四、 执行文件

Ctrl+~快捷键启动终端

编译:Ctrl+shift+B

刷新,添加环境变量source ./devel/setup.bash

运行launchroslaunch hello_vscode start_turtle.launch
在这里插入图片描述
在这里插入图片描述

### ROS2 Launch 文件使用教程与示例 #### 什么是 Launch 文件Launch 文件用于启动多个节点和其他进程,简化复杂系统的配置和运行过程。通过定义一组动作(Actions),可以控制节点的启动顺序、参数传递以及其他依赖项。 --- #### 基本结构 一个典型的 ROS2 Launch 文件由 `LaunchDescription` 对象组成,该对象包含一系列的动作(Actions)。以下是基本语法: ```python from launch import LaunchDescription from launch_ros.actions import Node def generate_launch_description(): node_action = Node( package='example_package', executable='example_node', name='example_node_name', parameters=[{'param_name': 'value'}], output='screen' ) return LaunchDescription([node_action]) ``` 此代码片段展示了如何创建并返回一个简单的 Launch 描述符[^1]。 --- #### 调用其他 Launch 文件 可以通过 `IncludeLaunchDescription` 动作来嵌套调用其他的 Launch 文件。以下是一个例子: ```python from launch import LaunchDescription from launch.actions import IncludeLaunchDescription from launch.launch_description_sources import PythonLaunchDescriptionSource import os from ament_index_python.packages import get_package_share_directory def generate_launch_description(): pkg_dir = get_package_share_directory('my_package') include_launch = IncludeLaunchDescription( PythonLaunchDescriptionSource(os.path.join(pkg_dir, 'launch', 'other_launch_file.py')) ) return LaunchDescription([include_launch]) ``` 这段代码演示了如何在一个 Launch 文件中加载另一个 Launch 文件的内容[^1]。 --- #### 定时器延迟启动节点 如果希望某个节点在特定时间之后再启动,可以使用 `TimerAction` 来实现延时效果: ```python from launch import LaunchDescription from launch_ros.actions import Node from launch.actions import TimerAction def generate_launch_description(): delayed_node = Node( package='ros2_test', executable='ros2_test_publisher_node', name='ros2_test_publisher_node', output='screen' ) timer_action = TimerAction(period=5.0, actions=[delayed_node]) return LaunchDescription([timer_action]) ``` 这里展示了一个定时器,在等待 5 秒钟后再启动指定的节点[^1]。 --- #### 参数化 Launch 文件 可以在 Launch 文件中传入动态参数,并将其应用于不同的节点或操作。例如: ```python from launch import LaunchDescription from launch.substitutions import LaunchConfiguration from launch.actions import DeclareLaunchArgument from launch_ros.actions import Node def generate_launch_description(): param_arg = DeclareLaunchArgument( 'param_name', default_value='default_value', description='A parameter to pass into the nodes.' ) example_node = Node( package='example_package', executable='example_node', parameters=[{ 'dynamic_param': LaunchConfiguration('param_name') }] ) return LaunchDescription([ param_arg, example_node ]) ``` 这个例子说明了如何声明命令行可选参数并通过它们调整节点行为[^1]。 --- #### 集成 Gazebo 的 Launch 文件 当需要集成 Gazebo 模拟环境时,通常会结合 `.sdf` 或 `.urdf` 文件一起工作。下面是一段常见的 Gazebo 启动脚本: ```xml <launch> <!-- 加载 Gazebo --> <include file="$(find-pkg-share ros_gz_sim)/launch/gz_sim.launch.py"> <arg name="gz_args" value="-v 4 -r visualize_lidar.sdf"/> </include> <!-- 添加机器人模型到仿真环境中 --> <node pkg="ros_gz_spawn" exec="spawn_entity.py" args="-topic robot_description -entity my_robot"/> </launch> ``` 这是基于 XML 格式的 Launch 文件,适用于某些旧版工具链中的场景[^2]。 对于纯 Python 实现,则可能如下所示: ```python from launch import LaunchDescription from launch.actions import ExecuteProcess from launch_ros.actions import Node def generate_launch_description(): gazebo_cmd = ExecuteProcess( cmd=['gz', 'sim', '-v', '4', '--pause', 'visualize_lidar.sdf'], output='screen' ) spawn_entity = Node( package='ros_gz_utils', executable='spawn_entity.py', arguments=[ '-string', 'robot_description', '-name', 'my_robot', '-allow_renaming', 'true' ], output='screen' ) return LaunchDescription([ gazebo_cmd, spawn_entity ]) ``` 以上代码实现了 Gazebo 的初始化及其机器人的加载[^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello xiǎo lěi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值