学习视频:https://www.bilibili.com/video/BV1zt411G7Vn?t=603&p=19
Launch文件
Launch文件:通过XML文件实现多字节的配置与启动(可自动启动ROS Master)
优点:不需要打开许多终端来运行rosrun命令,不需要运行roscore命令
Launch文件语法
launch文件中的根元素采用标签定义
启动节点
<node pkg="package-name" type="execuatable-name" name="node-name" />
pkg
: 节点所在的功能包名称type
: 节点可执行文件的名称name
: 节点运行时的名称(运行节点之后在ROS系统中的节点名)- 可选:
output
、respawn
、required
、ns
、args
<param>/<rosparam>
:加载参数
<arg>
: launch文件内部的局部变量
<remap>
: 重映射
<include>
: 嵌套
更多标签可参考:http://wiki.ros.org/roslaunch/XML
Launch示例
1.创建功能包
cd ~/catkin_ws/src
catkin_create_pkg learning_launch
2.创建launch文件夹,管理launch文件
mkdir ~/catkin_ws/src/learning_launch/launch
3.创建simple.launch文件
gedit ~/catkin_ws/src/learning_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>
编译工作空间
cd ~/catkin_ws/
catkin_make
运行simple.launch文件
roslaunch learning_launch simple.launch
4.创建turtlesim_parameter_config.launch文件
gedit ~/catkin_ws/src/learning_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>
添加param.yaml配置文件
mkdir ~/catkin_ws/src/learning_launch/config
gedit ~/catkin_ws/src/learning_launch/config/param.yaml
内容为
A: 123
B: "hello"
group:
C: 456
D: "hello"
运行turtlesim_parameter_config.launch文件
roslaunch learning_launch turtlesim_parameter_config.launch
接着打开新的终端,查看参数,结果如下
5.创建start_tf_demo_c++.launch文件
gedit ~/catkin_ws/src/learning_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
效果如下
6.创建start_tf_demo_py.launch文件
gedit ~/catkin_ws/src/learning_launch/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>
运行指令
roslaunch learning_launch start_tf_demo_py.launch
效果如下
7.创建turtlesim_remap.launch文件
gedit ~/catkin_ws/src/learning_launch/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>
运行指令
roslaunch learning_launch turtlesim_remap.launch
效果如下