The ros::Time
object is made from seconds and nanoseconds, as mentioned here. Therefore, we need to extract these values (sec and nsec) from std::chrono
. See an example below:
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);
Please note that the above code is taken from here.
Furthermore, you can see the API that ros::Time
can be made seconds only. Here seconds are represented by a double
datatype. Please see here and here also for more info. In short, you just need to convert nanoseconds i.e., std::chrono::nanoseconds
to seconds and use it as ros::Time n(sec)
.
This may help.
@ravijoshi You should make your comment the answer.