odom: goc quay EKF -- GyroYawFuser (gyro giu tong, encoder lap khoang chua ve) thay cua so 0.5-1.5 buoc roi vut: IMU 200 Hz toi theo cum lam ban do 07/10 dem thieu 9.9% moi goc quay; log odom/yaw 30 s

This commit is contained in:
trung09122002
2026-10-07 13:25:51 +07:00
parent b5574a17b5
commit 3935ffe0cc
7 changed files with 410 additions and 35 deletions

View File

@@ -8,6 +8,7 @@ vslam_add_library(vslam_odometry
wheel_odometry.h
ekf_se2.h
height_estimator.h
gyro_yaw_fuser.h
PUBLIC_DEPS
vslam_common
FIND_DEPS

View File

@@ -0,0 +1,87 @@
#ifndef VSLAM_GYRO_YAW_FUSER_H
#define VSLAM_GYRO_YAW_FUSER_H
// Goc quay (yaw) cho EKF: GYRO giu TONG, encoder chi lap khoang gyro chua ve.
//
// ================== VI SAO ==================
// Cach cu tich gyro THEO LUC MAU TOI giua hai buoc encoder, roi chi nhan khi do
// dai cua so gyro nam trong 0.5-1.5 lan buoc encoder, ngoai do thi VUT. Tren
// robot that mau IMU toi THEO CUM (log 07/10: cua so 40 ms = 8 mau, buoc
// encoder 22.5 ms): cua so ngan (thieu) duoc nhan, cua so dai (mang phan bu)
// bi vut -> goc DEM THIEU mot chieu. Do tren ban do real_20261007_112455: ICP
// phai cong them 9.9% moi goc quay (ban do 28/09 va 01/10 dung encoder: 0% va
// -1.8%), trong khi chinh gyro van khop encoder 0.99 luc quay. Sim khong lo vi
// IMU va encoder toi deu.
//
// ================== CACH LAM ==================
// Goc tai buoc encoder t:
// Y(t) = Theta_gyro(b) + [theta_enc(t) - theta_enc(b)]
// b = dau thoi gian mau gyro MOI NHAT (da bu tre). Moi buoc tra ve
// Y(t_i) - Y(t_{i-1}): tong cong don lai triet tieu, nen ve lau dai goc theo
// DUNG gyro (khong mau nao bi vut), con khoang (b, t] -- vai chuc ms gyro chua
// toi -- lay tu encoder nen khong tre. Truot banh lam encoder sai trong khoang
// do thi buoc sau, khi gyro toi, phan lech duoc tra lai.
//
// IMU cham (chu ky > max_gyro_period, vd WT61PC 10 Hz) hoac het tuoi thi chi
// dung encoder -- nhu truoc 06/10. Gyro dut quang (khe > max_gap) thi dat lai
// moc, khong bu qua khe.
#include <deque>
#include "vslam/unix_time.h"
namespace vslam {
struct GyroYawFuserConfig {
double max_gyro_period = 0.03; // s; IMU cham hon -> chi encoder
double stale_sec = 0.2; // s; khong co mau gyro moi -> chi encoder
double max_gap = 0.1; // s; hai mau gyro cach hon -> khong tich phan qua khe
double enc_history_sec = 2.0; // s; lich su goc encoder de noi suy tai moc gyro
};
class GyroYawFuser {
public:
explicit GyroYawFuser(GyroYawFuserConfig cfg = GyroYawFuserConfig());
// Mot mau gyro z (rad/s), dau thoi gian DA bu tre IMU. Goi theo thu tu toi.
void AddGyro(UnixTime t, double wz);
// Mot buoc encoder ket thuc tai `t` voi goc encoder `d_theta_enc` (rad).
// `*d_theta` = goc da ghep cho buoc nay. Tra true neu co dung gyro.
bool Step(UnixTime t, double d_theta_enc, double* d_theta);
void Reset();
// Chu ky gyro uoc luong (s), 0 neu chua biet. De log.
double gyro_period() const { return period_; }
private:
struct EncSample {
UnixTime t;
double theta;
};
bool EncThetaAt(UnixTime t, double* out) const;
GyroYawFuserConfig cfg_;
// Gyro: goc tich luy (hinh thang) toi mau moi nhat.
bool have_g_ = false;
UnixTime g_t_;
double g_w_ = 0.0;
double g_theta_ = 0.0;
double period_ = 0.0;
// Encoder: goc tich luy theo thoi gian.
std::deque<EncSample> enc_;
double enc_theta_ = 0.0;
// Moc cua buoc truoc: thoi gian b, goc gyro va goc encoder tai b.
bool anchored_ = false;
UnixTime b_;
double g_at_b_ = 0.0;
double e_at_b_ = 0.0;
};
} // namespace vslam
#endif // VSLAM_GYRO_YAW_FUSER_H

View File

@@ -0,0 +1,106 @@
#include "vslam/gyro_yaw_fuser.h"
#include <algorithm>
namespace vslam {
GyroYawFuser::GyroYawFuser(GyroYawFuserConfig cfg) : cfg_(cfg) {}
void GyroYawFuser::Reset() {
have_g_ = false;
g_w_ = 0.0;
g_theta_ = 0.0;
period_ = 0.0;
enc_.clear();
enc_theta_ = 0.0;
anchored_ = false;
}
void GyroYawFuser::AddGyro(UnixTime t, double wz) {
if (!have_g_) {
have_g_ = true;
g_t_ = t;
g_w_ = wz;
return;
}
const double dt = SecondsBetween(t, g_t_);
if (dt <= 0.0) {
// Dau thoi gian bang mau truoc (driver kep cho khong lui): khong co khoang
// nao de tich, mau sau se phu khoang nay. Giu toc do moi nhat.
g_w_ = wz;
return;
}
if (dt < cfg_.max_gap) {
g_theta_ += 0.5 * (g_w_ + wz) * dt;
period_ = period_ > 0.0 ? 0.9 * period_ + 0.1 * dt : dt;
} else {
// Dut quang: goc gyro khong biet trong khe -- dat lai moc, khong bu qua khe.
anchored_ = false;
}
g_t_ = t;
g_w_ = wz;
}
bool GyroYawFuser::EncThetaAt(UnixTime t, double* out) const {
if (enc_.empty()) return false;
if (SecondsBetween(t, enc_.back().t) >= 0.0) {
if (SecondsBetween(t, enc_.back().t) > 0.0) return false; // sau buoc moi nhat
*out = enc_.back().theta;
return true;
}
for (size_t k = enc_.size() - 1; k > 0; --k) {
const EncSample& hi = enc_[k];
const EncSample& lo = enc_[k - 1];
if (SecondsBetween(t, lo.t) < 0.0) continue;
const double span = SecondsBetween(hi.t, lo.t);
*out = span > 0.0 ? lo.theta + (hi.theta - lo.theta) * SecondsBetween(t, lo.t) / span
: hi.theta;
return true;
}
return false; // truoc mau encoder cu nhat
}
bool GyroYawFuser::Step(UnixTime t, double d_theta_enc, double* d_theta) {
*d_theta = d_theta_enc;
if (!enc_.empty() && SecondsBetween(t, enc_.back().t) <= 0.0) {
// Thoi gian encoder lui (reset mo phong, phat lai): bat dau lai.
Reset();
}
enc_theta_ += d_theta_enc;
enc_.push_back(EncSample{t, enc_theta_});
while (enc_.size() > 2 && SecondsBetween(t, enc_.front().t) > cfg_.enc_history_sec) {
enc_.pop_front();
}
const bool usable = have_g_ && period_ > 0.0 && period_ <= cfg_.max_gyro_period &&
SecondsBetween(t, g_t_) < cfg_.stale_sec;
if (!usable) {
anchored_ = false;
return false;
}
// Moc moi: mau gyro moi nhat, khong vuot buoc encoder nay.
const UnixTime b = SecondsBetween(g_t_, t) > 0.0 ? t : g_t_;
double e_b = 0.0;
if (!EncThetaAt(b, &e_b)) {
anchored_ = false;
return false;
}
// Gyro toi tre hon buoc encoder thi g_theta_ la goc tai g_t_; b = t chi khi
// gyro di truoc (it gap) -- khi do goc gyro tai t lay bang mau moi nhat.
if (!anchored_ || SecondsBetween(b, b_) < 0.0) {
anchored_ = true;
b_ = b;
g_at_b_ = g_theta_;
e_at_b_ = e_b;
return false;
}
// Goc tren khoang moc [b_, b]: gyro thay encoder.
const double corr = (g_theta_ - g_at_b_) - (e_b - e_at_b_);
*d_theta = d_theta_enc + corr;
b_ = b;
g_at_b_ = g_theta_;
e_at_b_ = e_b;
return true;
}
} // namespace vslam