阅读量:0
在ROS 2(Robot Operating System 2)中,节点之间的通信主要通过话题(Topics)、服务(Services)和动作(Actions)来实现。下面是一个简单的C++ ROS 2节点通信示例,包括一个发布者(Publisher)和一个订阅者(Subscriber)。
首先,确保你已经安装了ROS 2和相应的开发工具。
创建一个新的ROS 2工作空间和包:
mkdir -p ~/ros2_ws/src cd ~/ros2_ws/src colcon build --symlink-install source install/setup.bash
- 在
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>
创建一个名为
my_package/src
的文件夹,并在其中创建两个C++源文件:publisher.cpp
和subscriber.cpp
。在
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; }
- 在
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; }
- 在
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})
- 编译并运行节点:
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节点通信示例。你可以根据需要扩展此示例,以实现更复杂的消息传递和处理逻辑。