in src/k4a_ros_device.cpp [1492:1525]
void K4AROSDevice::updateTimestampOffset(const std::chrono::microseconds& k4a_device_timestamp_us,
const std::chrono::nanoseconds& k4a_system_timestamp_ns)
{
// System timestamp is on monotonic system clock.
// Device time is on AKDK hardware clock.
// We want to continuously estimate diff between realtime and AKDK hardware clock as low-pass offset.
// This consists of two parts: device to monotonic, and monotonic to realtime.
// First figure out realtime to monotonic offset. This will change to keep updating it.
std::chrono::nanoseconds realtime_clock = std::chrono::system_clock::now().time_since_epoch();
std::chrono::nanoseconds monotonic_clock = std::chrono::steady_clock::now().time_since_epoch();
std::chrono::nanoseconds monotonic_to_realtime = realtime_clock - monotonic_clock;
// Next figure out the other part (combined).
std::chrono::nanoseconds device_to_realtime =
k4a_system_timestamp_ns - k4a_device_timestamp_us + monotonic_to_realtime;
// If we're over a second off, just snap into place.
if (device_to_realtime_offset_.count() == 0 ||
std::abs((device_to_realtime_offset_ - device_to_realtime).count()) > 1e7)
{
ROS_WARN_STREAM("Initializing or re-initializing the device to realtime offset: " << device_to_realtime.count()
<< " ns");
device_to_realtime_offset_ = device_to_realtime;
}
else
{
// Low-pass filter!
constexpr double alpha = 0.10;
device_to_realtime_offset_ = device_to_realtime_offset_ +
std::chrono::nanoseconds(static_cast<int64_t>(
std::floor(alpha * (device_to_realtime - device_to_realtime_offset_).count())));
}
}