reloc,web: dinh vi lai TREN DOC (slope.enabled) -- loc gia thuyet bang do nghieng IMU vs do doc ban do, chan mu doc truc (ShiftScoreRatio), vong DUNG YEN khong xoay (ty le nho nhat >= 3 vong / hai cho cung pose), tu chay bo thang tren doc; camera VPR+PnP tren doc (>= 2 anh trung, khop nghieng, LiDAR khong phan doi, pose = trung binh camera) -- sim 10 cho chot dung 1.4-2.9 cm, 0 chot sai; web 3D: ban do uon theo san (FLH1), scan/quy dao theo z, het nen lung, sua ro GPU; CMake: TensorRT tren CMake < 3.17

This commit is contained in:
trung09122002
2026-10-06 10:17:17 +07:00
parent b1ab04c51e
commit f0c5ddb6b8
19 changed files with 1947 additions and 68 deletions

View File

@@ -261,6 +261,292 @@ TEST(GlobalLocalizer, PhongDoiXungQuay180ChanOCongHuong) {
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);