map,lidar,web: ban do theo lan nhin MOI NHAT + scan dung gio. Luoi 2D: GridGlobal/* (3 lan nhin xuyen qua -> trong, truoc ~9; truoc 07/10 khong truyen vao bo ghep luoi); lop camera + 3D: xoa voxel ma >= 2 node sau nhin xuyen qua (VoxelCarver) -- nguoi dung roi di thi quay lai quet la mat. LiDAR: stamp_offset -55 ms + chinh meo theo chieu quet (do tren ban do that 28/09 + 01/10); web: robot troi muot

This commit is contained in:
trung09122002
2026-10-07 16:04:28 +07:00
parent 61c3a75fb1
commit 31f84eb082
20 changed files with 855 additions and 50 deletions

View File

@@ -6,6 +6,7 @@
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>
@@ -232,6 +233,102 @@ TEST(ScanLeveler, ScanLakiBeam0Den2PiGiuCaHaiNua) {
}
}
namespace {
// Phong chu nhat |x| <= 4, |y| <= 3 (khung laser luc stamp). Tia tu (ox, oy)
// huong th -> khoang cach toi tuong.
double RoomRay(double ox, double oy, double th) {
const double c = std::cos(th), s = std::sin(th);
double t = 1e9;
if (c > 1e-12) t = std::min(t, (4.0 - ox) / c);
if (c < -1e-12) t = std::min(t, (-4.0 - ox) / c);
if (s > 1e-12) t = std::min(t, (3.0 - oy) / s);
if (s < -1e-12) t = std::min(t, (-3.0 - oy) / s);
return t;
}
// Scan kieu LakiBeam (1440 tia, [0, 2pi), quat mu 90 do phia sau) chup khi
// robot quay w, tien v: tia i chup truoc stamp `lag` giay, luc do robot lui
// v*lag va quay -w*lag so voi luc stamp.
LaserScan LakiScan(const SweepTiming& tm, double w, double v) {
const size_t n = 1440;
const double inc = 2.0 * M_PI / static_cast<double>(n);
LaserScan s;
s.angle_min = 0.0f;
s.angle_increment = static_cast<float>(inc);
s.angle_max = static_cast<float>(2.0 * M_PI - inc);
s.range_min = 0.05f;
s.range_max = 30.0f;
s.ranges.assign(n, std::numeric_limits<float>::infinity());
for (size_t i = 0; i < n; ++i) {
const double a = static_cast<double>(i) * inc;
if (std::fabs(NormalizeAngle(a - M_PI)) < 45 * kDeg) continue; // quat mu
double f = std::fmod(tm.dir > 0 ? a - tm.start_rad : tm.start_rad - a, 2.0 * M_PI);
if (f < 0.0) f += 2.0 * M_PI;
const double lag = tm.period_s * (1.0 - f / (2.0 * M_PI));
const double yaw = -w * lag;
s.ranges[i] = static_cast<float>(RoomRay(-v * lag, 0.0, a + yaw));
}
return s;
}
// Sai lech |r - r_dung| tren cac o ca hai cung huu han; tra trung vi va p95.
void RangeErr(const LaserScan& got, const LaserScan& ref, double* med, double* p95) {
std::vector<double> e;
for (size_t i = 0; i < ref.ranges.size(); ++i) {
if (std::isfinite(got.ranges[i]) && std::isfinite(ref.ranges[i])) {
e.push_back(std::fabs(got.ranges[i] - ref.ranges[i]));
}
}
std::sort(e.begin(), e.end());
*med = e.empty() ? 1e9 : e[e.size() / 2];
*p95 = e.empty() ? 1e9 : e[e.size() * 95 / 100];
}
} // namespace
TEST(ScanLeveler, ChinhMeoDungYenThiGiuNguyen) {
const SweepTiming tm{178.75 * kDeg, -1, 0.04};
const LaserScan in = LakiScan(tm, 0.0, 0.0);
LaserScan out;
DeskewScan(in, LaserToBase(), tm, 0.0, 0.0, &out);
ASSERT_EQ(out.ranges.size(), in.ranges.size());
for (size_t i = 0; i < in.ranges.size(); ++i) {
if (std::isfinite(in.ranges[i])) {
EXPECT_NEAR(out.ranges[i], in.ranges[i], 1e-4) << "tia " << i;
} else {
EXPECT_TRUE(std::isinf(out.ranges[i])) << "tia " << i;
}
}
}
TEST(ScanLeveler, ChinhMeoKhiQuayVaChayKhopScanDungYen) {
// Robot quay 0.8 rad/s (toi da tay cam) va tien 0.5 m/s: scan tho lech scan
// dung yen tai stamp; chinh meo dung chieu quet phai keo ve gan nhu trung.
const SweepTiming tm{178.75 * kDeg, -1, 0.04};
const LaserScan ref = LakiScan(tm, 0.0, 0.0);
const LaserScan raw = LakiScan(tm, 0.8, 0.5);
double med_raw = 0, p95_raw = 0;
RangeErr(raw, ref, &med_raw, &p95_raw);
LaserScan out;
DeskewScan(raw, LaserToBase(), tm, 0.8, 0.5, &out);
double med = 0, p95 = 0;
RangeErr(out, ref, &med, &p95);
EXPECT_GT(p95_raw, 0.05); // khong chinh: lech ro
EXPECT_LT(med, 0.005);
EXPECT_LT(p95, 0.02);
EXPECT_LT(p95, p95_raw / 4.0);
// Sai chieu quet thi KHONG keo ve duoc -- chieu la thu phai do (07/10: do
// tren ban do that, dir -1).
SweepTiming wrong = tm;
wrong.dir = +1;
DeskewScan(raw, LaserToBase(), wrong, 0.8, 0.5, &out);
double med_w = 0, p95_w = 0;
RangeErr(out, ref, &med_w, &p95_w);
EXPECT_GT(p95_w, 2.0 * p95);
}
TEST(ScanLeveler, SentinelSectorMaskGiuNguyen) {
// Tia bi sector mask cat (range_max + 1 = than robot): giu nguyen, va khong
// tia nao duoc xep de len o do.

View File

@@ -8,11 +8,18 @@
#include <gtest/gtest.h>
#include <rtabmap/core/LocalGrid.h>
#include <rtabmap/core/Parameters.h>
#include <rtabmap/core/global_map/OccupancyGrid.h>
#include <opencv2/core.hpp>
#include <cmath>
#include <map>
#include <cstdlib>
#include <limits>
#include <utility>
#include <vector>
#include "vslam/geometry.h"
#include "vslam/occupancy_export.h"
@@ -388,6 +395,68 @@ TEST(OccupancyExport, DbKhongTonTaiThiBaoKhongTonTai) {
EXPECT_FALSE(f.exists);
}
namespace {
// Luoi cuc bo 2D cua mot node: 1 x N, CV_32FC2 (x, y) trong frame node.
cv::Mat Cells(const std::vector<std::pair<float, float>>& xy) {
cv::Mat m(1, static_cast<int>(xy.size()), CV_32FC2);
for (size_t i = 0; i < xy.size(); ++i) {
m.at<cv::Vec2f>(0, static_cast<int>(i)) = cv::Vec2f(xy[i].first, xy[i].second);
}
return m;
}
// O tai (x, y) sau khi ghep 6 node: node 1-3 thay NGUOI o x = 1.0 (tia dung o
// do); node 4-6 tia di XUYEN qua x = 1.0 toi tuong x = 2.0.
int NguoiRoiDi(const rtabmap::ParametersMap& grid_global, float x, float y) {
rtabmap::LocalGridCache cache;
std::map<int, rtabmap::Transform> poses;
for (int id = 1; id <= 6; ++id) {
const bool nguoi = id <= 3;
const float hit = nguoi ? 1.0f : 2.0f;
std::vector<std::pair<float, float>> empty;
for (float e = 0.05f; e < hit - 0.01f; e += 0.05f) empty.emplace_back(e, 0.0f);
// Mot o lech ngang: RTAB-Map KHONG dung ban do khi moi o thang hang
// (khung ymin == ymax).
empty.emplace_back(0.05f, 0.5f);
cache.add(id, cv::Mat(), Cells({{hit, 0.0f}}), Cells(empty), 0.05f);
poses[id] = rtabmap::Transform::getIdentity();
}
rtabmap::ParametersMap p = grid_global;
p[rtabmap::Parameters::kGridCellSize()] = "0.05";
p[rtabmap::Parameters::kGrid3D()] = "false";
rtabmap::OccupancyGrid grid(&cache, p);
grid.update(poses);
float xmin = 0.0f, ymin = 0.0f;
const cv::Mat m = grid.getMap(xmin, ymin);
const int c = static_cast<int>(std::floor((x - xmin) / 0.05f));
const int r = static_cast<int>(std::floor((y - ymin) / 0.05f));
if (m.empty() || r < 0 || c < 0 || r >= m.rows || c >= m.cols) return -2;
return m.at<int8_t>(r, c);
}
} // namespace
TEST(OccupancyExport, LuoiTheoLanNhinMoiNhat) {
// 07/10: 3 nguoi dung tren duong luc quet, roi di; quay lai quet van con tren
// ban do. GridGlobal/* cua slam.yaml (repo): 3 lan nhin xuyen qua -> TRONG.
// Mac dinh RTAB-Map (kep 0.971, truot 0.4): van CHIEM -- do la loi cu.
const char* dir = std::getenv("VSLAM_CONFIG_SRC_DIR");
if (dir == nullptr) GTEST_SKIP() << "khong co VSLAM_CONFIG_SRC_DIR";
RtabmapConfig c = LoadRtabmapConfig(ConfigNode::LoadFile(std::string(dir) + "/slam.yaml"));
ASSERT_TRUE(ApplySlamProfile("real", &c).ok());
std::vector<std::string> unknown;
const std::map<std::string, std::string> all = BuildRtabmapParams(c, &unknown);
rtabmap::ParametersMap gg;
for (const auto& kv : all) {
if (kv.first.rfind("GridGlobal/", 0) == 0) gg.insert(kv);
}
ASSERT_FALSE(gg.empty());
EXPECT_EQ(NguoiRoiDi(gg, 1.0f, 0.0f), 0); // cho nguoi dung: TRONG
EXPECT_EQ(NguoiRoiDi(gg, 2.0f, 0.0f), 100); // tuong van CHIEM
EXPECT_EQ(NguoiRoiDi(rtabmap::ParametersMap(), 1.0f, 0.0f), 100); // mac dinh: loi cu
}
TEST(SensorDataBuilder, FrameLaserSaiLamMoiDiemLechDungBangNo) {
// Loi da mac: `RtabmapSlam` co bien `laser_to_base` nhung KHONG co setter.
// Moi scan bi dat o goc toa do thay vi o (0.24, 0, 0.20). ICP van hoi tu --

View File

@@ -382,3 +382,121 @@ TEST(VoxelHitCounter, CatZVanDungNhuVoxelAccumulator) {
ASSERT_EQ(g.cells.size(), 1u);
EXPECT_TRUE(Has(g.cells, 0, 0, 10));
}
// ---------------- Lan nhin moi nhat quyet dinh (07/10) ----------------
// 3 nguoi dung tren duong luc quet thanh vat can VINH VIEN: lop camera chi
// them, khong bao gio xoa. VoxelCarver hoi cac node SAU lan cuoi voxel duoc
// thay: nhin xuyen qua -> xoa; con thay -> giu; bi che -> khong biet.
namespace {
// Camera cao 0.4 m nhin +x, anh nho 80x60, f = 60 px. Tuong o x = 3 m.
// `nguoi`: khoi depth 1.5 m giua anh. `vat_gan` > 0: khoi do o khoang cach nay
// (che tam nhin).
NodeDepthView Canh(bool nguoi, double vat_gan = 0.0) {
NodeDepthView v;
v.w = 80;
v.h = 60;
v.fx = v.fy = 60.0f;
v.cx = 39.5f;
v.cy = 29.5f;
const float cam[12] = {0, 0, 1, 0, -1, 0, 0, 0, 0, -1, 0, 0.4f}; // base <- optical
std::copy(cam, cam + 12, v.cam);
v.depth_mm.assign(80 * 60, 3000);
for (int y = 22; y < 38; ++y) {
for (int x = 32; x < 48; ++x) {
if (nguoi) v.depth_mm[y * 80 + x] = 1500;
if (vat_gan > 0.0) v.depth_mm[y * 80 + x] = static_cast<uint16_t>(vat_gan * 1000.0);
}
}
return v;
}
const float kPoseGoc[12] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0};
} // namespace
TEST(VoxelCarver, NguoiDaDiThiXoa) {
// Node 0-2 thay nguoi o x = 1.5 m; node 3-5 nhin thang qua cho do toi tuong.
std::vector<NodeDepthView> v;
for (int n = 0; n < 6; ++n) v.push_back(Canh(n < 3));
VoxelCarver c(3.5, 2);
for (int n = 0; n < 6; ++n) c.AddView(n, kPoseGoc, &v[n]);
EXPECT_TRUE(c.Gone(1.5, 0.0, 0.4, 2)); // lan cuoi thay: node 2
// Tuong: lan cuoi thay la node 5, khong con node nao sau -> giu.
EXPECT_FALSE(c.Gone(3.0, 0.0, 0.4, 5));
}
TEST(VoxelCarver, LanNhinMoiNhatQuyetDinh) {
// Node 3-4 nhin xuyen qua nhung node 5 (MOI NHAT) lai thay nguoi -> giu.
std::vector<NodeDepthView> v;
for (int n = 0; n < 6; ++n) v.push_back(Canh(n < 3 || n == 5));
VoxelCarver c(3.5, 2);
for (int n = 0; n < 6; ++n) c.AddView(n, kPoseGoc, &v[n]);
EXPECT_FALSE(c.Gone(1.5, 0.0, 0.4, 2));
}
TEST(VoxelCarver, BiCheThiKhongXoa) {
// Node sau co vat GAN hon (0.8 m) che tam nhin: khong biet -> khong xoa.
std::vector<NodeDepthView> v;
for (int n = 0; n < 6; ++n) v.push_back(n < 3 ? Canh(true) : Canh(false, 0.8));
VoxelCarver c(3.5, 2);
for (int n = 0; n < 6; ++n) c.AddView(n, kPoseGoc, &v[n]);
EXPECT_FALSE(c.Gone(1.5, 0.0, 0.4, 2));
}
TEST(VoxelCarver, NgoaiKhungNhinThiKhongTinh) {
std::vector<NodeDepthView> v;
for (int n = 0; n < 6; ++n) v.push_back(Canh(false));
VoxelCarver c(3.5, 2);
for (int n = 0; n < 6; ++n) c.AddView(n, kPoseGoc, &v[n]);
EXPECT_FALSE(c.Gone(-1.0, 0.0, 0.4, 0)); // sau lung camera
EXPECT_FALSE(c.Gone(5.0, 0.0, 0.4, 0)); // xa hon ban kinh 3.5 m
EXPECT_FALSE(c.Gone(1.0, 3.0, 0.4, 0)); // ngoai goc nhin
}
TEST(VoxelHitCounter, XoaVoxelMaNodeSauNhinXuyenQua) {
// 6 node deu thay tuong (3, 0, 0.4); node 0-2 thay them nguoi (1.5, 0, 0.4).
// Node 3-5 nhin xuyen qua cho nguoi -> nguoi bi xoa, tuong giu.
VoxelExportOptions o = Opt(0.05);
o.min_node_hits = 3;
VoxelHitCounter h(o);
std::vector<NodeDepthView> v;
for (int n = 0; n < 6; ++n) v.push_back(Canh(n < 3));
VoxelCarver c(3.5, 2);
for (int n = 0; n < 6; ++n) {
c.AddView(n, kPoseGoc, &v[n]);
h.BeginNode(n);
h.AddPoint(3.01, 0.01, 0.41);
if (n < 3) h.AddPoint(1.51, 0.01, 0.41);
h.EndNode();
}
VoxelGrid g;
h.Finish(&g, &c);
EXPECT_EQ(h.carved(), 1u);
ASSERT_EQ(g.cells.size(), 1u);
EXPECT_TRUE(Has(g.cells, 60, 0, 8)); // tuong
}
TEST(NodeDepthView, LayMinMoiKhoiVaChiaTieuCu) {
// Depth 320x240 nen 3 m, MOT pixel vat gan 1 m: khoi 4x4 chua no phai la
// 1 m (than trong -- chan nguoi vai pixel van la "thay vat").
const int w = 320, h = 240;
cv::Mat depth(h, w, CV_16UC1, cv::Scalar(3000));
depth.at<uint16_t>(121, 161) = 1000;
cv::Mat gray = cv::Mat::zeros(h, w, CV_8UC1);
const rtabmap::Transform base_to_optical =
rtabmap::Transform(0, 0, 0.4f, 0, 0, 0) * rtabmap::CameraModel::opticalRotation();
rtabmap::CameraModel model(300.0, 300.0, 159.5, 119.5, base_to_optical, 0.0,
cv::Size(w, h));
rtabmap::SensorData data(gray, depth, model, 1, 0.0);
NodeDepthView v;
NodeDepthViewFromSensorData(data, 4, &v);
ASSERT_EQ(v.w, 80);
ASSERT_EQ(v.h, 60);
EXPECT_NEAR(v.fx, 75.0, 1e-3);
EXPECT_NEAR(v.cx, 39.5, 1e-3);
EXPECT_EQ(v.depth_mm[30 * 80 + 40], 1000);
EXPECT_EQ(v.depth_mm[0], 3000);
EXPECT_NEAR(v.cam[11], 0.4, 1e-6);
}