ROS roslaunch 学习

来源:互联网 发布:开罗旅游自助攻略知乎 编辑:程序博客网 时间:2024/05/17 23:35

感谢分享,转自http://blog.csdn.net/yiranhaiziqi/article/details/52949121

roslaunch 用处:将多个rosnode 结合起来,一起运行。这样就不需要一个个的运行。

roslaunch格式 
(add_two.launch)

<launch>        <arg name="a" default="1" />        <arg name="b" default="2" />    <node pkg="beginner_tutorials" name="add_two_ints_server" type="add_two_ints_server"/>    <node pkg="beginner_tutorials" name="add_two_ints_client" type="add_two_ints_client" args="$(arg a) $(arg b)"/>    <node name = "stage" pkg="stage_ros" type = "stageros" args="$(find stage_ros)/world/willow-erratic.world"/>   </launch>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

launch文件为xml格式。 
每个tag的格式至少包含下面三部分 
1.pkg = “your package name”,

2.name = “your name”//一般为可执行文件的名称 
可执行文件的名称是robot_cleaner_node

3.type =”可执行文件的名称”, 
(即在CMakeList.txt中的add_executable(node_name src/C++file.cpp)中的node_name) 
例如:add_executable(robot_cleaner_node src/robot_cleaner_move_rotate.cpp)中的robot_cleaner_node

此处的type是可执行文件的名称,而name则是可以任意给出的,它覆盖了原有文件中ros::init指定的node name。

4.(非必须)args 为运行时的参数 
定义参数a,默认值为0,运行时可以重新赋值; 
定义参数b,值为2,运行时不能赋值; 
若赋值会出现错误:Invalid tag: cannot override arg ‘b’, which has already been set.

args="$(arg a) $(arg b)":a,b作为参数传入节点,运行时可以将a重新赋值,不赋值使用默认值1.

$(find package_name)找到package_name 所在路径 
$(find stage_ros)/world/willow-erratic.world" 表示 /opt/ros/indigo/share/stage_ros/world/willow-erratic.world 
运行:1.(未定义arg时) roslaunch beginner_tutorials add_two.launch a:=4 b:=5 
2.(定义arg后) roslaunch beginner_tutorials add_two.launch 或者roslaunch beginner_tutorials add_two.launch a:=4(只给a赋值,b不需要)

运行之后输出会保存在log文件中:例如log file: /home/server/.ros/log/e907e8c8-9be8-11e6-bf00-74d43562c7a3/add_two_ints_client-2*.log,并不会出现在控制台 
要想输出在控制台,添加output属性

<node name = "my_stage" pkg = "my_stage" type = "my_stage"output = "screen" />
  • 1
  • 2
  • 1
  • 2

5 若显示所有nodes的输出,用–screen命令行。 
$ roslaunch –screen package_name launch_file_name 
6

 respawn="true"eg:<node pkg="turtlesim" name="sim" type="turtlesim_node"  respawn="true"/>
  • 1
  • 2
  • 1
  • 2

代表当我们启动一个node时,roslaunch会监控它,当它关闭时,比如启动turtlesim节点,当我们关闭turtlesim窗口时,roslaunch会重新启动一个新的。

7

required="true"
  • 1
  • 1

此属性表示当这个节点退出后,roslaunch会关闭所有的节点,并退出。

8让一个节点在单独的终端窗口中启动: 
roslaunch 使所有的节点都共用一个终端,要想节点有自己的终端可以使用

launch-prefix="xterm -e"
  • 1
  • 1

9 命名空间ns

ns="turtlesim1"
  • 1
  • 1

如果同一个节点使用这个属性可以创建了两个无关的节点。 
eg:

<launch>    <node    name="turtlesim_node "    pkg="turtlesim"    type="turtlesim_node "    ns="sim1"    />    <node    pkg="turtlesim"    type="turtle_teleop_key "    name="teleop_key"    required="true"    launch −prefix="xterm −e"    ns="sim1"    />    <node    name="turtlesim_node "    pkg="turtlesim"    type="turtlesim_node "    ns="sim2"    />    <node    pkg="a gitr "    type="pubvel"    name="velocity_publisher "    ns="sim2"    /></launch>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

10.remap :在launch文件中重新命名:使用 remap 元素 
格式:<remap from="original-name" to="new-name" /> 
如果这个 remap 是 launch 元素的一个child(子类),与 node 元素同一层级, 并在 launch 元素内的最顶层。那么这个 remapping 将会作用于后续所有的节点。 
将turtle1/pose 重新命名为tim 
使用rosrun 命令rosrun turtlesim turtlesim_node turtle1/pose:=tim 
使用roslaunch

 <node pkg="turtlesim" type="turtlesim_node"        name="turtlesim" >        <remap from="turtle1/pose" to="tim" />    </node>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
11.including 
这个属性期望我们添加想要包含的文件的完整路径。但是大多数时候,include 元素使用一个 find 命令来搜索一个程序包,代替一个明确的完整路径:
<include file="$(find package-name)/launch-file-name" />
  • 1
  • 1

roslaunch 命令 将会在程序包(package)的子目录里搜索launch文件。 include 元素必须要指定文件的特定路径,你可以使用 find 来找到这个程序包,但是却不能在这个程序包目录里面自动的找到某个子目录里有launch文件。举例:

这样做是正确的:<include file = "find learning_tutrols"/launch/start_demo.launch" / >这样做是错误的:<include file = "find learning_tutrols"/start_demo.launch" />
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

include 元素也支持 ns 属性,可以让这个文件里的内容推送到一个命名空间里面:

<include file=". . . " ns="namespace" />
  • 1
  • 1

一般我们都会给 include 元素设置一个 ns 属性。

12 .param 标签 
wiki.ros.org/roslaunch/XML/param 
定义了一个参数服务器上的参数,属性有 
name : 参数名字 
type :参数的类型,”str|int|double|bool” 
value:参数的值,除了value还可以使用以下三种 
textfile= $(find pkg-name)/path/file.txt,该文件的内容将被读取并存储为一个字符串,此文件必须是本地的,可访问的。所以强烈推荐使用$(find pkg-name) 形式来。避免在另一台机器上找不到文件路径运行失败。 
binfile="$(find pkg-name)/path/file"该文件的内容将被读取并存储为Base64编码的XML-RPC二进制对象 
command="$(find pkg-name)/exe '$(find pkg-name)/arg.txt'" 
parameter server 参数除了上述方法外,可以通过以下方式设置 
命令行 rosparam set / rosparam get 设置 
代码中 
roscpp: ros::param::set / ros::param::get 
rospy: set_param / get_param

13.rosparam 标签 
wiki.ros.org/roslaunch/XML/rosparam 
rosparam :可以使用从rosparam YAML文件加载,删除,dump ,ROS参数服务器参数 
rosparam 参数: 
command=”load|dump|delete” (optional, default=load) 
file=”$(find pkg-name)/path/foo.yaml” (load or dump commands) 
param=”param-name” 
ns=”namespace” (optional) 
subst_value=true|false (optional) 是否允许替换yaml文本中的参数

eg:

<rosparam command="load" file="$(find rosparam)/example.yaml" />//加载文件中的参数<rosparam command="delete" param="my/param" />//删除参数<arg name="whitelist" default="[3, 2]"/><rosparam param="whitelist" subst_value="True">$(arg whitelist)</rosparam> //替换<rosparam param="a_list">[1, 2, 3, 4]</rosparam><rosparam>  a: 1  b: 2</rosparam>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

参考: 
blog.csdn.NET/fengmengdan/article/details/42984429 
blog.csdn.Net/github_35160620/article/details/52618271

0 0
原创粉丝点击