上节的海龟跟随实验,输入的指令比较麻烦,很容易输错命令
launch文件就提供了一种比较简单的方式让我们快速地启动一些指令、一些节点,减少我们不断开终端,不断输入的工作
Launch文件
通过XML文件实现多节点的配置和启动 (可自动启动ROS Master)
Launch文件语法
Launch文件中的所有内容都是通过xml标签来做描述的
<launch>
launch文件中的根元素采用<launch>标签定义
<node>
启动节点
<node pkg="package-name" type="executable-name" name="node-name" />
* pkg:节点所在的功能包名称
* type:节点的可执行文件名称
* name:节点运行时的名称
* output、respawn、required、ns、args
name是会取代掉程序中初始化时的节点名的
<param>/<rosparam>
<arg>
param是表示存在ROS参数服务器里面的参数
arg是表示在launch文件内部使用的参数
<remap>重映射
<include>嵌套
Launch文件示例
示例一 simple.launch
新建功能包
catkin_create_pkg learning_launch
在功能包中创建launch文件的时候会创建一个文件夹,就叫launch,方便我们管理功能包中的各种资源
这是一个大家默认的命名规范
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>
编译工作空间
让工作空间知道我们有一个learning_launch功能包加入进来了
编译完后也要source环境变量
roslaunch learning_launch simple.launch
先跟功能包名,再跟功能包里的launch文件名
示例二 turtlesim_parameter_config.launch
用launch文件来配置参数的
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>
注意要把config目录一起复制过来
启动一下
roslaunch learning_launch turtlesim_parameter_config.launch
rosparam list
看下参数
可以看到turtle_name1和2 跟 turtle_number比起来前面加了个前缀
这就是param写到外面和写到里面的区别
写到里面会默认加上节点名字的前缀作为命名空间
示例三 start_tf_demo_c++.launch
用单个launch文件来启动上节课的多个终端
start_tf_demo_c++.launch
<launch> <!-- Turtlesim Node--> <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>
roslaunch learning_launch start_tf_demo_c++.launch
示例四 start_tf_demo_py.launch
start_tf_demo_py.launch
<launch> <!-- Turtlesim Node--> <node pkg="turtlesim" type="turtlesim_node"
name="sim"/> <node pkg="turtlesim" type="turtle_teleop_key" name="teleop"
output="screen"/> <node name="turtle1_tf_broadcaster" pkg="learning_tf"
type="turtle_tf_broadcaster.py"> <param name="turtle" type="string"
value="turtle1" /> </node> <node name="turtle2_tf_broadcaster"
pkg="learning_tf" type="turtle_tf_broadcaster.py"> <param name="turtle"
type="string" value="turtle2" /> </node> <node pkg="learning_tf"
type="turtle_tf_listener.py" name="listener" /> </launch>
示例五 turtlesim_remap.launch
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>
给/cmd_vel发送指令,正常的话海龟会动
动了,所以正常