using namespace std::literals::chrono_literals; timer_ = this->create_wall_timer( 500ms, std::bind(&ParametersBasicNode::timer_callback, this)); } private: int log_level; rclcpp::TimerBase::SharedPtr timer_; void timer_callback() { this->get_parameter("rcl_log_level", log_level);...
定时器是ROS2中的另外一个常用功能,通过定时器可以实现按照一定周期调用某个函数以实现定时发布等逻辑。 定时器对应的类是rclcpp::TimerBase,调用create_wall_timer将返回其共享指针。 创建定时器时传入了两个参数,这两个参数都利用了C++11的新特性。 std::chrono::milliseconds(500),代表500ms,chrono是c++ 11中的...
robot_status_publisher_ = this->create_publisher<example_ros2_interfaces::msg::RobotStatus>("robot_status", 10); /*创建一个周期为500ms的定时器*/ timer_ = this->create_wall_timer(std::chrono::milliseconds(500), std::bind(&ExampleInterfacesRobot::timer_callback, this)); } private: Robot...
options) {// 创建发布者,发布在"topic_name"主题上publisher_ =this->create_publisher<std_msgs::msg::String>("topic_name",10);// 创建定时器,每秒调用一次timer_callbacktimer_ =this->create_wall_timer(std::chrono::...
#include <rclcpp/rclcpp.hpp> #include <std_msgs/msg/string.hpp> class Talker : public rclcpp::Node { public: Talker() : Node("talker") { publisher_ = this->create_publisher<std_msgs::msg::String>("chatter", 10); timer_ = this->create_wall_timer( std...
timer_ = this->create_wall_timer( 500ms, std::bind(&ParametersBasicNode::timer_callback, this)); } private: int log_level; rclcpp::TimerBase::SharedPtr timer_; void timer_callback() { this->get_parameter("rcl_log_level", log_level); /*获取参数*/ ...
>create_publisher<std_msgs::msg::String>("topic_name", 10); // 创建定时器,每秒调用一次timer_callback timer_ = this->create_wall_timer( std::chrono::seconds(1), [this]() { this->timer_callback(); }); } private: // 定时器回调函数,用于发布消息 void timer_callback() { auto ...