feat(leg_pattern): add LegPatternMatcher for 4-leg trolleys

Finds a 4-leg trolley in a raw LaserScan without line extraction:
shadow filter, leg clusters in ray order with free/occluded/attached edges,
pair hypotheses from cart_opening/cart_depth/diagonal, then a Gauss-Newton
pose fit over every leg point with an along-ray residual, Huber weights and
backtracking. Gates: pair spacing after the fit, see-through leg positions,
optional empty interior, entry side (entry tape / prior yaw / facing the
sensor), optional prior, confidence, and ambiguity between two carts.

LegProfile validates every key at load. Two gtests (7 + 23) run on a
synthetic ray-cast scan (test/leg_scan_sim.h). Used by trolley_detection_action.
This commit is contained in:
2026-10-28 13:43:56 +07:00
parent edc6b30431
commit c568e90c53
11 changed files with 3324 additions and 0 deletions

230
test/leg_pose_fit_test.cpp Normal file
View File

@@ -0,0 +1,230 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* Kiểm phép khớp tư thế xe trên mọi điểm chân — điểm sinh bằng ray-cast lên mô hình đã biết, nên
* đáp án biết trước.
*
* Author: DuongTD
*********************************************************************/
#include <gtest/gtest.h>
#include <cmath>
#include <vector>
#include <dock_pattern_detector/leg_pose_fit.h>
#include "leg_scan_sim.h"
namespace
{
using dock_pattern_detector::FitNoise;
using dock_pattern_detector::FitPoint;
using dock_pattern_detector::LegModel;
using dock_pattern_detector::LegProfile;
using dock_pattern_detector::LegShape;
using dock_pattern_detector::Pose2D;
using dock_pattern_detector::PoseFitResult;
constexpr double kDeg = M_PI / 180.0;
/// @brief Xe `tci_trolley` (mô hình sim): cạnh vào 0.517 m, dọc 0.720 m, chân hộp 30 mm.
LegProfile tciTrolley()
{
LegProfile p;
p.cart_opening = 0.517;
p.cart_depth = 0.720;
p.leg_shape = LegShape::kBox;
p.leg_width = 0.030;
return p;
}
/// @brief Điểm của từng chân lấy từ ray-cast, gán đúng góc theo id vật trúng.
std::vector<FitPoint> pointsOf(const LegProfile& p, double cx, double cy, double axis,
double sigma = 0.0)
{
leg_scan_sim::World world;
const std::vector<int> ids = leg_scan_sim::addCart(world, p, cx, cy, axis);
const auto g = leg_scan_sim::s300Geometry();
const leg_scan_sim::Scan scan = leg_scan_sim::rayCast(world, g, sigma);
std::vector<FitPoint> points;
for (std::size_t i = 0; i < g.size; ++i)
{
for (int k = 0; k < 4; ++k)
{
if (scan.hit[i] >= 0 && scan.hit[i] == ids[k])
{
const double b = g.angle_min + static_cast<double>(i) * g.angle_increment;
FitPoint fp;
fp.x = scan.ranges[i] * std::cos(b);
fp.y = scan.ranges[i] * std::sin(b);
fp.corner = k;
points.push_back(fp);
}
}
}
return points;
}
FitNoise s300Noise()
{
FitNoise n;
n.range_sigma = 0.01;
n.angle_increment = leg_scan_sim::s300Geometry().angle_increment;
return n;
}
double yawError(double a, double b)
{
return std::fabs(std::atan2(std::sin(a - b), std::cos(a - b)));
}
TEST(LegPoseFit, ConvergesOnNoiseFreeBoxLegs)
{
const LegProfile p = tciTrolley();
const double cx = 1.26, cy = 0.05, axis = M_PI + 10.0 * kDeg;
const std::vector<FitPoint> pts = pointsOf(p, cx, cy, axis);
ASSERT_GE(pts.size(), 12u);
Pose2D init;
init.x = cx + 0.05; // lệch 5 cm, 5°
init.y = cy - 0.05;
init.yaw = axis + 5.0 * kDeg;
const PoseFitResult fit = fitCartPose(LegModel::fromProfile(p), pts, init, s300Noise());
ASSERT_TRUE(fit.valid);
EXPECT_NEAR(fit.pose.x, cx, 1e-3);
EXPECT_NEAR(fit.pose.y, cy, 1e-3);
EXPECT_LT(yawError(fit.pose.yaw, axis), 0.05 * kDeg);
EXPECT_LT(fit.rms, 1e-3);
}
TEST(LegPoseFit, ConvergesOnRoundLegs)
{
LegProfile p = tciTrolley();
p.leg_shape = LegShape::kRound;
p.leg_width = 0.040;
const double cx = 1.30, cy = -0.10, axis = M_PI - 8.0 * kDeg;
const std::vector<FitPoint> pts = pointsOf(p, cx, cy, axis);
Pose2D init;
init.x = cx - 0.04;
init.y = cy + 0.04;
init.yaw = axis - 4.0 * kDeg;
const PoseFitResult fit = fitCartPose(LegModel::fromProfile(p), pts, init, s300Noise());
ASSERT_TRUE(fit.valid);
EXPECT_NEAR(fit.pose.x, cx, 1e-3);
EXPECT_NEAR(fit.pose.y, cy, 1e-3);
EXPECT_LT(yawError(fit.pose.yaw, axis), 0.05 * kDeg);
}
TEST(LegPoseFit, HuberKeepsOneStrayPointFromPullingThePose)
{
const LegProfile p = tciTrolley();
const double cx = 1.26, cy = 0.0, axis = M_PI;
std::vector<FitPoint> pts = pointsOf(p, cx, cy, axis);
ASSERT_FALSE(pts.empty());
// Một điểm lai lạc 10 cm sau chân trước-trái.
FitPoint stray = pts.front();
stray.x += 0.10;
pts.push_back(stray);
Pose2D init;
init.x = cx + 0.02;
init.y = cy;
init.yaw = axis;
const PoseFitResult fit = fitCartPose(LegModel::fromProfile(p), pts, init, s300Noise());
ASSERT_TRUE(fit.valid);
// Điểm lạc nằm trên tia của chính nó, nên kéo xe DỌC tia đó (−15° so với trục x). Trục dọc (x) có
// mọi mặt trước giữ nên không xê dịch; trục ngang chỉ có điểm mép giữ nên nhích ~2.6 mm — cùng cỡ
// khi không có Huber. Trong matcher, điểm lạc như vậy bị bước "cắt điểm xa" bỏ trước khi khớp.
EXPECT_NEAR(fit.pose.x, cx, 1e-3);
EXPECT_NEAR(fit.pose.y, cy, 3e-3);
}
TEST(LegPoseFit, TwoLegsAreEnoughWhenTheModelIsKnown)
{
const LegProfile p = tciTrolley();
const double cx = 1.26, cy = 0.0, axis = M_PI + 5.0 * kDeg;
std::vector<FitPoint> all = pointsOf(p, cx, cy, axis);
std::vector<FitPoint> pts;
for (const FitPoint& fp : all)
{
if (fp.corner == dock_pattern_detector::kEntryLeft ||
fp.corner == dock_pattern_detector::kEntryRight)
{
pts.push_back(fp);
}
}
ASSERT_GE(pts.size(), 6u);
Pose2D init;
init.x = cx + 0.02;
init.y = cy + 0.02;
init.yaw = axis + 2.0 * kDeg;
const PoseFitResult fit = fitCartPose(LegModel::fromProfile(p), pts, init, s300Noise());
ASSERT_TRUE(fit.valid);
EXPECT_NEAR(fit.pose.x, cx, 2e-3);
EXPECT_NEAR(fit.pose.y, cy, 2e-3);
EXPECT_LT(yawError(fit.pose.yaw, axis), 0.2 * kDeg);
}
TEST(LegPoseFit, IsDeterministic)
{
const LegProfile p = tciTrolley();
const std::vector<FitPoint> pts = pointsOf(p, 1.2, 0.03, M_PI + 0.1, 0.01);
Pose2D init;
init.x = 1.22;
init.y = 0.0;
init.yaw = M_PI + 0.08;
const PoseFitResult a = fitCartPose(LegModel::fromProfile(p), pts, init, s300Noise());
const PoseFitResult b = fitCartPose(LegModel::fromProfile(p), pts, init, s300Noise());
EXPECT_EQ(a.pose.x, b.pose.x);
EXPECT_EQ(a.pose.y, b.pose.y);
EXPECT_EQ(a.pose.yaw, b.pose.yaw);
EXPECT_EQ(a.iterations, b.iterations);
}
TEST(LegPoseFit, ProcrustesRecoversAnExactPose)
{
const LegModel m = LegModel::fromProfile(tciTrolley());
Pose2D truth;
truth.x = 1.5;
truth.y = -0.3;
truth.yaw = 2.7;
std::vector<std::array<double, 2>> model_pts, scan_pts;
for (int k = 0; k < 3; ++k)
{
const std::array<double, 2> c = m.corner(k);
model_pts.push_back(c);
scan_pts.push_back({ truth.x + std::cos(truth.yaw) * c[0] - std::sin(truth.yaw) * c[1],
truth.y + std::sin(truth.yaw) * c[0] + std::cos(truth.yaw) * c[1] });
}
Pose2D out;
ASSERT_TRUE(dock_pattern_detector::procrustes2D(model_pts, scan_pts, out));
EXPECT_NEAR(out.x, truth.x, 1e-9);
EXPECT_NEAR(out.y, truth.y, 1e-9);
EXPECT_NEAR(yawError(out.yaw, truth.yaw), 0.0, 1e-9);
}
TEST(LegPoseFit, BoxSignedDistanceIsZeroOnTheFaceAndSignedInside)
{
const LegModel m = LegModel::fromProfile(tciTrolley());
const std::array<double, 2> c = m.corner(dock_pattern_detector::kEntryLeft);
EXPECT_NEAR(m.signedDistance(dock_pattern_detector::kEntryLeft, c[0] + 0.015, c[1]), 0.0, 1e-12);
EXPECT_NEAR(m.signedDistance(dock_pattern_detector::kEntryLeft, c[0], c[1]), -0.015, 1e-12);
EXPECT_NEAR(m.signedDistance(dock_pattern_detector::kEntryLeft, c[0] + 0.115, c[1]), 0.10, 1e-12);
}
} // namespace
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}