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

@@ -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