106 lines
3.4 KiB
C++
106 lines
3.4 KiB
C++
#ifndef VSLAM_CLOCK_SYNC_H
|
|
#define VSLAM_CLOCK_SYNC_H
|
|
|
|
// Dong bo dong ho MCU <-> host: host_time = skew * mcu_time + offset.
|
|
//
|
|
// Port tu robot_drivers/scripts/imu_bridge_node.py:51-111, kem MOT SUA LOI
|
|
// quan trong ma ban Python khong co.
|
|
//
|
|
// ================== BUG TRAN micros() ==================
|
|
// micros() tren MCU la uint32_t: no TRAN moi 71.58 phut (2^32 us).
|
|
//
|
|
// Ban Python khong unwrap. Sau 71 phut, mcu_time nhay tu ~4295 giay ve 0,
|
|
// fit tuyen tinh vo, kiem tra |skew - 1| > 1e-3 tu choi ket qua, va fallback
|
|
// `offset = (sy - sx) / n` la rac -> MOI stamp IMU sai hang nghin giay.
|
|
//
|
|
// Trieu chung tren robot that: sau hon mot gio chay, TransformTree tu choi moi
|
|
// lookup vi stamp o tuong lai xa, robot dung im, va khong co gi trong log noi
|
|
// tai sao. Day dung la loai bug chi hien ra khi chay lau -- tuc la chi hien ra
|
|
// khi dang trinh dien.
|
|
//
|
|
// McuTimeUnwrapper o duoi xu ly viec do. PHAI goi theo dung thu tu ban tin den.
|
|
//
|
|
// ================== VI SAO CHI GIU MAU RTT NHO ==================
|
|
// RTT lon nghia la ban tin bi tre boi lich CPU hoac buffer serial, va do tre
|
|
// do gan nhu luon BAT DOI XUNG. Gia dinh `host = (t_send + t_recv) / 2` chi
|
|
// dung khi do tre doi xung, nen mau RTT lon lam hong uoc luong. Giu 16 mau
|
|
// nho nhat trong cua so 64.
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <deque>
|
|
#include <memory>
|
|
|
|
namespace vslam {
|
|
|
|
// Go tran uint32_t cua micros(). PHAI goi theo thu tu ban tin den.
|
|
class McuTimeUnwrapper {
|
|
public:
|
|
// Tra ve thoi gian MCU tinh bang giay, da cong phan tran.
|
|
double UnwrapSeconds(uint32_t micros);
|
|
|
|
void Reset();
|
|
uint64_t wrap_count() const { return wraps_; }
|
|
|
|
private:
|
|
uint32_t prev_us_ = 0;
|
|
uint64_t high_ = 0; // tong phan da tran, tinh bang micro giay
|
|
uint64_t wraps_ = 0;
|
|
bool first_ = true;
|
|
};
|
|
|
|
// O pham vi namespace chu khong long trong ClockSync: mot struct long co
|
|
// default member initializer khong dung lam DOI SO MAC DINH cho ham thanh vien
|
|
// cua chinh lop bao duoc -- NSDMI chua hoan chinh cho toi het lop bao.
|
|
struct ClockSyncConfig {
|
|
size_t window = 64; // so mau giu lai
|
|
size_t keep_best = 16; // so mau RTT nho nhat dung de fit
|
|
double max_skew_dev = 1e-3; // |skew - 1| vuot nguong nay = du lieu rac
|
|
};
|
|
|
|
class ClockSync {
|
|
public:
|
|
using Config = ClockSyncConfig;
|
|
|
|
explicit ClockSync(ClockSyncConfig cfg = ClockSyncConfig());
|
|
|
|
// mcu_s: thoi gian MCU DA UNWRAP (giay).
|
|
// t_send, t_recv: thoi gian host luc gui ping va nhan pong (giay).
|
|
void Add(double mcu_s, double t_send, double t_recv);
|
|
|
|
// Doi thoi gian MCU sang thoi gian host. Chua san sang thi tra false.
|
|
bool ToHost(double mcu_s, double* host_s) const;
|
|
|
|
bool ready() const { return ready_; }
|
|
double skew() const { return skew_; }
|
|
double offset() const { return offset_; }
|
|
|
|
// Sai so ppm cua skew so voi 1.0. Thach anh thuong lech vai chuc ppm;
|
|
// vai nghin ppm nghia la giao thuc sai chu khong phai thach anh te.
|
|
double skew_ppm() const { return (skew_ - 1.0) * 1e6; }
|
|
|
|
uint64_t rejected_fits() const { return rejected_; }
|
|
size_t sample_count() const { return samples_.size(); }
|
|
void Reset();
|
|
|
|
private:
|
|
struct Sample {
|
|
double rtt;
|
|
double mcu_s;
|
|
double host_s;
|
|
};
|
|
|
|
void Fit();
|
|
|
|
ClockSyncConfig cfg_;
|
|
std::deque<Sample> samples_;
|
|
double skew_ = 1.0;
|
|
double offset_ = 0.0;
|
|
bool ready_ = false;
|
|
uint64_t rejected_ = 0;
|
|
};
|
|
|
|
} // namespace vslam
|
|
|
|
#endif // VSLAM_CLOCK_SYNC_H
|