一、launch文件的使用方法
在之前的章节中我们在最后运行代码的时候都需要启动多个终端,而本节中我们将使用launch文件一种通过xml文件格式实现多节点的配置和启动。
二、launch文件的语法
1、在launch中单行、多行表示和注释
<launch>
<!-- 这是一个注释 -->
<!-- 这是一个单行表示 -->
<node pkg = "learnging_tf" name = "test" type = "Turtle_tf_listener"/>
<!-- 这是一个多行表示 -->
<node pkg = "learning_tf" name = "test" type = "Turtle_tf_listener">
<!-- 在此定义节点的其他配置 -->
<param name = "talker_param" value = "a value" />
</node>
</launch>
2、启动节点node
<launch>
<node pkg = "功能包" , type = "可执行文件" , name = "节点名称"/>
<!-- 这三个是必要的其中还有些可选项output、respawn、required、ns、args等 -->
<node pkg = "功能包" , type = "可执行文件" , name = "节点名称" output = "screen"/>
</launch>
3、参数设置param、rosparam和arg
<launch>
<!-- 设置ROS系统运行中的参数,存储在参数服务器中 -->
<!-- param
<!-- param仅仅能加载一个参数名和参数值 -->
<param name = "参数名" value = "参数值"/>
<!-- rosparam加载参数文件中的多个参数 -->
<rosparam file = "文件名字" command = "load" ns = "param" param = "my_params"/>
<!-- arg定义的参数为launch文件内部的局部变量,仅限于launch文件使用 -->
<arg name = "arg_name" default = "默认值"/>
<!-- 下面是两个调用 -->
<param name = "foo" value = "$(arg arg_name)"/>
<node pkg = "learning_topic" type = "可执行文件" name = "node1" args = "$(arg arg_name)" />
</launch>
4、重映射remap
<launch>
<!-- 重映射ROS计算图资源的命名 -->
<remap from = "/turtle1/cmd_vel" to = "cmd_vel" />
<!-- 相当于给/turtle1/cmd_vel这个话题换了个名字 改为了/cmd_vel -->
</launch>
5、嵌套include
<launch>
<!-- 包含其他launch文件,类似于C语言中的头文件包含 -->
<include file = "$(file_name)/other.launch"/>
</launch>
当然更多详细内容您可以去wiki官网查看: roslaunch/XML - ROS Wiki
三、用launch文件运行之前的程序
1、创建新的功能包
cd ~/catkin_ws/src
catkin_creat_pkg learning_launch
cd learning_launch
mkdir launch
2、编写launch文件代码
cd ~/catkin_ws/src/learning_launch/launch
a、发布者publisher和订阅者subscriber
touch simple.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" />
<node pkg="rqt_graph" type="rqt_graph" name="rqt_graph" output="screen" />
</launch>
注意:您可以直接跳到编译并运行launch文件里去看看实际效果。
同时如果您没有相关可执行文件可以去这两篇笔记里去补上。
《Ubuntu20.04环境下的ROS学习笔记4》-CSDN博客
《Ubuntu20.04环境下的ROS学习笔记5》-CSDN博客
b、配置Parameter Server中的参数
cd ~/catkin_ws/src/learning_launch/launch
touch Turtlesim_parameter_config.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文件来存储全局字典的参数,让我们可以直接load。
cd ..
mkdir config
cd config
touch param.yaml
编辑param.yaml文件:
A: 123
B: "hello"
group:
C: 456
D: "hello"
c、海龟的广播broadcaster和监听listener
cd ~/catkin_ws/src/learning_launch/launch
touch start_tf_demo_cpp.launch
编辑 start_tf_demo_cpp.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" />
<node pkg="rqt_graph" type="rqt_graph" name="rqt_graph" output="screen" />
</launch>
同时如果您没有相关可执行文件可以去这篇笔记里去补上。
《Ubuntu20.04环境下的ROS学习笔记12》-CSDN博客
d、使用include包含其他launch文件和remap重映射
注意这里需要您把a、发布者publisher和订阅者subscirber小节做完才行。
cd ~/catkin_ws/src/learning_launch/launch
touch 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>
四、编译并运行launch文件
cd ~/catkin_ws
catkin_make
source devel/setup.bash 值得一提的是,这一步如果您按照了之前的文章来做,可以省略。(《Ubuntu20.04环境下的ROS学习笔记4》-CSDN博客)
到这里编译完成。
接下来就是运行了,但不是老样子了。注意这里补齐似乎不能使用,要手敲。
1、运行发布者publisher和订阅者subscriber
roslaunch learning_launch simple.launch
2、运行配置参数的launch文件
roslaunch learning_launch Turtlesim_parameter_config.launch
3、运行海龟的广播broadcaster和监听listener
roslaunch learning_launch start_tf_demo_cpp.launch
4、运行包含include和remap的头文件
roslaunch learning_launch Turtlesim_remap.launch
这里值得注意的一个点是,这里就算您使用:rosrun turtlesim turtle_teleop_key 产生了一个键盘控制也不能直接控制海龟,他不会通过/cmd_vel来控制/turtlesim_node这个节点。