当前位置:   article > 正文

ROS2学习之路之ros2时间_ros2获取当前时间

ros2获取当前时间

ros2时间
获取时间

#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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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();
  • 1
  • 2
  • 3
  • 4
#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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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.";
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

spin()和spin_some()用法总结:
1)程序执行到了rclcpp::spin()或者rclcpp::spin_some(),才能真正使回调函数生效。

2)rclcpp::spin():遇到spin,会一直等待话题消息。队列中如果有数据,则处理回调函数;否则,继续查看并且等待。也就是说遇到spin后,在其后面的代码不执行了。

3)当程序执行到rclcpp::spin_some()时,会去话题订阅缓冲区中查看有没有回调函数。如果有,就马上处理一个回调函数并退出;否则,就退出这个指令,执行后续的代码。

  1. rclcpp::spin_some比rclcpp::spin()更加灵活,常用于手动写循环,可以放在程序的任何位置下,但是需要考虑spinOnce执行频率与所订阅话题的发布频率之间的关系。而spin()就比较粗暴,反正一直在等话题产生,只要注意回调函数不持续超时,或者确保订阅缓冲区足够大就好。

用法:rclcpp::spin(nodePtr);
rclcpp::spin_some(nodePtr);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/540565
推荐阅读
相关标签
  

闽ICP备14008679号