赞
踩
#include <chrono>
using namespace std::chrono_literals;
获取系统当前时间
auto start = std::chrono::system_clock::now();
auto end = std::chrono::system_clock::now();
计算时间差
std::chrono::duration<double> diff = end - start;
std::chrono::steady_clock
获取系统当前时间
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
计算时间差
std::chrono::duration_cast<std::chrono::microseconds> diff = (end - start).count();
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
int main()
{
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
auto now = std::chrono::system_clock::now();//now为std::chrono::system_clock::time_point 类型
std::time_t now_ = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24));
std::cout << "the time was " << std::put_time(std::localtime(&now_), "%F %T") << std::endl;
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::chrono::duration_cast<std::chrono::microseconds> diff = (end - start).count();
}
std::chrono::high_resolution_clock
using namespace std::chrono;
high_resolution_clock::time_point start = high_resolution_clock::now();
high_resolution_clock::time_point end = high_resolution_clock::now();
duration<double> diff = duration_cast<duration<double>>(start - end);
std::cout << "time " << diff.count() << " seconds.";
ros2的时间戳
auto t1 = rclcpp::Clock().now(); double t1_s = t1.seconds(); double t1_n = t1.nanoseconds(); auto tn = rclcpp::Clock().now(); RCLCPP_INFO(nodePtr_->get_logger(), " time 1: %ld %ld %ld", t1_s,t1_n,tn-t1); auto t2 = std::chrono::system_clock::now(); time_t t2_s = std::chrono::system_clock::to_time_t ( t2 ); RCLCPP_INFO(nodePtr_->get_logger(), " time 2: %ld ",t2_s); std::chrono::steady_clock::time_point t3 = std::chrono::steady_clock::now(); std::chrono::steady_clock::duration td = t3.time_since_epoch(); double secs = td.count() * std::chrono::steady_clock::period::num / std::chrono::steady_clock::period::den; RCLCPP_INFO(nodePtr_->get_logger(), " time 3: %ld ",secs); auto t4 = nodePtr_->get_clock()->now(); double t4_s = t4.seconds(); double t4_n = t4.nanoseconds(); RCLCPP_INFO(nodePtr_->get_logger(), " time 4: %ld %ld ", t4_s,t4_n); auto t5 = nodePtr_->now(); double t5_s = t5.seconds(); double t5_n = t5.nanoseconds(); RCLCPP_INFO(nodePtr_->get_logger(), " time 5: %ld %ld ", t5_s,t5_n);
ros2延时
#include <iostream> // std::cout, std::endl
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
int main()
{
std::cout << "start sleep"<<std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "stop sleep"<<std::endl;
return 0;
}
ros2运行频率控制
int main(int argc, char** argv){
rclcpp::init(argc, argv);
rclcpp::WallRate loop_rate(1.0);//1HZ
while (rclcpp::ok())
{
/* code */
loop_rate.sleep();
}
}
spin()和spin_some()用法总结:
1)程序执行到了rclcpp::spin()或者rclcpp::spin_some(),才能真正使回调函数生效。
2)rclcpp::spin():遇到spin,会一直等待话题消息。队列中如果有数据,则处理回调函数;否则,继续查看并且等待。也就是说遇到spin后,在其后面的代码不执行了。
3)当程序执行到rclcpp::spin_some()时,会去话题订阅缓冲区中查看有没有回调函数。如果有,就马上处理一个回调函数并退出;否则,就退出这个指令,执行后续的代码。
用法:rclcpp::spin(nodePtr);
rclcpp::spin_some(nodePtr);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。