sim: dia hinh 3D -- doc va san cao tu model vslam_terrain_*, robot di tren mat san, LiDAR tia 3D, ground truth 6-DoF, world slope_test (5 doc 5-30 do)

This commit is contained in:
trung09122002
2026-10-03 18:31:13 +07:00
parent be2aa17e3e
commit 26ee1a3b72
27 changed files with 2373 additions and 37 deletions

311
test/test_sim_terrain.cc Normal file
View File

@@ -0,0 +1,311 @@
// Dia hinh 3D trong bo mo phong: doc, san cao, robot di TREN mat san.
//
// ================== VI SAO BAI KIEM NAY TON TAI ==================
// Truoc day bo mo phong phang hoan toan: robot o z = 0, LiDAR quet mat phang
// ngang, mat doc trong world thanh TUONG. Du an phuc vu robot di giua cac toa
// nha qua doc, va moi phep do tren doc (SLAM, VO, z) deu dua vao viec bo mo
// phong noi dung su that. Bai nay khoa cac su that do bang so tinh tay:
//
// * mat doc la SAN, do cao tai x bang x*tan(goc);
// * vat tren san cao van la vat can; mat ban KHONG thanh san;
// * robot len doc: z = x*tan, pitch = -goc, encoder dem quang duong DOC doc;
// * LiDAR dung o san phang thay "tuong gia" o chan doc cach 0.2165/tan(goc);
// * tich phan gyro bang dung goc nghieng that (khong thi VIO vo nghia);
// * mep bac chan robot.
//
// Fixture: test/data/world/ramp.world -- MOT doc 10 do len san cao 1.5 m.
#include <gtest/gtest.h>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include "vslam/gazebo_world.h"
#include "vslam/sim_source.h"
using namespace vslam;
namespace {
constexpr double kDeg = M_PI / 180.0;
constexpr double kH = 1.5; // san tren cua fixture
constexpr double kRampDeg = 10.0;
const double kTan = std::tan(kRampDeg * kDeg);
const double kRampLen = kH / kTan; // 8.507 m
// LiDAR robot that: tam truc + 0.13 m, banh r = 0.0865 m (robot.yaml).
constexpr double kLidarAboveFloor = 0.0865 + 0.13;
std::string WorldDir() {
const char* e = std::getenv("VSLAM_TEST_DATA_DIR");
return (e != nullptr ? std::string(e) : std::string("test/data")) + "/world";
}
GazeboWorld LoadRamp() {
GazeboWorld w;
const StatusResponse st = LoadGazeboWorld(WorldDir() + "/ramp.world",
{WorldDir() + "/models"}, &w);
EXPECT_TRUE(st.ok()) << st.message;
return w;
}
size_t CellOf(const OccupancyGrid& g, double x, double y) {
const int cx = static_cast<int>(std::floor((x - g.origin.position.x) / g.resolution));
const int cy = static_cast<int>(std::floor((y - g.origin.position.y) / g.resolution));
return static_cast<size_t>(cy) * g.width + static_cast<size_t>(cx);
}
double CellCenterX(const OccupancyGrid& g, double x) {
const double c = std::floor((x - g.origin.position.x) / g.resolution);
return g.origin.position.x + (c + 0.5) * g.resolution;
}
SimConfig RampCfg() {
SimConfig c;
c.speed = 0.0;
c.enable_depth_camera = false;
c.world_path = WorldDir() + "/ramp.world";
c.world_model_paths = {WorldDir() + "/models"};
c.wheel_radius = 0.0865;
c.wheel_separation = 0.40;
c.lidar_x = 0.0;
c.lidar_z = 0.13;
c.lidar_noise_stddev = 0.0;
c.gyro_noise_stddev = 0.0;
c.accel_noise_stddev = 0.0;
c.start_x = -2.0;
c.start_y = 0.0;
c.start_yaw = 0.0;
return c;
}
// Lai thang cho toi khi x vuot `x_stop`, hoac het `max_s` giay mo phong.
void DriveUntilX(SimSource* sim, double v, double x_stop, double max_s = 80.0) {
for (double t = 0.0; t < max_s; t += 0.05) {
sim->base()->SetVelocity(v, 0.0);
sim->RunFor(0.05);
if (sim->ground_truth().x >= x_stop) return;
}
}
// Tia thang truoc (goc 0) cua mot scan.
double FrontRange(const LaserScan& s) {
const int i = static_cast<int>(std::lround(-s.angle_min / s.angle_increment));
return s.ranges[static_cast<size_t>(i)];
}
} // namespace
// --- Raster world ------------------------------------------------------------
TEST(GazeboTerrain, DocLaSanKhongPhaiTuong) {
const GazeboWorld w = LoadRamp();
OccupancyGrid g;
WorldTerrain t;
ASSERT_TRUE(RasterizeGazeboWorld(w, WorldRasterOptions{}, &g, &t).ok());
ASSERT_EQ(t.floor_z.size(), static_cast<size_t>(g.width) * g.height);
EXPECT_GT(t.terrain_cells, 0u);
EXPECT_NEAR(t.floor_max, kH, 1e-3);
// Gia tri lay tai TAM o: so voi x tam o, khong voi x hoi.
for (double x : {1.0, 3.0, 5.0, 7.0}) {
const size_t i = CellOf(g, x, 0.0);
EXPECT_NEAR(t.floor_z[i], CellCenterX(g, x) * kTan, 1e-3) << "x = " << x;
EXPECT_LT(g.data[i], 50) << "mat doc thanh VAT CAN o x = " << x;
}
EXPECT_NEAR(t.floor_z[CellOf(g, 10.0, 0.0)], kH, 1e-4);
EXPECT_NEAR(t.floor_z[CellOf(g, -2.0, 0.0)], 0.0, 1e-6);
}
TEST(GazeboTerrain, VatTrenSanCaoVanLaVatCan) {
// Cot z 1.5..2.5 dung tren san cao. Dai z tuyet doi cu [0.05, 1.2] bo no --
// robot se di xuyen qua. Tinh tu san cuc bo thi no la vat can.
const GazeboWorld w = LoadRamp();
OccupancyGrid g;
WorldTerrain t;
ASSERT_TRUE(RasterizeGazeboWorld(w, WorldRasterOptions{}, &g, &t).ok());
const size_t i = CellOf(g, 10.5, 0.0);
EXPECT_GE(g.data[i], 50);
EXPECT_NEAR(t.wall_top[i], 2.5, 1e-3);
}
TEST(GazeboTerrain, MatBanKhongThanhSan) {
// Mat ban huong LEN nhung khong phai dia hinh: van la vat can, san duoi = 0.
const GazeboWorld w = LoadRamp();
OccupancyGrid g;
WorldTerrain t;
ASSERT_TRUE(RasterizeGazeboWorld(w, WorldRasterOptions{}, &g, &t).ok());
const size_t i = CellOf(g, -2.5, 1.0);
EXPECT_GE(g.data[i], 50);
EXPECT_NEAR(t.floor_z[i], 0.0, 1e-6);
}
TEST(GazeboTerrain, WorldPhangRaLuoiNhuCu) {
// World khong co model dia hinh: hai ban ham phai ra CUNG mot luoi -- day la
// dieu kien de ban ghi hoi quy cu khong doi.
GazeboWorld w;
ASSERT_TRUE(LoadGazeboWorld(WorldDir() + "/mini.world", {WorldDir() + "/models"}, &w).ok());
OccupancyGrid a, b;
WorldTerrain t;
ASSERT_TRUE(RasterizeGazeboWorld(w, WorldRasterOptions{}, &a).ok());
ASSERT_TRUE(RasterizeGazeboWorld(w, WorldRasterOptions{}, &b, &t).ok());
EXPECT_EQ(t.terrain_cells, 0u);
ASSERT_EQ(a.width, b.width);
ASSERT_EQ(a.height, b.height);
EXPECT_EQ(a.data, b.data);
}
// --- Bo mo phong -------------------------------------------------------------
TEST(SimTerrain, TuBatKhiWorldCoDiaHinh) {
{
SimSource sim(RampCfg());
ASSERT_TRUE(sim.Open().ok());
EXPECT_TRUE(sim.terrain_active());
}
{
SimConfig c = RampCfg();
c.terrain = SimConfig::TerrainMode::kOff;
SimSource sim(c);
ASSERT_TRUE(sim.Open().ok());
EXPECT_FALSE(sim.terrain_active());
EXPECT_EQ(sim.ground_truth_3d().z, 0.0);
}
}
TEST(SimTerrain, LenDocZVaPitchDung) {
SimSource sim(RampCfg());
ASSERT_TRUE(sim.Open().ok());
sim.base()->Open();
DriveUntilX(&sim, 0.35, 4.0);
const SimPose3 p = sim.ground_truth_3d();
ASSERT_GE(p.x, 4.0);
EXPECT_NEAR(p.z, p.x * kTan, 0.01);
// Pitch AM = mui ngoc len. Toc do da on dinh, lo xo giam xoc da tat dao dong.
EXPECT_NEAR(p.pitch, -kRampDeg * kDeg, 0.2 * kDeg);
EXPECT_NEAR(p.roll, 0.0, 1e-6);
// Len het doc: san tren phang.
DriveUntilX(&sim, 0.35, kRampLen + 1.0);
const SimPose3 q = sim.ground_truth_3d();
EXPECT_NEAR(q.z, kH, 0.01);
EXPECT_NEAR(q.pitch, 0.0, 0.2 * kDeg);
}
TEST(SimTerrain, EncoderDemQuangDuongDocMatDoc) {
// Banh lan TREN mat doc: encoder dem quang duong doc doc, dai hon quang
// duong ngang 1/cos(goc). Odometry 2D cong thang so nay vao ban do ngang la
// sai 1.5% o 10 do, 15% o 30 do.
SimSource sim(RampCfg());
ASSERT_TRUE(sim.Open().ok());
EncoderReading last;
sim.base()->SetEncoderCallback([&](const EncoderReading& e) { last = e; });
sim.base()->Open();
DriveUntilX(&sim, 0.30, 2.0);
const double x1 = sim.ground_truth().x;
const double e1 = (last.left_rad + last.right_rad) / 2.0;
DriveUntilX(&sim, 0.30, 6.0);
const double x2 = sim.ground_truth().x;
const double e2 = (last.left_rad + last.right_rad) / 2.0;
const double along = (e2 - e1) * 0.0865;
EXPECT_NEAR(along / (x2 - x1), 1.0 / std::cos(kRampDeg * kDeg), 0.003)
<< "doc doc " << along << " m, ngang " << (x2 - x1) << " m";
}
TEST(SimTerrain, LidarThayTuongGiaOChanDoc) {
// Dung o san phang, cach chan doc 1.5 m, nhin vao doc. Tia ngang o
// 0.2165 m cham mat doc o 0.2165 / tan(10 do) = 1.228 m sau chan doc.
SimConfig c = RampCfg();
c.start_x = -1.5;
{
SimSource sim(c);
ASSERT_TRUE(sim.Open().ok());
LaserScanPtr scan;
sim.lidar()->SetCallback([&](LaserScanPtr s) { scan = s; });
sim.lidar()->Open();
sim.RunFor(0.1);
ASSERT_TRUE(scan != nullptr);
EXPECT_NEAR(FrontRange(*scan), 1.5 + kLidarAboveFloor / kTan, 0.02);
}
{
// Tat dia hinh 3D: LiDAR 2D cu khong thay doc (doc la san), chi thay cot
// tren san cao o x = 10.3.
c.terrain = SimConfig::TerrainMode::kOff;
SimSource sim(c);
ASSERT_TRUE(sim.Open().ok());
LaserScanPtr scan;
sim.lidar()->SetCallback([&](LaserScanPtr s) { scan = s; });
sim.lidar()->Open();
sim.RunFor(0.1);
ASSERT_TRUE(scan != nullptr);
EXPECT_GT(FrontRange(*scan), 5.0);
}
}
TEST(SimTerrain, TichPhanGyroBangGocNghiengThat) {
// Gyro va gia toc ke phai noi CUNG mot chuyen. Truoc day pitch do dia hinh
// vao trong luc nhung khong vao gyro: tich phan gyro bao "chua he nghieng"
// trong khi gia toc ke bao "dang len doc 10 do".
SimConfig c = RampCfg();
c.start_x = -1.0;
SimSource sim(c);
ASSERT_TRUE(sim.Open().ok());
double integ = 0.0;
const double dt = 1.0 / c.imu_rate_hz;
sim.mcu()->SetImuCallback([&](ImuPtr m) { integ += m->angular_velocity.y * dt; });
sim.mcu()->Open();
sim.base()->Open();
const double p0 = sim.ground_truth_3d().pitch;
DriveUntilX(&sim, 0.35, 2.0);
const double p1 = sim.ground_truth_3d().pitch;
ASSERT_LT(p1, -5.0 * kDeg) << "chua len doc";
EXPECT_NEAR(integ, p1 - p0, 1e-3);
}
TEST(SimTerrain, MepBacChanRobot) {
// Bac cao 0.3 m truoc mat: khong leo duoc, robot dung truoc mep, van o z = 0.
SimConfig c;
c.speed = 0.0;
c.enable_depth_camera = false;
c.terrain = SimConfig::TerrainMode::kOn;
SimSource sim(c);
ASSERT_TRUE(sim.Open().ok());
sim.SetFloorHeight(1.0, -3.0, 3.0, 3.0, 0.3f);
sim.base()->Open();
for (int i = 0; i < 100; ++i) { sim.base()->SetVelocity(0.3, 0.0); sim.RunFor(0.05); }
const SimPose3 p = sim.ground_truth_3d();
EXPECT_LT(p.x, 1.0 - c.robot_radius + 0.05);
EXPECT_NEAR(p.z, 0.0, 1e-6);
EXPECT_GT(sim.stats().collisions, 0u);
}
TEST(SimTerrain, DocTongHopKhongCanWorld) {
// SetFloorRamp: doc tuyen tinh cho bai kiem khong co file world.
SimConfig c;
c.speed = 0.0;
c.enable_depth_camera = false;
c.terrain = SimConfig::TerrainMode::kOn;
c.wheel_separation = 0.40;
SimSource sim(c);
ASSERT_TRUE(sim.Open().ok());
// 15 do: len 0.536 m tren 2 m.
const double tan15 = std::tan(15.0 * kDeg);
sim.SetFloorRamp(1.0, -3.0, 3.0, 3.0, 0.0f, static_cast<float>(2.0 * tan15));
sim.base()->Open();
for (int i = 0; i < 200 && sim.ground_truth().x < 2.0; ++i) {
sim.base()->SetVelocity(0.3, 0.0);
sim.RunFor(0.05);
}
const SimPose3 p = sim.ground_truth_3d();
ASSERT_GE(p.x, 2.0);
EXPECT_NEAR(p.z, (p.x - 1.0) * tan15, 0.01);
EXPECT_NEAR(p.pitch, -15.0 * kDeg, 0.3 * kDeg);
}