169 lines
5.7 KiB
C++
169 lines
5.7 KiB
C++
// GyroYawFuser: goc quay EKF ghep gyro + encoder theo THOI GIAN. Xem
|
|
// vslam/gyro_yaw_fuser.h -- lop nay ra doi vi cach cu dem thieu ~10% goc quay
|
|
// tren robot that (07/10) khi mau IMU toi theo cum.
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
#include "vslam/gyro_yaw_fuser.h"
|
|
|
|
namespace vslam {
|
|
namespace {
|
|
|
|
// Toc do quay that theo thoi gian: dung, quay trai 0.6 rad/s 5 s, dung, quay
|
|
// phai 0.4 rad/s 5 s, dung. Tong |quay| 5 rad, quay rong 1 rad.
|
|
double TrueRate(double t) {
|
|
if (t >= 1.0 && t < 6.0) return 0.6;
|
|
if (t >= 7.0 && t < 12.0) return -0.4;
|
|
return 0.0;
|
|
}
|
|
double TrueTheta(double t) {
|
|
const double a = std::min(std::max(t - 1.0, 0.0), 5.0) * 0.6;
|
|
const double b = std::min(std::max(t - 7.0, 0.0), 5.0) * -0.4;
|
|
return a + b;
|
|
}
|
|
|
|
UnixTime At(double s) { return UnixTime::fromSec(1000.0 + s); }
|
|
|
|
struct Scenario {
|
|
double imu_period = 0.005; // 200 Hz
|
|
double latency = 0.03; // mau toi tre hon dau thoi gian (da bu tre)
|
|
double batch = 0.04; // mau duoc nha theo CUM moi `batch` giay; 0 = khong cum
|
|
double arrive_jitter = 0.0; // xo lech luc toi (s)
|
|
double enc_step = 0.0225; // buoc encoder (robot 07/10)
|
|
double cb_delay_max = 0.0; // callback encoder tre ngau nhien toi chung nay (s)
|
|
double enc_err = 0.0; // encoder bao them ti le nay (truot / sai ban kinh)
|
|
double gyro_off_from = 1e9; // gyro im lang trong [from, to)
|
|
double gyro_off_to = 1e9;
|
|
int dup_every = 0; // moi n mau gyro lap lai dau thoi gian truoc (driver kep)
|
|
double t_end = 13.0;
|
|
unsigned seed = 1;
|
|
};
|
|
|
|
struct Result {
|
|
double fused = 0.0; // tong goc da ghep
|
|
int used_gyro = 0;
|
|
int steps = 0;
|
|
};
|
|
|
|
Result Simulate(const Scenario& sc) {
|
|
GyroYawFuser f;
|
|
Result r;
|
|
std::mt19937 rng(sc.seed);
|
|
std::uniform_real_distribution<double> u01(0.0, 1.0);
|
|
// Mau gyro: dau thoi gian = luc do; toi tre `latency`, co the theo cum / xo lech.
|
|
struct G { double stamp, arrive, w; };
|
|
std::vector<G> gs;
|
|
int n = 0;
|
|
for (double s = 0.0; s < sc.t_end; s += sc.imu_period, ++n) {
|
|
if (s >= sc.gyro_off_from && s < sc.gyro_off_to) continue;
|
|
double stamp = s;
|
|
if (sc.dup_every > 0 && n % sc.dup_every == 0 && !gs.empty()) stamp = gs.back().stamp;
|
|
double arrive = s + sc.latency + sc.arrive_jitter * u01(rng);
|
|
if (sc.batch > 0.0) arrive = std::ceil(arrive / sc.batch) * sc.batch;
|
|
gs.push_back(G{stamp, arrive, TrueRate(s)});
|
|
}
|
|
// Mau toi theo THU TU toi; driver giu dau thoi gian khong lui.
|
|
std::stable_sort(gs.begin(), gs.end(), [](const G& a, const G& b) { return a.arrive < b.arrive; });
|
|
for (size_t i = 1; i < gs.size(); ++i) gs[i].stamp = std::max(gs[i].stamp, gs[i - 1].stamp);
|
|
size_t gi = 0;
|
|
double prev = 0.0, cb_last = 0.0;
|
|
for (double t = sc.enc_step; t < sc.t_end; t += sc.enc_step) {
|
|
const double cb = std::max(cb_last, t + sc.cb_delay_max * u01(rng));
|
|
cb_last = cb;
|
|
while (gi < gs.size() && gs[gi].arrive <= cb) {
|
|
f.AddGyro(At(gs[gi].stamp), gs[gi].w);
|
|
++gi;
|
|
}
|
|
const double d_enc = (TrueTheta(t) - TrueTheta(prev)) * (1.0 + sc.enc_err);
|
|
double d = 0.0;
|
|
if (f.Step(At(t), d_enc, &d)) ++r.used_gyro;
|
|
r.fused += d;
|
|
prev = t;
|
|
++r.steps;
|
|
}
|
|
return r;
|
|
}
|
|
|
|
// Sai lech cho phep: 0.5% cua tong |quay| (5 rad).
|
|
constexpr double kTol = 0.005 * 5.0;
|
|
|
|
TEST(GyroYawFuser, CumMauKhongDemThieu) {
|
|
const Scenario sc; // cum 40 ms, buoc encoder 22.5 ms -- nhu log robot 07/10
|
|
const Result r = Simulate(sc);
|
|
EXPECT_NEAR(r.fused, TrueTheta(sc.t_end), kTol);
|
|
EXPECT_GT(r.used_gyro, r.steps * 9 / 10);
|
|
}
|
|
|
|
TEST(GyroYawFuser, NhipNgauNhienKhongDemThieu) {
|
|
// Cach cu lech o nhung nhip nay (mo phong 07/10: cum 16 ms + xo lech, callback
|
|
// encoder tre 0-20 ms). Cach moi phai dung o MOI nhip.
|
|
for (unsigned seed = 1; seed <= 5; ++seed) {
|
|
Scenario sc;
|
|
sc.seed = seed;
|
|
sc.batch = 0.016;
|
|
sc.arrive_jitter = 0.004;
|
|
sc.cb_delay_max = 0.02;
|
|
const Result r = Simulate(sc);
|
|
EXPECT_NEAR(r.fused, TrueTheta(sc.t_end), kTol) << "seed " << seed;
|
|
Scenario sc2 = sc;
|
|
sc2.batch = 0.0;
|
|
const Result r2 = Simulate(sc2);
|
|
EXPECT_NEAR(r2.fused, TrueTheta(sc2.t_end), kTol) << "khong cum, seed " << seed;
|
|
}
|
|
}
|
|
|
|
TEST(GyroYawFuser, EncoderSaiThiGyroSua) {
|
|
Scenario sc;
|
|
sc.enc_err = 0.05; // banh bao them 5%
|
|
const Result r = Simulate(sc);
|
|
EXPECT_NEAR(r.fused, TrueTheta(sc.t_end), kTol);
|
|
}
|
|
|
|
TEST(GyroYawFuser, GyroMatThiDungEncoderRoiNoiLai) {
|
|
Scenario sc;
|
|
sc.enc_err = 0.05;
|
|
sc.gyro_off_from = 3.0; // mat gyro giua luc dang quay trai
|
|
sc.gyro_off_to = 8.0;
|
|
const Result r = Simulate(sc);
|
|
// Trong khoang mat gyro chi co encoder (sai 5% cua 3 s quay 0.6 rad/s =
|
|
// 0.09 rad, cong ~0.4 s quay phai luc gyro chua noi lai), ngoai khoang do gyro.
|
|
const double enc_part = (TrueTheta(8.2) - TrueTheta(3.0)) * 0.05;
|
|
EXPECT_NEAR(r.fused, TrueTheta(sc.t_end) + enc_part, 0.02);
|
|
}
|
|
|
|
TEST(GyroYawFuser, ImuChamThiChiEncoder) {
|
|
Scenario sc;
|
|
sc.imu_period = 0.1; // WT61PC mac dinh 10 Hz
|
|
sc.enc_err = 0.02;
|
|
const Result r = Simulate(sc);
|
|
EXPECT_EQ(r.used_gyro, 0);
|
|
EXPECT_NEAR(r.fused, TrueTheta(sc.t_end) * 1.02, 1e-9);
|
|
}
|
|
|
|
TEST(GyroYawFuser, DauThoiGianLapLai) {
|
|
Scenario sc;
|
|
sc.dup_every = 7; // driver kep dau thoi gian bang mau truoc
|
|
const Result r = Simulate(sc);
|
|
EXPECT_NEAR(r.fused, TrueTheta(sc.t_end), kTol);
|
|
}
|
|
|
|
TEST(GyroYawFuser, ThoiGianLuiThiBatDauLai) {
|
|
GyroYawFuser f;
|
|
double d = 0.0;
|
|
for (int i = 0; i < 50; ++i) {
|
|
f.AddGyro(At(0.005 * i), 0.5);
|
|
if (i % 4 == 3) f.Step(At(0.005 * i + 0.01), 0.01, &d);
|
|
}
|
|
// Encoder lui ve truoc (reset mo phong): khong dung gyro, tra dung goc encoder.
|
|
EXPECT_FALSE(f.Step(At(0.0), 0.02, &d));
|
|
EXPECT_DOUBLE_EQ(d, 0.02);
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace vslam
|