Files
vslam_cpp/tools/vslam_record.cc

569 lines
23 KiB
C++

// Ghi ban ghi `.vsr` tu bo mo phong, theo kich ban co ten.
//
// ================== VI SAO GHI TU SIM ==================
// Chua co phan cung. Nhung bo hoi quy can du lieu CO DINH, va bo mo phong voi
// hat giong co dinh cho ra dung the: cung kich ban => cung file, byte giong
// byte. Khi co phan cung that, `vslam_record --live` ghi cung dinh dang va
// `vslam_replay` khong phan biet duoc hai nguon -- do la ca ly do
// `SimSource` implement chinh cac interface driver.
//
// Kich ban duoc chon de moi cai cham vao MOT duong ma cac cai khac khong
// cham: thang -> tich phan odometry; vuong -> sai so goc; truot -> phat hien
// truot; vuc -> perception + safety; tuong -> chan theo LiDAR; xoay -> cong
// 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"
#include "vslam/record_format.h"
#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 {
struct Leg {
double vx = 0.0;
double wz = 0.0;
double seconds = 0.0;
};
struct Scenario {
const char* name;
const char* what; // duong nao duoc cham toi
std::vector<Leg> legs;
double slip = 0.0;
double diff_slip = 0.0;
bool cliff = false;
float cliff_z = -0.40f;
// Anh depth chiem ~96% kich thuoc ban ghi. Chi bat camera o kich ban thuc
// su cham vao perception; cac kich ban odometry/safety khong can.
bool camera = false;
double camera_hz = 10.0;
double glass_at = 0.0; // 0 = khong dat kinh
double us_dropout = 0.0; // 1.0 = day sieu am dut han
bool no_us = false; // khong LAP sieu am (khac han "dut")
double lidar_cut_at = 0.0; // giay thu may thi "rut day" LiDAR
double lidar_cut_for = 0.0;
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() {
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},
// Hanh lang tong hop rong 1.6 m -- KHONG lot mot hinh vuong 4x4 m.
// Bai vuong UMBmark can san trong; o day thay bang di-va-ve, van do
// duoc sai so goc tich luy ma khong dam vao tuong (dam tuong thi phep
// do thanh phep do cua LOP AN TOAN, khong phai cua odometry).
{"outback", "sai so goc tich luy: di 3 m, quay 180 do, ve",
{{0.30, 0.0, 10.0}, {0.0, 0.7, 4.49}, {0.30, 0.0, 10.0}, {0.0, 0.7, 4.49}},
0.0, 0.0, false, -0.40f, false, 10.0, 0.0, 0.0, false, 0.0, 0.0, -4.0, 0, 0},
// Truot VI SAI: gyro thay lech giua hai banh -> phat hien duoc.
{"slip_diff", "phat hien truot bang gyro, covariance cua wheel_odom",
{{0.35, 0.0, 10.0}}, 0.0, 0.25, false, -0.40f, false, 10.0, 0.0, 0.0, false, 0.0, 0.0, 0, 0, 0},
// Truot DOI XUNG: ca hai banh truot deu -> gyro KHONG thay gi.
// Kich ban nay ton tai de khoa mot GIOI HAN da biet, khong phai de
// chung minh he chay dung.
{"slip_sym", "gioi han: truot doi xung khong phat hien duoc",
{{0.35, 0.0, 10.0}}, 0.25, 0.0, false, -0.40f, false, 10.0, 0.0, 0.0, false, 0.0, 0.0, 0, 0, 0},
{"cliff", "cliff_detect + nhanh CLIFF_STOP cua FSM",
{{0.30, 0.0, 8.0}}, 0.0, 0.0, true, -0.40f, true, 10.0, 0.0, 0.0, false, 0.0, 0.0, 0, 0, 0},
// Bat dau cach tuong cuoi hanh lang ~3 m de tia truoc nhin thay no
// ngay tu khung dau. Bat dau xa qua thi robot chi chay tu do het ban
// ghi va nhanh chan KHONG BAO GIO chay -- da do: 598/598 khung OK.
// Bac NONG, sat nguong. Nguong no ra theo tam:
// thr = z_ref - (cliff_margin + r * tan(pitch_unc))
// = 0.089 m o r = 1.1, va 0.137 m o r = 2.5. Mot bac 0.12 m nam GIUA
// hai gia tri do, nen no chi bi phat hien khi robot lai du gan.
//
// Kich ban nay ton tai vi bai `cliff` (bac 0.40 m) cho hang tram diem
// va KHONG nhay cam voi nguong: da thu doi `min_cluster` 12 -> 20, ca
// bo hoi quy van xanh. Mot bo hoi quy chi chay xa nguong thi khong bat
// duoc truot nguong.
{"cliff_shallow", "sat nguong: bac 0.12 m, chi thay khi den gan",
{{0.25, 0.0, 12.0}}, 0.0, 0.0, true, -0.12f, true, 10.0, 0, 0, 0},
// KINH: LiDAR va depth XUYEN QUA, chi sieu am thay. Day la ly do
// nhanh US_BLOCKED cua FSM ton tai, va truoc kich ban nay no CHUA BAO
// GIO chay end-to-end -- chi co unit test.
{"glass", "US_BLOCKED: kinh vo hinh voi LiDAR",
{{0.35, 0.0, 14.0}}, 0.0, 0.0, false, -0.40f, false, 10.0, 3.0, 0.0, false, 0.0, 0.0, 0, 0, 0},
// Sieu am CHET (moi dau do tra NaN). Phai ra US_TIMEOUT, KHONG duoc
// coi nhu "khong thay gi".
{"us_dead", "US_TIMEOUT: day sieu am dut",
{{0.35, 0.0, 8.0}}, 0.0, 0.0, false, -0.40f, false, 10.0, 0.0, 1.0, false, 0.0, 0.0, 0, 0, 0},
// KINH + KHONG LAP sieu am. Day la kich ban AC MONG: khong cam bien
// nao thay duoc buc kinh, va robot dam vao. No la ly do DUY NHAT
// nhanh BUMPER ton tai, va la bang chung vi sao sieu am khong phai do
// trang tri.
//
// Khac han `us_dead`: o day KHONG CO cam bien nao (map rong -> FSM bo
// qua ca hai nhanh US), con `us_dead` la co cam bien nhung chung chet
// (-> US_TIMEOUT, robot dung an toan).
{"glass_blind", "BUMPER: kinh + khong lap sieu am",
{{0.35, 0.0, 16.0}}, 0.0, 0.0, false, -0.40f, false, 10.0, 3.0, 0.0, true, 0.0, 0.0, 0, 0, 0},
// RUT DAY LiDAR o giay thu 4, trong 3 giay. Tieu chi P4 ghi
// "SCAN_TIMEOUT < 1.1 s"; day la cach do duoc no ma khong phai rut day
// that.
{"lidar_cut", "SCAN_TIMEOUT: mat LiDAR giua chung",
{{0.30, 0.0, 10.0}}, 0.0, 0.0, false, -0.40f, false, 10.0, 0.0, 0.0, false,
4.0, 3.0, 0, 0, 0},
{"wall", "chan theo LiDAR: SCAN_SLOW roi SCAN_STOP",
{{0.35, 0.0, 14.0}}, 0.0, 0.0, false, -0.40f, false, 10.0, 0.0, 0.0, false, 0.0, 0.0, 8.5, 0, 0},
// Quay tai cho trong hanh lang 1.6 m: cong `max_obstructed_frac` cua
// cliff_detect phai kich, va KHONG duoc bao vuc.
{"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
int main(int argc, char** argv) {
std::string name, out_dir = ".";
bool list = false;
// Mac dinh GIU hinh hoc cu cua SimConfig va KHONG ghi su that: bo hoi quy
// (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];
auto next = [&]() -> std::string { return i + 1 < argc ? argv[++i] : ""; };
if (a == "--scenario") name = next();
else if (a == "--out-dir") out_dir = next();
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"
" %s --scenario all --out-dir D\n"
" --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"
" 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;
}
}
const std::vector<Scenario> all = Scenarios();
if (list || name.empty()) {
std::printf("kich ban:\n");
for (const Scenario& s : all) std::printf(" %-12s %s\n", s.name, s.what);
return name.empty() && !list ? 2 : 0;
}
vslam::RobotModel robot;
if (!robot_yaml.empty()) {
const vslam::StatusResponse rs = robot.LoadFile(robot_yaml);
if (!rs.ok()) {
std::fprintf(stderr, "%s: %s\n", robot_yaml.c_str(), rs.message.c_str());
return 1;
}
}
// 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) {
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
cfg.start_x = s.start_x;
cfg.start_y = s.start_y;
cfg.start_yaw = s.start_yaw;
cfg.slip_factor = s.slip;
cfg.slip_differential = s.diff_slip;
cfg.enable_depth_camera = s.camera;
cfg.us_dropout_rate = s.us_dropout;
cfg.enable_ultrasonic = !s.no_us;
cfg.lidar_cut_at = s.lidar_cut_at;
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.
vslam::OccupancyGrid m = sim.map();
const double res = m.resolution;
const int gcx = static_cast<int>((s.glass_at - m.origin.position.x) / res);
for (int cy = 0; cy < static_cast<int>(m.height); ++cy) {
if (gcx < 0 || gcx >= static_cast<int>(m.width)) break;
const size_t i = static_cast<size_t>(cy) * m.width + gcx;
if (m.data[i] == 0) m.data[i] = 100;
}
sim.SetMapForTest(m);
sim.SetGlass(s.glass_at - res, -50.0, s.glass_at + res, 50.0, true);
}
if (s.cliff) {
// Bac hut xuong 0.40 m o 4 m phia truoc: nam ngoai vung mu 1.10 m nen
// detector phai thay truoc khi robot toi.
sim.SetFloorHeight(4.0, -3.0, 20.0, 3.0, s.cliff_z);
}
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 || 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();
if (!st.ok()) {
std::fprintf(stderr, "%s: %s\n", path.c_str(), st.message.c_str());
return 1;
}
// Ghi THO: scan TRUOC sanitizer, imu, encoder. Dung nguyen tac cua
// topics_to_record.yaml cu.
// MOI bo dem MOT stream, va CLEAR truoc moi lan ma hoa.
//
// `Encode*Payload` NOI THEM vao `out` (xem record_format.h). Dung chung
// mot bo dem va khong xoa thi moi ban ghi to hon ban truoc: da lam ra mot
// file 17 GB cho mot kich ban 9 giay truoc khi phat hien.
std::string b_scan, b_imu, b_enc, b_info, b_depth;
sim.lidar()->SetCallback([&](vslam::LaserScanPtr sc) {
b_scan.clear();
vslam::EncodeScanPayload(*sc, &b_scan);
w.Write(vslam::VsrStream::kScanRaw, sc->header.stamp, b_scan);
});
sim.mcu()->SetImuCallback([&](vslam::ImuPtr m) {
b_imu.clear();
vslam::EncodeImuPayload(*m, &b_imu);
w.Write(vslam::VsrStream::kImu, m->header.stamp, b_imu);
});
std::string b_us, b_bump;
sim.mcu()->SetUltrasonicCallback([&](const vslam::UltrasonicReading& u) {
b_us.clear();
vslam::EncodeUltrasonicPayload(u.range, u.count, &b_us);
w.Write(vslam::VsrStream::kUltrasonic, u.stamp, b_us);
});
sim.mcu()->SetBumperCallback([&](const vslam::BumperReading& bm) {
b_bump.clear();
vslam::EncodeBumperPayload(bm.mask, &b_bump);
w.Write(vslam::VsrStream::kBumper, bm.stamp, b_bump);
});
sim.base()->SetEncoderCallback([&](const vslam::EncoderReading& e) {
if (!e.valid) return;
b_enc.clear();
vslam::EncodeEncoderPayload(e.left_rad, e.right_rad, &b_enc);
w.Write(vslam::VsrStream::kEncoder, e.stamp, b_enc);
});
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();
vslam::EncodeGroundTruthPayload(v, &b_gt);
w.Write(vslam::VsrStream::kGroundTruth, t, b_gt);
});
}
if (sim.camera() != nullptr) {
sim.camera()->SetCallback([&](const vslam::CameraFrame& f) {
if (!f.depth) return;
b_info.clear();
vslam::EncodeCameraInfoPayload(f.depth_info, &b_info);
w.Write(vslam::VsrStream::kCameraInfo, f.header.stamp, b_info);
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();
}
sim.lidar()->Open();
sim.mcu()->Open();
sim.base()->Open();
// Chay theo CHANG, chia thanh lat 50 ms va PHAT LAI lenh truoc moi lat.
//
// Base cua bo mo phong co watchdog: khong co lenh trong 0.3 s thi no tu
// dung robot -- dung nhu base that, va dung nhu nhanh NO_CMD cua FSM.
// Dat van toc MOT lan roi chay 8 giay thi robot chi di duoc 0.3 giay:
// da do 0.026 m thay vi 2.5 m, va khong co gi bao loi vi day la hanh vi
// DUNG cua base.
//
// 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) {
const double dt = std::min(kSlice, left);
sim.base()->SetVelocity(leg.vx, leg.wz);
sim.RunFor(dt);
left -= dt;
}
total += leg.seconds;
}
sim.base()->SetVelocity(0.0, 0.0);
w.Close();
const vslam::RecordWriterStats ws = w.Stats();
std::printf("%-12s %6.1f s %7llu chunk %8.2f MB drop %llu\n", s.name, total,
static_cast<unsigned long long>(ws.chunks_written),
ws.bytes_written / 1048576.0,
static_cast<unsigned long long>(ws.chunks_dropped));
if (ws.chunks_dropped > 0) {
std::fprintf(stderr, "%s: DROP %llu chunk -- ban ghi khong dung lam goc "
"hoi quy duoc\n", s.name,
static_cast<unsigned long long>(ws.chunks_dropped));
return 1;
}
written++;
}
if (written == 0) {
std::fprintf(stderr, "khong co kich ban ten '%s'\n", name.c_str());
return 2;
}
return 0;
}