【ROS】 ubuntu16.04下vs code学习ros下launch启动文件的使用方法
一、luanch文件的相关介绍
在以往的ros程序编写过程中,需要打开终端输入命令,显得麻烦,而launch文件将需要启动的节点写在一个launch文件中,通过在终端中运行launch文件,就可以同时启动多个节点,解决了以往需要打开多个终端不断输入指令的麻烦。
首先来介绍一下launch启动文件的编写规则
<launch>
//pkg是所在功能包的名字,type是所在功能包下的可执行文件,name是所运行节点的名字,该名字会取代原来程序中的节点名字
<node pkg="package_name" type="executable_name" name="node_name">
<launch>
这三个属性在node语句中是最重要的,当然,也可以增加一些可选属性,来满足不同的需求,不过这些需要自己在以后的学习过程中不断熟悉他们的用法
output(把日志信息打印到终端)
respawn(启动运行挂掉后是否要重启)
required(表示launch文件中的某个节点是不是一定要启动)
ns(命名空间)
args(给每个节点输入一些参数)
launch启动中用的比较多的标签有
(1)参数设置
<launch>
//设置ROS系统运行中的参数,存储在参数服务器中
<param name="output_frame" value="odom"/>
<node pkg="package_name" type="executable_name" name="node_name">
//加载参数文件中的多个参数
<rosparam file="params.yaml" command="load" ns="params"/>
<launch>
<launch>
//launch文件内部的局部变量,仅限于launch文件使用
<arg name="arg-name",default="arg-value"/>
//调用
调用arg的值并赋值给foo
<param name="foo" value="$(arg arg-name)"/>
//讲arg的值作为节点的输入参数
<node pkg="package_name" type="executable_name" name="node_name" args="$(arg arg-name)">
(2)重映射
<launch>
//重映射ROS计算图资源的命名
<remap from="/turtlebot/cmd_vel"to"/cmd_vel"/>
(3)嵌套
<launch>
//包含其他launch文件,类似C语言中的头文件包含,可以包含另外的launch文件
<include file="$(dirname)/other.launch"/>
二、luanch文件的使用方法
1、简单launch文件的编写和运行
在ubuntu16.04下打开vs code软件,打开在之前已经编写好的catkin_person文件,如果对catkin_person文件不太了解的话,可以去浏览我之前的博客,链接如下
链接: https://blog.csdn.net/weixin_45417246/article/details/115490635.
打开catkin_person文件后,界面如下
在/fjy/catkin_person/src路径下创建功能包my_launch,但是不需要添加依赖,因为launch文件是去运行其他功能包中的节点,自身并不涉及依赖
catkin_create_pkg my_launch
然后在my_launch文件夹下新建一个文件,取名为launch,一般都是取launch作为默认取名,当然,按自己喜好取其他的名也可以,然后在launch文件夹下新建simple.launch文件,要实现的目标是启动person_subscriber和person_publisher两个节点,并将输入信息打印到屏幕上
cd my_launch
mkdir launch
cd launch
touch simple.launch
在simple.launch中编写如下程序
<launch>
<node pkg="learning_topic" type="person_subscriber" name="listener" output="screen" />
<node pkg="learning_topic" type="person_publisher" name="talker" output="screen" />
</launch>
编写完程序后保存,并到/fjy/catkin_person路径下进行编译,并设置环境变量,然后输入以下指令运行simple.launch文件,运行结果如下,可以看到成功运行
roslaunch my_launch simple.launch
2、带参数launch文件的编写和运行
在ubuntu16.04下打开vs code软件,打开在之前已经编写好的catkin_parameter文件,如果对catkin_parameter文件不太了解的话,可以去浏览我之前的博客,链接如下
链接: https://blog.csdn.net/weixin_45417246/article/details/115490911.
打开catkin_parameter文件后,界面如下
在/fjy/catkin_parameter/src路径下创建功能包my_launch,但是不需要添加依赖,因为launch文件是去运行其他功能包中的节点,自身并不涉及依赖
catkin_create_pkg my_launch
然后在my_launch文件夹下新建一个文件,取名为launch,,然后在launch文件夹下新建turtlesim_parameter_config.launch文件
cd my_launch
mkdir launch
cd launch
touch turtlesim_parameter_config.launch
然后在my_launch文件夹下创建config文件夹,在config文件夹中创建param.yaml文件
cd my_launch
mkdir config
cd config
touch param.yaml
在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 my_launch)/config/param.yaml" command="load"/>
</node>
//启动键盘控制节点
<node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop" output="screen" />
</launch>
在param.yaml中编写如下程序
A: 123
B: "hello"
group:
C: 456
D: "world"
编写完程序后保存,并到/fjy/catkin_parameter路径下进行编译,并设置环境变量,然后输入以下指令运行turtlesim_parameter_config.launch文件,运行结果如下
roslaunch my_launch turtlesim_parameter_config.launch
3、tf坐标系下海龟跟随的launch文件的编写和运行
在ubuntu16.04下打开vs code软件,打开在之前已经编写好的catkin_tf文件,如果对catkin_tf文件不太了解的话,可以去浏览我之前的博客,链接如下
链接: https://blog.csdn.net/weixin_45417246/article/details/115495257.
打开catkin_tf文件后,界面如下
在/fjy/catkin_tf/src路径下创建功能包my_launch,但是不需要添加依赖,因为launch文件是去运行其他功能包中的节点,自身并不涉及依赖
catkin_create_pkg my_launch
然后在my_launch文件夹下新建一个文件,取名为launch,然后在launch文件夹下新建start_tf.launch
文件
cd my_launch
mkdir launch
cd launch
touch start_tf.launch
在simple.launch中编写如下程序
<launch>
<node name="sim" pkg="turtlesim" type="turtlesim_node"/>
<node name="teleop" pkg="turtlesim" type="turtle_teleop_key" output="screen"/>
<node pkg="learning_tf" type="turtle_tf_broadcaster" name="turtle1_tf_broadcaster" args="turtle1"/>
<node pkg="learning_tf" type="turtle_tf_broadcaster" name="turtle2_tf_broadcaster" args="turtle2"/>
<node pkg="learning_tf" type="turtle_tf_listener" name="listener"/>
</launch>
编写完程序后保存,并到/fjy/catkin_tf路径下进行编译,并设置环境变量,然后输入以下指令运行start_tf.launch文件,运行结果如下,可以看到成功运行
roslaunch my_launch start_tf.launch
4、重映射的launch文件的编写和运行
打开catkin_person文件后,界面如下
在/my_launch/launch路径下,新建turtlesim_remap.launch文件
cd launch
touch turtlesim_remap.launch
在turtlesim_remap.launch中编写如下程序
<launch>
//include包含其他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>
编写完程序后保存,并到/fjy/catkin_person路径下进行编译,并设置环境变量,然后输入以下指令运行turtlesim_remap.launch文件,运行结果如下,可以看到成功运行,我们可以看到原来的的/turtle1/cmd_vel变成了/cmd_vel
roslaunch my_launch turtlesim_remap.launch
这样,我们就完成了vs code下关于launch文件的使用方法了