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,170 @@
// Test TICH HOP duong odometry: sim -> encoder -> wheel_odom -> EKF.
//
// Khac cac test don vi o test_wheel_odometry.cc va test_ekf_se2.cc: bai nay
// chay CA CHUOI tren mot robot ao co truot banh that, va do sai so so voi
// ground truth. Day la bai L2 cua repo cu ("odometry lech < 2% so voi
// /ground_truth/odom"), viet lai khong ROS.
#include <gtest/gtest.h>
#include <cmath>
#include "vslam/ekf_se2.h"
#include "vslam/geometry.h"
#include "vslam/sim_source.h"
#include "vslam/wheel_odometry.h"
using namespace vslam;
namespace {
struct Result {
Pose2D ground_truth, ekf, wheel_raw;
double distance = 0.0;
uint64_t slip_events = 0;
double ekf_error() const { return Distance2D(ekf, ground_truth); }
double raw_error() const { return Distance2D(wheel_raw, ground_truth); }
double ekf_error_pct() const { return 100.0 * ekf_error() / distance; }
double ekf_yaw_error() const {
return std::fabs(AngleDiff(ekf.theta, ground_truth.theta));
}
double raw_yaw_error() const {
return std::fabs(AngleDiff(wheel_raw.theta, ground_truth.theta));
}
};
// Chay duong vuong (giong UMBmark): thang -- quay 90 -- lap.
Result RunSquare(double slip_sym, double slip_diff, int laps = 2) {
SimConfig sc;
sc.speed = 0.0; // chay nhanh nhat co the
// Bai nay khong dung depth. Mot khung 320x240 la 76800 tia raycast, va bai
// nay chay hang tram giay mo phong -- de bat se cham gap hang chuc lan.
sc.enable_depth_camera = false;
sc.slip_factor = slip_sym;
sc.slip_differential = slip_diff;
sc.slip_accel_threshold = 0.05;
SimSource sim(sc);
EXPECT_TRUE(sim.Open().ok());
WheelOdometryConfig wc;
wc.wheel_radius = sc.wheel_radius;
wc.wheel_separation = sc.wheel_separation;
WheelOdometry wo(wc);
EkfSe2 ekf;
sim.mcu()->SetImuCallback([&](ImuPtr m) {
wo.FeedGyroZ(m->header.stamp, m->angular_velocity.z);
ekf.UpdateImuYawRate(m->header.stamp, m->angular_velocity.z, 2.4e-7);
});
sim.base()->SetEncoderCallback([&](const EncoderReading& e) {
Odometry o;
if (!wo.Update(e.stamp, e.left_rad, e.right_rad, &o)) return;
// Truyen THANG covariance tu wheel_odom vao EKF -- day chinh la cach
// wheel_odom bao "dang truot, dung tin toi".
ekf.UpdateOdomTwist(e.stamp, o.twist.twist.linear.x, o.twist.twist.angular.z,
o.twist.covariance[0], o.twist.covariance[35]);
});
sim.mcu()->Open();
sim.base()->Open();
for (int lap = 0; lap < laps; ++lap) {
for (int side = 0; side < 4; ++side) {
for (int k = 0; k < 20; ++k) { sim.base()->SetVelocity(0.35, 0.0); sim.RunFor(0.4); }
for (int k = 0; k < 20; ++k) { sim.base()->SetVelocity(0.0, 0.7); sim.RunFor(0.11); }
}
}
for (int k = 0; k < 10; ++k) { sim.base()->SetVelocity(0.0, 0.0); sim.RunFor(0.1); }
Result r;
r.ground_truth = sim.ground_truth();
r.ekf = ekf.pose();
r.wheel_raw = wo.pose();
r.distance = sim.stats().distance_travelled;
r.slip_events = wo.stats().slip_events;
return r;
}
} // namespace
TEST(OdometryPipeline, KhongTruotThiSaiSoGanKhong) {
// Tieu chi L2 cua repo cu: lech < 2% quang duong. Khong truot thi phai tot
// hon han nguong do.
const Result r = RunSquare(0.0, 0.0);
EXPECT_GT(r.distance, 5.0) << "bai test phai thuc su di duoc mot doan";
EXPECT_LT(r.ekf_error_pct(), 0.5)
<< "sai so " << r.ekf_error() << " m tren " << r.distance << " m";
EXPECT_LT(r.ekf_yaw_error(), 0.02);
EXPECT_EQ(r.slip_events, 0u);
}
// ===========================================================================
// TRUOT VI SAI -- day la thu ca co che duoc thiet ke de xu ly.
//
// Mot banh truot nhieu hon banh kia sinh mot phep quay GIA ma gyro khong thay.
// wheel_odom bat duoc bang |omega_encoder - gyro_z|, nang covariance, va EKF
// tu dong chuyen sang tin gyro.
// ===========================================================================
TEST(OdometryPipeline, TruotViSai_EKF_PhucHoiDuocHuong) {
const Result r = RunSquare(0.0, 0.5);
EXPECT_GT(r.slip_events, 100u) << "phai phat hien duoc truot vi sai";
// wheel_odom THO bi hong nang: huong lech hang chuc do.
EXPECT_GT(r.raw_yaw_error(), 1.0)
<< "bai test chua tao du truot de co y nghia";
// EKF phai phuc hoi duoc huong nho gyro.
EXPECT_LT(r.ekf_yaw_error(), 0.05)
<< "EKF lech yaw " << r.ekf_yaw_error() * 180.0 / 3.14159 << " do";
// Va vi tri phai tot hon han ban tho.
EXPECT_LT(r.ekf_error(), r.raw_error() * 0.2)
<< "EKF " << r.ekf_error() << " m vs tho " << r.raw_error() << " m";
EXPECT_LT(r.ekf_error_pct(), 2.0) << "van phai dat tieu chi L2 < 2%";
}
// ===========================================================================
// TRUOT DOI XUNG -- GIOI HAN THAT CUA PHUONG PHAP, khong phai bug.
//
// Ca hai banh truot bang nhau thi KHONG sinh chenh lech quay, nen
// slip_metric = |omega_encoder - gyro_z| bang 0 va khong co gi de phat hien.
// Gyro chi nhin thay phep QUAY.
//
// Bai nay ton tai de ghi lai gioi han do bang code, thay vi de ai do sau nay
// phat hien lai bang cach dieu tra mot robot di lech 13% ma khong co canh bao.
// Muon bat loai truot nay can mot nguon thu hai: visual odometry, hoac scan
// matching -- tuc la RTAB-Map o P6.
// ===========================================================================
TEST(OdometryPipeline, TruotDoiXung_GYRO_KHONG_PHAT_HIEN_DUOC) {
const Result r = RunSquare(2.0, 0.0);
EXPECT_EQ(r.slip_events, 0u)
<< "truot doi xung khong sinh chenh lech quay -- gyro khong the thay";
// Va hau qua: sai so lon ma khong co canh bao nao.
EXPECT_GT(r.ekf_error_pct(), 5.0)
<< "neu sai so nho thi bai test chua tao du truot";
// Huong VAN dung -- chi co quang duong bi thoi phong.
EXPECT_LT(r.ekf_yaw_error(), 0.05)
<< "truot doi xung khong lam sai huong, chi lam sai quang duong";
}
TEST(OdometryPipeline, EKF_BamSatWheelOdomKhiKhongCoTruot) {
// Khong truot thi EKF khong duoc "sua" gi ca -- no chi lam muot.
const Result r = RunSquare(0.0, 0.0);
EXPECT_LT(Distance2D(r.ekf, r.wheel_raw), 0.05)
<< "EKF lech khoi wheel_odom qua nhieu khi khong co ly do gi";
}
TEST(OdometryPipeline, ChayLaiChoKetQuaGiongHET) {
// Toan bo duong ong phai tat dinh: khong stage nao doc dong ho he thong.
const Result a = RunSquare(0.0, 0.3);
const Result b = RunSquare(0.0, 0.3);
EXPECT_NEAR(a.ekf.x, b.ekf.x, 1e-12);
EXPECT_NEAR(a.ekf.y, b.ekf.y, 1e-12);
EXPECT_NEAR(a.ekf.theta, b.ekf.theta, 1e-12);
EXPECT_EQ(a.slip_events, b.slip_events);
}