|
|
|
|
@@ -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
|
|
|
|
|
|