ROS2:话题通信CPP语法速记
目录
- 发布方实现流程
- 重点代码
- 订阅方实现流程
- 重点代码
- 参考代码示例
- 发布方代码
- 订阅方代码
发布方实现流程
- 包含头文件(rclcpp.hpp与[interfaces_pkg].hpp)
- 初始化ROS2客户端(rclcpp::init)
- 自定义节点类(创建发布实例,伺机发布数据)
- 调用spin函数,循环运行节点实例
- 释放资源(rclcpp::shutdown())
重点代码
//1.创建发布方
rclcpp::Publisher<[MSG-T]>::SharedPtr publisher_;
publisher_ = this->create_publisher<[MSG-T]>("topic_name",10);//2.发布消息(定义->装填->发送)
[MSG-T] message;
/*
装填数据
*/
publisher_->publish(message);//3.创建定时器
using namespace std::chrono_literals;
rclcpp::TimerBase::SharedPtr timer_;
timer_ = this->create_wall_timer([duration_S], std::bind(&[Node_Class_Name]::[timer_callback_fun], this));
发布方创建后可以随时发布话题消息,一般可以选择创建定时器来实现周期性发发送。
订阅方实现流程
- 包含头文件(rclcpp.hpp与[interfaces_pkg].hpp)
- 初始化ROS2客户端(rclcpp::init)
- 自定义节点类,(绑定回调函数,回调时处理数据)
- 调用spin函数,循环运行节点实例
- 释放资源(rclcpp::shutdown())
重点代码
//1.创建订阅方
rclcpp::Subscription<[MSG-T]>::SharedPtr subscription_;
subscription_= this->create_subscription<[MSG-T]>("topic_name",10,std::bind(&[Node_Class_Name]::[sub_callback_fun], this,std::placeholders::_1));//2.实现订阅回调函数
void sub_callback_fun(const [MSG-T] &messgae){
/*
处理数据
*/
}
订阅方创建时需要绑定回调函数接收到话题消息时,调用一次回调函数。回调函数输入参数为const [MSG-T] &,即消息类型的引用。
参考代码示例
发布方代码
/* 需求:以某个固定频率发送文本“hello world!”,文本后缀编号,每发送一条消息,编号递增1。步骤:1.包含头文件;2.初始化 ROS2 客户端;3.定义节点类;3-1.创建发布方;3-2.创建定时器;3-3.组织消息并发布。4.调用spin函数,并传入节点对象指针;5.释放资源。
*/// 1.包含头文件;
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"using namespace std::chrono_literals;// 3.定义节点类;
class MinimalPublisher : public rclcpp::Node
{public:MinimalPublisher(): Node("minimal_publisher"), count_(0){// 3-1.创建发布方;publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);// 3-2.创建定时器;timer_ = this->create_wall_timer(500ms, std::bind(&MinimalPublisher::timer_callback, this));}private:void timer_callback(){// 3-3.组织消息并发布。auto message = std_msgs::msg::String();message.data = "Hello, world! " + std::to_string(count_++);RCLCPP_INFO(this->get_logger(), "发布的消息:'%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[])
{// 2.初始化 ROS2 客户端;rclcpp::init(argc, argv);// 4.调用spin函数,并传入节点对象指针。rclcpp::spin(std::make_shared<MinimalPublisher>());// 5.释放资源;rclcpp::shutdown();return 0;
}
订阅方代码
/* 需求:订阅发布方发布的消息,并输出到终端。步骤:1.包含头文件;2.初始化 ROS2 客户端;3.定义节点类;3-1.创建订阅方;3-2.处理订阅到的消息。4.调用spin函数,并传入节点对象指针;5.释放资源。
*/// 1.包含头文件;
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using std::placeholders::_1;// 3.定义节点类;
class MinimalSubscriber : public rclcpp::Node
{public:MinimalSubscriber(): Node("minimal_subscriber"){// 3-1.创建订阅方;subscription_ = this->create_subscription<std_msgs::msg::String>("topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));}private:// 3-2.处理订阅到的消息;void topic_callback(const std_msgs::msg::String & msg) const{RCLCPP_INFO(this->get_logger(), "订阅的消息: '%s'", msg.data.c_str());}rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};int main(int argc, char * argv[])
{// 2.初始化 ROS2 客户端;rclcpp::init(argc, argv);// 4.调用spin函数,并传入节点对象指针。rclcpp::spin(std::make_shared<MinimalSubscriber>());// 5.释放资源;rclcpp::shutdown();return 0;
}