653 lines
26 KiB
C++
653 lines
26 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, KhopCucBoQuanhUngVien) {
|
|
// Ung vien tu nguon khac (VPR) lech 0.8 m khoi cho that: khop cuc bo phai
|
|
// tim ra cho that. Ung vien o goc kia cua phong: diem phai thap han.
|
|
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};
|
|
const std::vector<Point> pts = ToOdom(Raycast(g, truth), odom);
|
|
|
|
Point near_c;
|
|
near_c.x = truth.x + 0.8;
|
|
near_c.y = truth.y - 0.5;
|
|
const RelocHypothesis h = LocalMatch(f, pts, odom, near_c, 2.5, cfg);
|
|
ASSERT_GT(h.score, 0.0);
|
|
const Pose2D est = Pose2DCompose(h.map_from_odom, odom);
|
|
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);
|
|
|
|
Point far_c;
|
|
far_c.x = 9.5;
|
|
far_c.y = 8.5;
|
|
const RelocHypothesis hf = LocalMatch(f, pts, odom, far_c, 1.0, cfg);
|
|
const Pose2D ef = Pose2DCompose(hf.map_from_odom, odom);
|
|
EXPECT_LT(hf.score, 0.7 * h.score) << "cho sai ma diem " << hf.score << " / " << h.score;
|
|
// Rang buoc tren ROBOT: pose tim duoc phai nam quanh tam da cho (ban kinh
|
|
// 1 m + nua o tho 0.4 m + tinh chinh toi 0.5 m), khong phai o cho that
|
|
// cach do ~6 m.
|
|
EXPECT_LT(std::hypot(ef.x - far_c.x, ef.y - far_c.y), 2.0);
|
|
}
|
|
|
|
TEST(GlobalLocalizer, GioiHanHuongChonDungNuaDoiXung) {
|
|
// Phong doi xung quay 180 do: LiDAR MOT MINH khong biet huong nao. Cho
|
|
// huong (vd tu anh VPR) thi khop cuc bo phai ra DUNG huong do -- va cho
|
|
// huong nguoc thi ra huong nguoc: chinh cua so huong quyet dinh, khong phai
|
|
// may rui.
|
|
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 Pose2D odom{-1.0, 0.5, -0.3}; // huong odom khac 0: kiem phep cong goc
|
|
const std::vector<Point> pts = ToOdom(Raycast(g, truth), odom);
|
|
Point c;
|
|
c.x = truth.x;
|
|
c.y = truth.y;
|
|
const double win = 45.0 * M_PI / 180.0;
|
|
|
|
const RelocHypothesis a = LocalMatch(f, pts, odom, c, 1.0, cfg, truth.theta + 0.2, win);
|
|
ASSERT_GT(a.score, 0.0);
|
|
const Pose2D ea = Pose2DCompose(a.map_from_odom, odom);
|
|
EXPECT_LT(std::fabs(AngleDiff(ea.theta, truth.theta)) * 180.0 / M_PI, 1.0);
|
|
EXPECT_LT(std::hypot(ea.x - truth.x, ea.y - truth.y), 0.1);
|
|
|
|
const RelocHypothesis b = LocalMatch(f, pts, odom, c, 1.0, cfg, truth.theta + M_PI, win);
|
|
ASSERT_GT(b.score, 0.0);
|
|
const Pose2D eb = Pose2DCompose(b.map_from_odom, odom);
|
|
EXPECT_LT(std::fabs(AngleDiff(eb.theta, truth.theta + M_PI)) * 180.0 / M_PI, 1.0);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
// --- Bang chung ngoai LiDAR (`admit`) -------------------------------------
|
|
//
|
|
// Tren doc, do nghieng IMU noi robot dang chuc bao nhieu, ve phia nao; ban do
|
|
// biet doc o dau. Hai test dau la hai ca LiDAR nhap nhang o tren -- bang chung
|
|
// ngoai phai PHAN XU dung. Hai test sau khoa phan an toan: khong bao gio day
|
|
// mot khop LiDAR kem ro len thang, va bang chung "cai gi cung duoc" khong lam
|
|
// lech nguoi thang.
|
|
|
|
TEST(GlobalLocalizer, BangChungNgoaiTachHaiPhongGiongHet) {
|
|
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 Pose2D odom{-1.0, 0.5, -0.3};
|
|
const std::vector<Point> pts = ToOdom(Raycast(g, truth), odom);
|
|
// Vd phong phai la san doc ma IMU do phang: robot KHONG the o do.
|
|
const RelocAdmit admit = [&](const Pose2D& T) { return Pose2DCompose(T, odom).x < 12.0; };
|
|
|
|
const RelocMatch m = GlobalMatch(f, pts, cfg, admit);
|
|
ASSERT_TRUE(m.ok);
|
|
EXPECT_TRUE(m.tilt_used);
|
|
EXPECT_FALSE(m.tilt_conflict);
|
|
EXPECT_GT(m.tilt_rejected, 0);
|
|
const Pose2D est = Pose2DCompose(m.map_from_odom, odom);
|
|
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);
|
|
EXPECT_EQ(Decide(m, cfg.min_cycles, cfg), RelocVerdict::kAccept)
|
|
<< "ty le " << m.ratio() << ", ty le huong " << m.ratio_yaw();
|
|
for (const RelocHypothesis& h : m.hyps) {
|
|
EXPECT_LT(Pose2DCompose(h.map_from_odom, odom).x, 12.0) << "gia thuyet bi loai van tra ve";
|
|
}
|
|
}
|
|
|
|
TEST(GlobalLocalizer, BangChungNgoaiChonHuongPhongDoiXung) {
|
|
// Phong doi xung quay 180 do: LiDAR khong biet huong (PhongDoiXungQuay180...).
|
|
// Bang chung chi nhan nua huong quanh `want` -> ra DUNG `want`, ca hai chieu:
|
|
// chinh bang chung quyet dinh, khong phai may rui.
|
|
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 Pose2D odom{-1.0, 0.5, -0.3};
|
|
const std::vector<Point> pts = ToOdom(Raycast(g, truth), odom);
|
|
for (const double want : {truth.theta, truth.theta + M_PI}) {
|
|
const RelocAdmit admit = [&](const Pose2D& T) {
|
|
return std::fabs(AngleDiff(Pose2DCompose(T, odom).theta, want)) < M_PI / 2;
|
|
};
|
|
const RelocMatch m = GlobalMatch(f, pts, cfg, admit);
|
|
ASSERT_TRUE(m.ok);
|
|
const Pose2D est = Pose2DCompose(m.map_from_odom, odom);
|
|
EXPECT_LT(std::fabs(AngleDiff(est.theta, want)) * 180.0 / M_PI, 1.0) << "want " << want;
|
|
EXPECT_LT(std::hypot(est.x - truth.x, est.y - truth.y), 0.1);
|
|
EXPECT_GE(m.ratio_yaw(), cfg.min_ratio_yaw) << "doi thu huong (lat 180) chua bi loai";
|
|
EXPECT_NE(Decide(m, cfg.min_cycles, cfg), RelocVerdict::kAmbiguousYaw);
|
|
}
|
|
}
|
|
|
|
TEST(GlobalLocalizer, BangChungMauThuanLiDARChacThiGiuLiDAR) {
|
|
// Phong KHONG doi xung: LiDAR chac chan. Bang chung noi "khong the o do"
|
|
// (vd IMU hong) -- KHONG duoc day mot cho kem han len thang. Ket qua phai y
|
|
// het khi khong co bang chung, va danh dau xung dot.
|
|
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};
|
|
const std::vector<Point> pts = ToOdom(Raycast(g, truth), odom);
|
|
const RelocMatch base = GlobalMatch(f, pts, cfg);
|
|
const RelocAdmit admit = [&](const Pose2D& T) {
|
|
const Pose2D p = Pose2DCompose(T, odom);
|
|
return std::hypot(p.x - truth.x, p.y - truth.y) > 1.5;
|
|
};
|
|
const RelocMatch m = GlobalMatch(f, pts, cfg, admit);
|
|
ASSERT_TRUE(m.ok);
|
|
EXPECT_TRUE(m.tilt_conflict);
|
|
EXPECT_FALSE(m.tilt_used);
|
|
EXPECT_NEAR(m.map_from_odom.x, base.map_from_odom.x, 1e-9);
|
|
EXPECT_NEAR(m.map_from_odom.y, base.map_from_odom.y, 1e-9);
|
|
EXPECT_NEAR(m.map_from_odom.theta, base.map_from_odom.theta, 1e-9);
|
|
EXPECT_DOUBLE_EQ(m.s1, base.s1);
|
|
EXPECT_DOUBLE_EQ(m.s2, base.s2);
|
|
EXPECT_DOUBLE_EQ(m.s2_yaw, base.s2_yaw);
|
|
}
|
|
|
|
TEST(GlobalLocalizer, BangChungChapNhanHetKhongLechNguoiThang) {
|
|
// San phang + IMU phang: moi gia thuyet hop le. Nguoi thang va cong phai
|
|
// nhu cu; ung vien them chi co the lam doi thu MANH hon (than trong hon).
|
|
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};
|
|
const std::vector<Point> pts = ToOdom(Raycast(g, truth), odom);
|
|
const RelocMatch base = GlobalMatch(f, pts, cfg);
|
|
const RelocMatch m = GlobalMatch(f, pts, cfg, [](const Pose2D&) { return true; });
|
|
ASSERT_TRUE(m.ok);
|
|
EXPECT_FALSE(m.tilt_used);
|
|
EXPECT_FALSE(m.tilt_conflict);
|
|
const Pose2D a = Pose2DCompose(base.map_from_odom, odom);
|
|
const Pose2D b = Pose2DCompose(m.map_from_odom, odom);
|
|
EXPECT_LT(std::hypot(a.x - b.x, a.y - b.y), 0.01);
|
|
EXPECT_LT(std::fabs(AngleDiff(a.theta, b.theta)), 0.002);
|
|
EXPECT_LE(m.ratio(), base.ratio() + 1e-9);
|
|
EXPECT_EQ(Decide(m, cfg.min_cycles, cfg), Decide(base, cfg.min_cycles, cfg));
|
|
}
|
|
|
|
TEST(GlobalLocalizer, SuyBienDocTrucHanhLangVaPhong) {
|
|
// Hanh lang dai 20 m chi co hai tuong song song: dich doc hanh lang khong
|
|
// lam xau khop (~100%). Phong khong doi xung: dich 1 m thi tut ro. Day la
|
|
// kiem chan chot nho do nghieng (sim: doc 30 do, chot lech 0.77 m doc lan).
|
|
OccupancyGrid g = MakeGrid(24.0, 6.0);
|
|
Fill(&g, 0.0, 1.5, 24.0, 4.5, 0);
|
|
Fill(&g, 0.0, 1.5, 24.0, 1.6, 100);
|
|
Fill(&g, 0.0, 4.4, 24.0, 4.5, 100);
|
|
const LikelihoodField fc = LikelihoodField::FromGrid(g, 0.15);
|
|
const Pose2D in_corridor{12.0, 3.0, 0.2};
|
|
const Pose2D odom{-1.0, 0.5, -0.3};
|
|
// T_map_odom dung = truth o (-) odom.
|
|
auto T_of = [&](const Pose2D& truth) { return Pose2DCompose(truth, Pose2DInverse(odom)); };
|
|
const double rc =
|
|
ShiftScoreRatio(fc, ToOdom(Raycast(g, in_corridor, 720, 6.0), odom), T_of(in_corridor), 1.0);
|
|
EXPECT_GT(rc, 0.95) << "hanh lang ma dich doc truc lai tut diem";
|
|
|
|
OccupancyGrid r = MakeGrid(12.0, 10.0);
|
|
AsymRoom(&r, 0.0, 0.0);
|
|
const LikelihoodField fr = LikelihoodField::FromGrid(r, 0.15);
|
|
const double rr =
|
|
ShiftScoreRatio(fr, ToOdom(Raycast(r, kAsymTruth), odom), T_of(kAsymTruth), 1.0);
|
|
EXPECT_GT(rr, 0.0);
|
|
EXPECT_LT(rr, 0.9) << "phong co dac diem ma dich 1 m van giu diem";
|
|
}
|
|
|
|
// --- Vong DUNG YEN (DecideStatic) ---
|
|
//
|
|
// Dung yen thi cac vong nhin gan y het: thu mai roi chot o vong VOT qua cong
|
|
// la dao nhieu. Sim 2026-10-05 da lot hai lan: chot vong le (1.63 sau 21 vong
|
|
// ~1.44), roi trung vi 3 vong (1.63 / 1.40 / 1.64). Luat: NHO NHAT qua moi vong
|
|
// cung cho.
|
|
namespace {
|
|
StaticRound Round(double x, double ratio, double yaw = 3.5, double shift = 0.85) {
|
|
StaticRound r;
|
|
r.robot = Pose2D{x, 23.0, 1.67};
|
|
r.map_from_odom = r.robot; // odom trung map trong test
|
|
r.ratio = ratio;
|
|
r.ratio_yaw = yaw;
|
|
r.shift = shift;
|
|
return r;
|
|
}
|
|
} // namespace
|
|
|
|
TEST(GlobalLocalizer, DungYenKhongDaoNhieu) {
|
|
const GlobalLocalizerConfig cfg;
|
|
std::vector<StaticRound> h;
|
|
bool ax = false;
|
|
std::string why;
|
|
// Doi thu that (~1.44) chi bi bo sot o vai vong -> KHONG bao gio chot.
|
|
for (const double r : {1.63, 1.40, 1.64, 1.63, 1.65, 1.62, 1.64}) {
|
|
EXPECT_NE(DecideStatic(&h, Round(1.2, r), {}, cfg, true, &ax, &why), RelocVerdict::kAccept)
|
|
<< "vong " << h.size() << ": " << why;
|
|
}
|
|
}
|
|
|
|
TEST(GlobalLocalizer, DungYenDuVongDeuCaoThiChot) {
|
|
const GlobalLocalizerConfig cfg;
|
|
std::vector<StaticRound> h;
|
|
bool ax = false;
|
|
std::string why;
|
|
EXPECT_EQ(DecideStatic(&h, Round(1.2, 1.64), {}, cfg, true, &ax, &why), RelocVerdict::kNeedCycles);
|
|
EXPECT_EQ(DecideStatic(&h, Round(1.21, 1.95), {}, cfg, true, &ax, &why), RelocVerdict::kNeedCycles);
|
|
EXPECT_EQ(DecideStatic(&h, Round(1.19, 1.96), {}, cfg, true, &ax, &why), RelocVerdict::kAccept) << why;
|
|
EXPECT_FALSE(ax);
|
|
}
|
|
|
|
TEST(GlobalLocalizer, DungYenDoiNguoiThangThiDemLai) {
|
|
const GlobalLocalizerConfig cfg;
|
|
std::vector<StaticRound> h;
|
|
bool ax = false;
|
|
std::string why;
|
|
DecideStatic(&h, Round(1.2, 2.0), {}, cfg, true, &ax, &why);
|
|
DecideStatic(&h, Round(1.2, 2.0), {}, cfg, true, &ax, &why);
|
|
// Vong 3 ra cho khac (cach 1 m) -> dem lai tu dau, chua du vong.
|
|
EXPECT_EQ(DecideStatic(&h, Round(2.2, 2.0), {}, cfg, true, &ax, &why), RelocVerdict::kNeedCycles);
|
|
EXPECT_EQ(h.size(), 1u);
|
|
}
|
|
|
|
TEST(GlobalLocalizer, DungYenChanMuDocTrucVaNhapNhang) {
|
|
const GlobalLocalizerConfig cfg;
|
|
bool ax = false;
|
|
std::string why;
|
|
{
|
|
// Mot vong mu doc truc (dich 1 m giu 100%) la du chan, ke ca ty le cao.
|
|
std::vector<StaticRound> h;
|
|
DecideStatic(&h, Round(1.2, 2.0), {}, cfg, true, &ax, &why);
|
|
DecideStatic(&h, Round(1.2, 2.0, 3.5, 1.0), {}, cfg, true, &ax, &why);
|
|
EXPECT_EQ(DecideStatic(&h, Round(1.2, 2.0), {}, cfg, true, &ax, &why), RelocVerdict::kAmbiguous);
|
|
EXPECT_TRUE(ax);
|
|
// Khong ap kiem doc truc (vd khong co bang chung nghieng) -> qua.
|
|
std::vector<StaticRound> h2 = h;
|
|
h2.pop_back();
|
|
EXPECT_EQ(DecideStatic(&h2, Round(1.2, 2.0), {}, cfg, false, &ax, &why), RelocVerdict::kAccept);
|
|
}
|
|
{
|
|
std::vector<StaticRound> h;
|
|
for (int i = 0; i < 3; ++i) DecideStatic(&h, Round(1.2, 2.0, 1.1), {}, cfg, true, &ax, &why);
|
|
EXPECT_EQ(DecideStatic(&h, Round(1.2, 2.0, 1.1), {}, cfg, true, &ax, &why),
|
|
RelocVerdict::kAmbiguousYaw);
|
|
}
|
|
{
|
|
std::vector<StaticRound> h;
|
|
for (int i = 0; i < 3; ++i) DecideStatic(&h, Round(1.2, 1.2), {}, cfg, true, &ax, &why);
|
|
EXPECT_EQ(DecideStatic(&h, Round(1.2, 1.2), {}, cfg, true, &ax, &why), RelocVerdict::kAmbiguous);
|
|
EXPECT_FALSE(ax);
|
|
}
|
|
}
|
|
|
|
// Hai cho (truoc / sau khi bo tren doc). Robot di 0.5 m nen pose robot doi,
|
|
// nhung T_map_odom cua CUNG gia thuyet giu nguyen.
|
|
namespace {
|
|
StaticRound Placed(double y, double ratio, const Pose2D& T) {
|
|
StaticRound r;
|
|
r.robot = Pose2D{1.2, y, 1.67};
|
|
r.map_from_odom = T;
|
|
r.ratio = ratio;
|
|
r.ratio_yaw = 3.5;
|
|
r.shift = 0.85;
|
|
return r;
|
|
}
|
|
// Chay cac vong o mot cho; tra verdict vong cuoi.
|
|
RelocVerdict AtPlace(std::vector<StaticRound>* h, const std::vector<StaticPlace>& prev, double y,
|
|
const Pose2D& T, std::initializer_list<double> ratios, std::string* why) {
|
|
const GlobalLocalizerConfig cfg;
|
|
bool ax = false;
|
|
RelocVerdict v = RelocVerdict::kFailed;
|
|
for (const double r : ratios) v = DecideStatic(h, Placed(y, r, T), prev, cfg, true, &ax, why);
|
|
return v;
|
|
}
|
|
} // namespace
|
|
|
|
TEST(GlobalLocalizer, HaiChoCungPoseMoiChoDeuQuaThiChot) {
|
|
// Sim: gan dinh doc 5 do, cho 1 ty le 1.57-1.60 (duoi chuan mot cho 1.60),
|
|
// bo 0.5 m, cho 2 ~1.49 -> hai cho cung pose, moi cho deu >= 1.30 -> chot.
|
|
const Pose2D T{0.1, -0.2, 0.05};
|
|
std::vector<StaticRound> h;
|
|
std::vector<StaticPlace> places;
|
|
std::string why;
|
|
EXPECT_NE(AtPlace(&h, places, 23.0, T, {1.57, 1.60, 1.58}, &why), RelocVerdict::kAccept) << why;
|
|
places.push_back(SummarizePlace(h));
|
|
h.clear();
|
|
EXPECT_EQ(AtPlace(&h, places, 23.5, T, {1.49}, &why), RelocVerdict::kNeedCycles) << "1 vong chua du";
|
|
EXPECT_EQ(AtPlace(&h, places, 23.5, T, {1.47, 1.50}, &why), RelocVerdict::kAccept) << why;
|
|
}
|
|
|
|
TEST(GlobalLocalizer, HaiChoMaMotChoTungTutThiKhongChot) {
|
|
// Dung kieu chot SAI o chan doc 10 / 15 do: nguoi thang sai on dinh, ty le
|
|
// 1.0-1.2 roi mot vong nhay 1.32. Moi cho phai qua o MOI vong cua no.
|
|
const Pose2D T{0.1, -0.2, 0.05};
|
|
std::vector<StaticRound> h;
|
|
std::vector<StaticPlace> places;
|
|
std::string why;
|
|
AtPlace(&h, places, 9.0, T, {1.05, 1.19, 1.32}, &why);
|
|
places.push_back(SummarizePlace(h));
|
|
h.clear();
|
|
EXPECT_NE(AtPlace(&h, places, 8.5, T, {1.32, 1.34, 1.31}, &why), RelocVerdict::kAccept) << why;
|
|
}
|
|
|
|
TEST(GlobalLocalizer, HaiChoKhacGiaThuyetThiKhongChot) {
|
|
// Hai cho deu qua 1.30 nhung chi ve HAI pose khac nhau (T lech 0.5 m) -> khong chot.
|
|
std::vector<StaticRound> h;
|
|
std::vector<StaticPlace> places;
|
|
std::string why;
|
|
AtPlace(&h, places, 23.0, Pose2D{0.1, -0.2, 0.05}, {1.5, 1.5, 1.5}, &why);
|
|
places.push_back(SummarizePlace(h));
|
|
h.clear();
|
|
EXPECT_NE(AtPlace(&h, places, 23.5, Pose2D{0.1, 0.3, 0.05}, {1.5, 1.5, 1.5}, &why),
|
|
RelocVerdict::kAccept) << why;
|
|
}
|
|
|
|
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);
|
|
}
|