系列文章目录

  • 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_topic
cd ~/dev_ws/src
ros2 pkg create --build-type ament_cmake cpp_intra_process_topic
  • 进入src目录,新建文件two_node_pipeline.cpp
cd ~/dev_ws/src/cpp_intra_process_topic/src
touch two_node_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;// Node that produces messages.
struct Producer : public rclcpp::Node
{Producer(const std::string & name, const std::string & output): Node(name, rclcpp::NodeOptions().use_intra_process_comms(true)){// Create a publisher on the output topic.pub_ = this->create_publisher<std_msgs::msg::Int32>(output, 10);std::weak_ptr<std::remove_pointer<decltype(pub_.get())>::type> captured_pub = pub_;// Create a timer which publishes on the output topic at ~1Hz.auto callback = [captured_pub]() -> void {auto pub_ptr = captured_pub.lock();if (!pub_ptr) {return;}static int32_t count = 0;std_msgs::msg::Int32::UniquePtr msg(new std_msgs::msg::Int32());msg->data = count++;printf("Published message with value: %d, and address: 0x%" PRIXPTR "\n", msg->data,reinterpret_cast<std::uintptr_t>(msg.get()));pub_ptr->publish(std::move(msg));};timer_ = this->create_wall_timer(1s, callback);}rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr pub_;rclcpp::TimerBase::SharedPtr timer_;
};// Node that consumes messages.
struct Consumer : public rclcpp::Node
{Consumer(const std::string & name, const std::string & input): Node(name, rclcpp::NodeOptions().use_intra_process_comms(true)){// Create a subscription on the input topic which prints on receipt of new messages.sub_ = this->create_subscription<std_msgs::msg::Int32>(input,10,[](std_msgs::msg::Int32::UniquePtr msg) {printf(" Received message with value: %d, and address: 0x%" PRIXPTR "\n", msg->data,reinterpret_cast<std::uintptr_t>(msg.get()));});}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;auto producer = std::make_shared<Producer>("producer", "number");auto consumer = std::make_shared<Consumer>("consumer", "number");executor.add_node(producer);executor.add_node(consumer);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(two_node_pipeline src/two_node_pipeline.cpp)
target_link_libraries(two_node_pipelinerclcpp::rclcpp${std_msgs_TARGETS})
  • 安装相关库文件和执行文件
install(TARGETStwo_node_pipelineDESTINATION lib/${PROJECT_NAME})
  • 编译包
colcon build --symlink-install --packages-select cpp_intra_process_topic
  • 加载工作空间
. install/setup.bash
  • 测试: 
ros2 run cpp_intra_process_topic two_node_pipeline
  • 效果如下: 
$ ros2 run cpp_intra_process_topic two_node_pipeline
Published message with value: 0, and address: 0x55AD15216420Received message with value: 0, and address: 0x55AD15216420
Published message with value: 1, and address: 0x55AD15216420Received message with value: 1, and address: 0x55AD15216420
Published message with value: 2, and address: 0x55AD15216420Received message with value: 2, and address: 0x55AD15216420
Published message with value: 3, and address: 0x55AD15216420Received message with value: 3, and address: 0x55AD15216420

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

  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. 我竟然混进了Python高级圈子!
  2. SqlServer 2014 还原数据库时提示:操作系统返回了错误5,,拒绝访问
  3. oracle怎么查询换行符,关于oracle:如何检查表中所有列的换行符
  4. 记录几款比较好用的jquery插件
  5. 产品观念:更好的捕鼠器_故事很重要:为什么您需要成为更好的讲故事的人
  6. 向iOS开发者介绍C++
  7. 蓝色起源起诉NASA,不服其将月球着陆器合同授予SpaceX
  8. BootStrap-
  9. 搭建IntelliJ IDEA+maven+jetty+SpringMVC 开发环境(一)
  10. Linux 启动流程学习
  11. 编译Libtorrent
  12. Word另存为PDF后无导航栏解决办法
  13. 遥感式雷达监测水位流速设备
  14. XML采用Boost::regex解析实例
  15. 每日一课 | AES加密和解密(CBC模式)
  16. 问题 B: 零基础学C/C++25——判断某整数是正整数、负整数还是零
  17. 当你想用Gitee对你的APK文件上传下载时
  18. OFDM载波间隔_LTE-子载波间隔与符号持续时长关系
  19. 35岁的程序员:第31章,波折
  20. 香港目前紧缺18类人才!你是他们需要的人才吗?

热门文章

  1. 阿里笔试 3.14 T1
  2. pm2 重启策略(restart strategies)
  3. php allow origin,Allow-Control-Allow-Origin:谷歌跨域扩展插件
  4. 计算机专业的文献综述题目,计算机专业文献综述格式及要求
  5. 计蒜客1185出书最多
  6. Firefox定位网页元素工具
  7. html 动态导航菜单,导航菜单,css3,javascript,响应式菜单,html,css
  8. 完整方法:摄像头打不开,驱动无法安装成功,设备状态显示由于其配置信息(注册表中的)不完整或已损坏,windows无法启动这个硬件设备。(代码19)
  9. 电信光纤ipv6-- 搭建属于自己的服务器
  10. 生活之美--需要设计