1.my_sub.cpp编写
1.1 my_sub.cpp代码如下
这里订阅的是 第2章 中 my_topic01 的话题
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
class topic_sub : public rclcpp::Node
{
private:
// 声明节点
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub;
void sub_back(const std_msgs::msg::String::SharedPtr msg)
{
RCLCPP_INFO(this->get_logger(), "收到[%s]指令", msg->data.c_str());
}
public:
// 构造函数,有一个参数为节点名称
topic_sub(std::string name) : Node(name)
{
RCLCPP_INFO(this->get_logger(), "大家好,我是%s.", name.c_str());
// 订阅的话题名 缓存