ROS复习笔记之——名称

81 篇文章 115 订阅

参考资料为:http://wiki.ros.org/Books/ROS_Robot_Programming_English

 

 

ROS服务中使用的节点、话题、消息以及ROS中使用的参数都具有唯一的名称(name)话题名称分为相对的方法、全局方法和私有方法,如下所示。

int main(int argc, char **argv) // 节点主函数
{
ros::init(argc, argv, "node1"); // 初始化节点
ros::NodeHandle nh; // 声明节点句柄
// 声明发布者,话题名 = bar
ros::Publisher node1_pub = nh.advertise<std_msg::Int32>("bar", 10);

此处,节点的名称是/node1。如果用一个没有任何字符的相对形式的bar来声明一个发布者,这个话题将和/bar具有相同的名字。如果以如下所示使用斜杠(/)字符用作全局形式,话题名也是/bar。所以下面两句是等价的

ros::Publisher node1_pub = nh.advertise<std_msg::Int32>("bar", 10);
ros::Publisher node1_pub = nh.advertise<std_msg::Int32>(“/bar”, 10);

但是,如果使用波浪号(~)字符将其声明为私有,则话题名称将变为/node1/bar。

ros::Publisher node1_pub = nh.advertise<std_msg::Int32>(“~bar”, 10);

如下表所示。(其中/wg意味着命名空间的修改)

 

若存在两个摄像头。简单地两次执行相关节点将导致之前执行的节点因ROS的性质而终止,因为ROS必须具有唯一的名称。用户可以在运行时更改节点的名称,而不需要运行额外的程序或更改源代码。方法包括命名空间(namespace)和重新映射(remapping)。

设有一个camera_package。执行camera_package功能包的camera_node会运行camera节点,此时的运行方法如下

rosrun camera_package camera_node

当该节点将相机的图像值发送给image话题时,可以通过rqt_image_view传送该image话题,如下所示。

rosrun rqt_imgae_view rqt_imgae_view

下面通过重新映射来修改这些节点的话题名称。如果执行如下命令,只有话题名称将被改为/front/image。其中,image是camera_node的话题名

rosrun camera_package camera_node image:=front/image
rosrun rqt_imgae_view rqt_imgae_view image:=front/image

例如,当有前、左、右,三个摄像头,且当多次执行同名的节点时,由于节点名重复,该节点将被重复执行,因此节点会被停止。为了避免这种情况,可以采取用同一个名称运行多个不同的节点的方法。下面的命令示例中,name选项使用了两个下划线
(__)。附加地说明,选项__ns、__name、__log、__ip、__hostname和__master是运行节点时使用的特殊选项。我们在话题名称选项中使用了一个下划线(_),如果它是private名称,则在现有名称前加上一个下划线。

《For example, if there are three cameras, such as front, left, and right, when the multiple nodes are executed under the same name, there will be conflicted names and therefore the previously executed node gets terminated. Therefore, nodes with the same name can be executed in the following way. Below, the name option is followed by consecutive underscores(__). Options such as ‘__ns’, ‘__name’, ‘__log’, ‘__ip’, ‘__hostname’, and ‘__master’ are special options used when running the node. Also, single underscore(_) is placed in front of the topic name if it is used as a private.》

rosrun camera_package camera_node __name:=front _device:=/dev/video0
rosrun camera_package camera_node __name:=left _device:=/dev/video1
rosrun camera_package camera_node __name:=right _device:=/dev/video2
rosrun rqt_imgae_view rqt_imgae_view

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值