原文地址:http://nootrix.com/2013/08/ros-namespaces/

 

In this tutorial, we will be talking about ROS namespaces which allow to combine nodes in ways unplanned by developers. This is actually what all ROS is about: allow building systems by connecting nodes, often developed by third parties according to your needs. This is possible thanks to a flexible node naming system. As usual, we will illustrate presented concepts with concrete examples that you can run on your own machine. As a prerequisite, you only need to be familiar with thebasic concepts of ROS introduced in a previous post.

View Running Nodes Using rqt_graph
Every ROS application is a graph of nodes typically connected through some topics. We illustrate this idea by the example of turtle simulator. First start the ROS infrastructure is running by evaluating in a terminal the command:
roscore

Now, start the turtle simulator node by evaluating in a new terminal:
rosrun turtlesim turtlesim_node

As a result, a simulator window opens. It looks like the one shown by Figure 1, except may be the turtle colors and shape that are different on each launch. On the terminal you’ll see two information lines similar to the ones blow. The[INFO] tag is followed by a time stamp ([<seconds since epoch>.<nano_seconds>]), and then a log about the node name (/turtlesim) and the turtle initial position.


[INFO] [1374406333.186210549]: Starting turtlesim with node name /turtlesim
[INFO] [1374406333.192485241]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]

Figure 1: The TurtleSim Simulator Window

To make the turtle move, launch a driver node by evaluating in another terminal:
rosrun turtlesim draw_square

You can display running ROS nodes using the rqt_graph utility under ROS Groovy (Use rxgraph under ROS Fuerte), by evaluating in a terminal the command line:
rqt_graph

Figure 2: rqt_graph Shows Nodes and Their Connections Through Topics

As a result, you’ll get the window shown by Figure 2. Ellipses represent nodes while arrows represent connexions through topics. We can see that the /turtlesim node (that is visualized by the graphical turtle in Figure 1) is the publisher of the /turtle1/pose topic that provides the current pose of the turtle . The unique subscriber of this topic is the /draw_square node. The second arrow corresponds to the /turtle1/command_velocity topic. It is used by the/draw_square node to send velocity commands to /turtlesim.

One Driver for Two simulated Turtles
Suppose now that we want to have two turtle simulations running side by side, both driven by the same driver (e.g.draw_square). The naive approach consisting in running yet another turtlesim_node does not work. Instead, the first turtle simulation terminates with a warning log ([WARN] tag) as below. It states that the new simulation node replaces the first one.


[WARN] [1374671981.742213033]: Shutdown request received.
[WARN] [1374671981.742445480]: Reason given for shutdown: [new node registered with same name]

To avoid name conflicts, you need to provide a different node name for the second turtle simulator. The rosruncommand does allow to assign a different name to the node by evaluating the following command line. The value assigned to the __name parameter (here turtlesim2) is the replacement for the default node name.

rosrun turtlesim turtlesim_node __name:=turtlesim2

Figure 3: Nodes of two turtle simulators sharing a single driver

Figure 3 shows the graph displayed by rqt_graph once you hit the update button. The arrows refer to connections between nodes. To actually see the topics, select the “Nodes/Topics (active)” item in the drop down list of rqt_graph. The result is shown on Figure 4. Topics are represented as rectangles nested into the turtle1 box. If you hover the mouse over a topic, it gets color highlight, as well as connected nodes.

Figure 4: Displaying Topics in rqt_graph

Namespaces
Now it’s time to introduce the concept of namespace. A namespace can be viewed as a directory which contents are items of different names. These items can be nodes, topics, or even other namespaces. Indeed, namespaces can be organized in hierarchies of arbitrary depth.

In the examples above, you already have been dealing with two namespaces. There is the root namespace, referred by a forward slash “/“. The root namespace of Figure 4 includes four items. Three of them are nodes (draw_square,turtlesim, and turtlesim2) while the fourth is the turtle1 namespace. The turtle1 namespace includes two items which are both topics: command_velocity and pose.

To talk to each other, nodes need to refer to same topics. In the previous examples, we used the default topic names. This was ok for our toy scenario. But, sometimes you need to make nodes use topics with different names. A typical example is a system where different sub-systems are similar.

Two Turtle Simulators Driven by Different Drivers
We illustrate the concept of namespace with two turtle simulators driven by two instances of the draw_square driver. Each simulator and is driven by a dedicated driver. Thus, we need to ensure that nodes have different names. We also need to ensure that within each simulation, nodes talk using different topics.

The renaming facility provided by rosrun does allow to avoid collisions. However, it result into multiple long command lines. A better solution is use roslaunch. It requires writing a XML file that contains all information about nodes to launch and their namespaces. The XML listing below allows running side by side two turtle simulation nodes driven by two different draw_square nodes.

<launch>
<group ns="sim1">
<node name="turtle" pkg="turtlesim" type="turtlesim_node"/>
<node name="controller" pkg="turtlesim" type="draw_square"/>
</group>
<group ns="sim2">
<node name="turtle" pkg="turtlesim" type="turtlesim_node"/>
<node name="controller" pkg="turtlesim" type="draw_square"/>
</group>
</launch>

The XML format for roslaunch provides tags for defining nodes. By changing the name argument we can have different instances of the same node type of a given package running side by side. In our example, we kept the names identical. But, we wrapped nodes inside group tags. Each group is mapped to a different namespace by setting the ns argument.

Time to launch all nodes by evaluating the following single command line. We assume that thetwoTurtleSimulations.launch file with the XML code above is in the folder where the command line is evaluated.

roslaunch twoTurtleSimulations.launch

Note that roslaunch takes care of everything. No need to run roscore! Still, if you have already lauched roscore, it will work just fine. Anyway, rqt_graph will display the set of nodes and connexions as shown by Figure 5.

(转) ROS NAMING AND NAMESPACES的更多相关文章

  1. ROS教程

    Learning ROS 学习ROS Depending on your learning style and preferences, you can take two approaches to ...

  2. ROS 5.x自动定时备份并发送到邮箱(实用)

    博主使用ROS已经有很长一段时间了,但经常会忘记备份配置与数据库,加上ROS本身自带的User-Man数据库并不是非常稳定,1年中总会出现1-2次数据丢失的情况.所以费了一定功夫才找到真正可用自动备份 ...

  3. ros下多机器人系统(1)

    multi-robot system 经过两个多月的ros学习,对ros的认识有了比较深入的了解,本篇博客主要记录在ros下开发多机器人系统以及对ros更深入的开发.本篇博客是假定读者已经学习完了全部 ...

  4. Naming Conventions for .NET / C# Projects

    http://www.akadia.com/services/naming_conventions.html Naming Conventions for .NET / C# Projects Mar ...

  5. [转]JavaScript Namespaces and Modules

    Namespaces In most programming languages we know the concept of namespaces (or packages).Namespaces ...

  6. Gazebo機器人仿真學習探索筆記(七)连接ROS

    中文稍后补充,先上官方原版教程.ROS Kinetic 搭配 Gazebo 7 附件----官方教程 Tutorial: ROS integration overview As of Gazebo 1 ...

  7. ROS launch总结

    1 运行Launch文件2 新建Launch文件3  在namespace中启动nodes 4   remapping names 5 其他的launch元素 1 运行Launch文件 Launch文 ...

  8. ROS C++ 规范概要

    一.动机 代码一致才能可读.联调.高效率.高复用.可移植性. 二.命名方式 CamelCased camelCased under_scored ALL_CAPITALS 2.1 Package命名方 ...

  9. ROS launch 总结

    ROS launch 总结 转自博客:https://www.cnblogs.com/Jessica-jie/p/6961837.html 1 运行Launch文件2 新建Launch文件3  在na ...

随机推荐

  1. c++11 auto

    auto 关键字指示编译器使用已声明变量的初始化表达式或 lambda 表达式参数来推导其类型. 在大多情况下,建议您使用 auto 关键字(除非您确实需要转换),因为此关键字可提供以下好处: 可靠性 ...

  2. ESP8266固件烧录方法

    今天拿到ESP8266的板子,第一步是进行烧录固件. 首先是使用官方自带的参考文档,进行操作.发现每次烧录均卡在等待同步上电. 之后发现是烧录方法错误. 正确的烧录方法: 先按下FLASH不放,再按烧 ...

  3. windows下自动删除n天前的文件

    使用windows2003下的内置命令forfiles配合计划任务可以实现自动删除n天前的文件. windows2003中设定自动执行的计划任务很简单. 一.脚本编写 forfiles命令用法: Fo ...

  4. Server2008R2:由于没有远程桌面授权服务器可以提供许可证,.....错误的解决 ---设计师零张

    一直使用远程桌面连接一台windows2008server服务器,今天突然报错,连不上了:   “由于没有远程桌面授权服务器可以提供许可证,远程会话被中断.请跟服务器管理员联系.”       由于是 ...

  5. NSString 去掉前后空格或回车符

    NSString *string = @" spaces in front and at the end "; NSString *trimmedString = [string ...

  6. URL传值中文乱码

    url含有中文 先encodeURI(url)编码 获取之后再解码decodeURI //加密 var param = "itname=" + slRows.ITNAME + &q ...

  7. ListView中RadioButton实现单项选择

    1:FragmentHack5.java public class FragmentHack5 extends Fragment { View view; ListView lvCountries; ...

  8. [android]-如何在向服务器发送request时附加已保存的cookie数据

    [android]-如何在向服务器发送request时附加已保存的cookie数据 应用场景:在开发android基于手机端+服务器端的应用时,登陆->获取用户信息->获取授权用户相关业务 ...

  9. Codeforces 582B Once Again

    http://codeforces.com/contest/582/problem/B 题目大意:给出一个序列,是由一个长度为n的序列复制T次得到的,问最长非下降子序列的长度. 思路:我们建立一个n* ...

  10. 查询反模式 - 正视NULL值

    一.提出问题 不可避免地,我们都数据库总有一些字段是没有值的.不管是插入一个不完整的行,还是有些列可以合法地拥有一些无效值.SQL 支持一个特殊的空值,就是NULL. 在很多时候,NULL值导致我们的 ...