tools: vslam_record kich ban doc (slope_loop, slope_lane5..30) lai theo waypoint khong qua FSM, --gl, --cam-decimate, nhieu IMU vao header; vslam_vsr_export doi .vsr sang dataset DM-VIO (mau IMU dung gio anh, T_cam_imu tu robot.yaml, su that IMU/camera/base, ha tan so/lech gio/nhieu IMU luc xuat)

This commit is contained in:
trung09122002
2026-10-03 19:37:29 +07:00
parent f9fd18b5aa
commit 213b5f23a6
3 changed files with 628 additions and 6 deletions

View File

@@ -13,10 +13,15 @@
// vat can cua cliff_detect.
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include "vslam/driver_interfaces.h"
@@ -24,6 +29,9 @@
#include "vslam/record_io.h"
#include "vslam/robot_model.h"
#include "vslam/sim_source.h"
#ifdef VSLAM_HAVE_SIM_GL
#include "vslam/sim_gl_renderer.h"
#endif
namespace {
@@ -53,8 +61,125 @@ struct Scenario {
double start_x = 0.0, start_y = 0.0, start_yaw = 0.0;
};
// --- Kich ban DOC ---
// Chay trong file .world (tuong doi VSLAM_RESOURCE_DIR), lai theo WAYPOINT
// bang bo bam duong -- KHONG qua lop an toan, nen doc 30 do van co du lieu.
// Luon dung hinh hoc robot THAT, luon ghi su that 6-DoF, anh IR neu --gl.
// KHONG nam trong `--scenario all`: bo hoi quy chi dung kich ban phang.
//
// Tach rieng khoi `Scenario` (tra theo ten): them truong vao `Scenario` lam
// moi dong khoi tao cu thieu truong va -Wextra canh bao.
struct SlopeInfo {
const char* world = nullptr;
std::vector<std::pair<double, double>> waypoints;
double cruise = 0.30; // m/s doc mat san
};
std::vector<std::pair<std::string, SlopeInfo>>& SlopeTable() {
static std::vector<std::pair<std::string, SlopeInfo>> t;
return t;
}
const SlopeInfo* FindSlope(const char* name) {
for (const auto& e : SlopeTable()) {
if (e.first == name) return &e.second;
}
return nullptr;
}
// Kich ban DOC trong worlds/slope_test.world (tools/gen_slope_world.py): tam
// lan x = 1.2 / 3.8 / 6.4 / 9.0 / 11.6 cho doc 5 / 10 / 15 / 20 / 30 do, chan
// doc y = 8, san cao 1.5 m tu y = 25.5.
void AddSlopeScenarios(std::vector<Scenario>* v) {
const char* kWorld = "worlds/slope_test.world";
SlopeTable().clear();
{
Scenario s{};
s.name = "slope_loop";
s.what = "doc: len lan 15 do, qua san tren, xuong lan 20 do, ve cho cu";
s.camera = true;
s.camera_hz = 20.0;
s.start_x = 6.4;
s.start_y = 4.0;
s.start_yaw = M_PI / 2.0;
v->push_back(s);
SlopeInfo si;
si.world = kWorld;
si.waypoints = {{6.4, 7.0}, {6.4, 24.5}, {6.4, 27.6}, {9.0, 27.6},
{9.0, 24.5}, {9.0, 6.6}, {6.4, 6.0}, {6.4, 4.0}};
SlopeTable().emplace_back(s.name, si);
}
// Quay dau o y = 27.5 (san tren, 2 m qua dinh lan): doc 5 do dai 17.15 m,
// dinh o y = 25.15, nen quay som hon (24.5) la con tren doc -- thieu doan
// dinh doc, cho kho nhat cua VO. San tren trong tu y 25.5 toi cot o 29.3.
const struct { const char* name; const char* what; double x; } lanes[] = {
{"slope_lane5", "doc 5 do: len het lan, qua dinh, quay xuong", 1.2},
{"slope_lane10", "doc 10 do: len het lan, qua dinh, quay xuong", 3.8},
{"slope_lane15", "doc 15 do: len het lan, qua dinh, quay xuong", 6.4},
{"slope_lane20", "doc 20 do: len het lan, qua dinh, quay xuong", 9.0},
{"slope_lane30", "doc 30 do: len het lan, qua dinh, quay xuong", 11.6},
};
for (const auto& l : lanes) {
Scenario s{};
s.name = l.name;
s.what = l.what;
s.camera = true;
s.camera_hz = 20.0;
s.start_x = l.x;
s.start_y = 5.0;
s.start_yaw = M_PI / 2.0;
v->push_back(s);
SlopeInfo si;
si.world = kWorld;
si.waypoints = {{l.x, 27.5}, {l.x, 5.5}};
SlopeTable().emplace_back(s.name, si);
}
}
// Bam waypoint bang SU THAT (nhu nguoi lai nhin thay robot): quay ve phia
// diem ke, tien khi da gan dung huong. Lenh phat lai moi lat 50 ms (watchdog
// cua base), `after_slice` chay sau moi lat (ham hang doi ghi).
bool DriveWaypoints(vslam::SimSource* sim, const std::vector<std::pair<double, double>>& wps,
double cruise, const std::function<void()>& after_slice,
double* sim_s) {
constexpr double kSlice = 0.05;
constexpr double kMaxS = 900.0;
size_t k = 0;
double t = 0.0;
while (k < wps.size() && t < kMaxS) {
const vslam::SimPose3 p = sim->ground_truth_3d();
const double dx = wps[k].first - p.x, dy = wps[k].second - p.y;
if (std::hypot(dx, dy) < 0.25) {
++k;
continue;
}
double err = std::atan2(dy, dx) - p.yaw;
err = std::atan2(std::sin(err), std::cos(err));
const double wz = std::max(-0.6, std::min(0.6, 1.5 * err));
const double vx = std::fabs(err) < 0.2 ? cruise : (std::fabs(err) < 0.6 ? 0.3 * cruise : 0.0);
sim->base()->SetVelocity(vx, wz);
sim->RunFor(kSlice);
t += kSlice;
after_slice();
}
// Dung han nua giay: khung cuoi co robot dung yen.
for (int i = 0; i < 10; ++i) {
sim->base()->SetVelocity(0.0, 0.0);
sim->RunFor(kSlice);
t += kSlice;
after_slice();
}
*sim_s = t;
return k == wps.size();
}
std::string ResourceDir() {
const char* e = std::getenv("VSLAM_RESOURCE_DIR");
return e != nullptr && *e != '\0' ? std::string(e) : std::string("resources");
}
std::vector<Scenario> Scenarios() {
return {
std::vector<Scenario> v = {
{"straight", "tich phan odometry, EKF, ScanSanitizer",
{{0.35, 0.0, 8.0}, {0.0, 0.0, 1.0}}, 0.0, 0.0, false, -0.40f, false, 10.0, 0.0, 0.0, false, 0.0, 0.0, 0, 0, 0},
@@ -131,6 +256,8 @@ std::vector<Scenario> Scenarios() {
{"spin", "cong vat can cua cliff_detect trong hanh lang hep",
{{0.0, 1.0, 7.0}}, 0.0, 0.0, false, -0.40f, true, 10.0, 0.0, 0.0, false, 0.0, 0.0, 0, 0, 0},
};
AddSlopeScenarios(&v);
return v;
}
} // namespace
@@ -142,6 +269,8 @@ int main(int argc, char** argv) {
// (tools/regress.sh) so voi ban ghi dung bang cach do.
std::string robot_yaml;
bool write_gt = false;
bool use_gl = false; // ve camera bang GPU (anh IR) -- chi kich ban doc
uint32_t cam_decimate = 2; // anh 640x480 / N, nhu crop_decimate cua robot
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
@@ -151,6 +280,8 @@ int main(int argc, char** argv) {
else if (a == "--list") list = true;
else if (a == "--robot") robot_yaml = next();
else if (a == "--gt") write_gt = true;
else if (a == "--gl") use_gl = true;
else if (a == "--cam-decimate") cam_decimate = static_cast<uint32_t>(std::atoi(next().c_str()));
else if (a == "--help") {
std::printf("cach dung: %s --scenario TEN --out-dir D [--robot robot.yaml] [--gt]\n"
" %s --list\n"
@@ -158,7 +289,12 @@ int main(int argc, char** argv) {
" --robot F hinh hoc robot THAT (banh, LiDAR, camera, IMU) tu robot.yaml\n"
" thay vi so mac dinh cu cua SimConfig\n"
" --gt ghi them tu the THAT 6-DoF (stream ground_truth) moi\n"
" buoc vat ly, de cham diem offline\n",
" buoc vat ly, de cham diem offline\n"
" kich ban doc (slope_*, hoac slope_all): chay worlds/slope_test.world,\n"
" hinh hoc robot that, luon ghi su that. Can\n"
" GAZEBO_MODEL_PATH=$PWD/resources/models.\n"
" --gl ve camera bang GPU: ghi them anh IR (build VSLAM_SIM_WITH_GL)\n"
" --cam-decimate N anh 640x480 / N (mac dinh 2 = 320x240 nhu robot)\n",
argv[0], argv[0], argv[0]);
return 0;
}
@@ -180,9 +316,38 @@ int main(int argc, char** argv) {
}
}
// Kich ban doc LUON dung hinh hoc robot that: --robot, hoac robot.yaml cua
// VSLAM_CONFIG_DIR / configuration_files.
vslam::RobotModel slope_robot;
bool slope_robot_ok = false;
auto load_slope_robot = [&]() -> bool {
if (slope_robot_ok) return true;
std::string p = robot_yaml;
if (p.empty()) {
const char* cd = std::getenv("VSLAM_CONFIG_DIR");
p = (cd != nullptr && *cd != '\0' ? std::string(cd) : std::string("configuration_files")) +
"/robot.yaml";
}
const vslam::StatusResponse rs = slope_robot.LoadFile(p);
if (!rs.ok()) {
std::fprintf(stderr, "kich ban doc can robot.yaml: %s: %s\n", p.c_str(), rs.message.c_str());
return false;
}
slope_robot_ok = true;
return true;
};
int written = 0;
for (const Scenario& s : all) {
if (name != "all" && name != s.name) continue;
const SlopeInfo* si = FindSlope(s.name);
const bool slope = si != nullptr;
// `all` = bo hoi quy (chi kich ban phang). `slope_all` = moi kich ban doc.
const bool pick = (name == "all" && !slope) || (name == "slope_all" && slope) || name == s.name;
if (!pick) continue;
if (use_gl && !slope) {
std::fprintf(stderr, "%s: --gl chi cho kich ban doc (can mesh cua world)\n", s.name);
return 2;
}
vslam::SimConfig cfg;
cfg.seed = 1234; // co dinh: cung kich ban => cung file
@@ -198,12 +363,36 @@ int main(int argc, char** argv) {
cfg.lidar_cut_for = s.lidar_cut_for;
cfg.camera_rate_hz = s.camera_hz;
if (!robot_yaml.empty()) vslam::SimConfigFromRobot(robot, &cfg);
if (slope) {
if (!load_slope_robot()) return 1;
vslam::SimConfigFromRobot(slope_robot, &cfg);
cfg.world_path = ResourceDir() + "/" + si->world;
// Nhanh nhat co the; ham theo hang doi ghi ben duoi nen khong rot chunk.
cfg.speed = 0.0;
cfg.depth_decimate = std::max<uint32_t>(1, cam_decimate);
// Noi suy D410 THAT o 640x480: fx = 595.914 -> HFOV 56.5 do (mac dinh sim 69).
cfg.depth_hfov_rad = 2.0 * std::atan(320.0 / 595.914);
}
vslam::SimSource sim(cfg);
if (!sim.Open().ok()) {
std::fprintf(stderr, "%s: sim khong mo duoc\n", s.name);
return 1;
}
if (use_gl) {
#ifdef VSLAM_HAVE_SIM_GL
std::string gerr;
auto r = vslam::CreateGlCameraRenderer(sim.world(), vslam::DefaultGlRendererConfig(), &gerr);
if (!r) {
std::fprintf(stderr, "%s: --gl: %s\n", s.name, gerr.c_str());
return 1;
}
sim.SetCameraRenderer(r);
#else
std::fprintf(stderr, "--gl: tool build KHONG co -DVSLAM_SIM_WITH_GL=ON\n");
return 2;
#endif
}
if (s.glass_at != 0.0) {
// Kinh phai la o BI CHIEM: no la vat can THAT, chi la vat can ma
// LiDAR va depth khong thay.
@@ -225,12 +414,24 @@ int main(int argc, char** argv) {
}
const std::string path = out_dir + "/" + s.name + ".vsr";
// Kich ban doc: ghi nhieu IMU vao header de bo xuat DM-VIO lay dung mat do
// nhieu, khong doan.
char imu_note[160] = "";
if (slope) {
std::snprintf(imu_note, sizeof(imu_note),
",\"gyro_noise_stddev\":%.6g,\"accel_noise_stddev\":%.6g",
cfg.gyro_noise_stddev, cfg.accel_noise_stddev);
}
vslam::RecordWriterConfig wc;
wc.path = path;
wc.compress = false; // nen = khong tat dinh giua cac phien ban zstd
wc.config_json = std::string("{\"scenario\":\"") + s.name + "\",\"seed\":1234" +
(robot_yaml.empty() ? "" : ",\"robot\":\"" + robot_yaml + "\"") +
(write_gt ? ",\"ground_truth\":true" : "") + "}";
(write_gt || slope ? ",\"ground_truth\":true" : "") +
(slope ? std::string(",\"world\":\"") + si->world + "\",\"gl\":" +
(use_gl ? "true" : "false") + imu_note
: std::string("")) +
"}";
vslam::RecordWriter w(wc);
const vslam::StatusResponse st = w.Open();
@@ -275,8 +476,8 @@ int main(int argc, char** argv) {
vslam::EncodeEncoderPayload(e.left_rad, e.right_rad, &b_enc);
w.Write(vslam::VsrStream::kEncoder, e.stamp, b_enc);
});
std::string b_gt;
if (write_gt) {
std::string b_gt, b_ir;
if (write_gt || slope) {
sim.SetGroundTruthCallback([&](vslam::UnixTime t, const vslam::SimPose3& p) {
const double v[6] = {p.x, p.y, p.z, p.roll, p.pitch, p.yaw};
b_gt.clear();
@@ -293,6 +494,13 @@ int main(int argc, char** argv) {
b_depth.clear();
vslam::EncodeImagePayload(*f.depth, &b_depth);
w.Write(vslam::VsrStream::kDepth, f.header.stamp, b_depth);
// Anh IR (chi khi ve bang GPU). SAU depth: replay rap khung theo thu
// tu camera_info -> depth -> infra1.
if (f.infra1) {
b_ir.clear();
vslam::EncodeImagePayload(*f.infra1, &b_ir);
w.Write(vslam::VsrStream::kInfra1, f.header.stamp, b_ir);
}
});
sim.camera()->Open();
}
@@ -311,6 +519,20 @@ int main(int argc, char** argv) {
// Phat lai o 20 Hz cung la dung cai ma luong safety lam tren robot that.
constexpr double kSlice = 0.05;
double total = 0.0;
if (slope) {
// Ham: writer chay luong rieng va DROP khi hang doi day -- cho no ghi
// bot truoc khi mo phong chay tiep (sim chay nhanh hon thoi gian thuc).
auto throttle = [&w]() {
while (w.Stats().queue_bytes > (48u << 20)) {
std::this_thread::sleep_for(std::chrono::milliseconds(2));
}
};
if (!DriveWaypoints(&sim, si->waypoints, si->cruise, throttle, &total)) {
std::fprintf(stderr, "%s: KHONG toi het waypoint trong thoi gian cho phep\n", s.name);
w.Close();
return 1;
}
}
for (const Leg& leg : s.legs) {
double left = leg.seconds;
while (left > 1e-9) {