347 lines
14 KiB
C++
347 lines
14 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 <cmath>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <string>
|
|
#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"
|
|
|
|
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;
|
|
};
|
|
|
|
std::vector<Scenario> Scenarios() {
|
|
return {
|
|
{"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},
|
|
};
|
|
}
|
|
|
|
} // 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;
|
|
|
|
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 == "--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",
|
|
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;
|
|
}
|
|
}
|
|
|
|
int written = 0;
|
|
for (const Scenario& s : all) {
|
|
if (name != "all" && name != s.name) continue;
|
|
|
|
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);
|
|
|
|
vslam::SimSource sim(cfg);
|
|
if (!sim.Open().ok()) {
|
|
std::fprintf(stderr, "%s: sim khong mo duoc\n", s.name);
|
|
return 1;
|
|
}
|
|
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";
|
|
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" : "") + "}";
|
|
|
|
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;
|
|
if (write_gt) {
|
|
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);
|
|
});
|
|
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;
|
|
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;
|
|
}
|