#include
#include
#include
#include
#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 //自定义一个类继承自Node类
{
public:
MinimalPublisher() //构造函数
: Node("minimal_publisher"), count_(0) //初始化参数列表,创建了一个名为
//minimal_publisher的对象
{//这个publisher_定义在类的后半部分,是一个指向publisher对象的指针
//create_publiser(“节点名称",缓冲队列长度 ),返回值publisher对象的指针
publisher_ = this->create_publisher
//create_wall_timer(),大体也就是create_timer那个意思吧,就是创建一个定时器
//第一个参数是时间间隔,第二个参数是处理函数;就这么简单
//其中bind()函数会返回一个新的函数对象,这个函数对象会调用timer_callback(),并且将this参数传递给timer_callback(),大概等于this->timer_callback();
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_++); //字符串内容是。。。
//这个函数相当于COUT<<
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
publisher_->publish(message); //发布消息
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher
size_t count_;
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
//spin()函数就是反复执行括号里面的函数因该约等于while(1){ }
rclcpp::spin(std::make_shared
rclcpp::shutdown();
return 0;
}