下面是一个利用ros::Time::now打印时间戳的示例代码:

#include <ros/ros.h>

int main(int argc, char** argv)
{
  ros::init(argc, argv, "time_test");
  ros::NodeHandle nh;

  while (ros::ok())
  {
    ros::Time now = ros::Time::now();
    ROS_INFO("Current time: %f", now.toSec());
    ros::Duration(1.0).sleep(); // sleep for 1 second
  }

  return 0;
}

在上面的代码中,我们首先初始化了ROS节点,然后在一个循环中利用ros::Time::now获取当前时间戳,并使用ROS_INFO打印出来。为了避免打印速度过快,我们在每次循环结束时使用ros::Duration::sleep函数暂停1秒钟。

在运行这个程序时,你会看到类似下面的输出:

[ INFO] [1548260798.500260836]: Current time: 1548260798.500260
[ INFO] [1548260799.500470062]: Current time: 1548260799.500470
[ INFO] [1548260800.500625701]: Current time: 1548260800.500626
[ INFO] [1548260801.500809150]: Current time: 1548260801.500809
...

这些输出显示了每隔1秒钟打印一次当前时间戳

利用rosTimenow打印时间戳

原文地址: https://www.cveoy.top/t/topic/g88B 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录