#include "vslam/clock_sync.h" #include #include #include #include "vslam/log.h" namespace vslam { namespace { constexpr uint64_t kWrapPeriodUs = 1ULL << 32; // 4294967296 us = 71.58 phut constexpr uint32_t kHalfRange = 1U << 31; } // namespace // --- McuTimeUnwrapper ------------------------------------------------------ double McuTimeUnwrapper::UnwrapSeconds(uint32_t micros) { if (first_) { first_ = false; prev_us_ = micros; return static_cast(micros) * 1e-6; } // Tran khi gia tri moi NHO HON gia tri truoc mot khoang lon. Nguong nua dai // (2^31) phan biet duoc "tran" voi "ban tin den khong dung thu tu": ban tin // lech thu tu chi lui vai mili giay, con tran thi lui gan 71 phut. // // GIA DINH: khoang cach giua hai lan goi < nua chu ky (35.79 phut). Neu goi // thua hon nua chu ky thi khong the phan biet "tran" voi "nhay lui" -- day // la gioi han co ban cua moi bo dem tran, khong phai thieu sot cai dat. // O 200 Hz (IMU) hay 50 Hz (encoder) thi gia dinh nay du xa an toan; neu // sau nay co nguon nao goi thua hon 35 phut mot lan thi nguon do can dong // ho rieng chu khong dung duoc lop nay. if (micros < prev_us_ && (prev_us_ - micros) > kHalfRange) { high_ += kWrapPeriodUs; wraps_++; VSLAM_INFO("clock_sync: micros() tran lan thu " << wraps_ << " (moi 71.58 phut) -- da bu"); } prev_us_ = micros; return static_cast(high_ + micros) * 1e-6; } void McuTimeUnwrapper::Reset() { prev_us_ = 0; high_ = 0; wraps_ = 0; first_ = true; } // --- ClockSync ------------------------------------------------------------- ClockSync::ClockSync(ClockSyncConfig cfg) : cfg_(cfg) { if (cfg_.window == 0) cfg_.window = 1; if (cfg_.keep_best == 0) cfg_.keep_best = 1; } void ClockSync::Add(double mcu_s, double t_send, double t_recv) { const double rtt = t_recv - t_send; if (!(rtt >= 0.0)) return; // bat ca NaN // Gia dinh do tre doi xung: thoi diem MCU tra loi nam giua luc gui va luc // nhan. Chi dung khi RTT nho -- do la ly do chi giu mau RTT nho nhat. const double host_s = 0.5 * (t_send + t_recv); samples_.push_back(Sample{rtt, mcu_s, host_s}); while (samples_.size() > cfg_.window) samples_.pop_front(); Fit(); } void ClockSync::Fit() { if (samples_.size() < 2) return; std::vector best(samples_.begin(), samples_.end()); const size_t k = std::min(cfg_.keep_best, best.size()); std::partial_sort(best.begin(), best.begin() + static_cast(k), best.end(), [](const Sample& a, const Sample& b) { return a.rtt < b.rtt; }); best.resize(k); if (best.size() < 2) return; const double n = static_cast(best.size()); // ⚠️ PHAI doi goc truoc khi fit. Cong thuc `(n*sxy - sx*sy) / (n*sxx - sx*sx)` // tren so THO la mot cai bay so hoc o day, vi hai truc lech nhau chin bac do // lon: `host_s` la thoi gian Unix (~1.8e9) con `mcu_s` la giay tu luc MCU // khoi dong (~vai tram, ve 0 moi lan cam lai dien). // // Voi mau trai dai vai giay thi `n*sxy` va `sx*sy` deu co do lon ~4e9 trong // khi HIEU that su cua chung chi co ~1e-2. double giu duoc 15-16 chu so co // nghia, tuc o do lon 4e9 thi buoc nho nhat da la ~1e-6 -- phan tin hieu bi // nuot gan het trong phep tru. Trieu triet tieu kinh dien. // // Da do tren robot that, KHONG phai lo ngai ly thuyet: fit dau tien cho // skew = 0.000318103, tuc lech -999682 ppm. ClockSync tu choi ket qua do // (dung), roi ve skew = 1.0 va o nguyen do -- nen chuoi dong ho MCU khong he // chay du log noi "ClockSync san sang". // // Doi goc ve trung binh thi ca hai truc con lai bien thien that (co giay), // phep tru khong con triet tieu, va ket qua khong doi ve mat toan hoc. double mx = 0.0, my = 0.0; for (const auto& s : best) { mx += s.mcu_s; my += s.host_s; } mx /= n; my /= n; double sxx = 0.0, sxy = 0.0; for (const auto& s : best) { const double dx = s.mcu_s - mx; sxx += dx * dx; sxy += dx * (s.host_s - my); } if (!(sxx > 1e-12)) { // Cac mau qua gan nhau ve thoi gian -> chua uoc luong duoc skew, nhung // offset thi van uoc luong duoc. offset_ = my - mx; skew_ = 1.0; ready_ = true; return; } const double skew = sxy / sxx; const double offset = my - skew * mx; // Skew phai rat gan 1.0 -- thach anh lech vai chuc ppm. Lech nhieu nghia la // du lieu rac (giao thuc sai, hoac micros() tran ma khong unwrap). if (std::abs(skew - 1.0) > cfg_.max_skew_dev) { rejected_++; VSLAM_WARN_THROTTLE(30.0, "clock_sync: skew bat thuong (" << skew << ", lech " << (skew - 1.0) * 1e6 << " ppm), bo qua ket qua fit. Kiem giao thuc MCU" " va viec unwrap micros()."); skew_ = 1.0; offset_ = my - mx; ready_ = true; return; } skew_ = skew; offset_ = offset; ready_ = true; } bool ClockSync::ToHost(double mcu_s, double* host_s) const { if (!ready_) return false; *host_s = skew_ * mcu_s + offset_; return true; } void ClockSync::Reset() { samples_.clear(); skew_ = 1.0; offset_ = 0.0; ready_ = false; rejected_ = 0; } } // namespace vslam