ros2中的定时器
rclcpp::TimerBase::SharedPtr
这是ros2终端定时器的类型;
使用定时器的配置
引用头文件:
#include <rclcpp/rclcpp.hpp>
#include <rclcpp/timer.hpp> //这个可以引入,也可以不引入
定时器创建
节点创建定时器
需要使用rclcpp::Node中的接口创建:
rclcpp::TimerBase::SharedPtr timer_=this->create_wall_timer()
Node::create_wall_timer()
template<typename DurationRepT, typename DurationT, typename CallbackT>
typename rclcpp::WallTimer<CallbackT>::SharedPtr
Node::create_wall_timer(
std::chrono::duration<DurationRepT, DurationT> period,
CallbackT callback,
rclcpp::CallbackGroup::SharedPtr group)
{
return rclcpp::create_wall_timer(
period,
std::move(callback),
group,
this->node_base_.get(),
this->node_timers_.get());
}
函数接受三个参数:
`period` 为时间间隔,即回调函数执行的时间间隔;
`callback` 为回调函数,当计时器触发时执行;
`group` 为指定的回调函数组,所有使用同一回调组的定时器将一起触发回调,可不传递。
class PublisherNode:public rclcpp::Node{
public:
PublisherNode(std::string name):Node(name){
publisher_=this->create_publisher<std_msgs::msg::String>("topic_01",10);
timer_=this->create_wall_timer(500ms,std::bind(&PublisherNode::_timer_callback,this));
}
private:
void timer_callback(){
auto msg=std_msgs::msg::String();
msg.data="hello world!";
publisher_->publish(msg);
RCLCPP_INFO(this->get_logger(),"Pulishing: '%s'",msg.data.c_str());
}
rclcpp::TimerBase::SharedPtr timer_{};
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_{};
};
ros2中如何获取当前时间
#include <rclcpp/rclcpp.hpp>
class TimeNowNode : public rclcpp::Node {
public:
TimeNowNode(std::string name) : rclcpp::Node(name) {
RCLCPP_INFO(this->get_logger(), "This is time node...");
auto time = this->get_clock()->now();
RCLCPP_INFO(this->get_logger(), "this->get_clock().now(): %s",
std::to_string(time.seconds()).c_str());
auto rclcpp_time = rclcpp::Clock().now();
RCLCPP_INFO(this->get_logger(), "rclcpp::Clock::now(): %s",
std::to_string(rclcpp_time.seconds()).c_str());
auto rclcpp_node_now = rclcpp::Node::now();
RCLCPP_INFO(this->get_logger(), "rclcpp::Node::now(): %s",
std::to_string(rclcpp_node_now.seconds()).c_str());
}
};
int main(int argc, char **argv) {
rclcpp::init(argc, argv);
RCLCPP_INFO(rclcpp::get_logger("node_log"), "starting node...");
auto node = std::make_shared<TimeNowNode>("test_time_now_node");
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
多个定时器
ros2的定时器计时和主线同步还是异步
ros2回调函数的执行和主线程同步还是异步
同步。
#include "rclcpp/rclcpp.hpp"
#include "rclcpp/timer.hpp"
#include <chrono>
using namespace std::chrono_literals;
//目的:加插定时器之间会不会相互阻塞
class MultiTimerNode : public rclcpp::Node {
public:
MultiTimerNode(std::string name) : Node(name) {
RCLCPP_INFO(this->get_logger(), "Start up node : %s", name.c_str());
timer1_ = this->create_wall_timer(
1s, std::bind(&MultiTimerNode::timer1_callback, this));
timer2_ = this->create_wall_timer(
1s, std::bind(&MultiTimerNode::timer2_callback, this));
}
void timer1_callback() {
RCLCPP_INFO(this->get_logger(), "timer1 thread pid is %d", gettid());
RCLCPP_INFO(this->get_logger(), "timer1 sleep for 5s...");
rclcpp::sleep_for(3s);
RCLCPP_INFO(this->get_logger(), "Timer 1 callback");
}
void timer2_callback() {
RCLCPP_INFO(this->get_logger(), "timer2 thread pid is %d", gettid());
RCLCPP_INFO(this->get_logger(), "timer2 sleep for 5s...");
rclcpp::sleep_for(3s);
RCLCPP_INFO(this->get_logger(), "Timer 2 callback");
}
private:
rclcpp::TimerBase::SharedPtr timer1_;
rclcpp::TimerBase::SharedPtr timer2_;
};
int main(int argc, char *argv[]) {
rclcpp::init(argc, argv);
auto node = std::make_shared<MultiTimerNode>("multi_timer_node");
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
多个定时器之间计时同步还是异步
多个定时器回调函数的执行同步还是异步
同步。
定时器控制
定时器重启
reset()
定时器暂停
cancle()
这个接口只是暂停定时器计时,还是直接销毁定时器?
只是暂停,不销毁,否则,reset就没有对象了。
定时器停止
同暂停--cancle()