ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to convert a std::chrono::high_resolution_clock to ros::Time

asked 2019-10-24 02:34:08 -0600

Markus Bader gravatar image

I like to convert a chrono::high_resolution_clock to ros:Time?

edit retag flag offensive close merge delete

1 Answer

Sort by ┬╗ oldest newest most voted
0

answered 2019-10-24 02:39:59 -0600

Markus Bader gravatar image

updated 2019-10-24 03:16:17 -0600

Hi ros::Time are sec and nsec since epoch

#include <iostream>
#include <chrono>
#include <ctime>
#include <ros/ros.h>

int main()
{
    const auto p0 = std::chrono::time_point<std::chrono::high_resolution_clock>{};
    const auto p3 = std::chrono::high_resolution_clock::now();

    auto tstamp = p3 - p0;
    int32_t sec = std::chrono::duration_cast<std::chrono::seconds>(tstamp).count();
    int32_t nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(tstamp).count() % 1000000000UL;
    std::cout << "sec: " << sec << " nsec: " << nsec << " since epoch: \n";
    ros::Time n(sec, nsec);
    return 0;
}
edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2019-10-24 02:34:08 -0600

Seen: 2,139 times

Last updated: Oct 24 '19