#include "vslam/rtabmap_params.h" #include #include #include #include "vslam/log.h" namespace vslam { namespace { const char* const kProfileNames[] = {"sim", "real"}; // Doc va KIEM khoi `profiles:`. Ba luat de hai moi truong khong bao gio dung // nhau hay lech nhau: // 1. Dung hai khoi `sim` va `real`, du ca hai. // 2. Hai khoi CUNG tap key -- sua mot ben quen ben kia la loi khoi dong, // khong phai mot lan chay sai tren robot. // 3. Key trong profiles KHONG nam o phan chung -- mot key, mot cho. Neu // khong, doc file se khong biet gia tri nao dang chay. void LoadProfiles(const ConfigNode& node, const ConfigNode& block, RtabmapConfig* cfg) { for (const std::string& k : block.Keys()) { if (k != "sim" && k != "real") { throw ConfigError("profiles: khoi la '" + k + "' -- chi co sim va real"); } } std::set keys[2]; for (int i = 0; i < 2; ++i) { const std::string name = kProfileNames[i]; const ConfigNode p = block[name]; if (p.IsNull()) { throw ConfigError("profiles: thieu khoi '" + name + "' -- can ca sim va real"); } SlamProfile sp; for (const std::string& k : p.Keys()) { if (k == "odom_sigma_m" || k == "odom_sigma_rad") { if (!node[k].IsNull()) { throw ConfigError(k + " vua o phan chung vua o profiles." + name + " -- mot key chi duoc o mot cho"); } const double v = p.get(k, 0.0); if (!(v > 0.0)) { throw ConfigError("profiles." + name + "." + k + " phai > 0"); } sp.numbers[k] = v; keys[i].insert(k); } else if (k == "rtabmap") { for (const auto& kv : p["rtabmap"].AsStringMap()) { if (cfg->overrides.count(kv.first) != 0) { throw ConfigError("rtabmap " + kv.first + " vua o khoi rtabmap chung vua o " "profiles." + name + " -- mot key chi duoc o mot cho"); } sp.rtabmap[kv.first] = kv.second; keys[i].insert("rtabmap." + kv.first); } } else { throw ConfigError("profiles." + name + ": khoa la '" + k + "' -- chi co odom_sigma_m, odom_sigma_rad, rtabmap"); } } cfg->profiles[name] = sp; } if (keys[0] != keys[1]) { std::string diff; for (int i = 0; i < 2; ++i) { for (const std::string& k : keys[i]) { if (keys[1 - i].count(k) == 0) { diff += " " + k + " (chi co o " + kProfileNames[i] + ")"; } } } throw ConfigError("profiles: sim va real phai co CUNG tap key, lech:" + diff); } } } // namespace const char* SlamModeName(SlamMode m) { switch (m) { case SlamMode::kMapping: return "mapping"; case SlamMode::kLocalization: return "localization"; } return "?"; } bool OptimizerAvailable(const std::string& strategy) { int s = 1; try { s = std::stoi(strategy); } catch (const std::exception&) { return false; } switch (s) { case 0: return rtabmap::Optimizer::isAvailable(rtabmap::Optimizer::kTypeTORO); case 1: return rtabmap::Optimizer::isAvailable(rtabmap::Optimizer::kTypeG2O); case 2: return rtabmap::Optimizer::isAvailable(rtabmap::Optimizer::kTypeGTSAM); case 3: return rtabmap::Optimizer::isAvailable(rtabmap::Optimizer::kTypeCeres); default: return false; } } std::map BuildRtabmapParams( const RtabmapConfig& cfg, std::vector* unknown) { const rtabmap::ParametersMap defaults = rtabmap::Parameters::getDefaultParameters(); std::map out(defaults.begin(), defaults.end()); // --- 1. Mac dinh cua DU AN NAY, tren mac dinh cua RTAB-Map --- // Day la 26 key trong rtabmap.launch. Chung o day chu khong o YAML vi // chung la HE QUA cua kien truc (2.5D, LiDAR la trong tai) chu khong phai // lua chon van hanh. YAML chi de tinh chinh. const std::pair project_defaults[] = { // 2.5D: pose la SE(2), khong uoc luong 6-DoF. // 0.21 gop Optimizer/Slam2D vao Reg/Force3DoF -- dat ca hai thi no // canh bao va bo qua cai cu, nen chi giu mot. {"Reg/Force3DoF", "true"}, // 1 = ICP. LiDAR la trong tai, ke ca khi tui tu thi giac bat. {"Reg/Strategy", "1"}, {"Icp/PointToPlane", "false"}, // scan 2D, khong co phap tuyen {"Icp/VoxelSize", "0.05"}, {"Icp/MaxCorrespondenceDistance", "0.15"}, {"Icp/Iterations", "30"}, {"Icp/Epsilon", "0.001"}, {"Icp/CorrespondenceRatio", "0.3"}, {"Icp/MaxTranslation", "0.5"}, {"RGBD/NeighborLinkRefining", "true"}, {"RGBD/ProximityBySpace", "true"}, {"RGBD/ProximityPathMaxNeighbors", "10"}, {"RGBD/OptimizeFromGraphEnd", "false"}, {"RGBD/LinearUpdate", "0.1"}, {"RGBD/AngularUpdate", "0.1"}, // ⚠️ BAT BUOC. Mac dinh cua RTAB-Map la `false`. // // Khong co dong nay, RTAB-Map KHONG dung luoi occupancy cho tung node. // Do thi van lon, vong dong van chay, `map_version` van tang -- nhung // `ExportMap()` tra ve mot luoi TOAN O CHUA BIET. Do duoc: quet 25 giay // roi xuat ra 94x21 o voi 0 bi chiem, 0 trong, 1974 chua biet. // // Trieu chung o web UI la mot tam xam dac lon dan ma khong bao gio co // noi dung -- trong het nhu "khung SLAM khong realtime", va do la huong // da di sai mot luc truoc khi tim ra cho nay. // // `map_hospital.db` co luoi vi rtabmap_ros dat tham so nay; wrapper cua // ta phai tu dat. {"RGBD/CreateOccupancyGrid", "true"}, // Grid/Sensor la INT: 0 = laser scan, 1 = depth, 2 = ca hai. // (`"false"` cung ra 0 qua atoi, nhung viet "0" cho dung kieu.) {"Grid/Sensor", "0"}, {"Grid/CellSize", "0.05"}, {"Grid/RangeMax", "12.0"}, {"Grid/RayTracing", "true"}, {"Grid/3D", "false"}, {"Grid/DepthDecimation", "4"}, {"Rtabmap/TimeThr", "0"}, {"Rtabmap/MemoryThr", "0"}, {"Rtabmap/DetectionRate", "1"}, }; for (const auto& kv : project_defaults) out[kv.first] = kv.second; // --- 2. Ghi de tu YAML --- // Kiem TRUOC khi ap: go sai ten key RTAB-Map hien tai khong bao loi gi, no // bi bo qua im lang va he chay bang mac dinh. for (const auto& kv : cfg.overrides) { if (defaults.find(kv.first) == defaults.end()) { if (unknown != nullptr) unknown->push_back(kv.first); continue; } out[kv.first] = kv.second; } // --- 3. Suy dien tu mode va co, GHI DE LEN CA YAML --- // Cac gia tri nay khong duoc phep bi YAML ghi de: chung la he qua truc tiep // cua mode. Mot file YAML dat Mem/IncrementalMemory=true trong khi goi // chay o che do localization se lam hong ban do da quet. out["Mem/IncrementalMemory"] = (cfg.mode == SlamMode::kMapping) ? "true" : "false"; // Localization: nap toan bo node vao working memory ngay tu dau, neu khong // thi vung chua tung den se khong duoc xet toi khi dinh vi lai. out["Mem/InitWMWithAllNodes"] = (cfg.mode == SlamMode::kLocalization) ? "true" : "false"; // Kp/MaxFeatures: -1 = tat han tui tu. >0 = so dac trung moi anh. out["Kp/MaxFeatures"] = cfg.visual_loop ? "400" : "-1"; // --- camera_3d KHONG dung vao Grid/* --- // // Truoc 2026-09-24 camera_3d ep Grid/Sensor=2 + Grid/3D=true de RTAB-Map // dung luoi cuc bo 3D TRONG process(). Do trong mo phong (hospital, 0.7 m/s, // sai so vs ground truth luc dung yen): 2D 0.009 m; luoi 3D 0.06-0.11 m; // luoi 3D tat ray tracing 0.035 m; depth vao nhung luoi 2D 0.024 m. Luoi 3D // trong process() keo pose lech -- co che ben trong RTAB-Map CHUA ro. // // Nen luoi giu 2D, depth van vao SensorData (luu trong DB), va ban do 3D // dung NGOAI process() tu depth cua tung node -- xem voxel_export.h. // Optimizer: dat TUONG MINH, luon luon. // // DB hien co (`map_house.db`) luu Optimizer/Strategy=2 (GTSAM). Tren Jetson // build voi -DWITH_GTSAM=OFF thi neu de RTAB-Map doc lai tham so tu DB no // se chon GTSAM va crash thieu symbol. Vi the: // - init(..., loadDatabaseParameters = FALSE), va // - ghi de Optimizer/Strategy o day, va // - assert sau init (xem RtabmapSlam::Init). // Ba lop, vi mot lop khong du: chinh ham init() la cho de lot. out["Optimizer/Strategy"] = cfg.optimizer_strategy; return out; } RtabmapConfig LoadRtabmapConfig(const ConfigNode& node) { RtabmapConfig cfg; cfg.enabled = node.get("enabled", false); const std::string mode = node.get("mode", "mapping"); cfg.mode = (mode == "localization") ? SlamMode::kLocalization : SlamMode::kMapping; // database_path la require<>: khong co duong dan DB thi khong co gi de // lam, va mot mac dinh im lang se ghi ban do vao cho khong ai tim thay. cfg.database_path = node.require("database_path"); cfg.visual_loop = node.get("visual_loop", false); cfg.camera_3d = node.get("camera_3d", false); cfg.odom_sigma_m = node.get("odom_sigma_m", cfg.odom_sigma_m); cfg.odom_sigma_rad = node.get("odom_sigma_rad", cfg.odom_sigma_rad); cfg.optimizer_strategy = node.get("optimizer_strategy", "1"); // Khoi `rtabmap:` di thang vao ParametersMap, giu nguyen van. Khong kiem // kieu o day -- RTAB-Map nhan moi thu la chuoi. Cai PHAI kiem la TEN key, // va viec do lam trong BuildRtabmapParams(). const ConfigNode block = node["rtabmap"]; if (!block.IsNull()) { for (const auto& kv : block.AsStringMap()) cfg.overrides[kv.first] = kv.second; } // SAU khoi rtabmap chung: luat "mot key, mot cho" can biet phan chung co gi. const ConfigNode profiles = node["profiles"]; if (!profiles.IsNull()) LoadProfiles(node, profiles, &cfg); return cfg; } StatusResponse ApplySlamProfile(const std::string& name, RtabmapConfig* cfg) { const auto it = cfg->profiles.find(name); if (it == cfg->profiles.end()) { return StatusResponse::Error( StatusCode::kInvalidArgument, "slam.yaml: khong co profiles." + name + " -- can khoi `profiles:` voi ca sim " "va real, neu khong sigma odom va ICP chay bang mac dinh cua code"); } for (const auto& kv : it->second.numbers) { if (kv.first == "odom_sigma_m") cfg->odom_sigma_m = kv.second; else if (kv.first == "odom_sigma_rad") cfg->odom_sigma_rad = kv.second; } // Ten key RTAB-Map duoc kiem o BuildRtabmapParams, nhu moi key trong // overrides. for (const auto& kv : it->second.rtabmap) cfg->overrides[kv.first] = kv.second; return StatusResponse::Ok(); } } // namespace vslam