slam.yaml: profiles sim/real -- odom sigma va ICP ratio tach theo moi truong

This commit is contained in:
trung09122002
2026-09-28 14:40:48 +07:00
parent c8c08223ed
commit 89ab2e7868
6 changed files with 367 additions and 12 deletions

View File

@@ -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<std::string, double> numbers; // odom_sigma_m, odom_sigma_rad
std::map<std::string, std::string> 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<std::string, std::string> 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<std::string, SlamProfile> 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<std::string, std::string> BuildRtabmapParams(
const RtabmapConfig& cfg, std::vector<std::string>* 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.<name>` 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);

View File

@@ -3,10 +3,82 @@
#include <rtabmap/core/Optimizer.h>
#include <rtabmap/core/Parameters.h>
#include <set>
#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<std::string> 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<double>(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