107 lines
3.0 KiB
C++
107 lines
3.0 KiB
C++
#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
|