当前位置:   article > 正文

【ROS2】演示:为有损网络使用服务质量设置

【ROS2】演示:为有损网络使用服务质量设置

 目录

  •  背景

  •  先决条件

  •  运行演示

 背景 

请阅读有关 QoS 设置的文档页面,以获取有关 ROS 2 中可用支持的背景信息。

在这个演示中,我们将生成一个发布相机图像的节点和另一个订阅图像并在屏幕上显示图像的节点。然后,我们将模拟它们之间的丢包网络连接,并展示不同的服务质量设置如何处理不良链接

 先决条件

本教程假设您已安装并运行 ROS 2 和 OpenCV。有关安装说明,请参阅 OpenCV 文档。您还需要 ROS 包 image_tools 。

  1. cxy@cxy-Ubuntu2404:~$ sudo apt-get install ros-jazzy-image-tools
  2. [sudo] password for cxy:
  3. Reading package lists... Done
  4. Building dependency tree... Done
  5. Reading state information... Done
  6. The following NEW packages will be installed:
  7. ros-jazzy-image-tools
  8. 0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
  9. Need to get 185 kB of archives.
  10. After this operation, 1,052 kB of additional disk space will be used.
  11. Get:1 http://packages.ros.org/ros2/ubuntu noble/main amd64 ros-jazzy-image-tools amd64 0.33.4-1noble.20240703.161128 [185 kB]
  12. Fetched 185 kB in 4s (46.2 kB/s)
  13. Selecting previously unselected package ros-jazzy-image-tools.
  14. (Reading database ... 270207 files and directories currently installed.)
  15. Preparing to unpack .../ros-jazzy-image-tools_0.33.4-1noble.20240703.161128_amd64.deb ...
  16. Unpacking ros-jazzy-image-tools (0.33.4-1noble.20240703.161128) ...
  17. 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

 然后运行:

  1. cxy@cxy-Ubuntu2404:~$ ros2 run image_tools showimage
  2. [INFO] [1721611318.045341212] [showimage]: Subscribing to topic 'image'
  3. [INFO] [1721611322.141314647] [showimage]: Received image #camera_frame
  4. Received image #camera_frame
  5. [INFO] [1721611323.644344772] [showimage]: Received image #camera_frame
  6. Received image #camera_frame

还没有任何事情发生。 showimage 是一个订阅节点,正在等待 image 主题的发布者。

注意:您必须稍后使用 Ctrl-C 关闭 showimage 进程。您不能仅仅关闭窗口。

在一个单独的终端中,源安装文件并运行发布者节点:

  1. cxy@cxy-Ubuntu2404:~$ ros2 run image_tools cam2image
  2. [ 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
  3. [INFO] [1721611322.128570743] [cam2image]: Publishing image #1
  4. [INFO] [1721611322.161307078] [cam2image]: Publishing image #2

24010e9ea45b894544179632ebb74087.png

这将从您的网络摄像头发布图像。如果您的计算机没有连接摄像头,可以使用命令行选项发布预定义的图像。

ros2 run image_tools cam2image --ros-args -p burger_mode:=True

230ad898567b99d6edbb643dacaba7e3.png

在此窗口中,您将看到终端输出:

  1. [INFO] [1715662452.055277255] [cam2image]: Publishing image #1
  2. [INFO] [1715662452.119336061] [cam2image]: Publishing image #2
  3. [INFO] [1715662452.187315139] [cam2image]: Publishing image #3
  4. ...

将弹出一个标题为“视图”的窗口,显示您的摄像头画面。在第一个窗口中,您将看到来自订阅者的输出:

  1. [INFO] [1715662452.188906764] [showimage]: Received image #camera_frame
  2. Received image #camera_frame
  3. [INFO] [1715662452.252836919] [showimage]: Received image #camera_frame
  4. Received image #camera_frame
  5. [INFO] [1715662452.320878578] [showimage]: Received image #camera_frame
  6. Received image #camera_frame
  7. ...

 注意

macOS 用户:如果这些示例不起作用或您收到类似 ddsi_conn_write failed -1 的错误,则需要增加系统范围的 UDP 数据包大小

  1. $ sudo sysctl -w net.inet.udp.recvspace=209715
  2. $ sudo sysctl -w net.inet.udp.maxdgram=65500
这些更改在重启后不会保留。如果您希望更改保留,请将这些行添加到 /etc/sysctl.conf (如果文件尚不存在,请创建该文件):
  1. net.inet.udp.recvspace=209715
  2. net.inet.udp.maxdgram=65500

命令行选项

在你的一个终端中,向原始命令添加一个 -h 标志:

  1. cxy@cxy-Ubuntu2404:~$ ros2 run image_tools showimage -h
  2. Usage: showimage [-h] [--ros-args [-p param:=value] ...]
  3. Subscribe to an image topic and show the images.
  4. Example: ros2 run image_tools showimage --ros-args -p reliability:=best_effort
  5. Options:
  6. -h, --help Display this help message and exit
  7. Parameters:
  8. reliability Reliability QoS setting. Either 'reliable' (default) or 'best_effort'
  9. history History QoS setting. Either 'keep_last' (default) or 'keep_all'.
  10. If 'keep_last', then up to N samples are stored where N is the depth
  11. depth Depth of the publisher queue. Only honored if history QoS is 'keep_last'. Default value is 10
  12. show_image Show the image. Either 'true' (default) or 'false'
  13.   window_name  Name of the display window. Default value is the topic name
  1. ros2 run image_tools showimage -h
  2. 用法: showimage [-h] [--ros-args [-p param:=value] ...]
  3. 订阅一个图像主题并显示图像。
  4. 示例: ros2 run image_tools showimage --ros-args -p reliability:=best_effort
  5. 选项:
  6. -h, --help 显示此帮助信息并退出
  7. 参数:
  8. reliability 可靠性QoS设置。可以是'reliable'(默认)或'best_effort'
  9. history 历史QoS设置。可以是'keep_last'(默认)或'keep_all'
  10. 如果是'keep_last',则最多存储N个样本,其中N是深度
  11. depth 发布者队列的深度。仅在历史QoS为'keep_last'时有效。默认值为10
  12. show_image 显示图像。可以是'true'(默认)或'false'
  13.   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%

c73b6f7ed0306ba339bb5c020947c19e.png

这个神奇的咒语将模拟本地回环设备上的 5%数据包丢失。如果您使用更高分辨率的图像(例如 --ros-args -p width:=640 -p height:=480 ),您可能需要尝试较低的数据包丢失率(例如 1% )。

ab7721450c0a72a4c3be3646d52d36de.png

接下来我们启动 cam2image 和 showimage ,我们很快会注意到两个程序似乎都减慢了图像传输的速度。这是由默认 QoS 设置的行为引起的。在有损信道上强制可靠性意味着发布者(在本例中为 cam2image )将重新发送网络数据包,直到收到消费者(即 showimage )的确认。

现在让我们尝试运行两个程序,但使用更合适的设置。首先,我们将使用 -p reliability:=best_effort 选项来启用尽力而为的通信。发布者现在只会尝试传递网络数据包,并且不期望消费者的确认。我们现在看到 showimage 端的一些帧被丢弃了,所以在运行 showimage 的 shell 中的帧号将不再是连续的。

  1. ros2 run image_tools cam2image --ros-args -p reliability:=best_effort
  2.  ros2 run image_tools showimage --ros-args --param reliability:=best_effort

完成后,请记得删除排队规则

sudo tc qdisc delete dev lo root netem loss 5%

68fcbdb7693ab69a615ada9ea5e79540.png

附录:

45f016740e7e70a00616ef2061cbb2cc.png

  1. cxy@cxy-Ubuntu2404:~/second_ros2_ws/src/demos/image_tools$ tree
  2. .
  3. ├── CHANGELOG.rst
  4. ├── CMakeLists.txt
  5. ├── doc
  6. │   └── qos-best-effort.png
  7. ├── Doxyfile
  8. ├── img
  9. │   └── result.png
  10. ├── include
  11. │   └── image_tools
  12. │   ├── cv_mat_sensor_msgs_image_type_adapter.hpp
  13. │   └── visibility_control.h
  14. ├── package.xml
  15. ├── README.md
  16. ├── src
  17. │   ├── burger.cpp
  18. │   ├── burger.hpp
  19. │   ├── cam2image.cpp
  20. │   ├── cv_mat_sensor_msgs_image_type_adapter.cpp
  21. │   ├── policy_maps.hpp
  22. │   └── showimage.cpp
  23. └── test
  24. ├── cam2image.txt
  25. ├── showimage.regex
  26. └── test_executables_demo.py.in
  27. 7 directories, 18 files

CMakeLists.txt

  1. cmake_minimum_required(VERSION 3.5) # 设置CMake的最低版本为3.5
  2. project(image_tools) # 定义项目名称为image_tools
  3. # Default to C++17
  4. if(NOT CMAKE_CXX_STANDARD) # 如果没有设置C++标准
  5. set(CMAKE_CXX_STANDARD 17) # 设置C++标准为C++17
  6. set(CMAKE_CXX_STANDARD_REQUIRED ON) # 强制使用C++17标准
  7. endif()
  8. if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") # 如果使用的是GNU编译器或Clang编译器
  9. add_compile_options(-Wall -Wextra -Wpedantic) # 添加编译选项:显示所有警告、额外警告和严格警告
  10. endif()
  11. find_package(ament_cmake REQUIRED) # 查找ament_cmake包,必需
  12. find_package(rclcpp REQUIRED) # 查找rclcpp包,必需
  13. find_package(rclcpp_components REQUIRED) # 查找rclcpp_components包,必需
  14. find_package(sensor_msgs REQUIRED) # 查找sensor_msgs包,必需
  15. find_package(std_msgs REQUIRED) # 查找std_msgs包,必需
  16. find_package(OpenCV REQUIRED COMPONENTS core highgui imgcodecs imgproc videoio) # 查找OpenCV包及其组件
  17. add_library(${PROJECT_NAME} SHARED # 添加共享库
  18. src/burger.cpp
  19. src/cam2image.cpp
  20. src/cv_mat_sensor_msgs_image_type_adapter.cpp
  21. src/showimage.cpp
  22. )
  23. target_compile_definitions(${PROJECT_NAME}
  24. PRIVATE "IMAGE_TOOLS_BUILDING_DLL") # 定义编译宏
  25. target_include_directories(${PROJECT_NAME} PUBLIC
  26. "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>" # 设置包含目录
  27. "$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
  28. target_link_libraries(${PROJECT_NAME}
  29. rclcpp::rclcpp # 链接rclcpp库
  30. ${sensor_msgs_TARGETS} # 链接sensor_msgs库
  31. ${std_msgs_TARGETS} # 链接std_msgs库
  32. rclcpp_components::component # 链接rclcpp_components库
  33. ${OpenCV_LIBS}) # 链接OpenCV库
  34. rclcpp_components_register_node(${PROJECT_NAME} PLUGIN "image_tools::Cam2Image" EXECUTABLE cam2image) # 注册节点Cam2Image
  35. rclcpp_components_register_node(${PROJECT_NAME} PLUGIN "image_tools::ShowImage" EXECUTABLE showimage) # 注册节点ShowImage
  36. install(
  37. TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME} # 安装目标文件
  38. ARCHIVE DESTINATION lib # 安装归档文件到lib目录
  39. LIBRARY DESTINATION lib # 安装库文件到lib目录
  40. RUNTIME DESTINATION bin) # 安装可执行文件到bin目录
  41. install(DIRECTORY include/ DESTINATION include/${PROJECT_NAME}) # 安装包含目录
  42. # TODO(sloretz) stop exporting old-style CMake variables in the future
  43. ament_export_libraries(${PROJECT_NAME}) # 导出库
  44. ament_export_targets(${PROJECT_NAME}) # 导出目标
  45. ament_export_dependencies(rclcpp_components) # 导出依赖
  46. if(BUILD_TESTING) # 如果启用了测试
  47. find_package(ament_lint_auto REQUIRED) # 查找ament_lint_auto包,必需
  48. ament_lint_auto_find_test_dependencies() # 查找测试依赖
  49. find_package(ament_cmake_pytest REQUIRED) # 查找ament_cmake_pytest包,必需
  50. find_package(launch_testing_ament_cmake REQUIRED) # 查找launch_testing_ament_cmake包,必需
  51. find_package(rmw_implementation_cmake REQUIRED) # 查找rmw_implementation_cmake包,必需
  52. # These are the regex's for validating the output of the executables.
  53. set(RCLCPP_DEMO_SHOWIMAGE_EXPECTED_OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/test/showimage") # 设置showimage的预期输出
  54. set(RCLCPP_DEMO_CAM2IMAGE_EXPECTED_OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/test/cam2image") # 设置cam2image的预期输出
  55. macro(testing_targets) # 定义宏testing_targets
  56. set(RCLCPP_DEMO_CAM2IMAGE_EXECUTABLE $<TARGET_FILE:cam2image>) # 设置cam2image可执行文件
  57. set(RCLCPP_DEMO_SHOWIMAGE_EXECUTABLE $<TARGET_FILE:showimage>) # 设置showimage可执行文件
  58. configure_file(
  59. test/test_executables_demo.py.in
  60. test_showimage_cam2image${target_suffix}.py.genexp
  61. @ONLY
  62. ) # 配置文件
  63. file(GENERATE
  64. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test_showimage_cam2image${target_suffix}_$<CONFIG>.py"
  65. INPUT "${CMAKE_CURRENT_BINARY_DIR}/test_showimage_cam2image${target_suffix}.py.genexp"
  66. ) # 生成文件
  67. add_launch_test(
  68. "${CMAKE_CURRENT_BINARY_DIR}/test_showimage_cam2image${target_suffix}_$<CONFIG>.py"
  69. TARGET test_showimage_cam2image${target_suffix}
  70. ENV
  71. RCL_ASSERT_RMW_ID_MATCHES=${rmw_implementation}
  72. RMW_IMPLEMENTATION=${rmw_implementation}
  73. TIMEOUT 30
  74. ) # 添加启动测试
  75. endmacro()
  76. call_for_each_rmw_implementation(testing_targets) # 为每个rmw实现调用testing_targets
  77. endif()
  78. ament_package() # 定义ament包

package.xml

  1. <!-- 声明XML版本为1.0 -->
  2. <?xml version="1.0"?>
  3. <!-- 声明XML模型的链接和模型类型 -->
  4. <?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
  5. <!-- 声明包的格式为2 -->
  6. <package format="2">
  7. <!-- 声明包的名称为image_tools -->
  8. <name>image_tools</name>
  9. <!-- 声明包的版本为0.33.4 -->
  10. <version>0.33.4</version>
  11. <!-- 声明包的描述 -->
  12. <description>Tools to capture and play back images to and from DDS subscriptions and publications.</description>
  13. <!-- 声明包的维护者 -->
  14. <maintainer email="aditya.pande@openrobotics.org">Aditya Pande</maintainer>
  15. <maintainer email="audrow@openrobotics.org">Audrow Nash</maintainer>
  16. <!-- 声明包的许可证 -->
  17. <license>Apache License 2.0</license>
  18. <!-- 声明包的作者 -->
  19. <author email="dthomas@osrfoundation.org">Dirk Thomas</author>
  20. <author email="mabel@openrobotics.org">Mabel Zhang</author>
  21. <!-- 声明构建工具依赖 -->
  22. <buildtool_depend>ament_cmake</buildtool_depend>
  23. <!-- 声明构建依赖 -->
  24. <build_depend>libopencv-dev</build_depend>
  25. <build_depend>rclcpp</build_depend>
  26. <build_depend>rclcpp_components</build_depend>
  27. <build_depend>sensor_msgs</build_depend>
  28. <build_depend>std_msgs</build_depend>
  29. <!-- 声明执行依赖 -->
  30. <exec_depend>libopencv-dev</exec_depend>
  31. <exec_depend>rclcpp</exec_depend>
  32. <exec_depend>rclcpp_components</exec_depend>
  33. <exec_depend>sensor_msgs</exec_depend>
  34. <exec_depend>std_msgs</exec_depend>
  35. <!-- 声明测试依赖 -->
  36. <test_depend>ament_cmake_pytest</test_depend>
  37. <test_depend>ament_lint_auto</test_depend>
  38. <test_depend>ament_lint_common</test_depend>
  39. <test_depend>launch</test_depend>
  40. <test_depend>launch_ros</test_depend>
  41. <test_depend>launch_testing</test_depend>
  42. <test_depend>launch_testing_ament_cmake</test_depend>
  43. <test_depend>launch_testing_ros</test_depend>
  44. <test_depend>rmw_implementation_cmake</test_depend>
  45. <!-- 声明导出信息 -->
  46. <export>
  47. <!-- 声明构建类型 -->
  48. <build_type>ament_cmake</build_type>
  49. </export>
  50. </package>

showimage.cpp

  1. #include <iostream> // 包含输入输出流库
  2. #include <sstream> // 包含字符串流库
  3. #include <string> // 包含字符串库
  4. #include <vector> // 包含向量库
  5. #include "opencv2/core/mat.hpp" // 包含OpenCV核心矩阵库
  6. #include "opencv2/highgui.hpp" // 包含OpenCV高层GUI库
  7. #include "opencv2/imgproc.hpp" // 包含OpenCV图像处理库
  8. #include "rcl_interfaces/msg/parameter_descriptor.hpp" // 包含参数描述符消息
  9. #include "rclcpp/rclcpp.hpp" // 包含rclcpp库
  10. #include "rclcpp_components/register_node_macro.hpp" // 包含注册节点宏
  11. #include "sensor_msgs/msg/image.hpp" // 包含图像消息
  12. #include "image_tools/cv_mat_sensor_msgs_image_type_adapter.hpp" // 包含自定义类型适配器
  13. #include "image_tools/visibility_control.h" // 包含可见性控制头文件
  14. #include "./policy_maps.hpp" // 包含策略映射头文件
  15. RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(
  16. image_tools::ROSCvMatContainer,
  17. sensor_msgs::msg::Image); // 使用自定义类型作为ROS消息类型
  18. namespace image_tools // 定义命名空间image_tools
  19. {
  20. class ShowImage : public rclcpp::Node // 定义ShowImage类,继承自rclcpp::Node
  21. {
  22. public:
  23. IMAGE_TOOLS_PUBLIC
  24. explicit ShowImage(const rclcpp::NodeOptions & options) // 显式构造函数
  25. : Node("showimage", options) // 调用基类构造函数,初始化节点名称为"showimage"
  26. {
  27. setvbuf(stdout, NULL, _IONBF, BUFSIZ); // 设置标准输出缓冲区
  28. // Do not execute if a --help option was provided
  29. if (help(options.arguments())) { // 如果提供了--help选项,则不执行
  30. // TODO(jacobperron): Replace with a mechanism for a node to "unload" itself
  31. // from a container.
  32. exit(0); // 退出程序
  33. }
  34. parse_parameters(); // 解析参数
  35. initialize(); // 初始化
  36. }
  37. private:
  38. IMAGE_TOOLS_LOCAL
  39. void initialize() // 初始化函数
  40. {
  41. // Set quality of service profile based on command line options.
  42. auto qos = rclcpp::QoS(
  43. rclcpp::QoSInitialization(
  44. // The history policy determines how messages are saved until taken by
  45. // the reader.
  46. // KEEP_ALL saves all messages until they are taken.
  47. // KEEP_LAST enforces a limit on the number of messages that are saved,
  48. // specified by the "depth" parameter.
  49. history_policy_, // 设置历史策略
  50. // Depth represents how many messages to store in history when the
  51. // history policy is KEEP_LAST.
  52. depth_ // 设置深度
  53. ));
  54. // The reliability policy can be reliable, meaning that the underlying transport layer will try
  55. // ensure that every message gets received in order, or best effort, meaning that the transport
  56. // makes no guarantees about the order or reliability of delivery.
  57. qos.reliability(reliability_policy_); // 设置可靠性策略
  58. auto callback =
  59. this { // 定义回调函数
  60. process_image(container, show_image_, this->get_logger()); // 处理图像
  61. };
  62. RCLCPP_INFO(this->get_logger(), "Subscribing to topic '%s'", topic_.c_str()); // 打印订阅信息
  63. sub_ = create_subscription<image_tools::ROSCvMatContainer>(topic_, qos, callback); // 创建订阅
  64. if (window_name_ == "") { // 如果没有自定义窗口名称
  65. // If no custom window name is given, use the topic name
  66. window_name_ = sub_->get_topic_name(); // 使用主题名称作为窗口名称
  67. }
  68. }
  69. IMAGE_TOOLS_LOCAL
  70. bool help(const std::vector<std::string> args) // 帮助函数
  71. {
  72. if (std::find(args.begin(), args.end(), "--help") != args.end() ||
  73. std::find(args.begin(), args.end(), "-h") != args.end()) // 如果提供了--help或-h选项
  74. {
  75. std::stringstream ss; // 创建字符串流
  76. ss << "Usage: showimage [-h] [--ros-args [-p param:=value] ...]" << std::endl; // 打印用法
  77. ss << "Subscribe to an image topic and show the images." << std::endl; // 打印描述
  78. ss << "Example: ros2 run image_tools showimage --ros-args -p reliability:=best_effort"; // 打印示例
  79. ss << std::endl << std::endl;
  80. ss << "Options:" << std::endl; // 打印选项
  81. ss << " -h, --help\tDisplay this help message and exit"; // 打印帮助选项
  82. ss << std::endl << std::endl;
  83. ss << "Parameters:" << std::endl; // 打印参数
  84. ss << " reliability\tReliability QoS setting. Either 'reliable' (default) or 'best_effort'"; // 打印可靠性参数
  85. ss << std::endl;
  86. ss << " history\tHistory QoS setting. Either 'keep_last' (default) or 'keep_all'."; // 打印历史参数
  87. ss << std::endl;
  88. ss << "\t\tIf 'keep_last', then up to N samples are stored where N is the depth"; // 打印深度参数
  89. ss << std::endl;
  90. ss << " depth\t\tDepth of the publisher queue. Only honored if history QoS is 'keep_last'."; // 打印深度参数
  91. ss << " Default value is 10"; // 打印默认值
  92. ss << std::endl;
  93. ss << " show_image\tShow the image. Either 'true' (default) or 'false'"; // 打印显示图像参数
  94. ss << std::endl;
  95. ss << " window_name\tName of the display window. Default value is the topic name"; // 打印窗口名称参数
  96. ss << std::endl;
  97. std::cout << ss.str(); // 输出帮助信息
  98. return true; // 返回true
  99. }
  100. return false; // 返回false
  101. }
  102. IMAGE_TOOLS_LOCAL
  103. void parse_parameters() // 解析参数函数
  104. {
  105. // Parse 'reliability' parameter
  106. rcl_interfaces::msg::ParameterDescriptor reliability_desc; // 创建参数描述符
  107. reliability_desc.description = "Reliability QoS setting for the image subscription"; // 设置描述
  108. reliability_desc.additional_constraints = "Must be one of: "; // 设置附加约束
  109. for (auto entry : name_to_reliability_policy_map) { // 遍历可靠性策略映射
  110. reliability_desc.additional_constraints += entry.first + " "; // 添加约束
  111. }
  112. const std::string reliability_param = this->declare_parameter(
  113. "reliability", "reliable", reliability_desc); // 声明参数
  114. auto reliability = name_to_reliability_policy_map.find(reliability_param); // 查找参数
  115. if (reliability == name_to_reliability_policy_map.end()) { // 如果参数无效
  116. std::ostringstream oss; // 创建字符串流
  117. oss << "Invalid QoS reliability setting '" << reliability_param << "'"; // 打印错误信息
  118. throw std::runtime_error(oss.str()); // 抛出运行时错误
  119. }
  120. reliability_policy_ = reliability->second; // 设置可靠性策略
  121. // Parse 'history' parameter
  122. rcl_interfaces::msg::ParameterDescriptor history_desc; // 创建参数描述符
  123. history_desc.description = "History QoS setting for the image subscription"; // 设置描述
  124. history_desc.additional_constraints = "Must be one of: "; // 设置附加约束
  125. for (auto entry : name_to_history_policy_map) { // 遍历历史策略映射
  126. history_desc.additional_constraints += entry.first + " "; // 添加约束
  127. }
  128. const std::string history_param = this->declare_parameter(
  129. "history", name_to_history_policy_map.begin()->first, history_desc); // 声明参数
  130. auto history = name_to_history_policy_map.find(history_param); // 查找参数
  131. if (history == name_to_history_policy_map.end()) { // 如果参数无效
  132. std::ostringstream oss; // 创建字符串流
  133. oss << "Invalid QoS history setting '" << history_param << "'"; // 打印错误信息
  134. throw std::runtime_error(oss.str()); // 抛出运行时错误
  135. }
  136. history_policy_ = history->second; // 设置历史策略
  137. // Declare and get remaining parameters
  138. depth_ = this->declare_parameter("depth", 10); // 声明深度参数
  139. show_image_ = this->declare_parameter("show_image", true);
  140. window_name_ = this->declare_parameter("window_name", ""); // 声明窗口名称参数
  141. }
  142. /// Convert the ROS Image message to an OpenCV matrix and display it to the user.
  143. // \param[in] container The image message to show.
  144. IMAGE_TOOLS_LOCAL
  145. void process_image(
  146. const image_tools::ROSCvMatContainer & container, bool show_image, rclcpp::Logger logger) // 处理图像函数
  147. {
  148. RCLCPP_INFO(logger, "Received image #%s", container.header().frame_id.c_str()); // 打印接收图像信息
  149. std::cerr << "Received image #" << container.header().frame_id.c_str() << std::endl; // 打印接收图像信息
  150. if (show_image) { // 如果显示图像
  151. cv::Mat frame = container.cv_mat(); // 获取图像矩阵
  152. if (frame.type() == CV_8UC3 /* rgb8 */) { // 如果图像类型为CV_8UC3
  153. cv::cvtColor(frame, frame, cv::COLOR_RGB2BGR); // 转换颜色空间
  154. } else if (frame.type() == CV_8UC2) { // 如果图像类型为CV_8UC2
  155. container.is_bigendian() ? cv::cvtColor(frame, frame, cv::COLOR_YUV2BGR_UYVY) :
  156. cv::cvtColor(frame, frame, cv::COLOR_YUV2BGR_YUYV); // 转换颜色空间
  157. }
  158. // Show the image in a window
  159. cv::imshow(window_name_, frame); // 在窗口中显示图像
  160. // Draw the screen and wait for 1 millisecond.
  161. cv::waitKey(1); // 刷新窗口并等待1毫秒
  162. }
  163. }
  164. rclcpp::Subscription<image_tools::ROSCvMatContainer>::SharedPtr sub_; // 订阅对象
  165. size_t depth_ = rmw_qos_profile_default.depth; // 深度参数
  166. rmw_qos_reliability_policy_t reliability_policy_ = rmw_qos_profile_default.reliability; // 可靠性策略
  167. rmw_qos_history_policy_t history_policy_ = rmw_qos_profile_default.history; // 历史策略
  168. bool show_image_ = true; // 是否显示图像
  169. std::string topic_ = "image"; // 主题名称
  170. std::string window_name_; // 窗口名称
  171. };
  172. } // namespace image_tools
  173. RCLCPP_COMPONENTS_REGISTER_NODE(image_tools::ShowImage) // 注册节点

cam2image.cpp

  1. #include <chrono> // 包含时间库
  2. #include <memory> // 包含智能指针库
  3. #include <sstream> // 包含字符串流库
  4. #include <string> // 包含字符串库
  5. #include <utility> // 包含实用工具库
  6. #include <vector> // 包含向量库
  7. #include "opencv2/core/mat.hpp" // 包含OpenCV核心矩阵库
  8. #include "opencv2/core.hpp" // 包含OpenCV核心库
  9. #include "opencv2/highgui.hpp" // 包含OpenCV高层GUI库
  10. #include "opencv2/videoio.hpp" // 包含OpenCV视频输入输出库
  11. #include "rcl_interfaces/msg/parameter_descriptor.hpp" // 包含参数描述符消息
  12. #include "rclcpp/rclcpp.hpp" // 包含rclcpp库
  13. #include "rclcpp_components/register_node_macro.hpp" // 包含注册节点宏
  14. #include "std_msgs/msg/bool.hpp" // 包含布尔消息
  15. #include "image_tools/cv_mat_sensor_msgs_image_type_adapter.hpp" // 包含自定义类型适配器
  16. #include "image_tools/visibility_control.h" // 包含可见性控制头文件
  17. #include "./burger.hpp" // 包含汉堡头文件
  18. #include "./policy_maps.hpp" // 包含策略映射头文件
  19. RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(
  20. image_tools::ROSCvMatContainer,
  21. sensor_msgs::msg::Image); // 使用自定义类型作为ROS消息类型
  22. namespace image_tools // 定义命名空间image_tools
  23. {
  24. class Cam2Image : public rclcpp::Node // 定义Cam2Image类,继承自rclcpp::Node
  25. {
  26. public:
  27. IMAGE_TOOLS_PUBLIC
  28. explicit Cam2Image(const rclcpp::NodeOptions & options) // 显式构造函数
  29. : Node("cam2image", options), // 调用基类构造函数,初始化节点名称为"cam2image"
  30. is_flipped_(false), // 初始化是否翻转图像为false
  31. publish_number_(1u) // 初始化发布编号为1
  32. {
  33. setvbuf(stdout, NULL, _IONBF, BUFSIZ); // 设置标准输出缓冲区
  34. // Do not execute if a --help option was provided
  35. if (help(options.arguments())) { // 如果提供了--help选项,则不执行
  36. // TODO(jacobperron): Replace with a mechanism for a node to "unload" itself
  37. // from a container.
  38. exit(0); // 退出程序
  39. }
  40. parse_parameters(); // 解析参数
  41. initialize(); // 初始化
  42. }
  43. private:
  44. IMAGE_TOOLS_LOCAL
  45. void initialize() // 初始化函数
  46. {
  47. auto qos = rclcpp::QoS(
  48. rclcpp::QoSInitialization(
  49. // The history policy determines how messages are saved until taken by
  50. // the reader.
  51. // KEEP_ALL saves all messages until they are taken.
  52. // KEEP_LAST enforces a limit on the number of messages that are saved,
  53. // specified by the "depth" parameter.
  54. history_policy_, // 设置历史策略
  55. // Depth represents how many messages to store in history when the
  56. // history policy is KEEP_LAST.
  57. depth_ // 设置深度
  58. ));
  59. // The reliability policy can be reliable, meaning that the underlying transport layer will try
  60. // ensure that every message gets received in order, or best effort, meaning that the transport
  61. // makes no guarantees about the order or reliability of delivery.
  62. qos.reliability(reliability_policy_); // 设置可靠性策略
  63. pub_ = create_publisher<image_tools::ROSCvMatContainer>("image", qos); // 创建发布者
  64. // Subscribe to a message that will toggle flipping or not flipping, and manage the state in a
  65. // callback
  66. auto callback = this -> void // 定义回调函数
  67. {
  68. this->is_flipped_ = msg->data; // 设置是否翻转图像
  69. RCLCPP_INFO(this->get_logger(), "Set flip mode to: %s", this->is_flipped_ ? "on" : "off"); // 打印翻转模式信息
  70. };
  71. // Set the QoS profile for the subscription to the flip message.
  72. sub_ = create_subscription<std_msgs::msg::Bool>(
  73. "flip_image", rclcpp::SensorDataQoS(), callback); // 创建订阅
  74. if (!burger_mode_) { // 如果不是汉堡模式
  75. // Initialize OpenCV video capture stream.
  76. cap.open(device_id_); // 打开视频捕获设备
  77. // Set the width and height based on command line arguments.
  78. cap.set(cv::CAP_PROP_FRAME_WIDTH, static_cast<double>(width_)); // 设置宽度
  79. cap.set(cv::CAP_PROP_FRAME_HEIGHT, static_cast<double>(height_)); // 设置高度
  80. if (!cap.isOpened()) { // 如果视频流未打开
  81. RCLCPP_ERROR(this->get_logger(), "Could not open video stream"); // 打印错误信息
  82. throw std::runtime_error("Could not open video stream"); // 抛出运行时错误
  83. }
  84. }
  85. // Start main timer loop
  86. timer_ = this->create_wall_timer(
  87. std::chrono::milliseconds(static_cast<int>(1000.0 / freq_)), // 设置定时器频率
  88. this {return this->timerCallback();}); // 设置定时器回调函数
  89. }
  90. /// Publish camera, or burger, image.
  91. IMAGE_TOOLS_LOCAL
  92. void timerCallback() // 定时器回调函数
  93. {
  94. cv::Mat frame; // 定义图像矩阵
  95. // Initialize a shared pointer to an Image message.
  96. // Get the frame from the video capture.
  97. if (burger_mode_) { // 如果是汉堡模式
  98. frame = burger_cap.render_burger(width_, height_); // 渲染汉堡图像
  99. } else {
  100. cap >> frame; // 从视频捕获设备获取图像
  101. }
  102. // If no frame was grabbed, return early
  103. if (frame.empty()) { // 如果未获取到图像
  104. return; // 提前返回
  105. }
  106. // Conditionally flip the image
  107. if (is_flipped_) { // 如果需要翻转图像
  108. cv::flip(frame, frame, 1); // 翻转图像
  109. }
  110. // Conditionally show image
  111. if (show_camera_) { // 如果需要显示图像
  112. // Show the image in a window called "cam2image".
  113. cv::imshow("cam2image", frame); // 在窗口中显示图像
  114. // Draw the image to the screen and wait 1 millisecond.
  115. cv::waitKey(1); // 刷新窗口并等待1毫秒
  116. }
  117. std_msgs::msg::Header header; // 定义消息头
  118. header.frame_id = frame_id_; // 设置帧ID
  119. header.stamp = this->now(); // 设置时间戳
  120. image_tools::ROSCvMatContainer container(frame, header); // 创建图像容器
  121. // Publish the image message and increment the publish_number_.
  122. RCLCPP_INFO(get_logger(), "Publishing image #%zd", publish_number_++); // 打印发布信息
  123. pub_->publish(std::move(container)); // 发布图像消息
  124. }
  125. IMAGE_TOOLS_LOCAL
  126. bool help(const std::vector<std::string> & args) // 帮助函数
  127. {
  128. if (std::find(args.begin(), args.end(), "--help") != args.end() ||
  129. std::find(args.begin(), args.end(), "-h") != args.end()) // 如果提供了--help或-h选项
  130. {
  131. std::stringstream ss; // 创建字符串流
  132. ss << "Usage: cam2image [-h] [--ros-args [-p param:=value] ...]" << std::endl; // 打印用法
  133. ss << "Publish images from a camera stream." << std::endl; // 打印描述
  134. ss << "Example: ros2 run image_tools cam2image --ros-args -p reliability:=best_effort"; // 打印示例
  135. ss << std::endl << std::endl;
  136. ss << "Options:" << std::endl; // 打印选项
  137. ss << " -h, --help\tDisplay this help message and exit"; // 打印帮助选项
  138. ss << std::endl << std::endl;
  139. ss << "Parameters:" << std::endl; // 打印参数
  140. ss << " reliability\tReliability QoS setting. Either 'reliable' (default) or 'best_effort'"; // 打印可靠性参数
  141. ss << std::endl;
  142. ss << " history\tHistory QoS setting. Either 'keep_last' (default) or 'keep_all'."; // 打印历史参数
  143. ss << std::endl;
  144. ss << "\t\tIf 'keep_last', then up to N samples are stored where N is the depth"; // 打
  145. ss << std::endl;
  146. ss << " depth\t\tDepth of the publisher queue. Only honored if history QoS is 'keep_last'."; // 打印深度参数
  147. ss << " Default value is 10"; // 打印默认值
  148. ss << std::endl;
  149. ss << " frequency\tPublish frequency in Hz. Default value is 30"; // 打印频率参数
  150. ss << std::endl;
  151. ss << " burger_mode\tProduce images of burgers rather than connecting to a camera"; // 打印汉堡模式参数
  152. ss << std::endl;
  153. ss << " show_camera\tShow camera stream. Either 'true' or 'false' (default)"; // 打印显示摄像头参数
  154. ss << std::endl;
  155. ss << " device_id\tDevice ID of the camera. 0 (default) selects the default camera device."; // 打印设备ID参数
  156. ss << std::endl;
  157. ss << " width\t\tWidth component of the camera stream resolution. Default value is 320"; // 打印宽度参数
  158. ss << std::endl;
  159. ss << " height\tHeight component of the camera stream resolution. Default value is 240"; // 打印高度参数
  160. ss << std::endl;
  161. ss << " frame_id\t\tID of the sensor frame. Default value is 'camera_frame'"; // 打印帧ID参数
  162. ss << std::endl << std::endl;
  163. ss << "Note: try running v4l2-ctl --list-formats-ext to obtain a list of valid values."; // 打印提示信息
  164. ss << std::endl;
  165. std::cout << ss.str(); // 输出帮助信息
  166. return true; // 返回true
  167. }
  168. return false; // 返回false
  169. }
  170. IMAGE_TOOLS_LOCAL
  171. void parse_parameters() // 解析参数函数
  172. {
  173. // Parse 'reliability' parameter
  174. rcl_interfaces::msg::ParameterDescriptor reliability_desc; // 创建参数描述符
  175. reliability_desc.description = "Reliability QoS setting for the image publisher"; // 设置描述
  176. reliability_desc.additional_constraints = "Must be one of: "; // 设置附加约束
  177. for (auto entry : name_to_reliability_policy_map) { // 遍历可靠性策略映射
  178. reliability_desc.additional_constraints += entry.first + " "; // 添加约束
  179. }
  180. const std::string reliability_param = this->declare_parameter(
  181. "reliability", "reliable", reliability_desc); // 声明参数
  182. auto reliability = name_to_reliability_policy_map.find(reliability_param); // 查找参数
  183. if (reliability == name_to_reliability_policy_map.end()) { // 如果参数无效
  184. std::ostringstream oss; // 创建字符串流
  185. oss << "Invalid QoS reliability setting '" << reliability_param << "'"; // 打印错误信息
  186. throw std::runtime_error(oss.str()); // 抛出运行时错误
  187. }
  188. reliability_policy_ = reliability->second; // 设置可靠性策略
  189. // Parse 'history' parameter
  190. rcl_interfaces::msg::ParameterDescriptor history_desc; // 创建参数描述符
  191. history_desc.description = "History QoS setting for the image publisher"; // 设置描述
  192. history_desc.additional_constraints = "Must be one of: "; // 设置附加约束
  193. for (auto entry : name_to_history_policy_map) { // 遍历历史策略映射
  194. history_desc.additional_constraints += entry.first + " "; // 添加约束
  195. }
  196. const std::string history_param = this->declare_parameter(
  197. "history", name_to_history_policy_map.begin()->first, history_desc); // 声明参数
  198. auto history = name_to_history_policy_map.find(history_param); // 查找参数
  199. if (history == name_to_history_policy_map.end()) { // 如果参数无效
  200. std::ostringstream oss; // 创建字符串流
  201. oss << "Invalid QoS history setting '" << history_param << "'"; // 打印错误信息
  202. throw std::runtime_error(oss.str()); // 抛出运行时错误
  203. }
  204. history_policy_ = history->second; // 设置历史策略
  205. // Declare and get remaining parameters
  206. depth_ = this->declare_parameter("depth", 10); // 声明深度参数
  207. freq_ = this->declare_parameter("frequency", 30.0); // 声明频率参数
  208. burger_mode_ = this->declare_parameter("burger_mode", false); // 声明汉堡模式参数
  209. show_camera_ = this->declare_parameter("show_camera", false); // 声明显示摄像头参数
  210. device_id_ = this->declare_parameter("device_id", 0); // 声明设备ID参数
  211. width_ = this->declare_parameter("width", 320); // 声明宽度参数
  212. height_ = this->declare_parameter("height", 240); // 声明高度参数
  213. frame_id_ = this->declare_parameter("frame_id", "camera_frame"); // 声明帧ID参数
  214. }
  215. rclcpp::Publisher<image_tools::ROSCvMatContainer>::SharedPtr pub_; // 发布者对象
  216. rclcpp::Subscription<std_msgs::msg::Bool>::SharedPtr sub_; // 订阅者对象
  217. rclcpp::TimerBase::SharedPtr timer_; // 定时器对象
  218. cv::VideoCapture cap; // 视频捕获对象
  219. Burger burger_cap; // 汉堡捕获对象
  220. bool is_flipped_; // 是否翻转图像
  221. bool burger_mode_; // 是否为汉堡模式
  222. bool show_camera_; // 是否显示摄像头
  223. size_t depth_; // 深度参数
  224. double freq_; // 频率参数
  225. int device_id_; // 设备ID参数
  226. int width_; // 宽度参数
  227. int height_; // 高度参数
  228. std::string frame_id_; // 帧ID参数
  229. size_t publish_number_; // 发布编号
  230. rmw_qos_reliability_policy_t reliability_policy_; // 可靠性策略
  231. rmw_qos_history_policy_t history_policy_; // 历史策略
  232. };
  233. } // namespace image_tools
  234. RCLCPP_COMPONENTS_REGISTER_NODE(image_tools::Cam2Image) // 注册节点

policy_maps.hpp

  1. #ifndef POLICY_MAPS_HPP_ // 如果没有定义POLICY_MAPS_HPP_
  2. #define POLICY_MAPS_HPP_ // 定义POLICY_MAPS_HPP_
  3. #include <map> // 包含map库
  4. #include <string> // 包含string库
  5. namespace image_tools // 定义命名空间image_tools
  6. {
  7. static // 定义静态变量
  8. std::map<std::string, rmw_qos_reliability_policy_t> name_to_reliability_policy_map = { // 定义字符串到可靠性策略的映射
  9. {"reliable", RMW_QOS_POLICY_RELIABILITY_RELIABLE}, // 可靠性策略映射:reliable
  10. {"best_effort", RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT} // 可靠性策略映射:best_effort
  11. };
  12. static // 定义静态变量
  13. std::map<std::string, rmw_qos_history_policy_t> name_to_history_policy_map = { // 定义字符串到历史策略的映射
  14. {"keep_last", RMW_QOS_POLICY_HISTORY_KEEP_LAST}, // 历史策略映射:keep_last
  15. {"keep_all", RMW_QOS_POLICY_HISTORY_KEEP_ALL} // 历史策略映射:keep_all
  16. };
  17. } // 结束image_tools命名空间
  18. #endif // 结束对POLICY_MAPS_HPP_的定义
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/874737
推荐阅读
相关标签
  

闽ICP备14008679号