slam: pose odometry DUNG LUC QUET cho SLAM (noi suy lich su EKF -- luc quay tai cho, ghep scan voi pose hien tai lam lech 0.7 do va xoay ca ban do) + vong dong xa (slam.yaml far_loops, mac dinh bat: node vua tao vs node cu cach >= 100 buoc trong 1 m, ICP xac nhan, addLink) -- sim vong doc 52 m: cuoi vong 17-22 cm -> 1.5 cm

This commit is contained in:
trung09122002
2026-10-04 01:36:35 +07:00
parent f064dcf3ec
commit 6d552e231e
8 changed files with 232 additions and 0 deletions

View File

@@ -116,6 +116,19 @@ class RtabmapSlam {
// O 2D noi camera thay mat san trong lan ExportVoxels cuoi (che do doc).
const std::vector<uint64_t>& floor_cells() const;
// Vong dong XA: node vua tao vs node CU (cach >= min_graph_gap buoc trong do
// thi) nam trong ban kinh `radius_m` theo do thi da toi uu -- tuc lan di
// TRUOC qua cung cho. Dang ky ICP giua hai scan (tham so Icp/* cua chinh ban
// do); nhan neu ICP dat va chinh so voi do thi < max_corr; them lien ket.
//
// KHONG ghep node cung mot luot di: lien ket ICP trong hanh lang khong rang
// buoc doc truc va keo ban do truot (do trong sim 04/10: detectMoreLoopClosures
// cua RTAB-Map them hang tram lien ket nhu vay, sai 12 cm -> 33 cm).
// Tra 1 = da them, 0 = khong co ung vien / ICP khong dat, -1 = loi. `detail`:
// ly do. CHI luong slam, CHI che do mapping.
int TryFarLoopClosure(double radius_m, int min_graph_gap, double max_corr_m,
double max_corr_rad, std::string* detail);
StatusResponse Close(bool save_database);
const SlamStats& stats() const;

View File

@@ -3,12 +3,14 @@
#include <rtabmap/core/Memory.h>
#include <rtabmap/core/Optimizer.h>
#include <rtabmap/core/Parameters.h>
#include <rtabmap/core/RegistrationIcp.h>
#include <rtabmap/core/Rtabmap.h>
#include <rtabmap/core/SensorData.h>
#include <rtabmap/core/Statistics.h>
#include <rtabmap/utilite/ULogger.h>
#include <algorithm>
#include <cmath>
#include <chrono>
#include <mutex>
#include <set>
@@ -416,6 +418,92 @@ void RtabmapSlam::SetUseHeights(bool on) { impl_->use_heights = on; }
const std::vector<uint64_t>& RtabmapSlam::floor_cells() const { return impl_->floor_cells; }
int RtabmapSlam::TryFarLoopClosure(double radius_m, int min_graph_gap, double max_corr_m,
double max_corr_rad, std::string* detail) {
if (!impl_->inited || impl_->cfg.mode != SlamMode::kMapping) return 0;
const int cur = impl_->rtabmap.getLastLocationId();
const std::map<int, rtabmap::Transform>& poses = impl_->rtabmap.getLocalOptimizedPoses();
const auto itc = poses.find(cur);
if (cur <= 0 || itc == poses.end() || itc->second.isNull()) return 0;
// Ung vien: node CU (thu tu id = thu tu thoi gian), gan nhat trong ban kinh.
std::vector<int> ids;
ids.reserve(poses.size());
for (const auto& kv : poses) ids.push_back(kv.first);
const long ci = std::lower_bound(ids.begin(), ids.end(), cur) - ids.begin();
int cand = 0;
double best = radius_m;
for (long i = 0; i + min_graph_gap < ci; ++i) {
const rtabmap::Transform& p = poses.at(ids[static_cast<size_t>(i)]);
if (p.isNull()) continue;
const double d = std::hypot(p.x() - itc->second.x(), p.y() - itc->second.y());
if (d <= best) {
best = d;
cand = ids[static_cast<size_t>(i)];
}
}
if (cand == 0) return 0;
const rtabmap::Memory* mem = impl_->rtabmap.getMemory();
if (mem == nullptr) return -1;
const std::multimap<int, rtabmap::Link> links = mem->getLinks(cur);
if (links.find(cand) != links.end()) return 0; // da noi roi
try {
rtabmap::SensorData a = mem->getNodeData(cur, false, true, false, false);
rtabmap::SensorData b = mem->getNodeData(cand, false, true, false, false);
a.uncompressData();
b.uncompressData();
if (a.laserScanRaw().isEmpty() || b.laserScanRaw().isEmpty()) {
if (detail != nullptr) *detail = "node khong co scan";
return 0;
}
const rtabmap::Transform guess = itc->second.inverse() * poses.at(cand);
rtabmap::RegistrationIcp reg(impl_->rtabmap.getParameters());
rtabmap::RegistrationInfo info;
const rtabmap::Transform t = reg.computeTransformation(a, b, guess, &info);
if (t.isNull()) {
if (detail != nullptr) *detail = "ICP tu choi: " + info.rejectedMsg;
return 0;
}
const rtabmap::Transform corr = guess.inverse() * t;
const double ct = std::hypot(corr.x(), corr.y());
const double cr = std::fabs(corr.theta());
if (ct > max_corr_m || cr > max_corr_rad) {
if (detail != nullptr) {
*detail = "chinh qua lon (" + std::to_string(ct) + " m, " + std::to_string(cr) + " rad)";
}
return 0;
}
// Thong tin = nghich dao hiep phuong sai ICP, chan duoi phuong sai de
// khong co truc nao "chac tuyet doi".
cv::Mat inf = cv::Mat::eye(6, 6, CV_64FC1);
for (int k = 0; k < 6; ++k) {
const double v = info.covariance.empty() ? 1e-4 : info.covariance.at<double>(k, k);
inf.at<double>(k, k) = 1.0 / std::max(v, 1e-6);
}
if (!impl_->rtabmap.addLink(rtabmap::Link(cur, cand, rtabmap::Link::kUserClosure, t, inf))) {
if (detail != nullptr) *detail = "addLink tu choi";
return 0;
}
if (detail != nullptr) {
*detail = "noi " + std::to_string(cur) + " -> " + std::to_string(cand) + " (cach " +
std::to_string(ci - (std::lower_bound(ids.begin(), ids.end(), cand) - ids.begin())) +
" node, " + std::to_string(best) + " m), chinh " + std::to_string(ct) + " m / " +
std::to_string(cr * 180.0 / M_PI) + " do, ti le khop " +
std::to_string(info.icpInliersRatio);
}
} catch (const std::exception& e) {
if (detail != nullptr) *detail = std::string("nem: ") + e.what();
return -1;
}
std::lock_guard<std::mutex> lk(impl_->out_mu);
impl_->map_correction = FromRtabmap(impl_->rtabmap.getMapCorrection());
impl_->map_version++;
impl_->stats.loop_closures++;
return 1;
}
const FloorModel& RtabmapSlam::floor_model() const { return impl_->floor; }
StatusResponse RtabmapSlam::UpdateHeights(const ZGraphConfig& cfg, size_t* nodes_out) {