From e895f437928d7a7823dbfb0ce3213d5a8d0455eb Mon Sep 17 00:00:00 2001 From: Jacob Dahl Date: Sat, 30 May 2026 16:37:13 -0600 Subject: [PATCH] feat(uavcannode): stamp Fix2.timestamp from PPS-anchored HRT Subscribe to pps_capture on the node, learn the offset (rtc_timestamp - timestamp) per PPS edge, and stamp fix2.timestamp = hrt_now + offset so it is UTC-coherent with gnss_timestamp. Unfired or stale (>5 s) PPS leaves timestamp zero, so the FC guard falls through to SENS_GPS*_DELAY. --- .../uavcannode/Publishers/GnssFix2.hpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/drivers/uavcannode/Publishers/GnssFix2.hpp b/src/drivers/uavcannode/Publishers/GnssFix2.hpp index 4f0968c2698..c6c61e8485e 100644 --- a/src/drivers/uavcannode/Publishers/GnssFix2.hpp +++ b/src/drivers/uavcannode/Publishers/GnssFix2.hpp @@ -39,7 +39,10 @@ #include +#include +#include #include +#include #include namespace uavcannode @@ -73,6 +76,18 @@ public: { using uavcan::equipment::gnss::Fix2; + // Track PPS-anchored offset (GPS UTC - local HRT) so the broadcast can + // stamp fix2.timestamp with a UTC value coherent with fix2.gnss_timestamp. + // FC-side decoders (UavcanGnssBridge) use (timestamp - gnss_timestamp) as + // the receiver processing delay; that subtraction is only meaningful when + // both endpoints are on the same clock, which is what PPS provides here. + pps_capture_s pps; + + if (_pps_capture_sub.update(&pps) && pps.timestamp != 0 && pps.rtc_timestamp != 0) { + _pps_offset_us = static_cast(pps.rtc_timestamp) - static_cast(pps.timestamp); + _pps_last_update = pps.timestamp; + } + // sensor_gps -> uavcan::equipment::gnss::Fix2 sensor_gps_s gps; @@ -81,6 +96,13 @@ public: fix2.gnss_time_standard = fix2.GNSS_TIME_STANDARD_UTC; fix2.gnss_timestamp.usec = gps.time_utc_usec; + + const hrt_abstime now = hrt_absolute_time(); + + if (_pps_last_update != 0 && (now - _pps_last_update) < kPpsStaleTimeoutUs) { + fix2.timestamp.usec = static_cast(static_cast(now) + _pps_offset_us); + } + fix2.latitude_deg_1e8 = (int64_t)(gps.latitude_deg * 1e8); fix2.longitude_deg_1e8 = (int64_t)(gps.longitude_deg * 1e8); fix2.height_msl_mm = (int32_t)(gps.altitude_msl_m * 1e3); @@ -153,5 +175,12 @@ public: uORB::SubscriptionCallbackWorkItem::registerCallback(); } } + +private: + static constexpr hrt_abstime kPpsStaleTimeoutUs{5'000'000}; + + uORB::Subscription _pps_capture_sub{ORB_ID(pps_capture)}; + int64_t _pps_offset_us{0}; + hrt_abstime _pps_last_update{0}; }; } // namespace uavcannode