c++ ros2节点如何通信

avatar
作者
筋斗云
阅读量:0

在ROS 2(Robot Operating System 2)中,节点之间的通信主要通过话题(Topics)、服务(Services)和动作(Actions)来实现。下面是一个简单的C++ ROS 2节点通信示例,包括一个发布者(Publisher)和一个订阅者(Subscriber)。

  1. 首先,确保你已经安装了ROS 2和相应的开发工具。

  2. 创建一个新的ROS 2工作空间和包:

mkdir -p ~/ros2_ws/src cd ~/ros2_ws/src colcon build --symlink-install source install/setup.bash 
  1. package.xml文件中添加以下内容,以创建一个名为my_package的包,其中包含一个发布者(Publisher)和一个订阅者(Subscriber):
<package name="my_package" version="0.0.0">   <description>My ROS 2 package with publisher and subscriber</description>   <license>Apache License 2.0</license>   <authors>     <author email="user@example.com">Your Name</author>   </authors>   <build_type>ament_cmake</build_type>   <dependencies>     <dependency>rclcpp</dependency>     <dependency>std_msgs</dependency>   </dependencies> </package> 
  1. 创建一个名为my_package/src的文件夹,并在其中创建两个C++源文件:publisher.cppsubscriber.cpp

  2. publisher.cpp文件中,编写一个发布者节点,用于发布名为my_topic的话题:

#include <iostream> #include <memory> #include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/string.hpp"  class Publisher : public rclcpp::Node { public:     Publisher() : Node("publisher") {         publisher_ = this->create_publisher<std_msgs::msg::String>("my_topic", 10);         timer_ = this->create_wall_timer(1000ms, std::bind(&Publisher::publish_message, this));     }  private:     void publish_message() {         auto message = std_msgs::msg::String();         message.data = "Hello, ROS 2!";         publisher_->publish(message);         RCLCPP_INFO(this->get_logger(), "Published: '%s'", message.data.c_str());     }      std::shared_ptr<rclcpp::Publisher<std_msgs::msg::String>> publisher_;     std::shared_ptr<rclcpp::TimerBase> timer_; };  int main(int argc, char *argv[]) {     rclcpp::init(argc, argv);     rclcpp::spin(std::make_shared<Publisher>());     rclcpp::shutdown();     return 0; } 
  1. subscriber.cpp文件中,编写一个订阅者节点,用于订阅名为my_topic的话题:
#include <iostream> #include <memory> #include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/string.hpp"  class Subscriber : public rclcpp::Node { public:     Subscriber() : Node("subscriber") {         subscription_ = this->create_subscription<std_msgs::msg::String>("my_topic", 10, std::bind(&Subscriber::callback, this, std::placeholders::_1));     }  private:     void callback(const std_msgs::msg::String::SharedPtr msg) {         RCLCPP_INFO(this->get_logger(), "Received: '%s'", msg->data.c_str());     }      std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String>> subscription_; };  int main(int argc, char *argv[]) {     rclcpp::init(argc, argv);     rclcpp::spin(std::make_shared<Subscriber>());     rclcpp::shutdown();     return 0; } 
  1. my_package文件夹中创建一个名为CMakeLists.txt的文件,用于编译源文件:
cmake_minimum_required(VERSION 3.5) project(my_package)  find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED)  add_executable(publisher src/publisher.cpp) add_executable(subscriber src/subscriber.cpp)  ament_target_dependencies(publisher rclcpp std_msgs) ament_target_dependencies(subscriber rclcpp std_msgs)  install(TARGETS publisher subscriber LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}) 
  1. 编译并运行节点:
cd ~/ros2_ws colcon build --symlink-install source install/setup.bash  ros2 run my_package publisher ros2 run my_package subscriber 

现在,你应该能看到发布者在每隔1秒发布一条消息,而订阅者则接收并打印这些消息。这就是一个简单的C++ ROS 2节点通信示例。你可以根据需要扩展此示例,以实现更复杂的消息传递和处理逻辑。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!