系列文章目录

  • ROS2与C++入门教程-目录

  • ROS2与C++入门教程-新建ros2工作空间

  • ROS2与C++入门教程-新建ros2包

  • ROS2与C++入门教程-编写订阅和发布

  • ROS2与C++入门教程-编写服务端和客户端

  • ROS2与C++入门教程-创建消息(msg)文件

  • ROS2与C++入门教程-创建服务(srv)文件

  • ROS2与C++入门教程-创建ros2接口

  • ROS2与C++入门教程-使用参数

  • ROS2与C++入门教程-创建和使用插件

  • ROS2与C++入门教程-编写动作服务器

  • ROS2与C++入门教程-编写动作客户端

  • ROS2与C++入门教程-Topic Statistics查看话题

  • ROS2与C++入门教程-增加头文件

  • ROS2与C++入门教程-在C++包里增加python支持

  • ROS2与C++入门教程-增加命名空间

  • ROS2与C++入门教程-多线程演示

  • ROS2与C++入门教程-单线程演示

  • ROS2与C++入门教程-话题组件演示

  • ROS2与C++入门教程-话题组件增加launch启动演示

  • ROS2与C++入门教程-服务组件演示

  • ROS2与C++入门教程-服务组件增加launch启动演示

  • ROS2与C++入门教程-进程内(intra_process)话题发布和订阅演示

  • ROS2与C++入门教程-进程内(intra_process)话题发布和订阅演示2

  • ROS2与C++入门教程-进程内(intra_process)图像处理演示

  • ROS2与C++入门教程-生命周期节点演示

说明:

  • 介绍ros2下实现进程内(intra_process)话题发布和订阅
  • 在同一进程内的不同节点,可以通过共享指针方式实现内容读取,减少消息的拷贝开销
  • 对于图像之类数据量比较大的节点间处理的效率和性能将大大提高
  • 本演示实现两个话题循环处理

步骤:

  • 新建一个包cpp_intra_process_topic2
cd ~/dev_ws/src
ros2 pkg create --build-type ament_cmake cpp_intra_process_topic2
  • 进入src目录,新建文件cyclic_pipeline.cpp
cd ~/dev_ws/src/cpp_intra_process_topic2/src
touch cyclic_pipeline.cpp
  • 内容如下:
#include <chrono>
#include <cinttypes>
#include <cstdio>
#include <memory>
#include <string>
#include <utility>#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/int32.hpp"using namespace std::chrono_literals;// This node receives an Int32, waits 1 second, then increments and sends it.
struct IncrementerPipe : public rclcpp::Node
{IncrementerPipe(const std::string & name, const std::string & in, const std::string & out): Node(name, rclcpp::NodeOptions().use_intra_process_comms(true)){// Create a publisher on the output topic.pub = this->create_publisher<std_msgs::msg::Int32>(out, 10);std::weak_ptr<std::remove_pointer<decltype(pub.get())>::type> captured_pub = pub;// Create a subscription on the input topic.sub = this->create_subscription<std_msgs::msg::Int32>(in,10,[captured_pub](std_msgs::msg::Int32::UniquePtr msg) {auto pub_ptr = captured_pub.lock();if (!pub_ptr) {return;}printf("Received message with value:         %d, and address: 0x%" PRIXPTR "\n", msg->data,reinterpret_cast<std::uintptr_t>(msg.get()));printf("  sleeping for 1 second...\n");if (!rclcpp::sleep_for(1s)) {return;    // Return if the sleep failed (e.g. on ctrl-c).}printf("  done.\n");msg->data++;    // Increment the message's data.printf("Incrementing and sending with value: %d, and address: 0x%" PRIXPTR "\n", msg->data,reinterpret_cast<std::uintptr_t>(msg.get()));pub_ptr->publish(std::move(msg));    // Send the message along to the output topic.});}rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr pub;rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr sub;
};int main(int argc, char * argv[])
{setvbuf(stdout, NULL, _IONBF, BUFSIZ);rclcpp::init(argc, argv);rclcpp::executors::SingleThreadedExecutor executor;// Create a simple loop by connecting the in and out topics of two IncrementerPipe's.// The expectation is that the address of the message being passed between them never changes.auto pipe1 = std::make_shared<IncrementerPipe>("pipe1", "topic1", "topic2");auto pipe2 = std::make_shared<IncrementerPipe>("pipe2", "topic2", "topic1");rclcpp::sleep_for(1s);  // Wait for subscriptions to be established to avoid race conditions.// Publish the first message (kicking off the cycle).std::unique_ptr<std_msgs::msg::Int32> msg(new std_msgs::msg::Int32());msg->data = 42;printf("Published first message with value:  %d, and address: 0x%" PRIXPTR "\n", msg->data,reinterpret_cast<std::uintptr_t>(msg.get()));pipe1->pub->publish(std::move(msg));executor.add_node(pipe1);executor.add_node(pipe2);executor.spin();rclcpp::shutdown();return 0;
}
  • 编译package.xml
  • 在<buildtool_depend>ament_cmake</buildtool_depend>后增加
<build_depend>rclcpp</build_depend>
<build_depend>std_msgs</build_depend>
  • 编译CMakelist.txt
  • 在find_package(ament_cmake REQUIRED)后增加
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
  • 生成执行文件
include_directories(include)add_executable(cyclic_pipelinesrc/cyclic_pipeline.cpp)
target_link_libraries(cyclic_pipelinerclcpp::rclcpp${std_msgs_TARGETS})
  • 安装相关库文件和执行文件
install(TARGETScyclic_pipelineDESTINATION lib/${PROJECT_NAME})
  • 编译包
colcon build --symlink-install --packages-select cpp_intra_process_topic2
  • 加载工作空间
. install/setup.bash
  • 测试: 
ros2 run cpp_intra_process_topic2 cyclic_pipeline
  • 效果如下: 
$ ros2 run cpp_intra_process_topic2 cyclic_pipeline
Published first message with value:  42, and address: 0x55DBCB77E6F0
Received message with value:         42, and address: 0x55DBCB77E6F0sleeping for 1 second...done.
Incrementing and sending with value: 43, and address: 0x55DBCB77E6F0
Received message with value:         43, and address: 0x55DBCB77E6F0sleeping for 1 second...done.
Incrementing and sending with value: 44, and address: 0x55DBCB77E6F0
Received message with value:         44, and address: 0x55DBCB77E6F0sleeping for 1 second...done.
Incrementing and sending with value: 45, and address: 0x55DBCB77E6F0
Received message with value:         45, and address: 0x55DBCB77E6F0sleeping for 1 second...done.

ROS2与C++入门教程-进程内(intra_process)话题发布和订阅演示2相关推荐

  1. ROS2与C++入门教程-在C++包里增加python支持

    系列目录 ROS2与C++入门教程-目录 ROS2与C++入门教程-新建ros2工作空间 ROS2与C++入门教程-新建ros2包 ROS2与C++入门教程-编写订阅和发布 ROS2与C++入门教程- ...

  2. ROS2与C++入门教程-增加头文件

    系列文章目录 ROS2与C++入门教程-目录 ROS2与C++入门教程-新建ros2工作空间 ROS2与C++入门教程-新建ros2包 ROS2与C++入门教程-编写订阅和发布 ROS2与C++入门教 ...

  3. 《zw版·Halcon入门教程与内置demo》

    <zw版·Halcon入门教程与内置demo> halcon系统的中文教程很不好找,而且大部分是v10以前的版本. 例如,QQ群: 247994767(Delphi与halcon), 共享 ...

  4. ROS2与C++入门教程-目录 - 创客智造

    来源: https://www.ncnynl.com/archives/201806/2486.html 说明: 介绍如何在ros2下利用c++编程 所有例子放在github里 目录: ROS2与C+ ...

  5. ros2与windows入门教程-windows上安装ROS2 foxy

    系列文章目录 ros2与windows入门教程-windows上安装ROS2 foxy ros2与windows入门教程-控制小乌龟 ros2与windows入门教程-监听和发布话题 ros2与win ...

  6. ros2与Python入门教程-使用消息 - 创客智造

    **ros2与Python入门教程-使用消息 ** 说明: 介绍如何python来测试消息 步骤: 如何创建消息 ,参考ROS2与C++入门教程-创建消息(msg)文件 利用python来测试消息 使 ...

  7. linux 搭建开发stm32 stlink,ROS2与STM32入门教程-搭建开发环境(ubuntu+eclipse+cubemx+stlink+openocd)...

    ROS2与C++入门教程-搭建开发环境(ubuntu+eclipse+cubemx+stlink+opencd) 说明: 介绍如何在ubuntu下搭建开发环境 环境:ubuntu20.04 + ecl ...

  8. SEO零基础入门教程(外链的发布和软文编写)

    seo的作用是众所周知的,对网站进行seo优化,可以给网站带来大量的搜索引擎流量.但是想要做好网站优化也有难度,尤其是对于seo新手来说,因为缺乏理论和实战,所以seo新手需要多加练习.那么具体seo ...

  9. ROS入门学习笔记|话题发布与订阅

    文章目录 一.工作空间 1.创建一个名称为sor_ws的工作空间 2.编译工作空间 3.创建功能包 二.自定义话题消息 1.定义msg文件 2.配置package.xml和CMakeLists.txt ...

  10. ROS2与C++入门教程-创建服务(srv)文件 - 创客智造

    来源:https://www.ncnynl.com/archives/201806/2492.html 说明: 介绍如何创建服务srv文件 步骤: 使用已经建好的工作空间dev_ws 新建包,如果已经 ...

最新文章

  1. shell基础之if语句
  2. 修改远程桌面端口_花生壳(内网穿透)服务做远程桌面登录
  3. Python-DDT框架
  4. 【H.264/AVC视频编解码技术】第五章【哈夫曼编码】
  5. 22 FI配置-财务会计-定义收益留存科目(Retained Earning Account)
  6. python简单的小程序_Python简单小程序---名片简易系统
  7. flask 启动服务
  8. Linus Torvalds 命名 [ 冰封荒原 ] 版 Linux 内核的思考
  9. SecureCrt 常用命令
  10. string的replaceAll()
  11. 利用Matlab绘制梯度图、散度图、旋度图
  12. python实现syn半扫描_python 实现 syn 扫描
  13. 因证书过期导致Java 执行http相关动作失败
  14. 文明重启战局服务器维护中,王牌战争文明重启8月23日更新公告
  15. 第二证券|沪指缩量跌0.25%,地产、医药板块强势,钠电池概念表现亮眼
  16. 2-3文件+结构体实现实用系统
  17. originos和鸿蒙系统哪个更好,originos和emui11哪个好_originos和emui11哪个流畅
  18. 有限元方法简介与COMSOL操作入门
  19. java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResolver
  20. 《愤怒的小鸟》之父魏皮特:小鸟王国的品牌之路

热门文章

  1. LabVIEW心率监测装置
  2. 工业相机基本参数以及选型参考
  3. 简易的机器人聊天_如何制作一个简单的聊天机器人
  4. 生信分析用python还是r_生信分析利器:JupyterLab
  5. java word checkbox_springmvc poi 导出word 复选框 怎么用
  6. 写文献综述的28个要点
  7. 小红书种草营销的本质是什么?如何投入更有效?
  8. YY前端HTML规范
  9. (5)air202读取串口数据并上传到阿里云显示
  10. 计算机管理无法定位程序输入点,win10系统打开程序提示无法定位程序输入点于动态链接库怎么办...