Files
dock_pattern_detector/test/leg_pose_fit_test.cpp
duongtd cb401e6316 fix(leg_pattern): let cart_dim_tolerance widen the confidence score
The fit term divided the residual by the lidar noise only, so a trolley that
is a few cm off the profile (machining tolerance between carts of the same
type) scored a "poor fit" even though its pose was accurate: 3 legs seen and
a 3 cm error gave confidence 0.38 < 0.5 and the action timed out on the KIT
robot.

Add FitNoise::model_sigma and divide by sqrt(sigma^2 + model_sigma^2) when
scoring. The matcher passes cart_dim_tolerance / 2. Only the score changes:
the fit weights, the pose and the rms are bit-identical. A cart far outside
the tolerance is still rejected by the pair gates.

Tests: model_sigma changes the score but not the pose; a cart 3 cm narrower
and 2.5 cm longer than the profile is accepted with 4 and with 3 legs; a cart
15 cm wider is rejected. v/arc/circle tests unchanged.
2026-10-28 15:48:49 +07:00

269 lines
8.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*********************************************************************
*
* 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, ModelSigmaChangesTheScoreButNotThePose)
{
// Xe thật lệch so với profile 3 cm cạnh vào, 2.5 cm cạnh dọc: sai số gia công của xe cùng loại.
LegProfile real = tciTrolley();
real.cart_opening = 0.487;
real.cart_depth = 0.745;
const LegProfile model = tciTrolley();
const double cx = 1.3, cy = 0.0, axis = M_PI + 5.0 * kDeg;
const std::vector<FitPoint> pts = pointsOf(real, cx, cy, axis, 0.01);
Pose2D init;
init.x = cx + 0.02;
init.y = cy;
init.yaw = axis;
const FitNoise strict = s300Noise(); // model_sigma = 0: như trước khi có khoá này
FitNoise lenient = strict;
lenient.model_sigma = 0.02; // = cart_dim_tolerance / 2 của profile mặc định
const PoseFitResult a = fitCartPose(LegModel::fromProfile(model), pts, init, strict);
const PoseFitResult b = fitCartPose(LegModel::fromProfile(model), pts, init, lenient);
ASSERT_TRUE(a.valid);
ASSERT_TRUE(b.valid);
// Trọng số của phép khớp không dùng model_sigma, nên kết quả khớp phải y hệt từng bit.
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.rms, b.rms);
EXPECT_EQ(a.iterations, b.iterations);
// Điểm số thì khác: mẫu số σ² + model_sigma² làm phần dư do lệch mô hình đỡ nặng hơn rõ rệt.
EXPECT_GT(a.normalized_rms, 1.0);
EXPECT_LT(b.normalized_rms, 0.6 * a.normalized_rms);
EXPECT_EQ(FitNoise().model_sigma, 0.0);
}
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();
}