This commit is contained in:
trung09122002
2026-09-18 18:14:42 +07:00
commit 48bd5377dc
224 changed files with 37685 additions and 0 deletions

View File

@@ -0,0 +1,140 @@
#include "vslam/clock_sync.h"
#include <algorithm>
#include <cmath>
#include <vector>
#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<double>(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<double>(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<Sample> 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<long>(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<double>(best.size());
double sx = 0.0, sy = 0.0, sxx = 0.0, sxy = 0.0;
for (const auto& s : best) {
sx += s.mcu_s;
sy += s.host_s;
sxx += s.mcu_s * s.mcu_s;
sxy += s.mcu_s * s.host_s;
}
const double denom = n * sxx - sx * sx;
if (std::abs(denom) < 1e-12) {
// Cac mau qua gan nhau ve thoi gian -> chua uoc luong duoc skew, nhung
// offset thi van uoc luong duoc.
offset_ = (sy - sx) / n;
skew_ = 1.0;
ready_ = true;
return;
}
const double skew = (n * sxy - sx * sy) / denom;
const double offset = (sy - skew * sx) / n;
// 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_ = (sy - sx) / n;
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