Files
vslam_cpp/test/test_global_localizer.cc
trung09122002 ca3b7854ea update
2026-09-29 10:00:22 +07:00

304 lines
11 KiB
C++

// Test cho dinh vi toan cuc (vslam/global_localizer.h), tren ban do TONG HOP.
//
// Ba thu phai khoa, vi ban ROS da tra gia cho tung cai:
// 1. Tim dung pose khi ban do KHONG doi xung.
// 2. Hai cho giong het nhau -> NHAP NHANG, khong duoc chon bua.
// 3. Phong doi xung quay 180 do -> chan o cong HUONG (vi tri dung, huong sai
// van cho ty le vi tri cao).
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include "vslam/geometry.h"
#include "vslam/global_localizer.h"
using namespace vslam;
namespace {
constexpr double kRes = 0.05;
// O (r, c) co TAM tai goc + (c, r) * res -- cung quy uoc lam tron voi module.
OccupancyGrid MakeGrid(double w_m, double h_m) {
OccupancyGrid g;
g.resolution = static_cast<float>(kRes);
g.width = static_cast<uint32_t>(std::lround(w_m / kRes));
g.height = static_cast<uint32_t>(std::lround(h_m / kRes));
g.origin.position.x = 0.0;
g.origin.position.y = 0.0;
g.data.assign(static_cast<size_t>(g.width) * g.height, -1);
return g;
}
void Fill(OccupancyGrid* g, double x0, double y0, double x1, double y1, int8_t v) {
for (uint32_t r = 0; r < g->height; ++r) {
for (uint32_t c = 0; c < g->width; ++c) {
const double x = c * kRes, y = r * kRes;
if (x >= x0 - 1e-9 && x <= x1 + 1e-9 && y >= y0 - 1e-9 && y <= y1 + 1e-9) {
g->data[static_cast<size_t>(r) * g->width + c] = v;
}
}
}
}
// Phong chu nhat: ben trong trong, vien tuong day 0.1 m.
void Room(OccupancyGrid* g, double x0, double y0, double x1, double y1) {
Fill(g, x0, y0, x1, y1, 0);
Fill(g, x0, y0, x1, y0 + 0.1, 100);
Fill(g, x0, y1 - 0.1, x1, y1, 100);
Fill(g, x0, y0, x0 + 0.1, y1, 100);
Fill(g, x1 - 0.1, y0, x1, y1, 100);
}
// Tuong thang tu (x0, y0) toi (x1, y1), day ~0.1 m -- ke ca duong CHEO.
void Line(OccupancyGrid* g, double x0, double y0, double x1, double y1) {
const double vx = x1 - x0, vy = y1 - y0, len2 = vx * vx + vy * vy;
for (uint32_t r = 0; r < g->height; ++r) {
for (uint32_t c = 0; c < g->width; ++c) {
const double x = c * kRes, y = r * kRes;
const double t = std::max(0.0, std::min(1.0, ((x - x0) * vx + (y - y0) * vy) / len2));
if (std::hypot(x - (x0 + t * vx), y - (y0 + t * vy)) <= 0.06) {
g->data[static_cast<size_t>(r) * g->width + c] = 100;
}
}
}
}
// Phong KHONG doi xung, goc (dx, dy).
//
// ⚠️ Ban dau vach ngan nam o GIUA (x = 6): hai nua trai/phai la hai hop gan
// giong het nhau (5.4 m vs 5.3 m), tinh tien 5.4 m la khop gan het -> ty le
// chi 1.22 va cong tu choi DUNG. Gio: vach lech han sang trai, mot tuong CHEO
// pha moi doi xung vuong goc, them khoi va doan tuong o hai goc khac nhau.
void AsymRoom(OccupancyGrid* g, double dx, double dy) {
Room(g, dx + 0.5, dy + 0.5, dx + 11.5, dy + 9.5);
Fill(g, dx + 4.0, dy + 0.5, dx + 4.1, dy + 5.0, 100); // vach ngan lech trai
Line(g, dx + 7.0, dy + 6.0, dx + 10.0, dy + 8.5); // tuong cheo
Fill(g, dx + 8.5, dy + 1.5, dx + 9.5, dy + 2.5, 100); // khoi goc phai duoi
Fill(g, dx + 1.5, dy + 7.0, dx + 3.5, dy + 7.1, 100); // doan tuong goc trai tren
}
// Pose that dung trong AsymRoom: thay vach ngan, tuong cheo va khoi.
const Pose2D kAsymTruth{6.0, 3.0, 0.6};
// Scan 360 do tu `pose` (frame map), tra diem trong frame BASE.
std::vector<Point> Raycast(const OccupancyGrid& g, const Pose2D& pose, int n_rays = 720,
double max_range = 12.0) {
std::vector<Point> out;
for (int i = 0; i < n_rays; ++i) {
const double a_rel = -M_PI + 2.0 * M_PI * i / n_rays;
const double a = pose.theta + a_rel;
for (double r = 0.1; r < max_range; r += kRes / 2.0) {
const int c = static_cast<int>(std::lround((pose.x + r * std::cos(a)) / kRes));
const int rr = static_cast<int>(std::lround((pose.y + r * std::sin(a)) / kRes));
if (c < 0 || rr < 0 || c >= static_cast<int>(g.width) || rr >= static_cast<int>(g.height)) break;
if (g.data[static_cast<size_t>(rr) * g.width + c] >= 50) {
Point p;
p.x = r * std::cos(a_rel);
p.y = r * std::sin(a_rel);
out.push_back(p);
break;
}
}
}
return out;
}
// Diem frame base -> frame odom cua mot phien bat ky (odom lech han map).
std::vector<Point> ToOdom(const std::vector<Point>& base, const Pose2D& odom) {
std::vector<Point> out;
const double c = std::cos(odom.theta), s = std::sin(odom.theta);
for (const Point& p : base) {
Point q;
q.x = odom.x + c * p.x - s * p.y;
q.y = odom.y + s * p.x + c * p.y;
out.push_back(q);
}
return out;
}
std::vector<Point> ToMap(const std::vector<Point>& base, const Pose2D& pose) {
return ToOdom(base, pose);
}
} // namespace
TEST(GlobalLocalizer, LikelihoodTheoKhoangCachToiTuong) {
OccupancyGrid g = MakeGrid(2.0, 2.0);
Fill(&g, 0.0, 0.0, 2.0, 2.0, 0);
g.data[static_cast<size_t>(20) * g.width + 20] = 100; // mot o tuong o (1.0, 1.0)
const LikelihoodField f = LikelihoodField::FromGrid(g, 0.15);
EXPECT_NEAR(f.At(20, 20), 1.0, 1e-6);
// 3 o = 0.15 m = 1 sigma -> exp(-0.5)
EXPECT_NEAR(f.At(20, 23), std::exp(-0.5), 1e-4);
// cheo 3-4-5: 5 o = 0.25 m
EXPECT_NEAR(f.At(23, 24), std::exp(-(0.25 * 0.25) / (2 * 0.15 * 0.15)), 1e-4);
EXPECT_TRUE(f.Free(0, 0));
EXPECT_FALSE(f.Free(20, 20));
}
TEST(GlobalLocalizer, PhongKhongDoiXungTimDungPose) {
OccupancyGrid g = MakeGrid(12.0, 10.0);
AsymRoom(&g, 0.0, 0.0);
const LikelihoodField f = LikelihoodField::FromGrid(g, 0.15);
const GlobalLocalizerConfig cfg;
const Pose2D truth = kAsymTruth;
const Pose2D odom{-1.0, 0.5, -0.3}; // frame odom lech han frame map
const std::vector<Point> pts = ToOdom(Raycast(g, truth), odom);
const RelocMatch m = GlobalMatch(f, pts, cfg);
ASSERT_TRUE(m.ok);
const Pose2D est = Pose2DCompose(m.map_from_odom, odom);
// RTAB-Map "bat ngay" khi lech < 0.1 m / 3 do (docs/SLAM.md) -- doi chat hon.
EXPECT_LT(std::hypot(est.x - truth.x, est.y - truth.y), 0.1);
EXPECT_LT(std::fabs(AngleDiff(est.theta, truth.theta)) * 180.0 / M_PI, 1.0);
// ⚠️ Doi thu XA phai tim duoc (ban ROS: s2 = 0 -> ty le vo cuc -> chot bua).
EXPECT_GT(m.s2, 0.0);
EXPECT_LT(m.ratio(), 100.0);
// Du so vong thi phai qua moi cong (ty le vi tri >= 1.30, huong >= 1.30).
EXPECT_EQ(Decide(m, cfg.min_cycles, cfg), RelocVerdict::kAccept)
<< "ty le " << m.ratio() << ", ty le huong " << m.ratio_yaw();
}
TEST(GlobalLocalizer, HaiPhongGiongHetLaNhapNhang) {
// Hai phong KHONG doi xung nhung GIONG HET nhau, cach 12 m, khong thong
// nhau. Scan chi thay phong minh dang dung -> khong co cach nao biet la
// phong nao. Tra loi sai tu tin te hon nhieu so voi im lang.
OccupancyGrid g = MakeGrid(24.0, 10.0);
AsymRoom(&g, 0.0, 0.0);
AsymRoom(&g, 12.0, 0.0);
const LikelihoodField f = LikelihoodField::FromGrid(g, 0.15);
const GlobalLocalizerConfig cfg;
const Pose2D truth = kAsymTruth;
const RelocMatch m = GlobalMatch(f, ToOdom(Raycast(g, truth), Pose2D{}), cfg);
ASSERT_TRUE(m.ok);
EXPECT_LT(m.ratio(), cfg.min_ratio) << "hai phong giong het ma ty le " << m.ratio();
EXPECT_NE(Decide(m, 5, cfg), RelocVerdict::kAccept)
<< "streak cao cung KHONG duoc chot khi hai cho giong het";
}
TEST(GlobalLocalizer, PhongDoiXungQuay180ChanOCongHuong) {
// Robot o TAM phong chu nhat: huong theta va theta + 180 do cho CUNG mot
// scan. Vi tri dung, huong khong tin duoc -> phai chan truoc cong vi tri.
OccupancyGrid g = MakeGrid(9.0, 6.0);
Room(&g, 0.5, 0.5, 8.5, 5.5);
const LikelihoodField f = LikelihoodField::FromGrid(g, 0.15);
const GlobalLocalizerConfig cfg;
const Pose2D truth{4.5, 3.0, 0.3};
const RelocMatch m = GlobalMatch(f, ToOdom(Raycast(g, truth), Pose2D{}), cfg);
ASSERT_TRUE(m.ok);
EXPECT_LT(m.ratio_yaw(), cfg.min_ratio_yaw);
EXPECT_EQ(Decide(m, 5, cfg), RelocVerdict::kAmbiguousYaw);
}
TEST(GlobalLocalizer, DiemCanhLacTutKhiPoseSai) {
OccupancyGrid g = MakeGrid(12.0, 10.0);
AsymRoom(&g, 0.0, 0.0);
const LikelihoodField f = LikelihoodField::FromGrid(g, 0.15);
const Pose2D truth = kAsymTruth;
const std::vector<Point> base = Raycast(g, truth);
const double good = f.MeanScore(ToMap(base, truth));
// Lech CHEO + xoay: chi lech theo x thi diem tren tuong ngang van trung tuong.
const double bad =
f.MeanScore(ToMap(base, Pose2D{truth.x + 0.7, truth.y + 0.7, truth.theta + 0.4}));
ASSERT_GT(good, 0.0);
ASSERT_GE(bad, 0.0);
EXPECT_GT(good, 0.8);
EXPECT_LT(bad, GlobalLocalizerConfig().lost_score_ratio * good);
// Qua it diem roi vao luoi -> -1 ("chua do duoc"), khong phai 0 ("rat te").
EXPECT_EQ(f.MeanScore({}), -1.0);
}
TEST(GlobalLocalizer, StreakChiTangKhiDaDiChuyen) {
GlobalLocalizerConfig cfg;
HypothesisTracker tr(cfg);
RelocMatch m;
m.ok = true;
m.map_from_odom = Pose2D{2.0, 1.0, 0.4};
m.hyps.push_back({m.map_from_odom, 10.0});
m.hyps.push_back({Pose2D{9.0, 1.0, 0.4}, 8.0}); // doi thu o cho khac
tr.Add(m, Pose2D{0.0, 0.0, 0.0}, 0.0);
EXPECT_EQ(tr.Streak(m.map_from_odom), 1);
// Dung yen: ban sao doi xung cung lap lai y het -> KHONG cong.
tr.Add(m, Pose2D{0.05, 0.0, 0.0}, 10.0);
tr.Add(m, Pose2D{0.10, 0.0, 0.0}, 20.0);
EXPECT_EQ(tr.Streak(m.map_from_odom), 1);
// Di cham nhung CONG DON du 0.2 m -> cong (ban ROS dat lai moc moi vong nen
// di cham khong bao gio cong duoc).
tr.Add(m, Pose2D{0.21, 0.0, 0.0}, 30.0);
EXPECT_EQ(tr.Streak(m.map_from_odom), 2);
// Doi thu khong dung dau -> streak 0.
EXPECT_EQ(tr.Streak(Pose2D{9.0, 1.0, 0.4}), 0);
}
TEST(GlobalLocalizer, NguoiThangDoiThiStreakVe0) {
GlobalLocalizerConfig cfg;
HypothesisTracker tr(cfg);
RelocMatch a;
a.ok = true;
a.hyps = {{Pose2D{2.0, 1.0, 0.0}, 10.0}, {Pose2D{9.0, 1.0, 0.0}, 8.0}};
a.map_from_odom = a.hyps[0].map_from_odom;
RelocMatch b = a;
std::swap(b.hyps[0], b.hyps[1]);
b.map_from_odom = b.hyps[0].map_from_odom;
tr.Add(a, Pose2D{0.0, 0.0, 0.0}, 0.0);
tr.Add(a, Pose2D{0.5, 0.0, 0.0}, 10.0);
EXPECT_EQ(tr.Streak(a.map_from_odom), 2);
tr.Add(b, Pose2D{1.0, 0.0, 0.0}, 20.0);
EXPECT_EQ(tr.Streak(a.map_from_odom), 0);
EXPECT_EQ(tr.Streak(b.map_from_odom), 1);
}
TEST(GlobalLocalizer, ThuTuCong) {
const GlobalLocalizerConfig cfg;
RelocMatch m;
EXPECT_EQ(Decide(m, 9, cfg), RelocVerdict::kFailed);
m.ok = true;
m.s1 = 10.0;
// Huong truoc: ty le vi tri 5 van bi chan neu huong nhap nhang.
m.s2 = 2.0;
m.s2_yaw = 9.0;
EXPECT_EQ(Decide(m, 9, cfg), RelocVerdict::kAmbiguousYaw);
m.s2_yaw = 1.0;
m.s2 = 10.0 / 1.2;
EXPECT_EQ(Decide(m, 9, cfg), RelocVerdict::kAmbiguous);
m.s2 = 10.0 / 1.45; // dai bien 1.30-1.60
EXPECT_EQ(Decide(m, 1, cfg), RelocVerdict::kNeedCycles);
EXPECT_EQ(Decide(m, 2, cfg), RelocVerdict::kAccept);
m.s2 = 10.0 / 1.7; // tren nguong nhanh
EXPECT_EQ(Decide(m, 0, cfg), RelocVerdict::kAccept);
}
TEST(GlobalLocalizer, GopDiemVaRutCon) {
std::vector<Point> pts;
for (int i = 0; i < 100; ++i) {
Point p;
p.x = 1.0 + 0.0001 * i; // cung mot o 0.05 m
p.y = 2.0;
pts.push_back(p);
}
EXPECT_EQ(DownsamplePoints(pts, 0.05, 1500).size(), 1u);
pts.clear();
for (int i = 0; i < 3000; ++i) {
Point p;
p.x = 0.1 * i;
pts.push_back(p);
}
EXPECT_LE(DownsamplePoints(pts, 0.05, 1500).size(), 1500u);
}