This commit is contained in:
trung09122002
2026-09-23 13:36:48 +07:00
parent dbcbf84dc3
commit 0a1b1b13f1
38 changed files with 2331 additions and 138 deletions

View File

@@ -15,6 +15,9 @@
#include <string>
#include "vslam/vslam_common.h" // StatusResponse. Khong vong:
// vslam_common.h chi keo unix_time.h
namespace vslam {
// --- Duong dan mac dinh ----------------------------------------------------
@@ -93,6 +96,30 @@ std::string RecordDir();
// map_hospital.db ma khong phai copy 3.8 GB.
std::string CorpusDir();
// --- Duong dan ban do: giai tuong doi + no {stamp} -------------------------
// Giai database_path cua SLAM:
// "" -> "" (DB trong RAM -- giu nguyen nghia)
// "/abs/x.db" -> "/abs/x.db" (tuyet doi thi khong dung toi)
// "x.db" -> MapDir() + "/x.db" (tuong doi = nam trong project)
//
// `{stamp}` o bat ky dau bi thay bang dau thoi gian DIA PHUONG %Y%m%d_%H%M%S,
// tinh MOT lan cho ca chuoi (hai lan xuat hien ra cung mot gia tri).
//
// ⚠️ Dung dong ho TUONG (std::time), KHONG phai Clock cua he: replay 100x
// se duc ra ten trung nhau neu lay dong ho mo phong.
//
// Khong gian `~`: shell da lam roi, lam lai la lech voi doi so trong nhay.
// Idempotent: f(f(x)) == f(x).
std::string ResolveMapPath(const std::string& raw);
// mkdir -p cho thu muc CHA cua `path`. Ok neu da co.
//
// Dung mkdir() POSIX chu khong phai <filesystem>: tren GCC 9 (xem
// CMakeLists.txt:76-88) <filesystem> can link them -lstdc++fs, ma vslam_common
// la dep cua MOI target trong cay nay.
StatusResponse EnsureParentDir(const std::string& path);
} // namespace vslam
#endif // VSLAM_NODE_CONSTANTS_H

View File

@@ -151,6 +151,46 @@ struct OccupancyGrid {
std::vector<int8_t> data; // size = width * height
};
// Ban do VOXEL: dang thua, chi nhung o BI CHIEM.
//
// Nam o vslam_common chu khong o vslam_slam vi `vslam_web` phai ma hoa duoc
// kieu nay ma no chi phu thuoc vslam_common -- y het ly do OccupancyGrid nam
// o day.
//
// Chi so la SO NGUYEN CO DAU quanh goc (0,0,0) cua frame map:
// index = floor(toa_do / resolution)
// Goc ghim vinh vien tai goc cua map frame, khong troi theo ban do, nho vay
// hai lan xuat khac nhau so sanh truc tiep duoc bang phep tru tap hop.
// int16 o 0.10 m = +-3276 m, thua cho moi toa nha trong nha.
//
// Ten la `VoxelIndex` chu khong phai `VoxelKey`: `vslam::VoxelKey` DA TON TAI
// la mot HAM trong depth_filter.h. C++ cho phep ham va lop trung ten trong
// cung mot scope, nhung ham CHE ten lop -- `std::vector<VoxelKey>` khi do bat
// phai cai ham va bao "expected a type". Da mac dung loi nay mot lan.
struct VoxelIndex {
int16_t x = 0;
int16_t y = 0;
int16_t z = 0;
};
struct VoxelGrid {
Header header; // frame_id = kMapFrame
float resolution = 0.10f; // kich thuoc o PHAT (khac Grid/CellSize cua SLAM)
double origin_x = 0.0; // luon 0 hien tai; giu tren day de doi sau nay
double origin_y = 0.0;
double origin_z = 0.0;
// Tang khi tap cu KHONG con dung nua (do thi vua duoc toi uu lai sau mot
// vong dong, hoac doi resolution). Client thay epoch la se tai lai tu dau
// thay vi ap delta.
uint32_t epoch = 0;
bool truncated = false; // cham tran max_cells -> ban do bi cat bot
std::vector<VoxelIndex> cells; // DA SAP XEP theo khoa goi, khong trung
};
using VoxelGridPtr = std::shared_ptr<const VoxelGrid>;
// ---------------------------------------------------------------------------
// sensor_msgs
// ---------------------------------------------------------------------------

View File

@@ -1,6 +1,11 @@
#include "vslam/node_constants.h"
#include <sys/stat.h>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <ctime>
namespace vslam {
namespace {
@@ -18,6 +23,19 @@ std::string Join(const std::string& dir, const std::string& rel) {
return dir + "/" + rel;
}
constexpr char kStampToken[] = "{stamp}";
// Dau thoi gian DIA PHUONG. Sap xep duoc theo thu tu chu cai, va nguoi van
// hanh doc no bang `ls` tren chinh con robot -- do la ly do khong dung UTC.
std::string NowStamp() {
const std::time_t t = std::time(nullptr);
std::tm tm {};
localtime_r(&t, &tm);
char buf[32];
std::strftime(buf, sizeof(buf), "%Y%m%d_%H%M%S", &tm);
return std::string(buf);
}
} // namespace
std::string ConfigDir() { return EnvOr(kConfigDirEnv, kConfigurationDirectory); }
@@ -40,4 +58,58 @@ std::string CorpusDir() {
return MapDir();
}
std::string ResolveMapPath(const std::string& raw) {
if (raw.empty()) return raw; // DB trong RAM -- giu nguyen
std::string p = raw;
if (p.find(kStampToken) != std::string::npos) {
// MOT dau thoi gian cho ca chuoi: hai lan xuat hien phai ra cung gia tri,
// khong thi ten file va ten thu muc lech nhau mot giay.
const std::string stamp = NowStamp();
std::string out;
out.reserve(p.size() + 16);
size_t i = 0;
for (;;) {
const size_t k = p.find(kStampToken, i);
if (k == std::string::npos) {
out.append(p, i, std::string::npos);
break;
}
out.append(p, i, k - i);
out += stamp;
i = k + sizeof(kStampToken) - 1;
}
p.swap(out);
}
// Cung khuon mau voi dcf_path o app_config.cc: tuyet doi thi dung nguyen,
// tuong doi thi giai theo thu muc cua chinh loai file do.
if (p.front() == '/') return p;
return Join(MapDir(), p);
}
StatusResponse EnsureParentDir(const std::string& path) {
const size_t slash = path.find_last_of('/');
// Khong co '/' = file o thu muc hien tai; slash == 0 = ngay duoi root.
if (slash == std::string::npos || slash == 0) return StatusResponse::Ok();
const std::string dir = path.substr(0, slash);
for (size_t i = 1; i <= dir.size(); ++i) {
if (i != dir.size() && dir[i] != '/') continue;
const std::string part = dir.substr(0, i);
if (::mkdir(part.c_str(), 0775) == 0) continue;
if (errno == EEXIST) {
// EEXIST chua phai tin tot: co the la mot FILE thuong trung ten.
struct stat st {};
if (::stat(part.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) continue;
return StatusResponse::Error(StatusCode::kInvalidArgument,
"khong phai thu muc: " + part);
}
return StatusResponse::Error(
StatusCode::kUnavailable,
"khong tao duoc thu muc " + part + ": " + std::strerror(errno));
}
return StatusResponse::Ok();
}
} // namespace vslam