From 89ab2e7868a5da13ef6108599c1c1c9891370bbf Mon Sep 17 00:00:00 2001 From: trung09122002 Date: Mon, 28 Sep 2026 14:40:48 +0700 Subject: [PATCH] slam.yaml: profiles sim/real -- odom sigma va ICP ratio tach theo moi truong --- app/src/runtime.cc | 13 ++ configuration_files/slam.yaml | 42 ++++- .../vslam_slam/include/vslam/rtabmap_params.h | 19 +- libs/vslam_slam/src/rtabmap_params.cc | 94 ++++++++++ test/test_slam.cc | 169 ++++++++++++++++++ tools/vslam_slam_bench.cc | 42 ++++- 6 files changed, 367 insertions(+), 12 deletions(-) diff --git a/app/src/runtime.cc b/app/src/runtime.cc index f681046..4477b09 100644 --- a/app/src/runtime.cc +++ b/app/src/runtime.cc @@ -349,6 +349,19 @@ StatusResponse Runtime::Start() { #ifdef VSLAM_HAVE_SLAM if (cfg_->slam.enabled || opts_.force_slam) { RtabmapConfig sc = cfg_->slam; + // Mo phong va robot that can bo so KHAC nhau -- slam.yaml `profiles`. + // --replay phat lai du lieu robot that nen di theo `real`. + { + const char* profile = (opts_.source == SourceKind::kSim) ? "sim" : "real"; + const StatusResponse pst = ApplySlamProfile(profile, &sc); + if (!pst.ok()) return pst; + std::string keys; + for (const auto& kv : sc.profiles.at(profile).rtabmap) { + keys += ", " + kv.first + "=" + kv.second; + } + VSLAM_INFO("slam: profile " << profile << " -- odom_sigma " << sc.odom_sigma_m + << " m / " << sc.odom_sigma_rad << " rad" << keys); + } if (opts_.have_slam_db) sc.database_path = opts_.slam_db; if (opts_.slam_mode_override == "mapping") sc.mode = SlamMode::kMapping; else if (opts_.slam_mode_override == "localization") sc.mode = SlamMode::kLocalization; diff --git a/configuration_files/slam.yaml b/configuration_files/slam.yaml index 6e49a0c..7afda6a 100644 --- a/configuration_files/slam.yaml +++ b/configuration_files/slam.yaml @@ -62,9 +62,9 @@ optimizer_strategy: "1" # nhieu do/buoc (IMU 10 Hz dem goc hai lan), khai chac thi ICP khong nan # duoc. Chay lai DB that: 0.01/0.005 van xoan; 0.1/0.1 hanh lang thang, # phong vuong. (Mo phong thi 0.01/0.005 tot hon ~30% -- odom mo phong gan -# nhu hoan hao -- nhung robot that la dich.) -odom_sigma_m: 0.1 -odom_sigma_rad: 0.1 +# nhu hoan hao.) +# +# -> GIA TRI nam o `profiles:` cuoi file: mo phong va robot that khac nhau. # ============ THI GIAC ============ # Kp/MaxFeatures: -1 khi false, 400 khi true. @@ -187,11 +187,7 @@ rtabmap: Icp/MaxCorrespondenceDistance: "0.15" Icp/Iterations: "30" Icp/Epsilon: "0.001" - # 0.3 (chinh tren mo phong) loai HET ICP tren LiDAR that: ti le khop that - # chi 0.05-0.1, mot phan vi RTAB-Map chia cho 1440 tia khai trong header - # trong khi chi ~655 diem hop le. DB that 2026-09-25: 0.3 -> 0 lien ket, - # 0.1 -> 18, 0.05 -> 325 (ban do dung). - Icp/CorrespondenceRatio: "0.05" + # Icp/CorrespondenceRatio -> `profiles:` cuoi file (sim va real khac nhau). Icp/MaxTranslation: "0.5" # --- Vong dong va lien ket lan can --- @@ -254,3 +250,33 @@ rtabmap: # ⚠️ Doi lai: DB khong con MOI khung, nen vslam_slam_bench chay lai tu no # se thua hon phien goc. Quet de lam du lieu phan tich thi dat "true". Mem/NotLinkedNodesKept: "false" + +# ============ THEO MOI TRUONG ============ +# Mo phong va robot that can bo so KHAC nhau cho cung mot key. Chon TU DONG +# theo nguon: --sim -> sim; --live / --replay -> real (replay la du lieu +# robot that). +# +# Node TU CHOI khoi dong neu: +# - thieu mot trong hai khoi, hoac co khoi thu ba; +# - hai khoi KHONG cung tap key (sua mot ben quen ben kia); +# - mot key vua o day vua o phan chung phia tren (mot key, mot cho); +# - co key ngoai odom_sigma_m, odom_sigma_rad, rtabmap. +profiles: + sim: + # Odom mo phong gan nhu hoan hao, scan du 1440 tia. hospital_nam + # 2026-09-25 (run_N1): sai so vs ground truth 3-5 cm; cung ban do voi + # bo so `real` 2026-09-28: 8-12 cm sau 5 phut. + odom_sigma_m: 0.01 + odom_sigma_rad: 0.005 + rtabmap: + Icp/CorrespondenceRatio: "0.3" + real: + # 2026-09-25: 0.01/0.005 lam ban do xoan (odom that sai nhieu do/buoc); + # 0.1/0.1 hanh lang thang, phong vuong. + odom_sigma_m: 0.1 + odom_sigma_rad: 0.1 + rtabmap: + # Ti le khop that chi 0.05-0.1: RTAB-Map chia cho 1440 tia khai trong + # header trong khi LakiBeam chi ~655 diem hop le. DB that 2026-09-25: + # 0.3 -> 0 lien ket, 0.1 -> 18, 0.05 -> 325 (ban do dung). + Icp/CorrespondenceRatio: "0.05" diff --git a/libs/vslam_slam/include/vslam/rtabmap_params.h b/libs/vslam_slam/include/vslam/rtabmap_params.h index 9286e0a..9d4eeaf 100644 --- a/libs/vslam_slam/include/vslam/rtabmap_params.h +++ b/libs/vslam_slam/include/vslam/rtabmap_params.h @@ -23,6 +23,13 @@ namespace vslam { +// Ghi de theo MOI TRUONG -- khoi `profiles:` cua slam.yaml. Chi chua nhung +// key KHAC nhau giua mo phong va robot that; phan con lai dung chung. +struct SlamProfile { + std::map numbers; // odom_sigma_m, odom_sigma_rad + std::map rtabmap; // key RTAB-Map +}; + struct RtabmapConfig { // TAT mac dinh: tren may chua build RTAB-Map thi node van phai chay duoc // day du phan con lai. @@ -50,6 +57,11 @@ struct RtabmapConfig { // Cac key RTAB-Map doc tu khoi `rtabmap:` cua slam.yaml. Ghi de len mac // dinh, va bi ghi de boi cac suy dien tu mode/visual_loop o tren. std::map overrides; + + // "sim" va "real" tu khoi `profiles:`. CHUA ap: LoadRtabmapConfig chi doc + // va kiem, ben goi chon mot cai bang ApplySlamProfile() vi chi no biet dang + // chay mo phong hay robot that. + std::map profiles; }; // Tra ve tap tham so day du. `unknown` nhan ten cac key khong co trong bang @@ -58,9 +70,14 @@ struct RtabmapConfig { std::map BuildRtabmapParams( const RtabmapConfig& cfg, std::vector* unknown); -// Doc khoi `rtabmap:` + cac key cap cao tu ConfigNode. +// Doc khoi `rtabmap:` + cac key cap cao tu ConfigNode. Nem neu khoi +// `profiles:` sai luat (xem slam.yaml). RtabmapConfig LoadRtabmapConfig(const ConfigNode& node); +// Ap `profiles.` len cfg: sigma odom va cac key RTAB-Map cua profile. +// Loi neu khong co profile do -- KHONG lang le chay bang mac dinh cua struct. +StatusResponse ApplySlamProfile(const std::string& name, RtabmapConfig* cfg); + // Tra ve true neu RTAB-Map duoc build kem optimizer nay. bool OptimizerAvailable(const std::string& strategy); diff --git a/libs/vslam_slam/src/rtabmap_params.cc b/libs/vslam_slam/src/rtabmap_params.cc index 9651719..40d14e2 100644 --- a/libs/vslam_slam/src/rtabmap_params.cc +++ b/libs/vslam_slam/src/rtabmap_params.cc @@ -3,10 +3,82 @@ #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"; @@ -166,7 +238,29 @@ RtabmapConfig LoadRtabmapConfig(const ConfigNode& node) { 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 diff --git a/test/test_slam.cc b/test/test_slam.cc index d3535db..779b4d9 100644 --- a/test/test_slam.cc +++ b/test/test_slam.cc @@ -11,6 +11,7 @@ #include #include +#include #include #include "vslam/geometry.h" @@ -144,6 +145,174 @@ TEST(RtabmapParams, G2oPhaiCoMat) { EXPECT_FALSE(OptimizerAvailable("9")); } +// ==================== slam.yaml profiles (sim / real) ==================== +// +// Mo phong va robot that can bo so KHAC nhau (2026-09-28: bo so robot that +// lam mo phong lech 8-12 cm thay vi 3-5 cm). Cac test duoi day khoa luat +// "hai ben khong dung nhau". + +namespace { + +const char* kProfilesOk = + "database_path: x.db\n" + "rtabmap:\n" + " Icp/VoxelSize: \"0.05\"\n" + "profiles:\n" + " sim:\n" + " odom_sigma_m: 0.01\n" + " odom_sigma_rad: 0.005\n" + " rtabmap:\n" + " Icp/CorrespondenceRatio: \"0.3\"\n" + " real:\n" + " odom_sigma_m: 0.1\n" + " odom_sigma_rad: 0.1\n" + " rtabmap:\n" + " Icp/CorrespondenceRatio: \"0.05\"\n"; + +RtabmapConfig LoadYaml(const std::string& yaml) { + return LoadRtabmapConfig(ConfigNode::LoadString(yaml)); +} + +} // namespace + +TEST(SlamProfiles, SimVaRealRaDungGiaTriRieng) { + const RtabmapConfig base = LoadYaml(kProfilesOk); + + RtabmapConfig sim = base; + ASSERT_TRUE(ApplySlamProfile("sim", &sim).ok()); + EXPECT_DOUBLE_EQ(sim.odom_sigma_m, 0.01); + EXPECT_DOUBLE_EQ(sim.odom_sigma_rad, 0.005); + EXPECT_EQ(sim.overrides.at("Icp/CorrespondenceRatio"), "0.3"); + + RtabmapConfig real = base; + ASSERT_TRUE(ApplySlamProfile("real", &real).ok()); + EXPECT_DOUBLE_EQ(real.odom_sigma_m, 0.1); + EXPECT_DOUBLE_EQ(real.odom_sigma_rad, 0.1); + EXPECT_EQ(real.overrides.at("Icp/CorrespondenceRatio"), "0.05"); + + // Phan chung giu nguyen o ca hai. + EXPECT_EQ(sim.overrides.at("Icp/VoxelSize"), "0.05"); + EXPECT_EQ(real.overrides.at("Icp/VoxelSize"), "0.05"); + + // Key cua profile di qua CUNG phep kiem ten key nhu moi key khac. + std::vector unknown; + EXPECT_EQ(BuildRtabmapParams(sim, &unknown).at("Icp/CorrespondenceRatio"), "0.3"); + EXPECT_TRUE(unknown.empty()); +} + +TEST(SlamProfiles, ThieuKeyOMotBenLaLoi) { + // Sua `sim` ma quen `real`: phai la loi khoi dong, khong phai mot lan chay + // robot that bang so cua mo phong. + const std::string yaml = + "database_path: x.db\n" + "profiles:\n" + " sim:\n" + " odom_sigma_m: 0.01\n" + " odom_sigma_rad: 0.005\n" + " real:\n" + " odom_sigma_m: 0.1\n"; + EXPECT_THROW(LoadYaml(yaml), std::exception); + + const std::string rt = + "database_path: x.db\n" + "profiles:\n" + " sim:\n" + " rtabmap:\n" + " Icp/CorrespondenceRatio: \"0.3\"\n" + " real:\n" + " rtabmap:\n" + " Icp/VoxelSize: \"0.05\"\n"; + EXPECT_THROW(LoadYaml(rt), std::exception); +} + +TEST(SlamProfiles, KeyVuaOChungVuaOProfileLaLoi) { + // Mot key, mot cho: neu khong, doc file se khong biet gia tri nao dang chay. + const std::string sigma = + "database_path: x.db\n" + "odom_sigma_m: 0.05\n" + "profiles:\n" + " sim:\n" + " odom_sigma_m: 0.01\n" + " real:\n" + " odom_sigma_m: 0.1\n"; + EXPECT_THROW(LoadYaml(sigma), std::exception); + + const std::string rt = + "database_path: x.db\n" + "rtabmap:\n" + " Icp/CorrespondenceRatio: \"0.2\"\n" + "profiles:\n" + " sim:\n" + " rtabmap:\n" + " Icp/CorrespondenceRatio: \"0.3\"\n" + " real:\n" + " rtabmap:\n" + " Icp/CorrespondenceRatio: \"0.05\"\n"; + EXPECT_THROW(LoadYaml(rt), std::exception); +} + +TEST(SlamProfiles, TenKhoiHoacKeyLaLaLoi) { + const std::string khoi = + "database_path: x.db\n" + "profiles:\n" + " simulation:\n" + " odom_sigma_m: 0.01\n" + " real:\n" + " odom_sigma_m: 0.1\n"; + EXPECT_THROW(LoadYaml(khoi), std::exception); + + const std::string thieu = + "database_path: x.db\n" + "profiles:\n" + " sim:\n" + " odom_sigma_m: 0.01\n"; + EXPECT_THROW(LoadYaml(thieu), std::exception); + + const std::string key = + "database_path: x.db\n" + "profiles:\n" + " sim:\n" + " mode: mapping\n" + " real:\n" + " mode: mapping\n"; + EXPECT_THROW(LoadYaml(key), std::exception); + + const std::string am = + "database_path: x.db\n" + "profiles:\n" + " sim:\n" + " odom_sigma_m: 0\n" + " real:\n" + " odom_sigma_m: 0.1\n"; + EXPECT_THROW(LoadYaml(am), std::exception); +} + +TEST(SlamProfiles, KhongCoProfilesThiApLaLoi) { + // KHONG lang le chay bang mac dinh cua struct: tren robot that do la + // 0.01/0.005 -- bo so lam ban do xoan. + RtabmapConfig cfg = LoadYaml("database_path: x.db\n"); + EXPECT_TRUE(cfg.profiles.empty()); + EXPECT_FALSE(ApplySlamProfile("real", &cfg).ok()); +} + +TEST(SlamProfiles, SlamYamlCuaRepoApDuocCaHai) { + // ctest dat VSLAM_CONFIG_SRC_DIR = configuration_files. Ai sua slam.yaml sai + // luat thi test nay do ngay, khong doi toi luc chay tren robot. + const char* dir = std::getenv("VSLAM_CONFIG_SRC_DIR"); + if (dir == nullptr) GTEST_SKIP() << "khong co VSLAM_CONFIG_SRC_DIR"; + const RtabmapConfig base = + LoadRtabmapConfig(ConfigNode::LoadFile(std::string(dir) + "/slam.yaml")); + for (const char* name : {"sim", "real"}) { + RtabmapConfig c = base; + ASSERT_TRUE(ApplySlamProfile(name, &c).ok()) << name; + EXPECT_GT(c.odom_sigma_m, 0.0) << name; + EXPECT_GT(c.odom_sigma_rad, 0.0) << name; + std::vector unknown; + BuildRtabmapParams(c, &unknown); + EXPECT_TRUE(unknown.empty()) << name << ": key RTAB-Map sai ten"; + } +} + // ==================== sensor_data_builder ==================== TEST(SensorDataBuilder, Pose2DDiVaVeKhongMatGi) { diff --git a/tools/vslam_slam_bench.cc b/tools/vslam_slam_bench.cc index 3d7b1c6..1bb6b1f 100644 --- a/tools/vslam_slam_bench.cc +++ b/tools/vslam_slam_bench.cc @@ -34,6 +34,7 @@ #include #include +#include "vslam/config.h" #include "vslam/geometry.h" #include "vslam/occupancy_export.h" #include "vslam/occupancy_map_io.h" @@ -207,6 +208,10 @@ int main(int argc, char** argv) { // (0.1 m / 0.1 rad) -- dung cai runtime dang gui. double odom_sigma_lin = -1.0, odom_sigma_ang = -1.0; std::map extra; + // --profile sim|real: nap slam.yaml THAT (--config DIR, mac dinh + // $VSLAM_CONFIG_DIR) va ap profile do. Khong co thi giu cach cu: mac dinh + // cua code + --param. + std::string profile, config_dir; for (int i = 1; i < argc; ++i) { const std::string a = argv[i]; @@ -222,6 +227,8 @@ int main(int argc, char** argv) { else if (a == "--with-images") with_images = true; else if (a == "--ref-db") ref_db = next(); else if (a == "--ref") ref_kind = next(); + else if (a == "--profile") profile = next(); + else if (a == "--config") config_dir = next(); else if (a == "--odom-sigma") { // "LIN_M,ANG_RAD" const std::string v = next(); const size_t c = v.find(','); @@ -245,7 +252,10 @@ int main(int argc, char** argv) { std::printf( "cach dung: %s --db X.db [--localize] [--kidnap M,DEG]\n" " [--limit N] [--optimizer 0|1|2|3] [--export-map out.pgm]\n" - " [--ref opt|odom] [--odom-sigma LIN_M,ANG_RAD]\n", + " [--ref opt|odom] [--odom-sigma LIN_M,ANG_RAD]\n" + " [--profile sim|real [--config DIR]] nap slam.yaml that\n" + " (DIR mac dinh $VSLAM_CONFIG_DIR) va ap profile do;\n" + " --param / --odom-sigma van ghi de len tren\n", argv[0]); return 0; } @@ -398,7 +408,31 @@ int main(int argc, char** argv) { cfg.database_path = work_db; // rong = DB trong RAM (che do mapping) cfg.optimizer_strategy = optimizer; cfg.visual_loop = visual_loop; - cfg.overrides = extra; + if (!profile.empty()) { + // Chay DUNG bo so ma node se chay trong moi truong do, thay vi mac dinh + // cua struct (ma phai go lai tung key bang --param). + if (config_dir.empty()) { + const char* env = std::getenv("VSLAM_CONFIG_DIR"); + config_dir = env != nullptr ? env : "configuration_files"; + } + vslam::RtabmapConfig y; + try { + y = vslam::LoadRtabmapConfig(vslam::ConfigNode::LoadFile(config_dir + "/slam.yaml")); + } catch (const std::exception& e) { + std::fprintf(stderr, "%s/slam.yaml: %s\n", config_dir.c_str(), e.what()); + return 1; + } + const vslam::StatusResponse pst = vslam::ApplySlamProfile(profile, &y); + if (!pst.ok()) { + std::fprintf(stderr, "%s\n", pst.message.c_str()); + return 1; + } + cfg.odom_sigma_m = y.odom_sigma_m; + cfg.odom_sigma_rad = y.odom_sigma_rad; + cfg.overrides = y.overrides; + std::printf("%s/slam.yaml, profile %s\n", config_dir.c_str(), profile.c_str()); + } + for (const auto& kv : extra) cfg.overrides[kv.first] = kv.second; for (const auto& kv : extra) { std::printf("param: %s = %s\n", kv.first.c_str(), kv.second.c_str()); } @@ -492,7 +526,9 @@ int main(int argc, char** argv) { odom_sigma_lin > 0.0 ? (std::to_string(odom_sigma_lin) + " m, " + std::to_string(odom_sigma_ang) + " rad").c_str() - : ("mac dinh slam.yaml (" + std::to_string(cfg.odom_sigma_m) + " m, " + + : ((profile.empty() ? std::string("mac dinh cua code (") + : "slam.yaml profile " + profile + " (") + + std::to_string(cfg.odom_sigma_m) + " m, " + std::to_string(cfg.odom_sigma_rad) + " rad)").c_str()); std::fflush(stdout);