211 lines
8.7 KiB
C++
211 lines
8.7 KiB
C++
#ifndef VSLAM_GLOBAL_LOCALIZER_H
|
|
#define VSLAM_GLOBAL_LOCALIZER_H
|
|
|
|
// Dinh vi TOAN CUC bang khop scan LiDAR voi luoi chiem dung -- port tu
|
|
// vSLAM_ws/src/localization_ops/scripts/global_scan_init.py (ban ROS).
|
|
//
|
|
// ============ BAI TOAN ============
|
|
// Tha robot xuong mot cho bat ky tren ban do da quet, KHONG cho biet dang o
|
|
// dau. Robot xoay tai cho gom scan. Phai suy ra pose trong ban do -- roi dua
|
|
// cho RTAB-Map lam pose ban dau (no chi bat duoc khi lech < ~0.3 m / 5 do, xem
|
|
// docs/SLAM.md).
|
|
//
|
|
// ============ VI SAO KHOP SCAN, KHONG TUI TU ============
|
|
// Ban ROS do tren hospital: 194 lan tui tu de xuat, 186 lan co DUNG 0 inlier
|
|
// (D410 chi co anh IR, hanh lang giong nhau). Khop scan tinh diem MOI gia
|
|
// thuyet (x, y, theta) cung luc nen SO SANH duoc cac ung vien -- hai hanh lang
|
|
// giong nhau cho hai diem gan bang nhau -> biet la nhap nhang va IM LANG thay
|
|
// vi chon bua.
|
|
//
|
|
// ============ ⚠️ DA DO BEN ROS, KHONG DUOC QUEN ============
|
|
// - Xac minh bang depth QUANH mot gia thuyet (GICP) bi bac: pose SAI 22.4 m
|
|
// duoc cham TOT hon pose dung. Xac minh cuc bo khong bat duoc nham lan
|
|
// toan cuc.
|
|
// - Tren phan cung that da CHOT SAI mot lan (22.5 m, lat 180 do) du qua ca
|
|
// hai cong ty le. Ty le mot lan khop KHONG tach duoc dung/sai o dai
|
|
// 1.31-1.39 -> moi co cong nhieu vong (`min_ratio_fast`, `min_cycles`).
|
|
//
|
|
// Module nay KHONG goi RTAB-Map, khong co luong, khong doc dong ho: chi la
|
|
// ham thuan tren (luoi, diem). Viec gom scan khi xoay va dat pose ban dau la
|
|
// cua ben goi.
|
|
|
|
#include <cmath>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "vslam/vslam_common.h"
|
|
|
|
namespace vslam {
|
|
|
|
// So lay NGUYEN tu ban ROS (global_scan_init.py). Doi so nao thi ghi ly do va
|
|
// so do o ben canh -- ban ROS da hai lan rut ket luan tu 2 mau roi mau thu
|
|
// ba lat nguoc.
|
|
struct GlobalLocalizerConfig {
|
|
// Do rong truong likelihood exp(-d^2 / 2 sigma^2). 0.15 m ~ 3 o luoi 0.05:
|
|
// chiu duoc sai so luoi ma khong lam nhoe het dac trung.
|
|
double sigma = 0.15;
|
|
// = Grid/RangeMax: ban do chi dung tu tia <= 12 m. Dung tia xa hon la khop
|
|
// vao vung chua tung ghi.
|
|
double max_range = 12.0;
|
|
|
|
// --- Tim tho ---
|
|
double coarse_res = 0.80; // m
|
|
double angle_step_deg = 5.0;
|
|
int top_k = 24; // dinh tho xet de gom ung vien
|
|
int max_candidates = 8; // ung vien (da khu trung) dem tinh chinh
|
|
size_t max_points = 1500; // sau khi gop diem ve o luoi
|
|
|
|
// --- Doi thu ---
|
|
// Doi thu VI TRI phai cach nguoi thang it nhat ngan nay.
|
|
double reject_radius = 2.0;
|
|
// Doi thu HUONG: CUNG cho (trong reject_radius) nhung lech it nhat ngan nay.
|
|
// Ban ROS do: dam may 360 do doi xung quay -> vi tri dung, huong sai 40-160
|
|
// do ma ty le vi tri van 1.6.
|
|
double min_angle_sep_deg = 25.0;
|
|
|
|
// --- Cong ---
|
|
double min_ratio = 1.30; // s1 / s2 (vi tri)
|
|
double min_ratio_yaw = 1.30; // s1 / s2_yaw (huong)
|
|
// Tren nguong nay chot ngay. Duoi thi phai dung dau `min_cycles` vong lien
|
|
// tiep. Ban ROS: 7 ca DUNG 1.315-1.581, 3 ca SAI 1.31-1.39.
|
|
double min_ratio_fast = 1.60;
|
|
int min_cycles = 2;
|
|
double assoc_radius = 0.8; // m, ghep gia thuyet giua hai vong
|
|
// Chi tinh mot vong vao streak khi robot DA DI it nhat ngan nay: ban sao doi
|
|
// xung 180 do cung lap lai y het khi dung yen.
|
|
double acc_min_move = 0.20;
|
|
double acc_timeout = 120.0; // s
|
|
|
|
// --- 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
|
|
};
|
|
|
|
// Truong likelihood tren luoi chiem dung. Dung chung cho khop toan cuc VA cho
|
|
// canh "da lac" -- cung mot thang do.
|
|
class LikelihoodField {
|
|
public:
|
|
// o >= 50 la tuong, o == 0 la trong (robot chi co the dung o day), -1 chua
|
|
// biet. Khoang cach bang EDT chinh xac (Felzenszwalb-Huttenlocher).
|
|
static LikelihoodField FromGrid(const OccupancyGrid& g, double sigma);
|
|
|
|
bool empty() const { return width_ == 0 || height_ == 0; }
|
|
int width() const { return width_; }
|
|
int height() const { return height_; }
|
|
double resolution() const { return res_; }
|
|
double origin_x() const { return ox_; }
|
|
double origin_y() const { return oy_; }
|
|
|
|
// Gia tri tai o (r, c); 0 ngoai luoi.
|
|
float At(int r, int c) const {
|
|
if (r < 0 || c < 0 || r >= height_ || c >= width_) return 0.0f;
|
|
return lf_[static_cast<size_t>(r) * width_ + c];
|
|
}
|
|
bool Free(int r, int c) const {
|
|
if (r < 0 || c < 0 || r >= height_ || c >= width_) return false;
|
|
return free_[static_cast<size_t>(r) * width_ + c] != 0;
|
|
}
|
|
|
|
// Diem TRUNG BINH moi diem (frame map). Diem ngoai luoi khong tinh; tra -1
|
|
// neu it hon `min_points` diem roi vao luoi.
|
|
double MeanScore(const std::vector<Point>& pts_map, size_t min_points = 50) const;
|
|
|
|
private:
|
|
int width_ = 0, height_ = 0;
|
|
double res_ = 0.05, ox_ = 0.0, oy_ = 0.0;
|
|
std::vector<float> lf_;
|
|
std::vector<uint8_t> free_;
|
|
};
|
|
|
|
struct RelocHypothesis {
|
|
Pose2D map_from_odom; // T_map_odom
|
|
double score = 0.0; // diem sau tinh chinh
|
|
};
|
|
|
|
struct RelocMatch {
|
|
bool ok = false;
|
|
Pose2D map_from_odom; // nguoi thang
|
|
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)
|
|
size_t points = 0; // so diem da dung sau khi gop
|
|
std::vector<RelocHypothesis> hyps; // da tinh chinh, [0] = nguoi thang
|
|
|
|
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); }
|
|
};
|
|
|
|
// 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.
|
|
RelocMatch GlobalMatch(const LikelihoodField& field,
|
|
const std::vector<Point>& pts_odom,
|
|
const GlobalLocalizerConfig& cfg);
|
|
|
|
// 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
|
|
// odom luc gom -- rang buoc dat tren vi tri ROBOT, khong phai tam dam diem (o
|
|
// hanh lang tam lech robot nhieu met). score = -1 neu khong khop.
|
|
//
|
|
// `yaw_center` (rad, huong ROBOT trong map) + `yaw_window`: chi xet huong
|
|
// trong cua so do. Mac dinh (NAN) = moi huong.
|
|
//
|
|
// ⚠️ VI SAO CAN GIOI HAN HUONG: robot that 2026-09-29, 113/249 lan hoi LiDAR
|
|
// NHAP NHANG HUONG (mot scan LakiBeam ~270 do). Khop cuc bo MOI huong quanh
|
|
// ung vien VPR dung cho van truot sang huong LON cung voi LiDAR toan cuc (4
|
|
// lan chot sai, lech 105-177 do) -- vi no van dung LiDAR de chon huong. Anh
|
|
// VPR biet camera nhin ve dau: de NO chon huong, LiDAR chi chinh chinh xac.
|
|
RelocHypothesis LocalMatch(const LikelihoodField& field,
|
|
const std::vector<Point>& pts_odom, const Pose2D& odom_now,
|
|
const Point& center, double radius,
|
|
const GlobalLocalizerConfig& cfg,
|
|
double yaw_center = NAN, double yaw_window = M_PI);
|
|
|
|
// Tich luy gia thuyet qua nhieu vong, truyen bang ODOM (pose toan cuc chua
|
|
// biet). "Streak" = so vong LIEN TIEP mot gia thuyet dung dau.
|
|
class HypothesisTracker {
|
|
public:
|
|
explicit HypothesisTracker(const GlobalLocalizerConfig& cfg) : cfg_(cfg) {}
|
|
|
|
// `odom_now`: pose odom->base luc vong nay ket thuc. `now_s`: giay, don dieu.
|
|
void Add(const RelocMatch& m, const Pose2D& odom_now, double now_s);
|
|
// Streak cua gia thuyet gan `map_from_odom` nhat (trong assoc_radius).
|
|
int Streak(const Pose2D& map_from_odom) const;
|
|
size_t size() const { return acc_.size(); }
|
|
void Clear() { acc_.clear(); have_odom_ = false; }
|
|
|
|
private:
|
|
struct Entry {
|
|
Pose2D map_from_odom;
|
|
int streak = 0;
|
|
double t_last = 0.0;
|
|
};
|
|
GlobalLocalizerConfig cfg_;
|
|
std::vector<Entry> acc_;
|
|
Pose2D odom_mark_; // odom luc streak duoc cong lan cuoi
|
|
Pose2D last_odom_; // odom luc Add() gan nhat -- de so vi tri
|
|
bool have_odom_ = false;
|
|
double t_prev_ = 0.0;
|
|
};
|
|
|
|
enum class RelocVerdict {
|
|
kAccept, // qua het cong -> dat pose ban dau
|
|
kNeedCycles, // ty le o dai bien, chua du so vong lien tiep
|
|
kAmbiguous, // ty le vi tri < min_ratio (hanh lang lap)
|
|
kAmbiguousYaw, // vi tri co the dung, HUONG khong tin duoc
|
|
kFailed, // khong khop duoc gi
|
|
};
|
|
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);
|
|
|
|
} // namespace vslam
|
|
|
|
#endif // VSLAM_GLOBAL_LOCALIZER_H
|