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:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -80,12 +80,31 @@ void EncodeScan(const LaserScan& scan, UnixTime stamp, std::string* out);
|
||||
void EncodeCloud(const PointCloudXYZ& cloud, UnixTime stamp, uint32_t max_points,
|
||||
float quant_m, const Pose2D& delta, std::string* out);
|
||||
|
||||
// Do cao SAN cua ban do (slam.yaml slope.enabled): luoi THO, mm, kFloorUnknown
|
||||
// = chua biet. Web uon mat phang ban do 2D theo luoi nay -- khong co no thi
|
||||
// mat doc va san tang tren duoc ve o z = 0, nam DUOI tuong voxel (o z that)
|
||||
// va trong nhu san bi thung.
|
||||
constexpr int16_t kFloorUnknown = -32768;
|
||||
struct MapFloorGrid {
|
||||
float resolution = 0.2f;
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
float origin_x = 0.0f;
|
||||
float origin_y = 0.0f;
|
||||
std::vector<int16_t> mm; // hang chinh, hang 0 = y nho nhat (nhu OccupancyGrid)
|
||||
};
|
||||
|
||||
// RLE hang-chinh. Xem rle.h.
|
||||
// `type` cho phep dung chung bo ma hoa cho ban do SLAM (kMapFull) va ban do
|
||||
// THE GIOI cua bo mo phong (kWorldMap): khung giong het nhau, chi khac y
|
||||
// nghia. Mot bo ma hoa thu hai se la hai cho de lech dinh dang.
|
||||
//
|
||||
// `floor_grid` (tuy chon): khoi do cao san NOI SAU phan RLE -- magic "FLH1", f32
|
||||
// res, i32 w, i32 h, f32 ox, f32 oy, roi w*h int16 (mm). Khong co thi khung y
|
||||
// het cu.
|
||||
void EncodeMapFull(const OccupancyGrid& map, UnixTime stamp, std::string* out,
|
||||
BinaryType type = BinaryType::kMapFull);
|
||||
BinaryType type = BinaryType::kMapFull,
|
||||
const MapFloorGrid* floor_grid = nullptr);
|
||||
|
||||
// (kMapPatch = 4 da bi BO. No noi duoc vao dau: cong per-session chi la mot
|
||||
// atomic `acked_map_version_`, khong dien ta noi "session nay dang giu luoi
|
||||
@@ -149,7 +168,9 @@ struct BinaryHeader {
|
||||
|
||||
bool DecodeHeader(const std::string& buf, size_t offset, BinaryHeader* out);
|
||||
bool DecodeScan(const std::string& buf, LaserScan* out);
|
||||
bool DecodeMapFull(const std::string& buf, OccupancyGrid* out);
|
||||
// `floor_grid` (tuy chon) nhan khoi do cao san neu khung co; khong co thi width = 0.
|
||||
bool DecodeMapFull(const std::string& buf, OccupancyGrid* out,
|
||||
MapFloorGrid* floor_grid = nullptr);
|
||||
|
||||
// Giai mot manh. `out` duoc NOI THEM, khong xoa -- rap nhieu manh bang cach
|
||||
// goi lien tiep. `total`/`chunk_index`/`chunk_count`/`epoch` tra qua con tro
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "vslam/binary_codec.h"
|
||||
#include "vslam/stats.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
@@ -181,6 +182,8 @@ struct WebSnapshot {
|
||||
Pose2D cloud_delta;
|
||||
bool have_cloud_delta = false;
|
||||
OccupancyGridPtr map; // ban do SLAM dung
|
||||
// Do cao SAN cua ban do (che do doc), gui kem khung map. Null = phang.
|
||||
std::shared_ptr<const MapFloorGrid> map_floor;
|
||||
// Ban do THE GIOI cua bo mo phong (khung nhin "Gazebo"). Null khi chay
|
||||
// that -- luc do khong co su that nao de doi chieu.
|
||||
OccupancyGridPtr world_map;
|
||||
|
||||
@@ -147,7 +147,7 @@ void EncodeCloud(const PointCloudXYZ& cloud, UnixTime stamp, uint32_t max_points
|
||||
}
|
||||
|
||||
void EncodeMapFull(const OccupancyGrid& map, UnixTime stamp, std::string* out,
|
||||
BinaryType type) {
|
||||
BinaryType type, const MapFloorGrid* floor_grid) {
|
||||
PutHeader(out, type, stamp);
|
||||
PutF32(out, map.resolution);
|
||||
PutI32(out, static_cast<int32_t>(map.width));
|
||||
@@ -169,6 +169,19 @@ void EncodeMapFull(const OccupancyGrid& map, UnixTime stamp, std::string* out,
|
||||
(*out)[run_count_off + static_cast<size_t>(i)] =
|
||||
static_cast<char>((r32 >> (8 * i)) & 0xFF);
|
||||
}
|
||||
|
||||
// Khoi do cao san (che do doc), NOI SAU phan RLE -- xem binary_codec.h.
|
||||
const MapFloorGrid* fg = floor_grid;
|
||||
if (fg != nullptr && fg->width > 0 && fg->height > 0 &&
|
||||
fg->mm.size() == static_cast<size_t>(fg->width) * fg->height) {
|
||||
out->append("FLH1", 4);
|
||||
PutF32(out, fg->resolution);
|
||||
PutI32(out, static_cast<int32_t>(fg->width));
|
||||
PutI32(out, static_cast<int32_t>(fg->height));
|
||||
PutF32(out, fg->origin_x);
|
||||
PutF32(out, fg->origin_y);
|
||||
for (int16_t v : fg->mm) PutI16(out, v);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Ban do voxel 3D -------------------------------------------------------
|
||||
@@ -333,9 +346,10 @@ bool DecodeScan(const std::string& buf, LaserScan* out) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DecodeMapFull(const std::string& buf, OccupancyGrid* out) {
|
||||
bool DecodeMapFull(const std::string& buf, OccupancyGrid* out, MapFloorGrid* floor_grid) {
|
||||
BinaryHeader h;
|
||||
if (!DecodeHeader(buf, 0, &h) || h.type != BinaryType::kMapFull) return false;
|
||||
if (floor_grid != nullptr) *floor_grid = MapFloorGrid{};
|
||||
|
||||
size_t o = kBinaryHeaderSize;
|
||||
if (buf.size() < o + 28) return false;
|
||||
@@ -351,6 +365,25 @@ bool DecodeMapFull(const std::string& buf, OccupancyGrid* out) {
|
||||
const size_t expected = static_cast<size_t>(out->width) * out->height;
|
||||
if (!RleDecode(buf, o, runs, expected, &out->data)) return false;
|
||||
|
||||
// Khoi do cao san tuy chon, ngay sau RLE (3 byte moi run).
|
||||
o += static_cast<size_t>(runs) * 3;
|
||||
if (floor_grid != nullptr && buf.size() >= o + 24 && buf.compare(o, 4, "FLH1") == 0) {
|
||||
MapFloorGrid f;
|
||||
f.resolution = GetF32(buf, o + 4);
|
||||
f.width = static_cast<uint32_t>(GetI32(buf, o + 8));
|
||||
f.height = static_cast<uint32_t>(GetI32(buf, o + 12));
|
||||
f.origin_x = GetF32(buf, o + 16);
|
||||
f.origin_y = GetF32(buf, o + 20);
|
||||
const size_t n = static_cast<size_t>(f.width) * f.height;
|
||||
if (buf.size() >= o + 24 + 2 * n) {
|
||||
f.mm.resize(n);
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
f.mm[i] = static_cast<int16_t>(GetU16(buf, o + 24 + 2 * i));
|
||||
}
|
||||
*floor_grid = std::move(f);
|
||||
}
|
||||
}
|
||||
|
||||
out->header.stamp = UnixTime::fromNSec(static_cast<int64_t>(h.stamp_ns));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -830,7 +830,8 @@ void WebServer::Publish(const WebSnapshot& snap) {
|
||||
if (s->acked_map_version() == snap.slam.map_version) continue;
|
||||
if (!buf) {
|
||||
buf = std::make_shared<std::string>();
|
||||
EncodeMapFull(*snap.map, snap.stamp, buf.get());
|
||||
EncodeMapFull(*snap.map, snap.stamp, buf.get(), BinaryType::kMapFull,
|
||||
snap.map_floor.get());
|
||||
}
|
||||
s->Send(buf, true, /*is_map=*/true);
|
||||
s->set_acked_map_version(snap.slam.map_version);
|
||||
|
||||
@@ -12,7 +12,7 @@ const MAGIC = 0x314C5356; // 'VSL1' little-endian
|
||||
// Phien ban ma nguon giao dien. TANG khi doi app.js / app3d.js / style.css.
|
||||
// Hien ngay tren thanh tren cung de phan biet "chua sua" voi "trinh duyet con
|
||||
// cache ban cu".
|
||||
const UI_VERSION = '2026-09-30a';
|
||||
const UI_VERSION = '2026-10-05c';
|
||||
|
||||
// ⚠️ HAI bang mau, mot cho moi khung. Chung KHONG duoc giong nhau.
|
||||
//
|
||||
@@ -68,6 +68,8 @@ const T_SCAN = 1, T_CLOUD = 2, T_MAP_FULL = 3, T_PATH = 5;
|
||||
const T_WORLD_MAP = 6;
|
||||
// Ban do voxel 3D: tap day du (chia manh) va phan them/bot.
|
||||
const T_VOXEL_FULL = 7, T_VOXEL_DELTA = 8;
|
||||
// Khoi do cao SAN noi sau RLE cua khung ban do (che do doc, binary_codec.h).
|
||||
const FLOOR_MAGIC = 0x31484C46; // 'FLH1' little-endian
|
||||
|
||||
// Ba ngu nghia cua mot tia. Lan lon chung la lam sai ca man hinh.
|
||||
const R_NAN = 0xFFFF; // khong do duoc (duoi range_min)
|
||||
@@ -229,7 +231,21 @@ function onBinary(dv) {
|
||||
cells.fill(val, p, p + len);
|
||||
p += len;
|
||||
}
|
||||
const grid = { res, w, h, ox, oy, cells };
|
||||
// Do cao san (mm, -32768 = chua biet) ngay sau RLE. Khong co thi ban do
|
||||
// phang nhu cu.
|
||||
let floor = null;
|
||||
const fo = off + 28 + runs * 3;
|
||||
if (type === T_MAP_FULL && dv.byteLength >= fo + 24 &&
|
||||
dv.getUint32(fo, true) === FLOOR_MAGIC) {
|
||||
const fw = dv.getInt32(fo + 8, true), fh = dv.getInt32(fo + 12, true);
|
||||
if (fw > 0 && fh > 0 && dv.byteLength >= fo + 24 + fw * fh * 2) {
|
||||
const mm = new Int16Array(fw * fh);
|
||||
for (let i = 0; i < fw * fh; i++) mm[i] = dv.getInt16(fo + 24 + i * 2, true);
|
||||
floor = { res: dv.getFloat32(fo + 4, true), w: fw, h: fh,
|
||||
ox: dv.getFloat32(fo + 16, true), oy: dv.getFloat32(fo + 20, true), mm };
|
||||
}
|
||||
}
|
||||
const grid = { res, w, h, ox, oy, cells, floor };
|
||||
if (type === T_WORLD_MAP) {
|
||||
// Ban do THE GIOI: su that, de lai cho chuan. KHONG lan vao state.map.
|
||||
state.worldMap = grid;
|
||||
@@ -620,6 +636,11 @@ function anchorPose(robotPose, anchoredAt) {
|
||||
x: robotPose.x + c * lx - s * ly,
|
||||
y: robotPose.y + s * lx + c * ly,
|
||||
yaw: robotPose.yaw + (anchoredAt.yaw - state.pose.yaw),
|
||||
// Cao do san duoi robot va do nghieng than (che do doc): khung 3D nang
|
||||
// scan/cloud len theo z, va xoay scan tho theo roll/pitch.
|
||||
z: robotPose.z,
|
||||
roll: robotPose.roll,
|
||||
pitch: robotPose.pitch,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1314,7 +1335,7 @@ function draw3d() {
|
||||
v3dSetMap({ ...state.map, data: state.map.cells, version: state.mapVersion });
|
||||
}
|
||||
if (V3D.mapMesh) V3D.mapMesh.visible = on('lyMap') && !!state.map;
|
||||
if (on('lyPath')) v3dSetPath(state.path);
|
||||
if (on('lyPath')) v3dSetPath(state.path, state.map && state.map.floor);
|
||||
else if (V3D.pathLine) V3D.pathLine.visible = false;
|
||||
|
||||
v3dRender(cv3d, on('lyFollow'), split3d);
|
||||
|
||||
@@ -456,8 +456,16 @@ function v3dSetScan(arr, pose, r) {
|
||||
const n = arr.length / 2;
|
||||
const pos = new Float32Array(n * 3);
|
||||
// Scan nam o do cao cua LiDAR, khong phai o san. Ve o san se lam nguoi xem
|
||||
// tuong LiDAR thay duoc vat thap -- no KHONG thay.
|
||||
const z = r.laser_z + r.wheel_radius;
|
||||
// tuong LiDAR thay duoc vat thap -- no KHONG thay. Cong cao do san duoi
|
||||
// robot (che do doc), khong thi o tang tren scan ve chui duoi san.
|
||||
const z0 = pose.z || 0;
|
||||
const zl = r.laser_z + r.wheel_radius; // goc LiDAR tren san, he footprint
|
||||
// Web nhan scan THO: tia nam trong mat phang LiDAR, NGHIENG theo than robot.
|
||||
// Tren doc 15 do ve phang thi diem cach 3 m lech ~0.8 m (chui vao mat doc)
|
||||
// -> xoay theo roll/pitch quanh footprint, nhu v3dSetPose xoay hinh robot.
|
||||
// ROS R = Rz(yaw) Ry(pitch) Rx(roll); khong co tilt thi y het cu.
|
||||
const cr = Math.cos(pose.roll || 0), sr = Math.sin(pose.roll || 0);
|
||||
const cp = Math.cos(pose.pitch || 0), sp = Math.sin(pose.pitch || 0);
|
||||
// ⚠️ Scan den trong frame LASER, khong phai frame BASE.
|
||||
//
|
||||
// `laser_link` cach `base_link` 0.24 m ve phia truoc (robot.yaml). Dat thang
|
||||
@@ -469,9 +477,12 @@ function v3dSetScan(arr, pose, r) {
|
||||
const lx = r.laser_x || 0, ly = r.laser_y || 0;
|
||||
for (let i = 0; i < n; ++i) {
|
||||
const x = arr[i * 2] + lx, y = arr[i * 2 + 1] + ly;
|
||||
pos[i * 3] = pose.x + c * x - s * y;
|
||||
pos[i * 3 + 1] = z;
|
||||
pos[i * 3 + 2] = -(pose.y + s * x + c * y);
|
||||
// Rx(roll) roi Ry(pitch), trong he footprint (truoc khi quay yaw).
|
||||
const y1 = y * cr - zl * sr, z1 = y * sr + zl * cr;
|
||||
const x2 = x * cp + z1 * sp, z2 = -x * sp + z1 * cp;
|
||||
pos[i * 3] = pose.x + c * x2 - s * y1;
|
||||
pos[i * 3 + 1] = z0 + z2;
|
||||
pos[i * 3 + 2] = -(pose.y + s * x2 + c * y1);
|
||||
}
|
||||
V3D.scanPts.geometry.dispose();
|
||||
V3D.scanPts.geometry = new THREE.BufferGeometry();
|
||||
@@ -493,10 +504,11 @@ function v3dSetCloud(arr, pose) {
|
||||
const c = Math.cos(pose.yaw), s = Math.sin(pose.yaw);
|
||||
const n = arr.length / 3;
|
||||
const pos = new Float32Array(n * 3), col = new Float32Array(n * 3);
|
||||
const z0 = pose.z || 0; // cao do san duoi robot (che do doc)
|
||||
for (let i = 0; i < n; ++i) {
|
||||
const x = arr[i * 3], y = arr[i * 3 + 1], z = arr[i * 3 + 2];
|
||||
pos[i * 3] = pose.x + c * x - s * y;
|
||||
pos[i * 3 + 1] = z;
|
||||
pos[i * 3 + 1] = z0 + z;
|
||||
pos[i * 3 + 2] = -(pose.y + s * x + c * y);
|
||||
// To mau theo do cao, cung bang mau voi khung 2D.
|
||||
const t = Math.max(0, Math.min(1, z / 1.2));
|
||||
@@ -618,12 +630,22 @@ function v3dSetMapCloud(v, visible) {
|
||||
// San dat o TOA DO THE GIOI chu khong theo robot. Dat theo robot la dung cai
|
||||
// bay "sàn troi theo robot": robot dung yen giua man hinh con ca the gioi
|
||||
// truot qua, trong y het loi dinh vi.
|
||||
//
|
||||
// ⚠️ Chi dung lai khi BAO the gioi doi. Chay tu `.world`, v3dSetWorld() goi
|
||||
// ham nay MOI khung hinh (nhanh mesh that thoat som truoc khoa worldKey), va
|
||||
// ban cu dung lai mat san + GridHelper moi khung ma khong giai phong cai cu.
|
||||
// Do tren slope_test.world: geometry tren GPU tang ~1 moi khung (20 -> 203
|
||||
// sau 183 khung), tuc ~3600/phut o 60 fps -- trinh duyet cham dan.
|
||||
function v3dSetGround(grid) {
|
||||
if (!V3D.ready || !grid) return;
|
||||
const key = grid.w + 'x' + grid.h + '@' + grid.res + ':' + grid.ox + ',' + grid.oy;
|
||||
if (V3D.groundKey === key && V3D.floorMesh) return;
|
||||
V3D.groundKey = key;
|
||||
const r = grid.res;
|
||||
if (V3D.floorMesh) {
|
||||
V3D.root.remove(V3D.floorMesh);
|
||||
V3D.floorMesh.geometry.dispose();
|
||||
V3D.floorMesh.material.dispose();
|
||||
}
|
||||
const fw = grid.w * r, fh = grid.h * r;
|
||||
V3D.floorMesh = new THREE.Mesh(
|
||||
@@ -640,7 +662,11 @@ function v3dSetGround(grid) {
|
||||
const span = Math.max(fw, fh);
|
||||
V3D.scene.fog.near = span * 0.35;
|
||||
V3D.scene.fog.far = span * 1.4;
|
||||
if (V3D.gridHelper) V3D.root.remove(V3D.gridHelper);
|
||||
if (V3D.gridHelper) {
|
||||
V3D.root.remove(V3D.gridHelper);
|
||||
V3D.gridHelper.geometry.dispose();
|
||||
V3D.gridHelper.material.dispose();
|
||||
}
|
||||
V3D.gridHelper = new THREE.GridHelper(Math.ceil(span), Math.ceil(span),
|
||||
0x93a2b0, 0xa6b3c0);
|
||||
V3D.root.add(V3D.gridHelper);
|
||||
@@ -844,11 +870,69 @@ function v3dSetWorld(grid, wallHeight) {
|
||||
v3dSetGround(grid);
|
||||
}
|
||||
|
||||
// Ban do occupancy -> mot mat phang co texture, dat o san.
|
||||
// --- Do cao SAN cua ban do (che do doc) ---
|
||||
//
|
||||
// Luoi tho (0.2 m, mm, -32768 = chua biet) di kem khung ban do SLAM. O chua
|
||||
// biet (xa moi node > 4 m: tuong LiDAR thay tu xa) lay gia tri o DA BIET gan
|
||||
// nhat -- de 0 thi mat phang tu tang tren cam xuong z = 0 thanh mot tam xien.
|
||||
const FLOOR_UNKNOWN = -32768;
|
||||
// Mat ban do uon dat CAO hon san uoc luong 4 cm. Trong khung gop, mesh san
|
||||
// THAT cua world (doc, san) la mat duc trung cho voi no: thap hon du chi 2 mm
|
||||
// la bi che, ngang nhau la nhap nhay rang cua -- chinh la "nen bi lung". Da do
|
||||
// (sim, map 645 node): sai uoc - that p1 -2.3 cm, p50 +3.3 cm; nang 3 cm con
|
||||
// 0.3% o chim, 4 cm con 0%. Robot that khong co mesh world: chi noi 4 cm.
|
||||
const FLOOR_LIFT = 0.04;
|
||||
|
||||
function v3dFloorFill(fl) {
|
||||
const n = fl.w * fl.h;
|
||||
const out = new Int16Array(fl.mm);
|
||||
const q = new Int32Array(n);
|
||||
let head = 0, tail = 0;
|
||||
for (let i = 0; i < n; ++i) if (out[i] !== FLOOR_UNKNOWN) q[tail++] = i;
|
||||
if (tail === 0) return null;
|
||||
while (head < tail) {
|
||||
const i = q[head++], c = i % fl.w, r = (i - c) / fl.w;
|
||||
const nb = [c > 0 ? i - 1 : -1, c + 1 < fl.w ? i + 1 : -1,
|
||||
r > 0 ? i - fl.w : -1, r + 1 < fl.h ? i + fl.w : -1];
|
||||
for (const j of nb) {
|
||||
if (j >= 0 && out[j] === FLOOR_UNKNOWN) { out[j] = out[i]; q[tail++] = j; }
|
||||
}
|
||||
}
|
||||
return { ...fl, mm: out };
|
||||
}
|
||||
|
||||
// Do cao san (m) tai (x, y) ROS: noi suy song tuyen giua TAM 4 o quanh diem
|
||||
// (dinh mat phang nam dung GOC o -- lay o gan nhat thi tren doc 15 do lech
|
||||
// ~3 cm theo phia lam tron). null neu ngoai luoi / chua biet.
|
||||
function v3dFloorAt(fl, x, y) {
|
||||
if (!fl) return null;
|
||||
const gx = (x - fl.ox) / fl.res - 0.5, gy = (y - fl.oy) / fl.res - 0.5;
|
||||
const c0 = Math.floor(gx), r0 = Math.floor(gy);
|
||||
const fx = gx - c0, fy = gy - r0;
|
||||
let sw = 0, sz = 0;
|
||||
for (let k = 0; k < 4; ++k) {
|
||||
const c = c0 + (k & 1), r = r0 + (k >> 1);
|
||||
if (c < 0 || r < 0 || c >= fl.w || r >= fl.h) continue;
|
||||
const v = fl.mm[r * fl.w + c];
|
||||
if (v === FLOOR_UNKNOWN) continue;
|
||||
const wgt = ((k & 1) ? fx : 1 - fx) * ((k >> 1) ? fy : 1 - fy);
|
||||
sw += wgt;
|
||||
sz += wgt * v;
|
||||
}
|
||||
return sw > 1e-6 ? (sz / sw) * 0.001 : null;
|
||||
}
|
||||
|
||||
// Ban do occupancy -> mot mat phang co texture, dat o san -- UON theo do cao
|
||||
// san khi ban do co (che do doc): mat doc va san tang tren o dung cao do,
|
||||
// khong nam bep o z = 0 duoi tuong voxel.
|
||||
function v3dSetMap(map) {
|
||||
if (!V3D.ready || !map || !map.w || !map.h) return;
|
||||
if (V3D.mapKey === map.version) return;
|
||||
V3D.mapKey = map.version;
|
||||
// Khoa theo MANG O, khong theo so phien ban: khung JSON mang phien ban moi
|
||||
// toi TRUOC khung ban do, nen khoa theo phien ban co the chot luoi CU voi
|
||||
// phien ban MOI roi khong bao gio ve lai -- ma o che do dinh vi phien ban
|
||||
// chi doi khi co lien ket moi.
|
||||
if (V3D.mapData === map.data) return;
|
||||
V3D.mapData = map.data;
|
||||
|
||||
const cv = document.createElement('canvas');
|
||||
cv.width = map.w; cv.height = map.h;
|
||||
@@ -880,23 +964,57 @@ function v3dSetMap(map) {
|
||||
tex.magFilter = THREE.NearestFilter;
|
||||
tex.minFilter = THREE.LinearMipmapLinearFilter;
|
||||
|
||||
if (V3D.mapMesh) { V3D.root.remove(V3D.mapMesh); V3D.mapMesh.geometry.dispose(); }
|
||||
if (V3D.mapMesh) {
|
||||
V3D.root.remove(V3D.mapMesh);
|
||||
V3D.mapMesh.geometry.dispose();
|
||||
if (V3D.mapMesh.material.map) V3D.mapMesh.material.map.dispose();
|
||||
V3D.mapMesh.material.dispose();
|
||||
}
|
||||
const wM = map.w * map.res, hM = map.h * map.res;
|
||||
const fl = map.floor ? v3dFloorFill(map.floor) : null;
|
||||
let geom;
|
||||
if (fl) {
|
||||
// Mot dinh moi o luoi san, tran 256 doan moi chieu (ban do 100 m van
|
||||
// chi ~66k dinh).
|
||||
const sx = Math.max(1, Math.min(256, Math.round(wM / fl.res)));
|
||||
const sy = Math.max(1, Math.min(256, Math.round(hM / fl.res)));
|
||||
geom = new THREE.PlaneGeometry(wM, hM, sx, sy);
|
||||
// Dinh cuc bo (x, y, z) -> sau rotation.x = -PI/2 la ROS (x, y, z) quanh
|
||||
// tam mat phang: cuc bo +y = ROS +y, cuc bo +z = LEN.
|
||||
const p = geom.attributes.position;
|
||||
for (let i = 0; i < p.count; ++i) {
|
||||
const z = v3dFloorAt(fl, map.ox + wM / 2 + p.getX(i), map.oy + hM / 2 + p.getY(i));
|
||||
p.setZ(i, z === null ? 0 : z);
|
||||
}
|
||||
p.needsUpdate = true;
|
||||
} else {
|
||||
geom = new THREE.PlaneGeometry(wM, hM);
|
||||
}
|
||||
V3D.mapMesh = new THREE.Mesh(
|
||||
new THREE.PlaneGeometry(wM, hM),
|
||||
geom,
|
||||
new THREE.MeshBasicMaterial({ map: tex, transparent: true, depthWrite: false }));
|
||||
V3D.mapMesh.rotation.x = -Math.PI / 2;
|
||||
// Hoi thap hon san mot chut de khong z-fight voi luoi va vanh cliff.
|
||||
V3D.mapMesh.position.set(map.ox + wM / 2, -0.002, -(map.oy + hM / 2));
|
||||
// Phang: hoi thap hon san mot chut de khong z-fight voi luoi va vanh cliff.
|
||||
// Uon: nang FLOOR_LIFT len tren mesh san that cua world (xem tren).
|
||||
V3D.mapMesh.position.set(map.ox + wM / 2, fl ? FLOOR_LIFT : -0.002, -(map.oy + hM / 2));
|
||||
V3D.mapHasFloor = !!fl;
|
||||
V3D.root.add(V3D.mapMesh);
|
||||
}
|
||||
|
||||
function v3dSetPath(pts) {
|
||||
// `floor` (tuy chon): luoi do cao san cua ban do -- quy dao di theo mat doc /
|
||||
// tang tren thay vi chui duoi san.
|
||||
function v3dSetPath(pts, floor) {
|
||||
if (!V3D.ready) return;
|
||||
if (V3D.pathLine) { V3D.root.remove(V3D.pathLine); V3D.pathLine.geometry.dispose(); }
|
||||
if (!pts || pts.length < 4) return;
|
||||
// Quy dao luon nam sat node nen khong can lap o chua biet nhu v3dSetMap.
|
||||
// Ve TREN mat ban do uon (nang FLOOR_LIFT), khong thi lop trong suot phu len
|
||||
// lam quy dao mo di.
|
||||
const v = [];
|
||||
for (let i = 0; i + 1 < pts.length; i += 2) v.push(toScene(pts[i], pts[i + 1], 0.01));
|
||||
for (let i = 0; i + 1 < pts.length; i += 2) {
|
||||
const z = v3dFloorAt(floor, pts[i], pts[i + 1]);
|
||||
v.push(toScene(pts[i], pts[i + 1], z === null ? 0.01 : z + FLOOR_LIFT + 0.01));
|
||||
}
|
||||
V3D.pathLine = new THREE.Line(
|
||||
new THREE.BufferGeometry().setFromPoints(v),
|
||||
new THREE.LineBasicMaterial({ color: 0xffb300 }));
|
||||
@@ -912,6 +1030,10 @@ function v3dSetPath(pts) {
|
||||
function v3dSetWorldVisible(on) {
|
||||
if (V3D.meshWorld) V3D.meshWorld.visible = on;
|
||||
if (V3D.worldMesh) V3D.worldMesh.visible = on;
|
||||
// Mat san the gioi (z = 0) o lai lam moc khi tat the gioi -- tru khi ban do
|
||||
// co do cao san: luc do no nam duoi tang tren nhu mot san sai cho (xem nua
|
||||
// SLAM trong v3dRender).
|
||||
if (V3D.floorMesh) V3D.floorMesh.visible = on || !V3D.mapHasFloor;
|
||||
}
|
||||
|
||||
// Dat khung nhin ve mot goc chuan.
|
||||
@@ -989,17 +1111,21 @@ function v3dRender(canvas, follow, splitSlam) {
|
||||
}
|
||||
|
||||
// Tam quay: bam theo robot neu bat, khong thi theo diem pan cua nguoi dung.
|
||||
let cx = V3D.orbit.tx, cz = V3D.orbit.ty;
|
||||
// Bam ca CAO DO (che do doc): o tang tren 1.5 m ma tam quay van o san duoi
|
||||
// thi camera xoay quanh mot diem chui duoi chan robot. Pose 2D (khong z)
|
||||
// thi robotGroup o y = 0, y het cu.
|
||||
let cx = V3D.orbit.tx, cz = V3D.orbit.ty, cy = 0;
|
||||
if (follow && V3D.robotGroup) {
|
||||
cx = V3D.robotGroup.position.x + V3D.orbit.tx;
|
||||
cz = V3D.robotGroup.position.z + V3D.orbit.ty;
|
||||
cy = V3D.robotGroup.position.y;
|
||||
}
|
||||
const o = V3D.orbit;
|
||||
V3D.camera.position.set(
|
||||
cx + o.dist * Math.cos(o.el) * Math.cos(o.az),
|
||||
o.dist * Math.sin(o.el),
|
||||
cy + o.dist * Math.sin(o.el),
|
||||
cz + o.dist * Math.cos(o.el) * Math.sin(o.az));
|
||||
V3D.camera.lookAt(cx, 0.2, cz);
|
||||
V3D.camera.lookAt(cx, cy + 0.2, cz);
|
||||
|
||||
const r = V3D.renderer;
|
||||
if (!splitSlam) {
|
||||
@@ -1038,7 +1164,10 @@ function v3dRender(canvas, follow, splitSlam) {
|
||||
show(hid);
|
||||
|
||||
// --- Nua PHAI: chi SLAM, khong co the gioi ---
|
||||
hid = hide([V3D.meshWorld, V3D.worldMesh]);
|
||||
// Ban do co do cao san (che do doc) thi an ca mat san THE GIOI (z = 0, tu
|
||||
// v3dSetGround): duoi tang tren 1.5 m no hien qua lop ban do trong suot
|
||||
// nhu mot "san" sai cho -- SLAM da tu mang mat san cua no.
|
||||
hid = hide([V3D.meshWorld, V3D.worldMesh, V3D.mapHasFloor ? V3D.floorMesh : null]);
|
||||
r.setViewport(half, 0, w - half, h);
|
||||
r.setScissor(half, 0, w - half, h);
|
||||
r.render(V3D.scene, V3D.camera);
|
||||
|
||||
Reference in New Issue
Block a user