话题的发布者
- 导入消息类型,ros自带的消息类型在
std_msgs/msg/下面,有string等类型
- 定义一个类,里面声明一个发布者话题的共享指针,指针标注发布的数据类型
- 在构造函数中使用
create_publisher创建声明的这个发布者对象,也可以在声明时创建对象。
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
class publisher: public rclcpp::Node
{
private:
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub_er;
public:
publisher(std::string name);
~publisher();
void publish_data();
};
publisher::publisher(std::string name):Node(name)
{
RCLCPP_INFO(this->get_logger(),"create publisher");
pub_er = this->create_publisher<std_msgs::msg::String>("topic_name",10)