slam,odometry,perception: di tren doc (G4, slam.yaml slope.enabled, mac dinh tat) -- z robot = tich -ds*sin(pitch IMU) kep phang < 1.5 do va neo vao ban do (HeightEstimator), odometry ngang, scan cho SLAM bu nghieng + bo tia cham san/doc theo ban do do cao tu depth (ScanLeveler, ElevationMap), z/roll/pitch luu vao node + do thi z 1-D + mat san theo node (height_map), voxel dat o z that va vat can tinh tu san cuc bo, xoa tuong gia LiDAR tren doc noi camera da thay mat san, web z uoc luong, --sim-imu-hz

This commit is contained in:
trung09122002
2026-10-04 00:03:02 +07:00
parent 6735b53423
commit f064dcf3ec
32 changed files with 1799 additions and 10 deletions

View File

@@ -52,6 +52,7 @@ endif()
if(VSLAM_BUILD_ODOMETRY)
vslam_add_test(test_wheel_odometry LIBS vslam_odometry vslam_common)
vslam_add_test(test_ekf_se2 LIBS vslam_odometry vslam_common)
vslam_add_test(test_height_estimator LIBS vslam_odometry vslam_common)
endif()
if(VSLAM_BUILD_SAFETY)
@@ -73,6 +74,7 @@ endif()
if(VSLAM_BUILD_PERCEPTION)
vslam_add_test(test_perception LIBS vslam_perception vslam_common)
vslam_add_test(test_scan_leveler LIBS vslam_perception vslam_common)
if(VSLAM_BUILD_SIM)
# Test TICH HOP sim -> perception. Khoa quy uoc Z-depth cua anh mo phong
# va bai "san phang thi khong duoc bao vuc" -- thu ma ban ROS chi kiem
@@ -107,6 +109,7 @@ endif()
if(VSLAM_BUILD_SLAM)
vslam_add_test(test_slam LIBS vslam_slam vslam_common)
vslam_add_test(test_voxel_export LIBS vslam_slam vslam_common)
vslam_add_test(test_height_map LIBS vslam_slam vslam_common)
vslam_add_test(test_global_localizer LIBS vslam_slam vslam_common)
endif()

View File

@@ -0,0 +1,89 @@
// HeightEstimator: z tren doc tu ds*sin(pitch), kep mat bang, neo ban do.
#include <gtest/gtest.h>
#include <cmath>
#include "vslam/height_estimator.h"
using namespace vslam;
namespace {
constexpr double kDeg = M_PI / 180.0;
// Lai `dist` m doc mat doc voi pitch co dinh, buoc 1 cm.
void Drive(HeightEstimator* h, double dist, double pitch) {
const double step = dist >= 0 ? 0.01 : -0.01;
const int n = static_cast<int>(std::round(std::fabs(dist) / 0.01));
for (int i = 0; i < n; ++i) h->OnWheelStep(step, pitch);
}
} // namespace
TEST(HeightEstimator, LenDocDungDoCao) {
// Doc 15 do, cao 1.5 m: duong doc dai 1.5 / sin(15) = 5.796 m. Len doc thi
// mui NGOC len: pitch AM (quy uoc ROS, duong = chuc xuong).
HeightEstimator h;
Drive(&h, 3.0, 0.0);
Drive(&h, 1.5 / std::sin(15 * kDeg), -15 * kDeg);
Drive(&h, 3.0, 0.0);
EXPECT_NEAR(h.z(), 1.5, 0.002);
}
TEST(HeightEstimator, LuiXuongDocCungDung) {
// Lui xuong doc: ds am, mui van ngoc (pitch am) -> z giam.
HeightEstimator h;
h.Reset(1.5, 0.0);
Drive(&h, -1.5 / std::sin(10 * kDeg), -10 * kDeg);
EXPECT_NEAR(h.z(), 0.0, 0.002);
}
TEST(HeightEstimator, KepMatBangTrietTieuBiasPitch) {
// Bias pitch 0.5 do tren san phang 100 m: khong kep la 87 cm, kep la 0.
HeightEstimator h;
Drive(&h, 100.0, 0.5 * kDeg);
EXPECT_DOUBLE_EQ(h.z(), 0.0);
EXPECT_EQ(h.stats().clamped, h.stats().steps);
EXPECT_FALSE(h.on_slope());
}
TEST(HeightEstimator, DocNhoHonNguongBiCoiLaPhang) {
// Gioi han NOI RO: doc 1 do < nguong 1.5 do -> bi bo qua.
HeightEstimator h;
Drive(&h, 30.0, -1.0 * kDeg);
EXPECT_DOUBLE_EQ(h.z(), 0.0);
}
TEST(HeightEstimator, NeoKeoVeZBanDo) {
// Sau mot doc, z lech 10 cm (vd truot banh): neo keo ve gan ban do.
HeightEstimator h;
Drive(&h, 5.0 * 1.1, -15 * kDeg); // truot 10%: dem nhieu hon that
const double z_true = 5.0 * std::sin(15 * kDeg);
ASSERT_GT(h.z() - z_true, 0.1);
EXPECT_TRUE(h.Anchor(z_true));
EXPECT_LT(std::fabs(h.z() - z_true), 0.03);
// Neo lan nua thi sat hon, P giam.
const double s1 = h.sigma();
EXPECT_TRUE(h.Anchor(z_true));
EXPECT_LT(h.sigma(), s1);
}
TEST(HeightEstimator, NeoQuaXaBiTuChoi) {
// z ban do lech 1 m so voi uoc luong vua di 3 m phang: nhieu kha nang la
// node sai tang / sai cho -- khong duoc keo.
HeightEstimator h;
Drive(&h, 3.0, 0.0);
EXPECT_FALSE(h.Anchor(1.0));
EXPECT_DOUBLE_EQ(h.z(), 0.0);
EXPECT_EQ(h.stats().anchors_rejected, 1u);
}
TEST(HeightEstimator, TrenPhangNeoKeoNhe) {
// Di 50 m phang (P nho): neo lech 5 cm chi keo mot phan -- mot node ban do
// khong duoc dat lai z o noi robot da biet chac.
HeightEstimator h;
h.Reset(0.0, 0.0);
Drive(&h, 1.0, 0.0);
h.Anchor(0.05);
EXPECT_GT(h.z(), 0.0);
EXPECT_LT(h.z(), 0.03);
}

99
test/test_height_map.cc Normal file
View File

@@ -0,0 +1,99 @@
// Do thi z 1-D va mat san theo node (height_map.h).
#include <gtest/gtest.h>
#include <cmath>
#include <map>
#include <vector>
#include "vslam/height_map.h"
using namespace vslam;
namespace {
constexpr double kDeg = M_PI / 180.0;
}
TEST(ZGraph, VongCoDocDonSaiDeuVaKhepVe0) {
// Vong: 10 node phang z=0, 6 node len doc 15 do (+0.25 moi node), 10 node
// phang tren z=1.5, 6 node xuong, 10 node phang ve gan cho xuat phat. z DO
// troi them 1 cm moi node tren doc (truot banh): ve den noi dz do = +0.12.
std::map<int, NodeHeight> meas;
int id = 1;
double z = 0.0;
auto push = [&](int n, double step, double pitch) {
for (int k = 0; k < n; ++k) {
z += step;
meas[id++] = NodeHeight{z, 0.0, pitch};
}
};
push(10, 0.0, 0.0);
push(6, 0.25 + 0.01, -15 * kDeg);
push(10, 0.0, 0.0);
push(6, -0.25 + 0.01, 15 * kDeg);
push(10, 0.0, 0.0);
const int last = id - 1;
ASSERT_NEAR(meas[last].z, 0.12, 1e-9);
std::vector<ZGraphLink> links;
for (int i = 1; i < last; ++i) links.push_back(ZGraphLink{i, i + 1, true, 1.0, 0.0});
// Ve dung cho cu: vong dong node cuoi -> node dau, ca hai phang.
links.push_back(ZGraphLink{last, 1, false, 0.05, 0.0});
const std::map<int, double> zo = SolveZGraph(meas, links);
EXPECT_NEAR(zo.at(last), zo.at(1), 0.02);
EXPECT_NEAR(zo.at(1), 0.0, 0.01);
// San tren: that 1.5, do 1.56. Sai don ra -> gan 1.5 hon so do.
EXPECT_LT(std::fabs(zo.at(20) - 1.5), std::fabs(meas.at(20).z - 1.5));
}
TEST(ZGraph, VongTrenDocTheoMatPhangDoc) {
// Hai lan qua CUNG doc 15 do: node B nam 1 m phia truoc node A (doc len). dz
// dung = 1 * tan(15) = 0.268, KHONG phai 0. So do cua B lech 0.2 m.
std::map<int, NodeHeight> meas;
meas[1] = NodeHeight{0.5, 0.0, -15 * kDeg};
meas[2] = NodeHeight{0.5 + 0.268 + 0.2, 0.0, -15 * kDeg};
std::vector<ZGraphLink> links{ZGraphLink{1, 2, false, 1.0, 0.0}};
ZGraphConfig cfg;
cfg.sigma_first = 0.001;
const std::map<int, double> zo = SolveZGraph(meas, links, cfg);
EXPECT_NEAR(zo.at(2) - zo.at(1), std::tan(15 * kDeg), 0.02);
}
TEST(ZGraph, NodeKhongLienKetGiuZDo) {
std::map<int, NodeHeight> meas;
meas[1] = NodeHeight{0.0, 0.0, 0.0};
meas[7] = NodeHeight{1.5, 0.0, 0.0};
const std::map<int, double> zo = SolveZGraph(meas, {});
EXPECT_NEAR(zo.at(7), 1.5, 1e-6);
}
TEST(FloorModel, MatPhangDocVaNodeGanNhat) {
// Node phang o x = -2 (z 0), node tren doc 15 do o x = 0 (z 0.5, huong +x,
// mui ngoc = pitch am).
std::vector<FloorNode> nodes(2);
nodes[0].x = -2.0;
nodes[1].z = 0.5;
nodes[1].pitch = -15 * kDeg;
FloorModel fm;
fm.Build(nodes, 4.0, 0.25);
double z = 0;
ASSERT_TRUE(fm.FloorAt(1.0, 0.0, &z));
EXPECT_NEAR(z, 0.5 + std::tan(15 * kDeg), 1e-6);
// Gan node phang hon -> mat phang phang.
ASSERT_TRUE(fm.FloorAt(-1.7, 0.1, &z));
EXPECT_NEAR(z, 0.0, 1e-9);
// Ngoai ban kinh moi node.
EXPECT_FALSE(fm.FloorAt(10.0, 0.0, &z));
}
TEST(FloorModel, RollNghiengSangTrai) {
// roll duong = ben TRAI (+y) cao len.
std::vector<FloorNode> nodes(1);
nodes[0].theta = M_PI / 2; // huong +y cua map; ben trai = -x
nodes[0].roll = 5 * kDeg;
FloorModel fm;
fm.Build(nodes);
double z = 0;
ASSERT_TRUE(fm.FloorAt(-1.0, 0.0, &z));
EXPECT_NEAR(z, std::tan(5 * kDeg), 1e-6);
}

237
test/test_scan_leveler.cc Normal file
View File

@@ -0,0 +1,237 @@
// ScanLeveler + ElevationMap: bu nghieng scan LiDAR tren doc, bo tia cham
// san / mat doc.
//
// Hinh hoc robot nay: laser_link o (0, 0, 0.13) tren base_link, banh r 0.0865
// -> LiDAR cao 0.2165 m tren san khi robot nam ngang.
#include <gtest/gtest.h>
#include <cmath>
#include <limits>
#include <vector>
#include "vslam/elevation_map.h"
#include "vslam/geometry.h"
#include "vslam/gravity_align.h"
#include "vslam/scan_leveler.h"
using namespace vslam;
namespace {
constexpr double kDeg = M_PI / 180.0;
constexpr double kWheel = 0.0865;
Transform LaserToBase() {
Transform t = TransformIdentity();
t.translation.z = 0.13;
return t;
}
// 360 tia, 1 do / tia, tia i o goc -180 + i do.
LaserScan EmptyScan() {
LaserScan s;
s.angle_min = static_cast<float>(-M_PI);
s.angle_increment = static_cast<float>(kDeg);
s.angle_max = static_cast<float>(-M_PI + 359 * kDeg);
s.range_min = 0.05f;
s.range_max = 30.0f;
s.ranges.assign(360, std::numeric_limits<float>::infinity());
return s;
}
size_t Bin(double deg) { return static_cast<size_t>(std::lround(deg + 180.0)); }
// Goc LiDAR trong frame base_footprint da can, luc than nghieng (roll, pitch).
Point LaserOrigin(double roll, double pitch) {
const Transform T = TransformCompose(TiltLift(roll, pitch, kWheel), LaserToBase());
return TransformPoint(T, Point{0, 0, 0});
}
// Huong tia goc a trong frame da can. CHI phep quay: `TransformVector` cua
// repo cong ca tinh tien.
Vector3 RayDir(double a, double roll, double pitch) {
const Transform T = TransformCompose(TiltLift(roll, pitch, kWheel), LaserToBase());
return RotateVector(T.rotation, Vector3{std::cos(a), std::sin(a), 0.0});
}
} // namespace
TEST(ScanLeveler, NamNgangThiGiuNguyen) {
// Tuong x = 3 m: robot ngang thi scan ra phai Y HET vao.
LaserScan in = EmptyScan();
for (int d = -60; d <= 60; ++d) in.ranges[Bin(d)] = static_cast<float>(3.0 / std::cos(d * kDeg));
LaserScan out;
ScanLevelStats st;
LevelScan(in, LaserToBase(), 0.0, 0.0, kWheel, Pose2D{}, 0.0, nullptr, ScanLevelerConfig{}, &out, &st);
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;
}
}
EXPECT_EQ(st.rebinned, 0u);
EXPECT_EQ(st.kept, 121u);
}
TEST(ScanLeveler, LenDocTuongDoRaKhoangCachNgang) {
// Than ngoc 15 do (pitch AM). Tuong dung o x = 2.0 m trong frame
// base_footprint da can: tia 0 do di xien len, cham tuong o r > 2, va scan
// can phai bao DUNG 2.0 m (RTAB-Map dat diem bang ngoai lai tinh (0, 0)).
const double pitch = -15 * kDeg;
const Point o = LaserOrigin(0.0, pitch);
const Vector3 d = RayDir(0.0, 0.0, pitch);
const double r = (2.0 - o.x) / d.x;
LaserScan in = EmptyScan();
in.ranges[Bin(0)] = static_cast<float>(r);
LaserScan out;
LevelScan(in, LaserToBase(), 0.0, pitch, kWheel, Pose2D{}, 0.0, nullptr, ScanLevelerConfig{}, &out,
nullptr);
EXPECT_GT(r, 2.05);
EXPECT_NEAR(out.ranges[Bin(0)], 2.0, 1e-4);
}
TEST(ScanLeveler, TiaXienDoiSangOGocMoi) {
// Than ngoc 15 do: tia 45 do chieu xuong mat phang ngang thanh ~46 do.
const double pitch = -15 * kDeg;
LaserScan in = EmptyScan();
in.ranges[Bin(45)] = 3.0f;
LaserScan out;
ScanLevelStats st;
LevelScan(in, LaserToBase(), 0.0, pitch, kWheel, Pose2D{}, 0.0, nullptr, ScanLevelerConfig{}, &out, &st);
const double a_new = std::atan2(std::sin(45 * kDeg), std::cos(45 * kDeg) * std::cos(15 * kDeg));
const size_t j = Bin(std::round(a_new / kDeg));
EXPECT_NE(j, Bin(45));
EXPECT_TRUE(std::isfinite(out.ranges[j]));
// O cu khong con tia nao: "khong do duoc" (NaN), khong phai "trong" (inf).
EXPECT_TRUE(std::isnan(out.ranges[Bin(45)]));
EXPECT_EQ(st.rebinned, 1u);
}
TEST(ScanLeveler, XuongDocTiaChamSanDuoiBiBo) {
// Dang xuong doc 15 do (chuc mui = pitch DUONG), giua doc o z = 0.75: tia
// truoc cam xuong san duoi chan doc. Khong co ElevationMap van bo duoc vi
// diem cham THAP hon mat dat duoi robot.
const double pitch = 15 * kDeg, z_robot = 0.75;
const Point o = LaserOrigin(0.0, pitch);
const Vector3 d = RayDir(0.0, 0.0, pitch);
const double r = (o.z + z_robot) / -d.z; // toi z tuyet doi = 0
LaserScan in = EmptyScan();
in.ranges[Bin(0)] = static_cast<float>(r);
LaserScan out;
ScanLevelStats st;
LevelScan(in, LaserToBase(), 0.0, pitch, kWheel, Pose2D{}, z_robot, nullptr, ScanLevelerConfig{},
&out, &st);
EXPECT_TRUE(std::isnan(out.ranges[Bin(0)]));
EXPECT_EQ(st.dropped_below, 1u);
}
TEST(ScanLeveler, TuongGiaChanDocLenBoKhiCameraDaThayDoc) {
// Robot ngang tren san, chan doc 15 do o x = 1.0 m: tia ngang cao 0.2165 m
// cham mat doc o x = 1.0 + 0.2165 / tan(15) = 1.808 m -- "tuong" gia.
const double foot = 1.0, t15 = std::tan(15 * kDeg);
const double h_laser = LaserOrigin(0.0, 0.0).z;
const double x_hit = foot + h_laser / t15;
LaserScan in = EmptyScan();
in.ranges[Bin(0)] = static_cast<float>(x_hit);
// Khong co ban do do cao: khong phan biet duoc voi tuong that -> GIU (gioi
// han da noi ro trong scan_leveler.h).
LaserScan out;
LevelScan(in, LaserToBase(), 0.0, 0.0, kWheel, Pose2D{}, 0.0, nullptr, ScanLevelerConfig{}, &out,
nullptr);
EXPECT_NEAR(out.ranges[Bin(0)], x_hit, 1e-4);
// Camera da thay mat doc (diem moi 2 cm tren be mat): bo.
ElevationMap em;
std::vector<PointXYZ> pts;
for (double x = 0.0; x < 4.0; x += 0.02) {
for (double y = -0.5; y <= 0.5; y += 0.02) {
pts.push_back(PointXYZ{static_cast<float>(x), static_cast<float>(y),
static_cast<float>(x > foot ? (x - foot) * t15 : 0.0)});
}
}
em.AddPoints(pts, UnixTime::fromNSec(1));
ScanLevelStats st;
LevelScan(in, LaserToBase(), 0.0, 0.0, kWheel, Pose2D{}, 0.0, &em, ScanLevelerConfig{}, &out, &st);
EXPECT_TRUE(std::isnan(out.ranges[Bin(0)]));
EXPECT_EQ(st.dropped_floor, 1u);
}
TEST(ScanLeveler, TuongThatCanhDocVanGiu) {
// Tuong that o x = 1.8 m ngay canh vung doc: san duoi chan tuong (camera
// thay) o z = 0 -> diem cao 0.2165 m tren san -> GIU.
ElevationMap em;
std::vector<PointXYZ> pts;
for (double x = 0.0; x < 3.0; x += 0.02) pts.push_back(PointXYZ{static_cast<float>(x), 0.0f, 0.0f});
em.AddPoints(pts, UnixTime::fromNSec(1));
LaserScan in = EmptyScan();
in.ranges[Bin(0)] = 1.8f;
LaserScan out;
LevelScan(in, LaserToBase(), 0.0, 0.0, kWheel, Pose2D{}, 0.0, &em, ScanLevelerConfig{}, &out, nullptr);
EXPECT_NEAR(out.ranges[Bin(0)], 1.8, 1e-4);
}
TEST(ScanLeveler, PoseOdomDungTrongElevationMap) {
// ElevationMap o frame ODOM: robot o (10, 5) quay 90 do, tia 0 do huong +y
// cua odom. Mat doc o vung y > 6 chi co o odom -- phai tra dung o.
const double t15 = std::tan(15 * kDeg);
ElevationMap em;
std::vector<PointXYZ> pts;
for (double y = 5.0; y < 9.0; y += 0.02) {
for (double x = 9.5; x <= 10.5; x += 0.02) {
pts.push_back(PointXYZ{static_cast<float>(x), static_cast<float>(y),
static_cast<float>(y > 6.0 ? (y - 6.0) * t15 : 0.0)});
}
}
em.AddPoints(pts, UnixTime::fromNSec(1));
const double h_laser = LaserOrigin(0.0, 0.0).z;
LaserScan in = EmptyScan();
in.ranges[Bin(0)] = static_cast<float>(1.0 + h_laser / t15);
LaserScan out;
LevelScan(in, LaserToBase(), 0.0, 0.0, kWheel, Pose2D{10.0, 5.0, M_PI / 2}, 0.0, &em,
ScanLevelerConfig{}, &out, nullptr);
EXPECT_TRUE(std::isnan(out.ranges[Bin(0)]));
}
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.
LaserScan in = EmptyScan();
in.ranges[Bin(90)] = in.range_max + 1.0f;
in.ranges[Bin(89)] = 2.0f;
LaserScan out;
LevelScan(in, LaserToBase(), 0.0, -15 * kDeg, kWheel, Pose2D{}, 0.0, nullptr, ScanLevelerConfig{}, &out,
nullptr);
EXPECT_FLOAT_EQ(out.ranges[Bin(90)], in.range_max + 1.0f);
}
TEST(ElevationMap, MinVetBanhVaPrune) {
ElevationMap em;
double z = 0;
EXPECT_FALSE(em.FloorAt(0.05, 0.05, &z));
// Hai diem cung o: giu MIN.
em.AddPoints({PointXYZ{0.05f, 0.05f, 0.30f}, PointXYZ{0.06f, 0.04f, 0.10f}}, UnixTime::fromNSec(1));
ASSERT_TRUE(em.FloorAt(0.05, 0.05, &z));
EXPECT_FLOAT_EQ(z, 0.10f);
// Toa do am khong dung o voi toa do duong (khoa ghep hai chi so co dau).
em.AddPoints({PointXYZ{-0.05f, 0.05f, 1.0f}, PointXYZ{0.05f, -0.05f, 2.0f}}, UnixTime::fromNSec(1));
ASSERT_TRUE(em.FloorAt(-0.05, 0.05, &z));
EXPECT_FLOAT_EQ(z, 1.0f);
ASSERT_TRUE(em.FloorAt(0.05, -0.05, &z));
EXPECT_FLOAT_EQ(z, 2.0f);
// Vet banh GHI DE (ke ca cao hon min cu).
em.AddFootprint(0.0, 0.0, 0.5, 0.25, UnixTime::fromNSec(2));
ASSERT_TRUE(em.FloorAt(0.05, 0.05, &z));
EXPECT_FLOAT_EQ(z, 0.5f);
// Prune: o xa hon ban kinh bi bo.
em.AddPoints({PointXYZ{20.0f, 0.0f, 0.0f}}, UnixTime::fromNSec(3));
em.Prune(0.0, 0.0, UnixTime::fromNSec(4));
EXPECT_FALSE(em.FloorAt(20.0, 0.0, &z));
EXPECT_TRUE(em.FloorAt(0.05, 0.05, &z));
// Prune: o cu hon max_age bi bo.
em.Prune(0.0, 0.0, UnixTime::fromNSec(static_cast<int64_t>(61e9)));
EXPECT_EQ(em.size(), 0u);
}