最新消息:

ROS2入门示例-使用Qos

ROS2/二代机器人系统 少儿编程 2704浏览 0评论
ROS2入门示例

ROS2入门示例-使用Qos

说明:

  • 介绍Quality of service的使用

Background

  • ROS2的Quality of service的相关参考链接:https://github.com/ros2/ros2/wiki/About-Quality-of-Service-Settings

  • 在这个demo中,首先用一个节点发布一个相机图像, 另一个节点订阅图像并在屏幕上显示。 然后, 将模拟它们之间的一个有损网络连接, 并显示不同的quality of service如何处理不良链接。

Build/install the demo

  • From pre-compiled binaries

  • 操作系统的相关下载链接:https://github.com/ros2/ros2/wiki/Installation

  • From source

  • 运行QoS demo需要用到OpenCV

  • OpenCV的相关安装说明文档: http://docs.opencv.org/doc/tutorials/introduction/table_of_content_introduction/table_of_content_introduction.html#table-of-content-introduction

Run the demo

运行demo前,请确定你的计算机上有一个可用的摄像头

  • 但若在linux上安装了ROS2,那么source一下setup.bash文件:
. <path to ROS 2 install space>/setup.bash

#<path to ROS 2 install space>为ROS2的安装路径
  • 若使用的是Windows系统,那么打开cmd
call <path to ROS 2 install space>/local_setup.bat

#<path to ROS 2 install space>为ROS2的安装路径
  • 接着执行以下命令:
ros2 run image_tools showimage
  • 由于没有相关的话题发布,故没有内容出现。Showimage是一个在image话题上等待发布者的订阅者节点。

  • 注意:必须用 Ctrl -C 关闭showimage过程。不能直接关掉终端窗口。

  • 新终端,源代码安装文件和运行发布者节点:

ros2 run image_tools cam2image
  • 这时使用摄像头拍照从而发布一张图片。 如果你的电脑上没有摄像头, 那么执行以下命令发布预先准备的图片。
ros2 run image_tools cam2image -b
  • 此时在这个终端会显示:
Publishing image #1
Publishing image #2
Publishing image #3
...
  • 同时会弹出一个标题为"view"的窗口。 在最初打开的showimag订阅者节点终端会出现:
Received image #1
Received image #2
Received image #3
...
  • 注意: 运行 cam2image 时, 可能会出现错误提示:
Publishing image #1
COMMENDSrWriterService_write:!write. Reliable large data requires asynchronous writer.
PRESPsWriter_writeInternal:!srw->write
terminate called after throwing an instance of 'std::runtime_error'
  what():  failed to publish message: failed to publish message, at /home/jackie/code/ros2_ws/src/ros2/rmw_connext/rmw_connext_cpp/src/functions.cpp:410
Aborted (core dumped)
  • You will need to launch cam2image with the reliable QoS setting turned off. Read on to learn how to do this.

  • 至于使用 OS X 系统的用户: 如果这些例子不起作用或者出现ddsi_conn_write failed -1的错误提示, 那么需要增加系统范围的 UDP 数据包大小:

$ sudo sysctl -w net.inet.udp.recvspace=209715
$ sudo sysctl -w net.inet.udp.maxdgram=65500
  • 但若想把以上的修改设置为开机自启动,请把以下代码添加到/etc/sysctl.conf文件:
net.inet.udp.recvspace=209715
net.inet.udp.maxdgram=65500
  • 命令行参数

  • 例如在ros2 run image_tools showimage命令添加-h参数,可看到相关的参数介绍

ros2 run image_tools showimage -- -h
  • -h:帮助信息。

  • -r:Reliability. There are two options for this policy: reliable or best effort. Reliable means that values may be reset and the underlying DDS publisher might block, in order for messages to get delivered in order. Best effort means that messages will get sent as is, and they may get dropped or lost without effecting the behavior of the publisher.

  • -k:History policy(“k”代表“keep”)。确定DDS如何在发布的用户代码和实际发送消息的时间之间 DDS 缓冲消息。历史记录有两个选项:KEEP_ALL和KEEP_LAST。KEEP_ALL将在发送之前缓冲所有消息。KEEP_LAST将缓冲消息的数量限制在用户指定的Queue depth。

  • -d:Queue depth。 只有在History policy被设定为KEEP_LAST的情况下才使用。 Queue depth决定了在消息开始被删除之前,在发件方缓冲的未接收消息的最大数量。

  • -s:切换显示摄像头输入流。 如果运行cam2image -s,这时会看到一个摄像头窗口。 如果同时运行showimage,这时会看到两个摄像头窗口。

  • -x-y:设置摄像头的大小(x设置宽度,y设置高度)。

  • 默认的QoS设置被调整为最大可靠性:Reliability设置是reliable,history policy设置是“KEEP_ALL”。

  • It’s worth noting that both ends must have the same reliability settings for this to work. If the consumer requires the publisher to be reliable, DDS will not match them and there won’t be any exchange between them.

  • We won’t see much of a difference if we change the quality of service settings, since the publisher and subscriber are passing messages over inter-process communication, and messages are unlikely to get dropped if they are travelling within the same machine.

  • Add network traffic

这部分教程只针对Linux系统。至于OS X 和 Windows系统,可以使用Network Link Conditione程序和Clumsy(http://jagt.github.io/clumsy/index.html)

  • 使用Linux网络流量控制工具tc:(http://linux.die.net/man/8/tc)
sudo tc qdisc add dev lo root netem loss 5%
  • 这个命令将在本地环回设备上模拟5%的数据包丢失。如果使用较高分辨率的图像(比如 -x 640 -y 480),则可能需要尝试较低的丢包率(例如1%)。

  • 接下来将打开 cam2imageshowimage 节点,可以看到图像传输的速度已经减慢。 这是由于默认的QoS设置引起的。 在有损信道上强制执行可靠性意味着发布者(在这个例子中是cam2image)将重新发送网络包, 直到它收到接收者的确认(即showimage)。

  • Let’s now try running both programs, but with more suitable settings.
    First of all, we’ll use the -r 0 option to enable best effort
    communication. The publisher will now just attempt to deliver the
    network packets, and don’t expect acknowledgement from the consumer.
    We see now that some of the frame on the showimage side were dropped,
    the frame numbers in the shell running showimage won’t be consecutive
    anymore:

ROS2入门示例-使用Qos

  • 完成后,执行以下命令,删除之前的丢包率的设置
sudo tc qdisc delete dev lo root netem loss 5%

参考链接

  • https://github.com/ros2/ros2/wiki/Quality-Of-Service

您必须 登录 才能发表评论!