Mirror tape on the KIT lidar blooms: a taped leg returns a saturated core plus a skirt of dim rays whose range climbs smoothly to the background. Clustering by geometry first made taped legs attached (no depth jump at the edge) or too wide for the width gate, so the robot found the cart in 6 of 227 scans at 1.2 m. The saturated core also reads 1-3 cm short. All changes sit behind tape_enabled plus their own key; with the keys at 0 or tape off the detector runs exactly as before. - tape_bloom_max_angle [rad]: bright runs become leg candidates before geometric clustering. The bloom skirt is walked and masked, and the edge class comes from the walk. A bright core spread over more than the leg's apparent width in range is rejected: it is a leg corrupted by a nearer mirror's bloom. - tape_range_bias_sigma [m]: the pose fit estimates one common range bias for bright points (x, y, yaw, b) with a Gaussian prior. This only happens when at least one bare leg anchors the range; otherwise the 3-DoF fit is unchanged. |b| > 0.1 m invalidates the fit. Pair spacing and measured dimensions use bias-corrected leg centres. - Real KIT scans with intensities: 9 from the 2-leg-taped run (11:06) and 2 with all four legs taped (12:12).
381 lines
13 KiB
C++
381 lines
13 KiB
C++
/*********************************************************************
|
||
*
|
||
* 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 <array>
|
||
#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, EstimatesCommonBrightRangeBiasAndZeroSigmaKeepsThreeDoF)
|
||
{
|
||
const LegProfile p = tciTrolley();
|
||
const double cx = 1.26, cy = 0.04, axis = M_PI + 7.0 * kDeg;
|
||
std::vector<FitPoint> points = pointsOf(p, cx, cy, axis);
|
||
ASSERT_GE(points.size(), 12u);
|
||
for (FitPoint& point : points)
|
||
{
|
||
if (point.corner == dock_pattern_detector::kEntryLeft ||
|
||
point.corner == dock_pattern_detector::kEntryRight)
|
||
{
|
||
point.bright = true;
|
||
const double range = std::hypot(point.x, point.y);
|
||
const double scale = (range - 0.025) / range;
|
||
point.x *= scale;
|
||
point.y *= scale;
|
||
}
|
||
}
|
||
|
||
Pose2D initial;
|
||
initial.x = cx;
|
||
initial.y = cy;
|
||
initial.yaw = axis;
|
||
FitNoise corrected_noise = s300Noise();
|
||
corrected_noise.bright_range_bias_sigma = 0.05;
|
||
const PoseFitResult corrected =
|
||
fitCartPose(LegModel::fromProfile(p), points, initial, corrected_noise);
|
||
ASSERT_TRUE(corrected.valid) << corrected.reason;
|
||
EXPECT_TRUE(corrected.bright_range_bias_estimated);
|
||
EXPECT_NEAR(corrected.bright_range_bias, 0.025, 0.003);
|
||
EXPECT_NEAR(corrected.pose.x, cx, 0.003);
|
||
EXPECT_NEAR(corrected.pose.y, cy, 0.003);
|
||
EXPECT_LT(yawError(corrected.pose.yaw, axis), 0.3 * kDeg);
|
||
EXPECT_LT(corrected.rms, 1e-3);
|
||
|
||
FitNoise legacy_noise = s300Noise();
|
||
const PoseFitResult legacy_with_flags =
|
||
fitCartPose(LegModel::fromProfile(p), points, initial, legacy_noise);
|
||
std::vector<FitPoint> points_without_flags = points;
|
||
for (FitPoint& point : points_without_flags)
|
||
{
|
||
point.bright = false;
|
||
}
|
||
const PoseFitResult legacy_without_flags =
|
||
fitCartPose(LegModel::fromProfile(p), points_without_flags, initial, legacy_noise);
|
||
ASSERT_TRUE(legacy_with_flags.valid) << legacy_with_flags.reason;
|
||
ASSERT_TRUE(legacy_without_flags.valid) << legacy_without_flags.reason;
|
||
EXPECT_FALSE(legacy_with_flags.bright_range_bias_estimated);
|
||
EXPECT_EQ(legacy_with_flags.pose.x, legacy_without_flags.pose.x);
|
||
EXPECT_EQ(legacy_with_flags.pose.y, legacy_without_flags.pose.y);
|
||
EXPECT_EQ(legacy_with_flags.pose.yaw, legacy_without_flags.pose.yaw);
|
||
EXPECT_EQ(legacy_with_flags.rms, legacy_without_flags.rms);
|
||
EXPECT_EQ(legacy_with_flags.iterations, legacy_without_flags.iterations);
|
||
}
|
||
|
||
TEST(LegPoseFit, MixedBrightAndDimPointsOnEveryCornerDoNotEstimateBias)
|
||
{
|
||
const LegProfile p = tciTrolley();
|
||
const double cx = 1.26, cy = 0.04, axis = M_PI + 7.0 * kDeg;
|
||
std::vector<FitPoint> points = pointsOf(p, cx, cy, axis);
|
||
ASSERT_GE(points.size(), 12u);
|
||
|
||
std::array<std::size_t, dock_pattern_detector::kLegCount> bright_by_corner = { { 0, 0, 0, 0 } };
|
||
std::array<std::size_t, dock_pattern_detector::kLegCount> dim_by_corner = { { 0, 0, 0, 0 } };
|
||
std::vector<FitPoint> dim_points;
|
||
dim_points.reserve(points.size() * 2);
|
||
for (FitPoint& point : points)
|
||
{
|
||
point.bright = true;
|
||
++bright_by_corner[static_cast<std::size_t>(point.corner)];
|
||
for (double offset : { 0.004, 0.008 })
|
||
{
|
||
FitPoint dim = point;
|
||
const double range = std::hypot(dim.x, dim.y);
|
||
const double scale = (range + offset) / range;
|
||
dim.x *= scale;
|
||
dim.y *= scale;
|
||
dim.bright = false;
|
||
dim_points.push_back(dim);
|
||
++dim_by_corner[static_cast<std::size_t>(dim.corner)];
|
||
}
|
||
}
|
||
for (int corner = 0; corner < dock_pattern_detector::kLegCount; ++corner)
|
||
{
|
||
EXPECT_GT(bright_by_corner[static_cast<std::size_t>(corner)], 0u) << corner;
|
||
EXPECT_GT(dim_by_corner[static_cast<std::size_t>(corner)], 0u) << corner;
|
||
}
|
||
points.insert(points.end(), dim_points.begin(), dim_points.end());
|
||
|
||
Pose2D initial;
|
||
initial.x = cx + 0.01;
|
||
initial.y = cy - 0.01;
|
||
initial.yaw = axis + 1.0 * kDeg;
|
||
FitNoise legacy_noise = s300Noise();
|
||
FitNoise positive_sigma = legacy_noise;
|
||
positive_sigma.bright_range_bias_sigma = 0.05;
|
||
|
||
const LegModel model = LegModel::fromProfile(p);
|
||
const PoseFitResult legacy = fitCartPose(model, points, initial, legacy_noise);
|
||
const PoseFitResult with_sigma = fitCartPose(model, points, initial, positive_sigma);
|
||
ASSERT_TRUE(legacy.valid) << legacy.reason;
|
||
ASSERT_TRUE(with_sigma.valid) << with_sigma.reason;
|
||
EXPECT_FALSE(with_sigma.bright_range_bias_estimated);
|
||
EXPECT_EQ(with_sigma.pose.x, legacy.pose.x);
|
||
EXPECT_EQ(with_sigma.pose.y, legacy.pose.y);
|
||
EXPECT_EQ(with_sigma.pose.yaw, legacy.pose.yaw);
|
||
EXPECT_EQ(with_sigma.rms, legacy.rms);
|
||
EXPECT_EQ(with_sigma.normalized_rms, legacy.normalized_rms);
|
||
EXPECT_EQ(with_sigma.iterations, legacy.iterations);
|
||
}
|
||
|
||
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();
|
||
}
|