目录
背景
先决条件
运行演示
命令行选项
添加网络流量
背景
请阅读有关 QoS 设置的文档页面,以获取有关 ROS 2 中可用支持的背景信息。
在这个演示中,我们将生成一个发布相机图像的节点和另一个订阅图像并在屏幕上显示图像的节点。然后,我们将模拟它们之间的丢包网络连接,并展示不同的服务质量设置如何处理不良链接。
先决条件
本教程假设您已安装并运行 ROS 2 和 OpenCV。有关安装说明,请参阅 OpenCV 文档。您还需要 ROS 包 image_tools
。
cxy@cxy-Ubuntu2404:~$ sudo apt-get install ros-jazzy-image-tools
[sudo] password for cxy:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
ros-jazzy-image-tools
0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
Need to get 185 kB of archives.
After this operation, 1,052 kB of additional disk space will be used.
Get:1 http://packages.ros.org/ros2/ubuntu noble/main amd64 ros-jazzy-image-tools amd64 0.33.4-1noble.20240703.161128 [185 kB]
Fetched 185 kB in 4s (46.2 kB/s)
Selecting previously unselected package ros-jazzy-image-tools.
(Reading database ... 270207 files and directories currently installed.)
Preparing to unpack .../ros-jazzy-image-tools_0.33.4-1noble.20240703.161128_amd64.deb ...
Unpacking ros-jazzy-image-tools (0.33.4-1noble.20240703.161128) ...
Setting up ros-jazzy-image-tools (0.33.4-1noble.20240703.161128) ...
源码安装:
git clone https://github.com/ros2/demos.git -b jazzy
运行演示
在运行演示之前,请确保您的计算机已连接一个可用的网络摄像头。
一旦你安装了 ROS 2,请源化你的设置文件:
source ~/.bashrc
然后运行:
cxy@cxy-Ubuntu2404:~$ ros2 run image_tools showimage
[INFO] [1721611318.045341212] [showimage]: Subscribing to topic 'image'
[INFO] [1721611322.141314647] [showimage]: Received image #camera_frame
Received image #camera_frame
[INFO] [1721611323.644344772] [showimage]: Received image #camera_frame
Received image #camera_frame
还没有任何事情发生。 showimage
是一个订阅节点,正在等待 image
主题的发布者。
注意:您必须稍后使用 Ctrl-C
关闭 showimage
进程。您不能仅仅关闭窗口。
在一个单独的终端中,源安装文件并运行发布者节点:
cxy@cxy-Ubuntu2404:~$ ros2 run image_tools cam2image
[ WARN:0@1.283] global ./modules/videoio/src/cap_gstreamer.cpp (1405) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
[INFO] [1721611322.128570743] [cam2image]: Publishing image #1
[INFO] [1721611322.161307078] [cam2image]: Publishing image #2
这将从您的网络摄像头发布图像。如果您的计算机没有连接摄像头,可以使用命令行选项发布预定义的图像。
ros2 run image_tools cam2image --ros-args -p burger_mode:=True
在此窗口中,您将看到终端输出:
[INFO] [1715662452.055277255] [cam2image]: Publishing image #1
[INFO] [1715662452.119336061] [cam2image]: Publishing image #2
[INFO] [1715662452.187315139] [cam2image]: Publishing image #3
...
将弹出一个标题为“视图”的窗口,显示您的摄像头画面。在第一个窗口中,您将看到来自订阅者的输出:
[INFO] [1715662452.188906764] [showimage]: Received image #camera_frame
Received image #camera_frame
[INFO] [1715662452.252836919] [showimage]: Received image #camera_frame
Received image #camera_frame
[INFO] [1715662452.320878578] [showimage]: Received image #camera_frame
Received image #camera_frame
...
注意
macOS 用户:如果这些示例不起作用或您收到类似 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
命令行选项
在你的一个终端中,向原始命令添加一个 -h 标志:
cxy@cxy-Ubuntu2404:~$ ros2 run image_tools showimage -h
Usage: showimage [-h] [--ros-args [-p param:=value] ...]
Subscribe to an image topic and show the images.
Example: ros2 run image_tools showimage --ros-args -p reliability:=best_effort
Options:
-h, --help Display this help message and exit
Parameters:
reliability Reliability QoS setting. Either 'reliable' (default) or 'best_effort'
history History QoS setting. Either 'keep_last' (default) or 'keep_all'.
If 'keep_last', then up to N samples are stored where N is the depth
depth Depth of the publisher queue. Only honored if history QoS is 'keep_last'. Default value is 10
show_image Show the image. Either 'true' (default) or 'false'
window_name Name of the display window. Default value is the topic name
ros2 run image_tools showimage -h
用法: showimage [-h] [--ros-args [-p param:=value] ...]
订阅一个图像主题并显示图像。
示例: ros2 run image_tools showimage --ros-args -p reliability:=best_effort
选项:
-h, --help 显示此帮助信息并退出
参数:
reliability 可靠性QoS设置。可以是'reliable'(默认)或'best_effort'
history 历史QoS设置。可以是'keep_last'(默认)或'keep_all'。
如果是'keep_last',则最多存储N个样本,其中N是深度
depth 发布者队列的深度。仅在历史QoS为'keep_last'时有效。默认值为10
show_image 显示图像。可以是'true'(默认)或'false'
window_name 显示窗口的名称。默认值是主题名称
添加网络流量
警告
本演示的这一部分在 RTI 的 Connext DDS 和 Fast-DDS 上无法运行。当在同一主机上运行多个节点时,这些 DDS 实现使用共享内存和回环接口。降低回环接口的吞吐量不会影响共享内存,因此两个节点之间的流量不会受到影响。
注意
下一节是特定于 Linux 的。
然而,对于 macOS 和 Windows,你可以使用“Network Link Conditioner”(xcode 工具套件的一部分)和“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%数据包丢失。如果您使用更高分辨率的图像(例如 --ros-args -p width:=640 -p height:=480
),您可能需要尝试较低的数据包丢失率(例如 1%
)。
接下来我们启动 cam2image
和 showimage
,我们很快会注意到两个程序似乎都减慢了图像传输的速度。这是由默认 QoS 设置的行为引起的。在有损信道上强制可靠性意味着发布者(在本例中为 cam2image
)将重新发送网络数据包,直到收到消费者(即 showimage
)的确认。
现在让我们尝试运行两个程序,但使用更合适的设置。首先&