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

@@ -63,7 +63,22 @@ if(VSLAM_WITH_TENSORRT)
if(NOT DEFINED CUDAToolkit_ROOT AND EXISTS /usr/local/cuda)
set(CUDAToolkit_ROOT /usr/local/cuda)
endif()
find_package(CUDAToolkit REQUIRED)
# FindCUDAToolkit chi co tu CMake 3.17 (JetPack 6: 3.22). May dev Ubuntu 20.04 co CMake
# 3.16 nhung van co TensorRT + CUDA (chay VPR trong mo phong) -> tim tay cudart.
if(CMAKE_VERSION VERSION_LESS 3.17)
find_path(VSLAM_CUDA_INCLUDE_DIR cuda_runtime_api.h HINTS ${CUDAToolkit_ROOT}/include)
find_library(VSLAM_CUDART cudart HINTS ${CUDAToolkit_ROOT}/lib64)
if(NOT VSLAM_CUDA_INCLUDE_DIR OR NOT VSLAM_CUDART)
message(FATAL_ERROR "VSLAM_WITH_TENSORRT=ON nhung khong thay CUDA runtime trong "
"${CUDAToolkit_ROOT}")
endif()
set(_vslam_cudart ${VSLAM_CUDART})
set(_vslam_cuda_inc ${VSLAM_CUDA_INCLUDE_DIR})
else()
find_package(CUDAToolkit REQUIRED)
set(_vslam_cudart CUDA::cudart)
set(_vslam_cuda_inc "")
endif()
find_path(TENSORRT_INCLUDE_DIR NvInfer.h PATH_SUFFIXES aarch64-linux-gnu x86_64-linux-gnu)
find_library(TENSORRT_NVINFER nvinfer)
if(NOT TENSORRT_INCLUDE_DIR OR NOT TENSORRT_NVINFER)
@@ -71,7 +86,7 @@ if(VSLAM_WITH_TENSORRT)
"(JetPack: goi libnvinfer-dev)")
endif()
target_sources(vslam_slam PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/vpr_tensorrt.cc)
target_include_directories(vslam_slam SYSTEM PRIVATE ${TENSORRT_INCLUDE_DIR})
target_link_libraries(vslam_slam PRIVATE ${TENSORRT_NVINFER} CUDA::cudart)
target_include_directories(vslam_slam SYSTEM PRIVATE ${TENSORRT_INCLUDE_DIR} ${_vslam_cuda_inc})
target_link_libraries(vslam_slam PRIVATE ${TENSORRT_NVINFER} ${_vslam_cudart})
target_compile_definitions(vslam_slam PRIVATE VSLAM_HAVE_TENSORRT=1)
endif()

View File

@@ -32,6 +32,7 @@
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <string>
#include <vector>
@@ -81,6 +82,37 @@ struct GlobalLocalizerConfig {
// --- Canh "da lac" ---
double lost_score_ratio = 0.55; // diem tut duoi ty le nay so voi luc khop
double lost_hold = 3.0; // s, phai tut lien tuc ngan nay
// --- Do nghieng IMU vs do doc ban do (che do doc, xem GlobalMatch) ---
// Gia thuyet ma mat san o do gay ra do nghieng lech qua nguong nay so voi
// IMU thi bi loai. 3.5 do: tach duoc doc 5 do khoi san phang (5 do) va cac
// lan 5/10/15/20/30 do khoi nhau, van chiu sai IMU + sai do nghieng node
// ~1-2 do.
double tilt_tol_deg = 3.5;
// Chi tin do doc cua node trong ban kinh nay; xa hon = khong biet san o do
// (khong loai).
double tilt_node_radius = 1.5; // m
// Chot NHO do nghieng (tilt_used) con phai qua kiem suy bien doc truc: dich
// pose `tilt_shift_m` theo 8 huong ma diem LiDAR van >= ty le nay so voi
// pose chot -> hanh lang / doc deu, vi tri doc truc khong quan sat duoc ->
// KHONG chot. Xem ShiftScoreRatio. Do sim 2026-10-05 (dich 1 m): giua lan doc
// MU = 100% (5 ca, mot ca da chot lech 0.77 m); gan dinh doc 5 do thay tuong
// sanh tren cach 10 m = 90-92% (8/8 vong dung cho, lech 7 cm); sanh 62-65%.
double tilt_shift_m = 1.0;
double tilt_max_shift_ratio = 0.95;
// --- Vong DUNG YEN tren doc (runtime: khong xoay robot) ---
// Dung yen thi cac vong nhin GAN NHU Y HET: streak (can di >= 0.2 m) khong
// ap dung, va thu mai roi chot o vong vot qua cong la "dao nhieu". Sim
// 2026-10-05, cung mot cho: ty le NHAY hai muc -- ~1.44 khi tim thay doi thu
// that (12 m doc cung lan), ~1.63 khi vong do bo sot no; trung vi 3 vong van
// lot (1.63 / 1.40 / 1.64). Nen quyet bang ty le NHO NHAT qua moi vong lien
// tiep CUNG nguoi thang (doi thu tung hien ra = co that), can >= `static_rounds`
// vong. Nguong mac dinh = min_ratio_fast (chuan mot goc nhin).
int static_rounds = 3;
double static_min_ratio = 1.60;
double static_agree_m = 0.3;
double static_agree_deg = 5.0;
};
// Truong likelihood tren luoi chiem dung. Dung chung cho khop toan cuc VA cho
@@ -130,22 +162,61 @@ struct RelocMatch {
double s1 = 0.0; // diem nguoi thang
double s2 = 0.0; // doi thu VI TRI tot nhat (> reject_radius)
double s2_yaw = 0.0; // doi thu HUONG tot nhat (cung cho)
// T_map_odom cua hai doi thu tren (de log "nhap nhang voi cho nao").
Pose2D s2_from_odom;
Pose2D s2_yaw_from_odom;
size_t points = 0; // so diem da dung sau khi gop
std::vector<RelocHypothesis> hyps; // da tinh chinh, [0] = nguoi thang
// Loc bang bang chung ngoai LiDAR (`admit` cua GlobalMatch):
bool tilt_used = false; // ket qua tren DA loai it nhat mot gia thuyet
int tilt_rejected = 0; // so gia thuyet mau thuan bi loai (ung vien + doi thu)
// LiDAR CHAC CHAN o mot cho ma bang chung noi khong the -> bo loc, giu ket
// qua LiDAR nhu cu (khong te hon truoc), nhung phai biet.
bool tilt_conflict = false;
double ratio() const { return s1 / (s2 > 1e-9 ? s2 : 1e-9); }
double ratio_yaw() const { return s1 / (s2_yaw > 1e-9 ? s2_yaw : 1e-9); }
};
// Bang chung DOC LAP voi LiDAR cho mot gia thuyet T_map_odom: false = mau
// thuan (vd do nghieng IMU khac do doc ban do tai cho do). Xem GlobalMatch.
using RelocAdmit = std::function<bool(const Pose2D& map_from_odom)>;
// Gop diem ve o luoi `res` roi rut con toi `max_points` (giu thu tu).
std::vector<Point> DownsamplePoints(const std::vector<Point>& pts, double res,
size_t max_points);
// Khop TOAN CUC. `pts_odom`: diem da gom trong frame ODOM (co dinh trong luc
// xoay), da cat theo max_range. Tra ok=false neu khong co dinh nao > 0.
//
// `admit` (tuy chon) chi PHAN XU giua cac gia thuyet LiDAR KHONG phan biet
// duoc: gia thuyet bi loai khong duoc thang va khong tinh la doi thu. Nguoi
// thang hop le phai co diem >= s_tot_nhat / min_ratio (trong dai ma LiDAR mot
// minh cung nhap nhang) -- khong bao gio day mot khop LiDAR KEM ro len thang.
// Khong co gia thuyet hop le nao trong dai do -> giu ket qua LiDAR nhu cu,
// danh dau `tilt_conflict`.
//
// ⚠️ Vi sao can (sim 2026-10-05, slope_test.world): robot dat giua doc 5/10/15
// do, 6 vong x 3 doc deu NHAP NHANG (ty le ~1.00). LiDAR nghieng theo doc chi
// thay hai tuong lan; ban sao doi xung (cho phang o dau kia lan, mui nguoc)
// khop y het. IMU biet robot dang chuc 15 do -- ban sao do o san phang.
RelocMatch GlobalMatch(const LikelihoodField& field,
const std::vector<Point>& pts_odom,
const GlobalLocalizerConfig& cfg);
const GlobalLocalizerConfig& cfg,
const RelocAdmit& admit = nullptr);
// Suy bien DOC TRUC: diem trung binh cua dam diem tai pose dich `shift_m` theo
// 8 huong (giu huong xoay), chia cho diem tai chinh pose -- lon nhat trong 8.
// ~1 = co mot huong dich ma khop khong xau di (hanh lang, doc deu): vi tri doc
// huong do KHONG quan sat duoc. -1 neu khong tinh duoc.
//
// ⚠️ Cong ty le chi so voi doi thu XA hon reject_radius (2 m). Sim 2026-10-05:
// robot DUNG YEN giua doc 30 do, bo loc nghieng loai het doi thu xa (lan khac,
// cho phang) -> ty le 2.17 -> chot lech 0.77 m DOC lan, cong khong thay.
double ShiftScoreRatio(const LikelihoodField& field, const std::vector<Point>& pts_odom,
const Pose2D& map_from_odom, double shift_m,
size_t max_points = 1500);
// Khop CUC BO: robot phai nam trong `radius` quanh `center` (frame map). De
// XAC MINH ung vien tu nguon khac (VPR). `odom_now`: pose robot trong frame
@@ -205,6 +276,40 @@ const char* RelocVerdictName(RelocVerdict v);
// Thu tu cong nhu ban ROS: huong truoc, roi vi tri, roi dai bien + streak.
RelocVerdict Decide(const RelocMatch& m, int streak, const GlobalLocalizerConfig& cfg);
// --- Vong DUNG YEN (robot khong xoay, vd tren doc) ---
struct StaticRound {
Pose2D robot; // pose robot trong map theo nguoi thang vong do
Pose2D map_from_odom; // T_map_odom cua nguoi thang (bat bien khi robot di)
double ratio = 0.0;
double ratio_yaw = 0.0;
double shift = -1.0; // ShiftScoreRatio; < 0 = khong tinh
};
// Tom tat cac vong o MOT cho dung (goi khi roi cho do).
struct StaticPlace {
Pose2D map_from_odom;
size_t rounds = 0;
double min_ratio = 0.0;
double min_yaw = 0.0;
double max_shift = -1.0;
};
StaticPlace SummarizePlace(const std::vector<StaticRound>& hist);
// Them `r` vao `hist` (xoa truoc neu nguoi thang KHAC vong truoc qua
// static_agree_m / static_agree_deg) roi quyet. Chot khi MOT trong hai:
// 1. MOT cho: >= static_rounds vong, ty le NHO NHAT >= static_min_ratio;
// 2. HAI cho khac nhau (`prev`, truoc khi bo): MOI cho >= static_rounds vong
// voi ty le NHO NHAT >= min_ratio, CUNG T_map_odom (static_agree_*).
// Ca hai: ty le huong nho nhat >= min_ratio_yaw, shift lon nhat < nguong (neu
// `use_shift`).
//
// ⚠️ Vi sao KHONG dung streak cua HypothesisTracker o day: sim 2026-10-05, bo
// xuong chan doc 10 / 15 do, nguoi thang SAI on dinh (cao hon 2 m doc lan),
// streak len 4-6 qua cac lan bo trong khi ty le 1.0-1.2; mot vong nhay 1.32 /
// 1.34 -> chot sai 2.0 / 1.5 m. Ty le NHO NHAT moi cho chan duoc kieu do.
RelocVerdict DecideStatic(std::vector<StaticRound>* hist, const StaticRound& r,
const std::vector<StaticPlace>& prev, const GlobalLocalizerConfig& cfg,
bool use_shift, bool* along_axis, std::string* why);
} // namespace vslam
#endif // VSLAM_GLOBAL_LOCALIZER_H

View File

@@ -93,6 +93,18 @@ class FloorModel {
// Do cao mat phang cua node n tai (x, y).
double PlaneZ(const FloorNode& n, double x, double y);
// Do doc SAN tai (x, y): gradient (dz/dx, dz/dy, frame map) cua mat phang node
// gan nhat. false neu node gan nhat xa hon `max_dist` (khong biet san o do).
bool FloorGradientAt(const FloorModel& fm, double x, double y, double max_dist, double* gx,
double* gy);
// Do lech giua do nghieng than DO DUOC (roll, pitch -- ROS, tu IMU) va do
// nghieng ma mat san gradient (gx, gy) GAY RA cho robot mui huong `yaw` (frame
// map). Don vi: do doc (tan) -- 0.087 ~ 5 do. Dinh vi lai dung no de loai gia
// thuyet mau thuan voi IMU: quay nguoc 180 do tren doc, doc o cho phang, doc
// sai goc.
double TiltResidual(double roll, double pitch, double yaw, double gx, double gy);
} // namespace vslam
#endif // VSLAM_HEIGHT_MAP_H

View File

@@ -2,6 +2,7 @@
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <limits>
#include <unordered_set>
@@ -321,7 +322,8 @@ Pose2D ToMap(const Refined& rf, const Point& cen, const LikelihoodField& f) {
RelocMatch GlobalMatch(const LikelihoodField& field,
const std::vector<Point>& pts_odom,
const GlobalLocalizerConfig& cfg) {
const GlobalLocalizerConfig& cfg,
const RelocAdmit& admit) {
RelocMatch out;
if (field.empty() || pts_odom.empty()) return out;
@@ -354,6 +356,15 @@ RelocMatch GlobalMatch(const LikelihoodField& field,
[](const AnglePeak& a, const AnglePeak& b) { return a.score > b.score; });
if (peaks.empty() || peaks[0].score <= 0.0) return out;
// T_map_odom cua mot o tho: TAM dam diem dat vao tam khoi (r, c), xoay th.
auto coarse_map = [&](int r, int c, double th) {
Refined q;
q.x = block_center(c);
q.y = block_center(r);
q.th = th;
return ToMap(q, cz.cen, field);
};
// --- Gom TOP-K dinh tho, khu trung trong reject_radius ---
// Xet nhieu dinh chu khong chi cai cao nhat: muc tho dung max-pool nen rat
// rong rai, thu tu o day chua chac dung.
@@ -370,17 +381,98 @@ RelocMatch GlobalMatch(const LikelihoodField& field,
if (static_cast<int>(cand.size()) >= cfg.max_candidates) break;
}
// Co bang chung ngoai: them dinh TOT NHAT HOP LE cua moi goc. Dinh tho cao
// nhat moi goc co the deu la ban sao doi xung bi loai, va khi do cho that
// khong bao gio duoc tinh chinh. Chi goi `admit` khi o vuot dinh hien tai.
std::vector<AnglePeak> acand;
if (admit) {
std::vector<AnglePeak> apeaks;
for (int ia = 0; ia < n_ang; ++ia) {
const double th = ia * step;
const std::vector<float>& sc = scores[ia];
AnglePeak bp{0.0, -1, -1, th};
for (int r = 0; r < Hc; ++r) {
for (int c = 0; c < Wc; ++c) {
const float v = sc[static_cast<size_t>(r) * Wc + c];
if (v > bp.score && admit(coarse_map(r, c, th))) bp = AnglePeak{v, r, c, th};
}
}
if (bp.r >= 0) apeaks.push_back(bp);
}
std::sort(apeaks.begin(), apeaks.end(),
[](const AnglePeak& a, const AnglePeak& b) { return a.score > b.score; });
const double min_sep = cfg.min_angle_sep_deg * kDegToRad;
for (int i = 0; i < std::min<int>(cfg.top_k, static_cast<int>(apeaks.size())); ++i) {
const AnglePeak& p = apeaks[i];
bool dup = false;
auto near = [&](const AnglePeak& q) {
const double dr = p.r - q.r, dc = p.c - q.c;
return dr * dr + dc * dc <= rad_c * rad_c;
};
// Dinh LiDAR goc o CUNG cho nhung lat huong la ung vien KHAC han.
for (const AnglePeak& q : cand) {
if (near(q) && std::fabs(AngleDiff(p.th, q.th)) < min_sep) { dup = true; break; }
}
for (const AnglePeak& q : acand) {
if (!dup && near(q)) { dup = true; break; }
}
if (!dup) acand.push_back(p);
if (static_cast<int>(acand.size()) >= cfg.max_candidates) break;
}
}
// ⚠️ Tinh chinh TUNG ung vien roi moi so diem voi nhau. So o muc tho (co
// max-pool) thi gan nhu moi cho canh tuong deu diem cao -- ban ROS do duoc
// tot/xau chi cach nhau 1.18 lan, cong chan loai ca ca DUNG.
auto refine_all = [&](const std::vector<AnglePeak>& src, std::vector<Refined>* dst) {
for (const AnglePeak& p : src) {
const Refined rf = Refine(field, P, block_center(p.c), block_center(p.r), p.th, rc, cfg);
if (rf.score >= 0.0) dst->push_back(rf);
}
std::sort(dst->begin(), dst->end(),
[](const Refined& a, const Refined& b) { return a.score > b.score; });
};
std::vector<Refined> refined;
for (const AnglePeak& p : cand) {
const Refined rf = Refine(field, P, block_center(p.c), block_center(p.r), p.th, rc, cfg);
if (rf.score >= 0.0) refined.push_back(rf);
}
refine_all(cand, &refined);
if (refined.empty()) return out;
std::sort(refined.begin(), refined.end(),
[](const Refined& a, const Refined& b) { return a.score > b.score; });
// --- Loc bang bang chung ngoai LiDAR (xem global_localizer.h) ---
// Nguoi thang hop le phai nam trong dai LiDAR mot minh cung nhap nhang
// (>= s_tot_nhat / min_ratio). Ngoai dai do = LiDAR CHAC o cho bang chung noi
// khong the -> giu nguyen ket qua LiDAR (y het khong co `admit`: ung vien them
// tu `acand` khong duoc dung), chi danh dau.
bool filt = false;
if (admit) {
std::vector<Refined> keep;
refine_all(acand, &keep); // dinh hop le them -- da hop le o muc tho
keep.erase(std::remove_if(keep.begin(), keep.end(),
[&](const Refined& rf) { return !admit(ToMap(rf, cz.cen, field)); }),
keep.end());
for (const Refined& rf : refined) {
if (admit(ToMap(rf, cz.cen, field))) {
keep.push_back(rf);
} else {
++out.tilt_rejected;
}
}
std::sort(keep.begin(), keep.end(),
[](const Refined& a, const Refined& b) { return a.score > b.score; });
if (!keep.empty() && keep[0].score >= refined[0].score / cfg.min_ratio) {
refined.swap(keep);
filt = true;
} else {
out.tilt_conflict = true;
}
}
// Doi thu cung phai hop le: gia thuyet bang chung noi khong the thi khong
// canh tranh. ⚠️ Ke ca khi KHONG ung vien nao bi loai: ban lat 180 do o CUNG
// cho khong nam trong danh sach tinh chinh (gom theo vi tri) ma chi hien o
// doi thu HUONG -- dung ca lan doc. Xung dot thi moi thu nhu cu.
auto rival_ok = [&](int r, int c, double th) {
if (!filt || admit(coarse_map(r, c, th))) return true;
++out.tilt_rejected;
return false;
};
const Refined& best = refined[0];
// --- Doi thu VI TRI ---
@@ -389,8 +481,10 @@ RelocMatch GlobalMatch(const LikelihoodField& field,
// chot bua (9.3% duong tinh gia, sai 9-13 m). Nen quet lai diem THO o vung xa.
double s2 = 0.0;
for (size_t i = 1; i < refined.size(); ++i) {
if (std::hypot(refined[i].x - best.x, refined[i].y - best.y) > cfg.reject_radius) {
s2 = std::max(s2, refined[i].score);
if (std::hypot(refined[i].x - best.x, refined[i].y - best.y) > cfg.reject_radius &&
refined[i].score > s2) {
s2 = refined[i].score;
out.s2_from_odom = ToMap(refined[i], cz.cen, field);
}
}
const int rw = static_cast<int>(std::lround(best.y / res)) / k;
@@ -408,13 +502,18 @@ RelocMatch GlobalMatch(const LikelihoodField& field,
for (int r = 0; r < Hc; ++r) {
for (int c = 0; c < Wc; ++c) {
const float v = sc[static_cast<size_t>(r) * Wc + c];
if (v > fv && is_far(r, c)) { fv = v; fr = r; fc = c; fth = ia * step; }
if (v > fv && is_far(r, c) && rival_ok(r, c, ia * step)) {
fv = v; fr = r; fc = c; fth = ia * step;
}
}
}
}
if (fr >= 0 && fv > 0.0) {
const Refined alt = Refine(field, P, block_center(fc), block_center(fr), fth, rc, cfg);
s2 = std::max(s2, alt.score);
if (alt.score > s2) {
s2 = alt.score;
out.s2_from_odom = ToMap(alt, cz.cen, field);
}
}
}
@@ -432,15 +531,18 @@ RelocMatch GlobalMatch(const LikelihoodField& field,
for (int r = 0; r < Hc; ++r) {
for (int c = 0; c < Wc; ++c) {
const float v = sc[static_cast<size_t>(r) * Wc + c];
if (v > gv && !is_far(r, c)) { gv = v; gr = r; gc = c; gth = th; }
if (v > gv && !is_far(r, c) && rival_ok(r, c, th)) { gv = v; gr = r; gc = c; gth = th; }
}
}
}
if (gr >= 0 && gv > 0.0) {
s2_yaw = Refine(field, P, block_center(gc), block_center(gr), gth, rc, cfg).score;
const Refined alt = Refine(field, P, block_center(gc), block_center(gr), gth, rc, cfg);
s2_yaw = alt.score;
out.s2_yaw_from_odom = ToMap(alt, cz.cen, field);
}
}
out.tilt_used = filt && out.tilt_rejected > 0;
for (const Refined& rf : refined) out.hyps.push_back({ToMap(rf, cz.cen, field), rf.score});
out.map_from_odom = out.hyps[0].map_from_odom;
out.s1 = best.score;
@@ -450,6 +552,32 @@ RelocMatch GlobalMatch(const LikelihoodField& field,
return out;
}
double ShiftScoreRatio(const LikelihoodField& field, const std::vector<Point>& pts_odom,
const Pose2D& map_from_odom, double shift_m, size_t max_points) {
if (field.empty() || pts_odom.empty()) return -1.0;
const std::vector<Point> pts = DownsamplePoints(pts_odom, field.resolution(), max_points);
auto score = [&](const Pose2D& T) {
const double c = std::cos(T.theta), s = std::sin(T.theta);
std::vector<Point> m(pts.size());
for (size_t i = 0; i < pts.size(); ++i) {
m[i].x = T.x + c * pts[i].x - s * pts[i].y;
m[i].y = T.y + s * pts[i].x + c * pts[i].y;
}
return field.MeanScore(m);
};
const double s0 = score(map_from_odom);
if (s0 <= 0.0) return -1.0;
double best = 0.0;
for (int k = 0; k < 8; ++k) {
const double a = k * M_PI / 4.0;
Pose2D t = map_from_odom;
t.x += shift_m * std::cos(a);
t.y += shift_m * std::sin(a);
best = std::max(best, score(t));
}
return best / s0;
}
RelocHypothesis LocalMatch(const LikelihoodField& field,
const std::vector<Point>& pts_odom, const Pose2D& odom_now,
const Point& center, double radius,
@@ -610,4 +738,95 @@ RelocVerdict Decide(const RelocMatch& m, int streak, const GlobalLocalizerConfig
return RelocVerdict::kAccept;
}
StaticPlace SummarizePlace(const std::vector<StaticRound>& hist) {
StaticPlace p;
if (hist.empty()) return p;
p.map_from_odom = hist.back().map_from_odom;
p.rounds = hist.size();
p.min_ratio = 1e9;
p.min_yaw = 1e9;
p.max_shift = -1.0;
for (const StaticRound& h : hist) {
p.min_ratio = std::min(p.min_ratio, h.ratio);
p.min_yaw = std::min(p.min_yaw, h.ratio_yaw);
p.max_shift = std::max(p.max_shift, h.shift);
}
return p;
}
RelocVerdict DecideStatic(std::vector<StaticRound>* hist, const StaticRound& r,
const std::vector<StaticPlace>& prev, const GlobalLocalizerConfig& cfg,
bool use_shift, bool* along_axis, std::string* why) {
*along_axis = false;
// Robot dung yen -> nguoi thang cac vong phai la CUNG mot pose robot.
if (!hist->empty()) {
const Pose2D& b = hist->back().robot;
if (std::hypot(r.robot.x - b.x, r.robot.y - b.y) > cfg.static_agree_m ||
std::fabs(AngleDiff(r.robot.theta, b.theta)) > cfg.static_agree_deg * kDegToRad) {
hist->clear();
}
}
hist->push_back(r);
// Giu MOI vong lien tiep cung cho (chan tren de khong phinh): doi thu tung
// hien ra o bat ky vong nao deu tinh.
constexpr size_t kMaxHist = 30;
while (hist->size() > kMaxHist) hist->erase(hist->begin());
const size_t k = static_cast<size_t>(std::max(1, cfg.static_rounds));
const StaticPlace cur = SummarizePlace(*hist);
const size_t n = cur.rounds;
char b[200];
if (cur.min_yaw < cfg.min_ratio_yaw) {
std::snprintf(b, sizeof(b), "ty le huong nho nhat %zu vong %.2f < %.2f", n, cur.min_yaw,
cfg.min_ratio_yaw);
*why = b;
return RelocVerdict::kAmbiguousYaw;
}
if (cur.min_ratio < cfg.min_ratio) {
std::snprintf(b, sizeof(b), "ty le nho nhat %zu vong %.2f < %.2f", n, cur.min_ratio, cfg.min_ratio);
*why = b;
return RelocVerdict::kAmbiguous;
}
if (use_shift && cur.max_shift >= cfg.tilt_max_shift_ratio) {
*along_axis = true;
std::snprintf(b, sizeof(b), "dich %.1f m van giu %.0f%% diem", cfg.tilt_shift_m,
100.0 * cur.max_shift);
*why = b;
return RelocVerdict::kAmbiguous;
}
if (n < k) {
std::snprintf(b, sizeof(b), "cho %zu vong lien tiep cung cho (%zu/%zu), ty le nho nhat %.2f", k, n,
k, cur.min_ratio);
*why = b;
return RelocVerdict::kNeedCycles;
}
// 1. Mot cho, chuan mot goc nhin.
if (cur.min_ratio >= cfg.static_min_ratio) {
std::snprintf(b, sizeof(b), "%zu vong cung cho: ty le nho nhat %.2f / huong %.2f", n, cur.min_ratio,
cur.min_yaw);
*why = b;
return RelocVerdict::kAccept;
}
// 2. Hai cho khac nhau, MOI cho deu qua min_ratio o moi vong cua no, cung gia thuyet.
for (const StaticPlace& p : prev) {
if (p.rounds < k || p.min_ratio < cfg.min_ratio || p.min_yaw < cfg.min_ratio_yaw) continue;
if (use_shift && p.max_shift >= cfg.tilt_max_shift_ratio) continue;
if (std::hypot(p.map_from_odom.x - cur.map_from_odom.x, p.map_from_odom.y - cur.map_from_odom.y) >
cfg.static_agree_m ||
std::fabs(AngleDiff(p.map_from_odom.theta, cur.map_from_odom.theta)) >
cfg.static_agree_deg * kDegToRad) {
continue;
}
std::snprintf(b, sizeof(b),
"hai cho cung pose: ty le nho nhat %.2f (cho nay, %zu vong) / %.2f (cho truoc, %zu vong)",
cur.min_ratio, n, p.min_ratio, p.rounds);
*why = b;
return RelocVerdict::kAccept;
}
std::snprintf(b, sizeof(b),
"ty le nho nhat %zu vong %.2f < %.2f -- can them mot cho khac (sau khi bo) cung pose",
n, cur.min_ratio, cfg.static_min_ratio);
*why = b;
return RelocVerdict::kNeedCycles;
}
} // namespace vslam

View File

@@ -147,4 +147,28 @@ bool FloorModel::FloorAt(double x, double y, double* z, double* dist) const {
return true;
}
bool FloorGradientAt(const FloorModel& fm, double x, double y, double max_dist, double* gx,
double* gy) {
FloorNode n;
double d = 0.0;
if (!fm.NearestNode(x, y, &n, &d) || d > max_dist) return false;
// Dao ham cua PlaneZ: z = z_n - fwd * tan(pitch) + left * tan(roll).
const double c = std::cos(n.theta), s = std::sin(n.theta);
const double tp = std::tan(n.pitch), tr = std::tan(n.roll);
*gx = -tp * c - tr * s;
*gy = -tp * s + tr * c;
return true;
}
double TiltResidual(double roll, double pitch, double yaw, double gx, double gy) {
const double c = std::cos(yaw), s = std::sin(yaw);
const double sf = gx * c + gy * s; // doc theo huong mui (len = duong)
const double sl = -gx * s + gy * c; // doc sang trai
// Than nam tren mat do, R = Rz(yaw) Ry(pitch) Rx(roll): truc x than nam trong
// mat -> tan(pitch) = -sf; truc y than nam trong mat -> tan(roll) = sl * cos(pitch).
const double mf = -std::tan(pitch);
const double ml = std::tan(roll) / std::max(1e-6, std::cos(pitch));
return std::hypot(mf - sf, ml - sl);
}
} // namespace vslam