赞
踩
Topic通信模型是一种发布订阅模型,ROS2使用了基于DDS的发布/订阅模型,数据传输过程如下图:
话题数据传输的特性是从一个节点到另外一个节点,发送数据的对象称之为发布者,接收数据的对象称之为订阅者,每一个话题都需要有一个名字,传输的数据也需要有固定的数据类型。
需要满足以下规则:
话题通信还有一个特性,那就是异步。所谓异步,只要是指发布者发出数据后,并不知道订阅者什么时候可以收到,类似古月居公众号发布一篇文章,你什么时候阅读的,古月居根本不知道,报社发出一份报纸,你什么时候收到,报社也是不知道的。这就叫做异步。
异步的特性也让话题更适合用于一些周期发布的数据,比如传感器的数据,运动控制的指令等等,如果某些逻辑性较强的指令,比如修改某一个参数,用话题传输就不太合适了。
在编码之前,要创建工作空间
mkdir -p dev_ws/src
cd dev_ws/src
ros2 pkg create learning02_topic --build-type ament_cmake
创建完工作空间之后的目录结构如下:
publisher.cpp
#include <chrono> #include <memory> #include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/string.hpp" using namespace std::chrono_literals; /* This example creates a subclass of Node and uses std::bind() to register a * member function as a callback from the timer. */ class MinimalPublisher : public rclcpp::Node { public: MinimalPublisher() : Node("minimal_publisher"), count_(0) { publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10); timer_ = this->create_wall_timer( 500ms, std::bind(&MinimalPublisher::timer_callback, this)); } private: void timer_callback() { auto message = std_msgs::msg::String(); message.data = "Hello, world! " + std::to_string(count_++); RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str()); publisher_->publish(message); } rclcpp::TimerBase::SharedPtr timer_; rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_; size_t count_; }; int main(int argc, char * argv[]) { rclcpp::init(argc, argv); rclcpp::spin(std::make_shared<MinimalPublisher>()); rclcpp::shutdown(); return 0; }
#include <memory> #include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/string.hpp" using std::placeholders::_1; class MinimalSubscriber : public rclcpp::Node { public: MinimalSubscriber() : Node("minimal_subscriber") { subscription_ = this->create_subscription<std_msgs::msg::String>( "topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1)); } private: void topic_callback(const std_msgs::msg::String::SharedPtr msg) const { RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str()); } rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_; }; int main(int argc, char * argv[]) { rclcpp::init(argc, argv); rclcpp::spin(std::make_shared<MinimalSubscriber>()); rclcpp::shutdown(); return 0; }
cmake_minimum_required(VERSION 3.5) project(learning02_topic) # Default to C++14 if(NOT CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 14) endif() if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) add_executable(talker src/publisher.cpp) ament_target_dependencies(talker rclcpp std_msgs) add_executable(listener src/subscriber.cpp) ament_target_dependencies(listener rclcpp std_msgs) install(TARGETS talker listener DESTINATION lib/${PROJECT_NAME}) ament_package()
<?xml version="1.0"?> <?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> <package format="3"> <name>learning02_topic</name> <version>0.0.0</version> <description>TODO: Package description</description> <maintainer email="Jing.Yu6@xxx.com">isd</maintainer> <license>TODO: License declaration</license> <buildtool_depend>ament_cmake</buildtool_depend> <depend>rclcpp</depend> <test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_common</test_depend> <export> <build_type>ament_cmake</build_type> <exec_depend>rclcpp</exec_depend> <exec_depend>std_msgs</exec_depend> </export> </package>
# 编译
colcon build
# source环境变量
source install/setup.sh
# 运行publisher
ros2 run learning02_topic listener
# 运行subsriber
ros2 run learning02_topic talker
node指令
# 启动节点
ros2 run <package_name> <node_name>
# 显示当前活动节点
ros2 node list
topic指令
# 显示当前活动topic
ros2 topic list
# 打印topic内容
ros2 topic echo <topic_name>
# 显示topic频率
ros2 topic hz <topic_name>
# 显示topic信息
rot2 topic info <topic_name>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。