diff --git a/msg/templates/urtps/microRTPS_timesync.cpp.em b/msg/templates/urtps/microRTPS_timesync.cpp.em index 51ada802e8..ba5d9bab3a 100644 --- a/msg/templates/urtps/microRTPS_timesync.cpp.em +++ b/msg/templates/urtps/microRTPS_timesync.cpp.em @@ -107,6 +107,16 @@ void TimeSync::reset() { _request_reset_counter = 0; } +int64_t TimeSync::getTimeNSec() { + auto time = std::chrono::steady_clock::now(); + return std::chrono::time_point_cast(time).time_since_epoch().count(); +} + +int64_t TimeSync::getTimeUSec() { + auto time = std::chrono::steady_clock::now(); + return std::chrono::time_point_cast(time).time_since_epoch().count(); +} + bool TimeSync::addMeasurement(int64_t local_t1_ns, int64_t remote_t2_ns, int64_t local_t3_ns) { int64_t rtti = local_t3_ns - local_t1_ns; @@ -167,15 +177,15 @@ void TimeSync::processTimesyncMsg(timesync_msg_t * msg) { _last_remote_msg_seq = getMsgSeq(msg); if (getMsgTC1(msg) > 0) { - if (!addMeasurement(getMsgTS1(msg), getMsgTC1(msg), getMonoRawTimeNSec())) { + if (!addMeasurement(getMsgTS1(msg), getMsgTC1(msg), getTimeNSec())) { if (_debug) std::cerr << "\033[1;33m[ micrortps__timesync ]\tOffset not updated\033[0m" << std::endl; } } else if (getMsgTC1(msg) == 0) { - setMsgTimestamp(msg, getMonoTimeUSec()); + setMsgTimestamp(msg, getTimeUSec()); setMsgSysID(msg, 0); setMsgSeq(msg, getMsgSeq(msg) + 1); - setMsgTC1(msg, getMonoRawTimeNSec()); + setMsgTC1(msg, getTimeNSec()); _timesync_pub.publish(msg); } @@ -185,11 +195,11 @@ void TimeSync::processTimesyncMsg(timesync_msg_t * msg) { timesync_msg_t TimeSync::newTimesyncMsg() { timesync_msg_t msg{}; - setMsgTimestamp(&msg, getMonoTimeUSec()); + setMsgTimestamp(&msg, getTimeUSec()); setMsgSysID(&msg, 0); setMsgSeq(&msg, _last_msg_seq); setMsgTC1(&msg, 0); - setMsgTS1(&msg, getMonoRawTimeNSec()); + setMsgTS1(&msg, getTimeNSec()); _last_msg_seq++; diff --git a/msg/templates/urtps/microRTPS_timesync.h.em b/msg/templates/urtps/microRTPS_timesync.h.em index 611839b4d4..2a3e12bd8d 100644 --- a/msg/templates/urtps/microRTPS_timesync.h.em +++ b/msg/templates/urtps/microRTPS_timesync.h.em @@ -130,23 +130,15 @@ public: /** * @@brief Get clock monotonic time (raw) in nanoseconds - * @@return System CLOCK_MONOTONIC_RAW time in nanoseconds + * @@return System CLOCK_MONOTONIC time in nanoseconds */ - inline int64_t getMonoRawTimeNSec() { - timespec t; - clock_gettime(CLOCK_MONOTONIC_RAW, &t); - return static_cast(t.tv_sec * 1000000000LL + t.tv_nsec); - } + static int64_t getTimeNSec(); /** * @@brief Get system monotonic time in microseconds * @@return System CLOCK_MONOTONIC time in microseconds */ - inline int64_t getMonoTimeUSec() { - timespec t; - clock_gettime(CLOCK_MONOTONIC, &t); - return static_cast(t.tv_sec * 1000000000LL + t.tv_nsec) / 1000LL; - } + static int64_t getTimeUSec(); /** * @@brief Adds a time offset measurement to be filtered