slope: test doc tu dong (--slope-test) -- dinh vi lai xong robot tu tim truc doc tren ban do (do cao san cua node), quay mat xuong doc, cho nut Y (gamepad.button_test) roi chay N luot xuong/len, moi luot mot van toc (mac dinh 10 luot 0.10..0.46 m/s)

Lenh di qua FSM an toan nhu moi lenh khac; dung han khi lech duong > 0.4 m, scan mat khop 1.5 s, pose nhay > 0.3 m, nghieng > 20 do. Do lech dinh vi 2 lan/giay (khop scan voi likelihood field, doc lap RTAB-Map) ra logs/slope_test_<gio>.csv + bang tong. Sim 08/10 (slope_two_ramps, 10 luot): lech that trung vi 0.9 cm, max 3.0 cm, sai z trung vi 0.3 cm, khong troi tu luot 1 den 10.
This commit is contained in:
trung09122002
2026-10-08 15:52:02 +07:00
parent f8049be038
commit fc03420d2c
13 changed files with 1462 additions and 1 deletions

View File

@@ -765,7 +765,7 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
"min_angular_kick", "idle_publish_sec", "reopen_period_sec",
"turbo_linear", "turbo_angular", "slow_linear", "slow_angular",
"axis_linear", "axis_linear_dpad", "axis_angular",
"axis_angular_dpad", "button_enable", "button_zero",
"axis_angular_dpad", "button_enable", "button_zero", "button_test",
"button_turbo", "button_slow", "invert_linear",
"invert_angular", "lt_stop", "axis_lt", "lt_press_above"});
if (!r.ok()) return r;
@@ -805,6 +805,7 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
out->gamepad.button_enable =
gp.get<int>("button_enable", out->gamepad.button_enable);
out->gamepad.button_zero = gp.get<int>("button_zero", out->gamepad.button_zero);
out->gamepad.button_test = gp.get<int>("button_test", out->gamepad.button_test);
out->gamepad.button_turbo =
gp.get<int>("button_turbo", out->gamepad.button_turbo);
out->gamepad.button_slow = gp.get<int>("button_slow", out->gamepad.button_slow);

View File

@@ -84,6 +84,15 @@ void Usage(const char* argv0) {
" kem --slam-reloc: CAMERA cung dinh vi lai (VPR + PnP,\n"
" chot khi trung LiDAR). Can build -DVSLAM_WITH_TENSORRT=ON;\n"
" mac dinh $VSLAM_RESOURCE_DIR/vpr/eigenplaces_r50_fp16.plan\n"
" --slope-test kem --slam-reloc: TEST DOC TU DONG. Dat robot o DINH doc;\n"
" chot xong tu tim doc tren ban do, quay mat xuong, cho Y\n"
" tren tay cam (mo phong: tu chay), roi xuong / len N luot,\n"
" moi luot mot van toc. Do lech dinh vi + z 2 Hz,\n"
" CSV logs/slope_test_*.csv + bang tong. Gat can = tam dung.\n"
" --slope-test-loops N so luot (mac dinh 10)\n"
" --slope-test-speeds V1,V2,.. van toc moi luot, m/s (toi da = tran an toan\n"
" 0.5; sim nang bang --max-speed). A:B = chia deu A -> B\n"
" cho so luot. Mac dinh 0.10,0.14,...,0.46\n"
" --check nap va KIEM cau hinh roi thoat, khong chay gi\n",
argv0, vslam::ConfigDir().c_str());
}
@@ -189,6 +198,27 @@ int main(int argc, char** argv) {
opts.slam_reloc_vpr = true;
if (i + 1 < argc && argv[i + 1][0] != '-') opts.vpr_engine = argv[++i];
}
else if (a == "--slope-test") opts.slope_test = true;
else if (a == "--slope-test-loops") opts.slope_test_loops = std::atoi(next().c_str());
else if (a == "--slope-test-speeds") {
const std::string v = next();
// "A:B" = chia deu A -> B cho so luot (runtime tinh, vi --slope-test-loops
// co the dung sau).
const size_t colon = v.find(':');
if (colon != std::string::npos) {
opts.slope_test_vmin = std::atof(v.substr(0, colon).c_str());
opts.slope_test_vmax = std::atof(v.substr(colon + 1).c_str());
continue;
}
size_t b = 0;
while (b <= v.size()) {
const size_t e = v.find(',', b);
const std::string tok = v.substr(b, e == std::string::npos ? std::string::npos : e - b);
if (!tok.empty()) opts.slope_test_speeds.push_back(std::atof(tok.c_str()));
if (e == std::string::npos) break;
b = e + 1;
}
}
else if (a == "--check") check_only = true;
else if (a == "--help" || a == "-h") { Usage(argv[0]); return 0; }
else {

View File

@@ -2,14 +2,17 @@
#include <pthread.h>
#include <sched.h>
#include <sys/stat.h>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <limits>
#include <sstream>
#include "vslam/binary_codec.h"
#include "vslam/clock.h"
@@ -835,12 +838,58 @@ StatusResponse Runtime::Start() {
safety_thread_ = std::thread([this] { SafetyLoop(); });
ApplyThreadPriority(&safety_thread_, cfg_->threads.safety_priority, "safety");
#ifdef VSLAM_HAVE_SLAM
// --- Test doc tu dong (--slope-test) ---
if (opts_.slope_test) {
if (!slam_enabled_ || !opts_.slam_reloc || !reloc_field_) {
return StatusResponse::Error(
StatusCode::kInvalidArgument,
"--slope-test can --slam --slam-mode localization --slam-reloc: dung luoi ban do de "
"tim doc va do sai so");
}
SlopeTestConfig sc;
sc.loops = std::max(1, opts_.slope_test_loops);
if (!opts_.slope_test_speeds.empty()) sc.speeds = opts_.slope_test_speeds;
if (opts_.slope_test_vmax > 0.0) {
// A:B -> chia deu cho so luot (1 luot = A).
sc.speeds.clear();
for (int k = 0; k < sc.loops; ++k) {
const double u = sc.loops > 1 ? static_cast<double>(k) / (sc.loops - 1) : 0.0;
sc.speeds.push_back(opts_.slope_test_vmin + u * (opts_.slope_test_vmax - opts_.slope_test_vmin));
}
}
// Tran = tran cua lop an toan: 0.5 m/s (safety.yaml); sim --max-speed V nang
// ca ba tang (teleop, safety, base sim). Robot that: base ZLAC con tu kep o
// robot.yaml base.max_linear -- nang tran that la quyet dinh AN TOAN rieng.
const double vcap = cfg_->safety.max_linear;
std::string sp;
for (const double v : sc.speeds) {
if (!(v > 0.0) || v > vcap + 1e-9) {
std::ostringstream os;
os << "--slope-test-speeds: moi van toc phai trong (0, " << vcap
<< "] m/s (tran an toan" << (opts_.source == SourceKind::kSim ? "; sim: them --max-speed V" : "")
<< ")";
return StatusResponse::Error(StatusCode::kInvalidArgument, os.str());
}
sp += (sp.empty() ? "" : " / ") + std::to_string(v).substr(0, 4);
}
slope_test_.reset(new SlopeTest(sc));
VSLAM_INFO("slope_test: BAT -- " << sc.loops << " luot (xuong + len), van toc moi luot " << sp
<< " m/s. Dinh vi lai xong se tu tim doc tren ban do va quay mat xuong, roi "
<< (opts_.source == SourceKind::kSim ? "TU CHAY (mo phong)." : "cho bam Y."));
}
#endif
if (slam_enabled_) {
slam_thread_ = std::thread([this] { SlamLoop(); });
// CFS, KHONG realtime. Luong nay chan hang tram ms; cho no uu tien cao la
// cach chac chan nhat de treo vong an toan.
}
#ifdef VSLAM_HAVE_SLAM
if (slope_test_) slope_thread_ = std::thread([this] { SlopeLoop(); });
#endif
web_thread_ = std::thread([this] { WebLoop(); });
// web_tx chay CFS: no khong o trong vong an toan, va nang no len se lay mat
// CPU cua luong dang giu robot khong dam vao tuong.
@@ -872,11 +921,26 @@ void Runtime::Join() {
// `raw_cmd_` con duoc ghi ma khong con ai don no ve 0.
if (gamepad_thread_.joinable()) gamepad_thread_.join();
if (gamepad_) gamepad_->Close();
#ifdef VSLAM_HAVE_SLAM
// Test doc cung la NGUON LENH: tat truoc vong safety, cung ly do voi tay cam.
if (slope_thread_.joinable()) slope_thread_.join();
#endif
if (safety_thread_.joinable()) safety_thread_.join();
if (web_thread_.joinable()) web_thread_.join();
if (slam_thread_.joinable()) slam_thread_.join();
if (source_thread_.joinable()) source_thread_.join();
#ifdef VSLAM_HAVE_SLAM
// Ctrl-C giua chung: van in bang tong phan da do (luong slam da dung).
if (slope_test_ && !slope_summary_done_) {
SlopeTestPhase ph;
{
std::lock_guard<std::mutex> lk(slope_mu_);
ph = slope_test_->phase();
}
SlopeTestSummary(ph);
}
#endif
// Dung base TRUOC khi dong web: neu dong web truoc thi trong khoang giua,
// khong con ai gui lenh nhung robot van con van toc cuoi cung.
@@ -1120,6 +1184,12 @@ void Runtime::GamepadLoop() {
while (!stop_.load()) {
const GamepadState s = gamepad_->Snapshot();
GamepadCommand c = gamepad_map_->Step(s, clock_->Now());
#ifdef VSLAM_HAVE_SLAM
if (c.test_pressed) {
slope_button_.fetch_add(1);
if (slope_test_) VSLAM_INFO("gamepad: bam Y (test doc)");
}
#endif
if (c.connected_changed && s.connected) {
VSLAM_INFO("gamepad: da ket noi (" << gamepad_->DeviceInfo()
@@ -1268,6 +1338,11 @@ void Runtime::SlamLoop() {
// sai 1 m (docs/SLAM.md). Chot roi thi pose ban dau tinh tu CHINH odom
// nay -- khong tre, khong phai cho robot dung nhu ban ROS.
if (reloc_active_) {
if (slope_test_) {
// Dinh vi lai chay LAI sau khi test da co duong = mat dinh vi giua chung.
std::lock_guard<std::mutex> lk(slope_mu_);
if (slope_planned_) slope_lost_.store(true);
}
RelocStep(*scan, odom);
if (reloc_active_) continue;
}
@@ -1384,6 +1459,9 @@ void Runtime::SlamLoop() {
}
}
// --- Test doc tu dong: lap duong sau khi chot, do 2 Hz ---
if (slope_test_) SlopeTestSlamStep(out, *scan);
bool need_map = false;
{
std::lock_guard<std::mutex> lk(slam_mu_);
@@ -3126,4 +3204,368 @@ void Runtime::WebLoop() {
}
}
#ifdef VSLAM_HAVE_SLAM
// ============================================================================
// Test doc tu dong (--slope-test). Logic lai o vslam/slope_test.h; o day chi
// noi day: lap duong tu ban do, lai 20 Hz, do 2 Hz, ghi CSV, bang tong.
// ============================================================================
void Runtime::SlopeLoop() {
const auto t0 = std::chrono::steady_clock::now();
auto next = t0;
int seen = slope_button_.load();
while (!stop_.load()) {
next += std::chrono::milliseconds(50);
bool planned = false;
{
std::lock_guard<std::mutex> lk(slope_mu_);
planned = slope_planned_;
}
const int b = slope_button_.load();
if (planned) {
SlopeTestInput in;
in.t = 1.0 + std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
Pose2D corr;
bool loc = false;
{
std::lock_guard<std::mutex> lk(slam_mu_);
corr = map_correction_;
loc = slam_localized_;
}
Pose2D od;
{
std::lock_guard<std::mutex> lk(odom_mu_);
od = ekf_->pose();
}
// Pose MAP 20 Hz: phep nan cua RTAB-Map (3 Hz) x EKF (44 Hz).
in.pose = Pose2DCompose(corr, od);
// Chi tin DO DOC LAP: dinh vi lai chay lai / scan khop kem lien tiep
// (SlopeTestSlamStep). `slam_localized_` cua RTAB-Map khong dung duoc o day:
// localization chi bat no sau khi robot da chay.
(void)loc;
in.localized = !slope_lost_.load();
in.roll = tilt_roll_.load(std::memory_order_relaxed);
in.pitch = tilt_pitch_.load(std::memory_order_relaxed);
// Tay cam / web vua ra lenh, hoac e-stop: nhuong NGAY, cho bam Y.
in.human = RelocHumanCommanding() || estop_.load();
in.start = b != seen;
SlopeTestOutput o;
{
std::lock_guard<std::mutex> lk(slope_mu_);
o = slope_test_->Step(in);
}
// CUNG duong voi dinh vi lai tu dong: raw_cmd_ -> FSM an toan -> base.
if (o.command) RelocCommand(o.v, o.w);
if (!o.event.empty()) VSLAM_INFO("slope_test: " << o.event);
}
seen = b;
std::this_thread::sleep_until(next);
}
}
void Runtime::SlopeTestSlamStep(const SlamOutput& out, const LaserScan& scan) {
if (!reloc_field_) return;
const LikelihoodField& lf = *reloc_field_;
bool planned = false;
bool approaching = false;
SlopeTestPhase ph = SlopeTestPhase::kIdle;
int loop = 0;
bool down = true;
double speed = 0.0, along = 0.0, cross = 0.0;
{
std::lock_guard<std::mutex> lk(slope_mu_);
planned = slope_planned_;
ph = slope_test_->phase();
approaching = slope_test_->approaching();
loop = slope_test_->loop();
down = slope_test_->down();
speed = slope_test_->speed();
along = slope_test_->along();
cross = slope_test_->cross();
}
if (!planned) {
// ---- Lap duong, MOT lan, ngay sau khi chot ----
// Toi duoc day nghia la dinh vi lai DA CHOT (vong slam `continue` khi chua).
// KHONG cho `out.localized`: o localization no chi bat khi RTAB-Map tu khop
// duoc -- can robot DA CHAY, ma robot dang dung cho chinh test nay.
if (slope_plan_failed_) return;
const FloorModel& fm = slam_->floor_model();
if (fm.empty()) {
VSLAM_WARN("slope_test: ban do khong co do cao node (slam.yaml slope.enabled? ban do quet "
"truoc khi co che do doc?) -- KHONG chay.");
slope_plan_failed_ = true;
return;
}
// O trong VA du cho than robot: ban kinh than + 10 cm, tren luoi ban do.
const double clear = cfg_->robot.geometry().robot_radius + 0.10;
const double res = lf.resolution();
const int rr = static_cast<int>(std::ceil(clear / res));
auto free = [&](double x, double y) {
// Robot DA di qua cho nay luc quet -> di duoc. Luoi dinh vi lai (dung thang
// tu DB) con TUONG GIA o chan doc (tia LiDAR cham mat doc tu xa) -- lop
// camera chi xoa chung tren ban do hien thi. Sim 08/10: chan het moi huong
// xuong doc ngay chan doc.
FloorNode n;
double dn = 0.0;
if (fm.NearestNode(x, y, &n, &dn) && dn <= 0.35) return true;
// Goc luoi RTAB-Map la GOC o (0, 0) (xep o bang phep cat): tra bang floor.
const int c0 = static_cast<int>(std::floor((x - lf.origin_x()) / res));
const int r0 = static_cast<int>(std::floor((y - lf.origin_y()) / res));
for (int dr = -rr; dr <= rr; ++dr) {
for (int dc = -rr; dc <= rr; ++dc) {
if ((dr * dr + dc * dc) * res * res > clear * clear) continue;
if (!lf.Free(r0 + dr, c0 + dc)) return false;
}
}
return true;
};
// Chi tin mat san gan node da quet (doc rong quet theo lan: giua hai lan ~1.5 m).
auto floor = [&](double x, double y, double* z) {
double d = 0.0;
return fm.FloorAt(x, y, z, &d) && d <= 2.0;
};
SlopePath p;
std::string why, fan;
if (!PlanSlopePath(out.pose_map.x, out.pose_map.y, floor, free, SlopePathConfig{}, &p, &why,
&fan)) {
VSLAM_WARN("slope_test: " << why << " -- KHONG chay.");
slope_plan_failed_ = true;
return;
}
{
std::lock_guard<std::mutex> lk(slope_mu_);
slope_test_->SetPath(p, opts_.source == SourceKind::kSim);
slope_planned_ = true;
}
slope_t0_ = std::chrono::steady_clock::now();
VSLAM_INFO("slope_test: duong thu A (" << p.a.x << ", " << p.a.y << ") -> B (" << p.bx << ", "
<< p.by << "): dai " << p.length << " m, xuong " << p.drop << " m, huong "
<< p.a.theta * 180.0 / M_PI << " do. Dang quay mat xuong doc...");
VSLAM_INFO("slope_test: cac huong da thu (huong:duong trong:dung vi:ket qua):" << fan);
// CSV: logs/ o thu muc chay (goc repo).
::mkdir("logs", 0755);
char stamp[32];
const std::time_t tt = std::time(nullptr);
std::tm tmv{};
localtime_r(&tt, &tmv);
std::strftime(stamp, sizeof(stamp), "%Y%m%d_%H%M%S", &tmv);
slope_csv_path_ = std::string("logs/slope_test_") + stamp + ".csv";
slope_csv_ = std::fopen(slope_csv_path_.c_str(), "w");
if (slope_csv_ != nullptr) {
std::fprintf(slope_csv_,
"t_s,luot,chieu,van_toc,pha,doc_duong_m,lech_ngang_cm,x,y,yaw_deg,khop,"
"khop_tot_nhat,dx_cm,dy_cm,dyaw_deg,lech_cm,z_uoc_luong,z_ban_do,z_sai_cm\n");
} else {
VSLAM_WARN("slope_test: khong mo duoc " << slope_csv_path_ << " -- chi log.");
}
return;
}
if (ph == SlopeTestPhase::kDone || ph == SlopeTestPhase::kHalted) {
if (!slope_summary_done_) {
SlopeTestSummary(ph);
if (ph == SlopeTestPhase::kDone) {
// Xong du luot: TU THOAT (cung duong voi Ctrl-C: dung base, dong DB).
// Truoc day node chay tiep va log khac day troi mat bang tong.
VSLAM_INFO("slope_test: xong -- node tu thoat.");
RequestStop();
} else {
// Dung han (vi pham an toan): robot co the dang GIUA doc -- giu node de
// nguoi lai tay ra khoi doc.
VSLAM_WARN("slope_test: DUNG HAN -- node van chay de lai tay ra khoi doc; "
"Ctrl-C de thoat. Bang tong o tren.");
}
}
return;
}
if (ph != SlopeTestPhase::kDrive && ph != SlopeTestPhase::kDwell) return;
if (approaching) return; // dang ve A: chua phai luot do
const auto now = std::chrono::steady_clock::now();
if (now - slope_meas_at_ < std::chrono::milliseconds(500)) return;
slope_meas_at_ = now;
// ---- Do: lech dinh vi = phep doi pose lam scan KHOP ban do nhat ----
// Do doc lap voi RTAB-Map: chi scan hien tai + luoi ban do. Tho (+-20 cm,
// +-3 do) roi tinh (1 cm, 0.25 do).
const Pose2D pose = out.pose_map;
std::vector<Point> pts = ScanToFrame(scan, pose);
if (pts.size() > 400) {
std::vector<Point> sub;
const size_t stride = (pts.size() + 399) / 400;
for (size_t i = 0; i < pts.size(); i += stride) sub.push_back(pts[i]);
pts.swap(sub);
}
if (pts.size() < 50) return;
const double res = lf.resolution(), ox = lf.origin_x(), oy = lf.origin_y();
auto score = [&](double dx, double dy, double dth) {
const double c = std::cos(dth), s = std::sin(dth);
double sum = 0.0;
for (const Point& q : pts) {
const double rx = q.x - pose.x, ry = q.y - pose.y;
const double qx = pose.x + c * rx - s * ry + dx;
const double qy = pose.y + s * rx + c * ry + dy;
// floor, KHONG lround: goc luoi la GOC o (0, 0). lround lech nua o (2.5 cm
// moi truc) -- sim 08/10: lech gia -4 / -4 cm khi dinh vi that sai 1 cm.
sum += lf.At(static_cast<int>(std::floor((qy - oy) / res)),
static_cast<int>(std::floor((qx - ox) / res)));
}
return sum / static_cast<double>(pts.size());
};
const double deg = M_PI / 180.0;
const double s0 = score(0.0, 0.0, 0.0);
double bx = 0.0, by = 0.0, bth = 0.0, bs = s0;
for (int it = -3; it <= 3; ++it) {
for (int ix = -5; ix <= 5; ++ix) {
for (int iy = -5; iy <= 5; ++iy) {
const double v = score(ix * 0.04, iy * 0.04, it * deg);
if (v > bs) {
bs = v;
bx = ix * 0.04;
by = iy * 0.04;
bth = it * deg;
}
}
}
}
const double cx = bx, cy = by, cth = bth;
for (int it = -3; it <= 3; ++it) {
for (int ix = -4; ix <= 4; ++ix) {
for (int iy = -4; iy <= 4; ++iy) {
const double dx = cx + ix * 0.01, dy = cy + iy * 0.01, dth = cth + it * 0.25 * deg;
const double v = score(dx, dy, dth);
if (v > bs) {
bs = v;
bx = dx;
by = dy;
bth = dth;
}
}
}
}
// Mat dinh vi: scan khop KEM 3 lan do lien tiep (1.5 s) -> luong lai dung han.
if (s0 < 0.15) {
if (++slope_low_ >= 3) slope_lost_.store(true);
} else {
slope_low_ = 0;
}
bool have_z = false;
double z_est = 0.0, z_map = 0.0;
if (slope_on_) {
{
std::lock_guard<std::mutex> lk(odom_mu_);
z_est = height_->z();
}
have_z = slam_->HeightAt(pose.x, pose.y, 1.0, &z_map);
}
SlopeSample smp;
smp.loop = loop;
smp.down = down;
smp.speed = speed;
smp.moving = ph == SlopeTestPhase::kDrive;
smp.err_cm = std::hypot(bx, by) * 100.0;
smp.dth_deg = bth / deg;
smp.score = s0;
smp.have_z = have_z;
smp.zerr_cm = have_z ? (z_est - z_map) * 100.0 : 0.0;
slope_samples_.push_back(smp);
if (slope_csv_ != nullptr) {
const double t = std::chrono::duration<double>(now - slope_t0_).count();
std::fprintf(slope_csv_,
"%.2f,%d,%s,%.2f,%s,%.2f,%.1f,%.3f,%.3f,%.2f,%.3f,%.3f,%.1f,%.1f,%.2f,%.1f,%s,%s,%s\n",
t, loop, down ? "xuong" : "len", speed, smp.moving ? "chay" : "dung", along,
cross * 100.0, pose.x, pose.y, pose.theta / deg, s0, bs, bx * 100.0, by * 100.0,
smp.dth_deg, smp.err_cm, have_z ? std::to_string(z_est).c_str() : "",
have_z ? std::to_string(z_map).c_str() : "",
have_z ? std::to_string(smp.zerr_cm).c_str() : "");
std::fflush(slope_csv_);
}
}
void Runtime::SlopeTestSummary(SlopeTestPhase end) {
slope_summary_done_ = true;
if (slope_csv_ != nullptr) {
std::fclose(slope_csv_);
slope_csv_ = nullptr;
}
// Moi dong vua log vua ghi ra logs/slope_test_<gio>_tong.txt: terminal con in
// log khac sau do (node chay tiep khi dung han) va bang troi mat.
std::FILE* tong = nullptr;
std::string tong_path;
if (!slope_csv_path_.empty()) {
tong_path = slope_csv_path_.substr(0, slope_csv_path_.size() - 4) + "_tong.txt";
tong = std::fopen(tong_path.c_str(), "w");
}
auto out = [&](const std::string& line) {
VSLAM_INFO("slope_test: " << line);
if (tong != nullptr) std::fprintf(tong, "%s\n", line.c_str());
};
if (slope_samples_.empty()) {
out(std::string("khong co mau do nao (") + SlopeTestPhaseName(end) + ").");
if (tong != nullptr) std::fclose(tong);
return;
}
// "trung vi / 95% / lon nhat" cua |x|.
auto stats = [](std::vector<double> v) {
for (double& x : v) x = std::fabs(x);
std::sort(v.begin(), v.end());
char buf[64];
if (v.empty()) return std::string("--");
const double med = v[v.size() / 2];
const double p95 = v[std::min(v.size() - 1, static_cast<size_t>(0.95 * (v.size() - 1) + 0.5))];
std::snprintf(buf, sizeof(buf), "%5.1f /%5.1f /%5.1f", med, p95, v.back());
return std::string(buf);
};
out("===== BANG TONG (" + std::string(SlopeTestPhaseName(end)) + ", " +
std::to_string(slope_samples_.size()) + " lan do, 2 Hz) =====");
out(" luot chieu v m/s n | lech cm tv / 95% / max | goc do tv / 95% / max"
" | z sai cm tv / 95% / max | khop tb");
int max_loop = 0;
for (const SlopeSample& s : slope_samples_) max_loop = std::max(max_loop, s.loop);
std::vector<double> first_err, last_err;
for (int l = 1; l <= max_loop; ++l) {
for (const bool dn : {true, false}) {
std::vector<double> e, a, z;
double sc = 0.0, v = 0.0;
for (const SlopeSample& s : slope_samples_) {
if (s.loop != l || s.down != dn) continue;
e.push_back(s.err_cm);
a.push_back(s.dth_deg);
if (s.have_z) z.push_back(s.zerr_cm);
sc += s.score;
v = s.speed;
}
if (e.empty()) continue;
if (l == 1) first_err.insert(first_err.end(), e.begin(), e.end());
if (l == max_loop) last_err.insert(last_err.end(), e.begin(), e.end());
char head[64];
std::snprintf(head, sizeof(head), "%4d %-5s %.2f %3zu", l, dn ? "xuong" : "len", v, e.size());
char sc_buf[16];
std::snprintf(sc_buf, sizeof(sc_buf), "%.3f", sc / e.size());
out(std::string(head) + " | " + stats(e) + " | " + stats(a) + " | " + stats(z) + " | " +
sc_buf);
}
}
std::vector<double> all_e, all_a, all_z;
for (const SlopeSample& s : slope_samples_) {
all_e.push_back(s.err_cm);
all_a.push_back(s.dth_deg);
if (s.have_z) all_z.push_back(s.zerr_cm);
}
out(" TONG | " + stats(all_e) + " | " + stats(all_a) + " | " + stats(all_z) + " |");
if (max_loop > 1) {
out("xu huong lech (trung vi) luot 1 -> luot " + std::to_string(max_loop) + ": " +
stats(first_err).substr(0, 5) + " -> " + stats(last_err).substr(0, 5) +
" cm (tang deu = ban do / dinh vi troi dan)");
}
if (!slope_csv_path_.empty()) out("CSV: " + slope_csv_path_);
if (tong != nullptr) {
std::fclose(tong);
VSLAM_INFO("slope_test: bang tong da ghi ra " << tong_path);
}
}
#endif
} // namespace vslam

View File

@@ -28,6 +28,7 @@
#include <atomic>
#include <chrono>
#include <cstdio>
#include <deque>
#include <functional>
#include <memory>
@@ -57,6 +58,7 @@
#include "vslam/global_localizer.h"
#include "vslam/rtabmap_slam.h"
#include "vslam/visual_verifier.h"
#include "vslam/slope_test.h"
#include "vslam/voxel_export.h"
#include "vslam/vpr_encoder.h"
#endif
@@ -119,6 +121,14 @@ struct RuntimeOptions {
// roi chay bang LiDAR nhu cu.
bool slam_reloc_vpr = false;
std::string vpr_engine; // rong = $VSLAM_RESOURCE_DIR/vpr/eigenplaces_r50_fp16.plan
// --slope-test: test doc tu dong sau khi dinh vi lai (can --slam-reloc). Robot tu
// tim huong doc tren ban do, quay mat xuong, cho nut Y (sim: tu chay), roi xuong /
// len `slope_test_loops` luot, moi luot mot van toc. Xem vslam/slope_test.h.
bool slope_test = false;
int slope_test_loops = 10;
std::vector<double> slope_test_speeds; // rong = mac dinh SlopeTestConfig
// `--slope-test-speeds A:B`: chia DEU tu A toi B cho so luot (vmax > 0 = dung).
double slope_test_vmin = 0.0, slope_test_vmax = 0.0;
};
class Runtime {
@@ -206,6 +216,13 @@ class Runtime {
// node anh. true = da CHOT (da vao RelocBeginSettle).
bool RelocCamera(const LaserScan& scan, const Pose2D& odom,
std::chrono::steady_clock::time_point now);
// Test doc tu dong (--slope-test). Luong slam: lap duong sau khi chot, do 2 Hz
// (lech dinh vi theo do khop scan-ban do, z), ghi CSV, bang tong khi xong.
void SlopeTestSlamStep(const SlamOutput& out, const LaserScan& scan);
void SlopeTestSummary(SlopeTestPhase end);
// Luong RIENG 20 Hz: lai. Vong slam khung 100-600 ms moi lan RTAB-Map xu ly --
// lenh tu do se cu qua cmd_timeout cua FSM va xe giat cuc.
void SlopeLoop();
#endif
void ApplyThreadPriority(std::thread* t, int prio, const char* name);
@@ -472,6 +489,32 @@ class Runtime {
UnixTime reloc_cam_last_stamp_;
int reloc_cam_shots_ = 0; // anh da thu trong vong gom nay
int reloc_cam_best_inl_ = 0; // inlier PnP tot nhat vong nay
// ---- Test doc tu dong (--slope-test) ----
std::mutex slope_mu_; // slope_test_ + slope_planned_
std::unique_ptr<SlopeTest> slope_test_; // null = khong bat
bool slope_planned_ = false;
std::atomic<int> slope_button_{0}; // so lan bam Y (luong tay cam ghi)
std::atomic<bool> slope_lost_{false}; // luong slam bao: mat dinh vi
std::thread slope_thread_;
// CHI luong slam.
bool slope_plan_failed_ = false;
std::chrono::steady_clock::time_point slope_meas_at_;
int slope_low_ = 0; // so lan do lien tiep scan khop kem
std::FILE* slope_csv_ = nullptr;
std::string slope_csv_path_;
std::chrono::steady_clock::time_point slope_t0_;
struct SlopeSample {
int loop;
bool down;
double speed;
bool moving;
double err_cm, dth_deg, score;
bool have_z;
double zerr_cm;
};
std::vector<SlopeSample> slope_samples_;
bool slope_summary_done_ = false;
#endif
// Doc-only sau Start(), nen khong can mutex.