当前位置:网站首页>Ros2 node communication realizes zero copy
Ros2 node communication realizes zero copy
2022-07-26 21:18:00 【Shoufei loves playing with robots】
The following example demonstrates how to realize zero copy communication between two nodes .
Be careful , The following test example ROS2 Version is Galactic.
#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;
}
There are several points to pay attention to in the establishment of nodes in the example :
- Can make
intra_process
Node(name, rclcpp::NodeOptions().use_intra_process_comms(true))
Enable when configuring nodes intra_process.
- Use when publishing messages
UniquePtrandstd::move(msg)
In addition to the writing in the above example , You can also write in the following way :
auto dis = std::make_unique<std_msgs::msg::Float32>();
dis->data = 10.0;
pub_->publish(std::move(dis));
- Add the nodes that need to communicate to the same process
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);
The complete function code can be obtained through the following link :
https://github.com/shoufei403/ros2_galactic_tutorials
The corresponding code is in demos/intra_process_demo Catalog .
After compiling the code, use the following command to start the sample program
source install/setup.bash
ros2 run intra_process_demo two_node_pipeline
Output results :
Published message with value: 0, and address: 0x5625E3159130
Received message with value: 0, and address: 0x5625E3159130
Published message with value: 1, and address: 0x5625E3159130
Received message with value: 1, and address: 0x5625E3159130
Published message with value: 2, and address: 0x5625E3159130
Received message with value: 2, and address: 0x5625E3159130
Published message with value: 3, and address: 0x5625E3159130
Received message with value: 3, and address: 0x5625E3159130
Published message with value: 4, and address: 0x5625E3159130
Received message with value: 4, and address: 0x5625E3159130
Published message with value: 5, and address: 0x5625E3159130
Received message with value: 5, and address: 0x5625E3159130
Published message with value: 6, and address: 0x5625E3159130
Received message with value: 6, and address: 0x5625E3159130
Published message with value: 7, and address: 0x5625E3159130
Received message with value: 7, and address: 0x5625E3159130
Published message with value: 8, and address: 0x5625E3159130
Received message with value: 8, and address: 0x5625E3159130
Published message with value: 9, and address: 0x5625E3159130
Received message with value: 9, and address: 0x5625E3159130
You can find , The address of the sending end data is consistent with that of the receiving end data . Therefore, the sender just sends the address where the data is stored to the receiver, and there is no data copy .
The zero copy feature is particularly useful for transmitting image data . For an example of image transmission, please refer to demos/intra_process_demo Other examples in the catalog .
Reference resources :
https://docs.ros.org/en/galactic/Tutorials/Demos/Intra-Process-Communication.html
If you think it's useful, just praise it !
It's me , One to help everyone Filling pit Robot development siege lion .
In addition, the official account. 《 First flight 》 Internal reply “ robot ” Get carefully recommended C/C++,Python,Docker,Qt,ROS1/2 And other commonly used technical data in the robot industry .
边栏推荐
- 测试用例千万不能随便,记录由一个测试用例异常引起的思考
- [must read new] Keya valuation analysis of University of technology, heating energy-saving products
- SSM整合实例
- [JVM series] JVM tuning
- 今天公司碰到一个阿里p8,算是真正见识到了基础的天花板
- HTTP cache browser cache that rabbits can understand
- 【HCIA安全】NAT网络地址转换
- PLSQL package
- SprinBoot面试题
- 2022开放原子全球开源峰会议程速递 | 7 月 27 日分论坛议程一览
猜你喜欢

Shell comprehensive application cases, archive files

AI technology, simplifying the complex world | teatalk online application practical series, issue 2

7-year-old boy playing chess too fast? The robot actually broke its finger

基于Hough变换的直线检测(Matlab)

Test cases should never be used casually, recording the thinking caused by the exception of a test case

Discussion on loan agreement mode with NFT as collateral

Pointpillars: fast encoders for object detection from point clouds reading notes

Leetcode linked list problem -- 24. Exchange the nodes in the linked list in pairs (learn the linked list with one question and one article)

Redis interview questions

Leetcode linked list problem - 19. Delete the penultimate node of the linked list (learn the linked list with one question and one article)
随机推荐
Sprinboot interview questions
Go+mysql+redis+vue3 simple chat room, bullet 5: synchronize messages to MySQL using message queues and scheduled tasks
JDBC的引入
Niuke brush questions - MySQL series
[OBS] solve the problem of OBS pushing two RTMP streams + timestamp
[must read new] Keya valuation analysis of University of technology, heating energy-saving products
What kind of security problems will the server encounter?
Remember the idea of solving the problem of invalid bound statement xxxxx once
PointPillars: Fast Encoders for Object Detection from Point Clouds 阅读笔记
Redis hash和string的区别
游览器——游览器游览器缓存
今天公司碰到一个阿里p8,算是真正见识到了基础的天花板
In the era of Web3.0, the technical theory of implementing a DAPP based on P2P DB
Interceptors
JDBC connection
arm tz硬件支撑
JVM学习----内存结构----程序计数器&虚拟机栈&本地方法栈&堆&方法区
【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复
【HCIA安全】用户认证
基于Hough变换的直线检测(Matlab)