??launch 文件的作用:通过XML 文件实现多节点的配置和启动(可自动启动ROS Master )。
launch文件语法
??典型的launch 文件如下:
<launch>
<node pkg="turtlesim" name="sim1" type="turtlesim_node"/>
<node pkg="turtlesim" name="sim2" type="turtlesim_node"/>
</launch>
<launch>
??launch 文件中的根元素采用<launch> 标签定义。
<node>
??启动节点:
<node pkg="helloworld" type="demo_hello" name="hello" output="screen"/>
pkg :节点所在的功能包名称。type :节点的可执行文件名称。name :节点运行时的名称。output :日志的输出目标。
<param>和<rosparam>
??设置ROS 系统运行中的参数,存储在参数服务器中:
<param name="output_frame" value="odom"/>
??加载参数文件中的多个参数:
<rosparam file="params.yaml" command="load" ns="params"/>
<arg>
??launch 文件内部的局部变量,仅限于launch 文件使用:
<arg name="arg-name" value="arg-value"/>
调用:
<param name="foo" value="$(arg arg-name)"/>
<node name="node" pkg="package" type="type" args="$(arg arg-name)"/>
<remap>
??重映射ROS 计算图资源的命名:
<remap from="/turtle1/cmd_vel" to="/cmd_vel"/>
<include>
??包含其他launch 文件,类似C 语言中的头文件包含:
<include file="$(dirname)/other.launch"/>
launch的用法
node的一般用法
??创建功能包:
$ cd ~/catkin_ws/src
$ catkin_create_pkg learning_launch
在catkin_ws/src/learning_launch 下新建一个目录launch ,在该目录中添加文件sample.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
$ source devel/setup.bash
$ roslaunch learning_launch sample.launch
node的args用法
??在launch 目录中添加文件broadcaster_listener.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>
param和rosparam的用法
??在catkin_ws/src/learning_launch 下新建一个目录config ,在该目录中添加文件param.yaml :
A: 123
B: "hello"
group:
C: 456
D: "hello"
在launch 目录中添加文件turtle_param.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>
??使用如下命令运行代码:
$ cd ~/catkin_ws
$ catkin_make
$ source devel/setup.bash
$ roslaunch learning_launch turtle_param.launch
可以看到小乌龟的界面被打开了,可以用方向键进行控制。 ??执行命令rosparam list :
$ rosparam list
/turtle_number
/turtlesim_node/A
/turtlesim_node/B
/turtlesim_node/group/C
/turtlesim_node/group/D
/turtlesim_node/turtle_name1
/turtlesim_node/turtle_name2
??执行命令rosparam get :
$ rosparam get /turtle_number
2
$ rosparam get /turtlesim_node/turtle_name1
Tom
$ rosparam get /turtlesim_node/turtle_name2
Jerry
remap的用法
??在launch 目录中添加文件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 文件后,查看ROS 服务器中的topic :
$ rostopic list
/cmd_vel
可以看出/turtle1/cmd_vel 被重命名为/cmd_vel 。
|