// Node that consumes messages. structConsumer : 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())); }); }
auto dis = std::make_unique<std_msgs::msg::Float32>();dis->data = 10.0;pub_->publish(std::move(dis));
将需通信的节点加入到同一个进程中
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);
1
Published message with value: 0, and address: 0x5625E3159130 Received message with value: 0, and address: 0x5625E3159130Published message with value: 1, and address: 0x5625E3159130 Received message with value: 1, and address: 0x5625E3159130Published message with value: 2, and address: 0x5625E3159130 Received message with value: 2, and address: 0x5625E3159130Published message with value: 3, and address: 0x5625E3159130 Received message with value: 3, and address: 0x5625E3159130Published message with value: 4, and address: 0x5625E3159130 Received message with value: 4, and address: 0x5625E3159130Published message with value: 5, and address: 0x5625E3159130 Received message with value: 5, and address: 0x5625E3159130Published message with value: 6, and address: 0x5625E3159130 Received message with value: 6, and address: 0x5625E3159130Published message with value: 7, and address: 0x5625E3159130 Received message with value: 7, and address: 0x5625E3159130Published message with value: 8, and address: 0x5625E3159130 Received message with value: 8, and address: 0x5625E3159130Published message with value: 9, and address: 0x5625E3159130 Received message with value: 9, and address: 0x5625E3159130