0


ROS学习(八)launch启动文件的使用方法

前言

使用命令行输入代码需要不断打开终端比较繁琐,而且容易输入错误,那么有没有什么方法可以快速启动所需节点呢?

一、launch文件介绍

Launch文件:通过XML文件实现多节点的配置和启动(可自启动ROS Master
命令行语法:

roslaunch 功能包名 launch文件名

二、Launch文件常用核心语法

<launch>
        <node pkg = "turtlesim" name="sim1" type="turtlesim_node" />
        <node pkg = "turtlesim" name="sim2" type="turtlesim_node" />
</launch>

< launch >launch文件中的根元素采用< launch >标签定义< node >启动节点
注:launch文件以< launch >开始,结束

1.< node >语法

<node pkg = "package-name" type = "executable-name" name= "node-name"/>

pkg节点所在的功能包名称type节点的可执行文件名称(.cpp文件或.py文件)name节点运行时的名称:可以取代type中的节点名(即覆盖掉ros::init()定义的节点名)outputoutput=“screen” 终端输出转储在当前的控制台上,而不是在日志文件中respawn是否要重启required控制某个节点是否一定要启动ns设置命名空间(避免命名冲突)args给节点输入参数

2.参数设置

(1)< param >/< rosparam >

< param >设置ROS系统运行中的参数,存储在参数服务其中。
<param name = "output_frame" value="odem" />
  • name:参数名
  • value:参数值
< rosparam > 加载参数文件中的多个参数:
<rosparam file="params.yaml" command="load" ns="params" />

其中,ns是命名空间(name space)

(2)< arg >

< arg > launch文件内部的局部变量,仅限于launch文件使用
设置参数
<arg name="arg-name" value="arg-value" />
  • name:参数名
  • value:参数值
参数调用:
<param name="foo" value="$(arg arg-name)" />
<node name="node" pkg="package" type="type" args="$(arg arg-name)" />

3.重映射

< remap >重映射ROS计算机资源的命名

<remap from="/turtlebot/cmd_vel" to="/cmd_vel"/>
  • from:原命名
  • to:映射之后的命名

4.嵌套

< include >内嵌包含其他launch文件,类似C语言中的头文件包含。

<include file="$(dirname)/other.launch" />
  • file:包含其他launch文件路径

三、launch案例

1. 创建功能包

cd ~/catkin_ws/src
catkin_create_pkg learning_launch
cd learning_launch
mkdir launch

注:launch功能包不需要依赖包

2.simple.launch

cd ~/catkin_ws/src/learning_launch/launch
touch simple.launch
gedit simple.launch
<launch>
    <node pkg="learning_topic" type="person_subscriber" name="talker" output="screen" />
    <node pkg="learning_topic" type="person_publisher" name="listener" output="screen" />
</launch>

注:启动的节点所在功能包和可执行文件(.cpp文件)的创建请参考:ROS学习(四)–1.发布者与订阅者案例

编译:

cd ~/catkin_ws
catkin_make

运行:

source ~/catkin_ws/devel/setup.bash
roslaunch learning_launch simple.launch

运行结果
由图可知,launch启动节点成功!

3.turtlesim_parameter_config.launch

cd ~/catkin_ws/src/learning_launch/launch
touch turtlesim_parameter_config.launch
gedit turtlesim_parameter_config.launch
<launch>
    <param name="/turtle_number" value="2" />
    <node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node" >
        <param name="turtle_name1" value="Tom" />
        <param name="turtle_name2" value="Jerry" />
        <rosparam file="$(find learning_launch)/config/param.yaml" command="load" />
    </node>
    <node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop_key" output="screen" />
</launch>

注:1.find learning_launch:自动寻找learning_launch路径

编译:

cd ~/catkin_ws
catkin_make

注:运行前需要先添加config/param.yaml文件

cd ~/catkin_ws/src/learning_launch
mkdir config
cd config
touch param.yaml
gedit param.yaml
A:123
B:"hello"

group:
  C:456
  D:"hello"

运行:

source ~/catkin_ws/devel/setup.bash
roslaunch learning_launch turtlesim_parameter_config.launch

在这里插入图片描述
由图可知,launch文件启动海龟仿真器和海龟移动控制器成功。同时launch文件参数设置和加载param.yaml文件参数成功

4.start_tf_demo_c++.launch

cd ~/catkin_ws/src/learning_launch/launch
touch start_tf_demo_c++.launch
gedit start_tf_demo_c++.launch
<launch>
    <node pkg="turtlesim" type="turtlesim_node" name="sim" />
    <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen" />

    <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle1" name="turtle1_tf_broadcaster" />
    <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle2" name="turtle2_tf_broadcaster" />
    <node pkg="learning_tf" type="turtle_tf_listener" name="listener" />
</launch>

注:启动的节点所在功能包和可执行文件(.cpp文件)的创建请参考:ROS学习(七)坐标系管理系统
编译:

cd ~/catkin_ws
catkin_make

运行:

source ~/catkin_ws/devel/setup.bash
roslaunch learning_launch start_tf_demo_c++.launch

5.turtlesim_remap.launch

cd ~/catkin_ws/src/learning_launch/launch
touch turtlesim_remap.launch
gedit turtlesim_remap.launch
<launch>
    <include file="$(find learning_launch)/launch/simple.launch" />

    <node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node">
        <remap from="/turtle1/cmd_vel" to="/cmd_vel" />
    </node>
</launch>

编译:

cd ~/catkin_ws
catkin_make

运行:

source ~/catkin_ws/devel/setup.bash
roslaunch learning_launch turtlesim_remap.launch

在这里插入图片描述
由图可知,simple.launch文件包含成功,同时remap重命名成功,成功将"/turtle1/cmd_vel"重命名为"/cmd_vel" ,向"/cmd_vel" 发布话题,海龟成功移动,说明重命名成功


本文转载自: https://blog.csdn.net/m0_56451176/article/details/126253984
版权归原作者 dtge 所有, 如有侵权,请联系我们删除。

“ROS学习(八)launch启动文件的使用方法”的评论:

还没有评论