fix(leg_pattern): segment mirror-taped legs by intensity and estimate their range bias

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).
This commit is contained in:
2026-10-29 13:05:55 +07:00
parent 979bef360d
commit 347896e252
12 changed files with 1144 additions and 20 deletions

View File

@@ -20,7 +20,9 @@
#include <gtest/gtest.h>
#include <cmath>
#include <limits>
#include <string>
#include <utility>
#include <vector>
#include <dock_pattern_detector/leg_pattern_matcher.h>
@@ -80,6 +82,93 @@ LegDetection detect(const LegProfile& p, const World& world, double sigma = 0.0,
return matcher.match(g, scan.ranges, scan.intensities, s300Limits(), Pose2D());
}
LegDetection matchScan(const LegProfile& p, const robot_line_extraction::ScanGeometry& geometry,
const leg_scan_sim::Scan& scan)
{
LegPatternMatcher matcher;
std::string error;
EXPECT_TRUE(matcher.configure(p, error)) << error;
return matcher.match(geometry, scan.ranges, scan.intensities, s300Limits(), Pose2D());
}
void addClimbingBloomSkirts(leg_scan_sim::Scan& scan, double threshold)
{
std::vector<std::pair<std::size_t, std::size_t>> runs;
std::size_t i = 0;
while (i < scan.intensities.size())
{
if (scan.intensities[i] < threshold)
{
++i;
continue;
}
const std::size_t first = i;
while (i + 1 < scan.intensities.size() && scan.intensities[i + 1] >= threshold)
{
++i;
}
runs.emplace_back(first, i);
++i;
}
const auto paint = [&](std::size_t edge, int step) {
const double r0 = scan.ranges[edge];
for (int k = 1; k <= 4; ++k)
{
const long index = static_cast<long>(edge) + step * k;
if (index < 0 || index >= static_cast<long>(scan.ranges.size()) ||
scan.intensities[index] >= threshold)
{
break;
}
scan.ranges[index] = r0 + 0.008 * k;
scan.intensities[index] = 100.0f;
}
const long background = static_cast<long>(edge) + step * 5;
if (background >= 0 && background < static_cast<long>(scan.ranges.size()) &&
scan.intensities[background] < threshold)
{
scan.ranges[background] = r0 + 0.20;
scan.intensities[background] = 100.0f;
}
};
for (const auto& run : runs)
{
paint(run.first, -1);
paint(run.second, +1);
}
}
std::pair<std::size_t, std::size_t> firstBrightRun(const leg_scan_sim::Scan& scan,
double threshold)
{
for (std::size_t i = 0; i < scan.intensities.size(); ++i)
{
if (scan.intensities[i] < threshold)
{
continue;
}
std::size_t last = i;
while (last + 1 < scan.intensities.size() && scan.intensities[last + 1] >= threshold)
{
++last;
}
return { i, last };
}
return { scan.intensities.size(), scan.intensities.size() };
}
LegProfile reflectiveTrolley()
{
LegProfile p = tciTrolley();
p.tape_enabled = true;
p.tape_legs = TapeLegs::kEntry;
p.tape_min_intensity = 1000.0;
p.tape_min_points = 2;
p.tape_bloom_max_angle = 0.10;
return p;
}
// ================================================================================================
// 1. Dò đúng
// ================================================================================================
@@ -345,6 +434,148 @@ TEST(LegPatternMatcher, EntryTapeDecidesWhichEndIsTheEntry)
}
}
TEST(LegPatternMatcher, BrightCoreWithClimbingBloomIsFreeAndNotAttached)
{
const LegProfile p = reflectiveTrolley();
World world;
const bool taped[4] = { true, true, false, false };
leg_scan_sim::addCart(world, p, kCx, 0.0, M_PI, nullptr, taped);
const auto geometry = leg_scan_sim::s300Geometry();
leg_scan_sim::Scan scan = leg_scan_sim::rayCast(world, geometry);
addClimbingBloomSkirts(scan, p.tape_min_intensity);
const LegDetection d = matchScan(p, geometry, scan);
ASSERT_TRUE(d.valid) << d.reason;
EXPECT_EQ(d.legs_seen, 4);
EXPECT_EQ(d.legs_attached, 0);
EXPECT_EQ(d.legs_partial, 0);
}
TEST(LegPatternMatcher, BrightCoreAgainstSameRangeWallRemainsAttached)
{
LegProfile p = reflectiveTrolley();
p.accept_attached_legs = true;
World world;
const bool taped[4] = { true, true, false, false };
leg_scan_sim::addCart(world, p, kCx, 0.0, M_PI, nullptr, taped);
const auto geometry = leg_scan_sim::s300Geometry();
leg_scan_sim::Scan scan = leg_scan_sim::rayCast(world, geometry);
addClimbingBloomSkirts(scan, p.tape_min_intensity);
const auto run = firstBrightRun(scan, p.tape_min_intensity);
ASSERT_LT(run.first, scan.ranges.size());
const double r0 = scan.ranges[run.first];
for (int step = 1; step <= 17; ++step)
{
const long index = static_cast<long>(run.first) - step;
ASSERT_GE(index, 0);
if (scan.intensities[index] >= p.tape_min_intensity)
{
break;
}
scan.ranges[index] = r0 + 0.002 * step;
scan.intensities[index] = 100.0f;
}
const LegDetection d = matchScan(p, geometry, scan);
ASSERT_TRUE(d.valid) << d.reason;
EXPECT_GE(d.legs_attached, 1);
}
TEST(LegPatternMatcher, OccluderAtBrightCoreEdgeMakesTheLegPartial)
{
const LegProfile p = reflectiveTrolley();
World world;
const bool taped[4] = { true, true, false, false };
leg_scan_sim::addCart(world, p, kCx, 0.0, M_PI, nullptr, taped);
const auto geometry = leg_scan_sim::s300Geometry();
leg_scan_sim::Scan scan = leg_scan_sim::rayCast(world, geometry);
addClimbingBloomSkirts(scan, p.tape_min_intensity);
const auto run = firstBrightRun(scan, p.tape_min_intensity);
ASSERT_LT(run.first, scan.ranges.size());
ASSERT_GT(run.first, 0u);
scan.ranges[run.first - 1] = scan.ranges[run.first] - 0.20;
scan.intensities[run.first - 1] = 100.0f;
const LegDetection d = matchScan(p, geometry, scan);
ASSERT_TRUE(d.valid) << d.reason;
EXPECT_GE(d.legs_partial, 1);
}
TEST(LegPatternMatcher, RejectsFarTapedCoreWithTenCentimeterRangeRamp)
{
LegProfile p = reflectiveTrolley();
p.tape_legs = TapeLegs::kAll;
p.tape_range_bias_sigma = 0.05;
p.leg_cluster_gap = 0.12;
World world;
const bool taped[4] = { true, true, true, true };
const std::vector<int> ids = leg_scan_sim::addCart(world, p, kCx, 0.0, M_PI, nullptr, taped);
const auto geometry = leg_scan_sim::s300Geometry();
leg_scan_sim::Scan scan = leg_scan_sim::rayCast(world, geometry);
const LegDetection baseline = matchScan(p, geometry, scan);
ASSERT_TRUE(baseline.valid) << baseline.reason;
ASSERT_EQ(baseline.legs_seen, 4);
std::vector<std::size_t> far_right_core;
for (std::size_t i = 0; i < scan.hit.size(); ++i)
{
if (scan.hit[i] == ids[dock_pattern_detector::kFarRight] &&
scan.intensities[i] >= p.tape_min_intensity)
{
far_right_core.push_back(i);
}
}
ASSERT_GE(far_right_core.size(), 2u);
const double ramp_start = scan.ranges[far_right_core.front()];
for (std::size_t i = 0; i < far_right_core.size(); ++i)
{
const double fraction = static_cast<double>(i) /
static_cast<double>(far_right_core.size() - 1);
scan.ranges[far_right_core[i]] = ramp_start + 0.10 * fraction;
}
const LegDetection detection = matchScan(p, geometry, scan);
ASSERT_TRUE(detection.valid) << detection.reason;
EXPECT_EQ(detection.legs_seen, 3);
EXPECT_EQ(detection.tape_bright_rays[dock_pattern_detector::kEntryLeft],
baseline.tape_bright_rays[dock_pattern_detector::kEntryLeft]);
EXPECT_EQ(detection.tape_bright_rays[dock_pattern_detector::kEntryRight],
baseline.tape_bright_rays[dock_pattern_detector::kEntryRight]);
EXPECT_EQ(detection.tape_bright_rays[dock_pattern_detector::kFarLeft],
baseline.tape_bright_rays[dock_pattern_detector::kFarLeft]);
EXPECT_EQ(detection.tape_bright_rays[dock_pattern_detector::kFarRight], 0u);
}
TEST(LegPatternMatcher, ZeroTapeCorrectionKeysPreserveLegacySyntheticResult)
{
LegProfile before = tciTrolley();
LegProfile disabled = before;
disabled.tape_enabled = true;
disabled.tape_legs = TapeLegs::kEntry;
disabled.tape_min_intensity = 1000.0;
disabled.tape_min_points = 2;
disabled.tape_bloom_max_angle = 0.0;
disabled.tape_range_bias_sigma = 0.0;
World world;
const bool taped[4] = { true, true, false, false };
leg_scan_sim::addCart(world, before, kCx, 0.0, M_PI, nullptr, taped);
const auto geometry = leg_scan_sim::s300Geometry();
const leg_scan_sim::Scan scan = leg_scan_sim::rayCast(world, geometry);
const LegDetection a = matchScan(before, geometry, scan);
const LegDetection b = matchScan(disabled, geometry, scan);
ASSERT_TRUE(a.valid) << a.reason;
ASSERT_TRUE(b.valid) << b.reason;
EXPECT_EQ(a.center_x, b.center_x);
EXPECT_EQ(a.center_y, b.center_y);
EXPECT_EQ(a.center_yaw, b.center_yaw);
EXPECT_EQ(a.legs_seen, b.legs_seen);
EXPECT_EQ(a.measured_opening, b.measured_opening);
EXPECT_EQ(a.measured_depth, b.measured_depth);
}
TEST(LegPatternMatcher, TapeWithoutIntensitiesSaysSo)
{
LegProfile p = tciTrolley();
@@ -616,6 +847,27 @@ TEST(LegProfile, ValidateRefusesBrokenProfiles)
EXPECT_FALSE(p.validate(error));
}
TEST(LegProfile, TapeBloomAndRangeBiasRequireTapeAndValidRanges)
{
std::string error;
LegProfile p = tciTrolley();
p.tape_bloom_max_angle = 0.10;
EXPECT_FALSE(p.validate(error));
p = reflectiveTrolley();
p.tape_bloom_max_angle = 0.5;
EXPECT_FALSE(p.validate(error));
p = reflectiveTrolley();
p.tape_range_bias_sigma = 0.201;
EXPECT_FALSE(p.validate(error));
p = reflectiveTrolley();
p.tape_range_bias_sigma = std::numeric_limits<double>::infinity();
EXPECT_FALSE(p.validate(error));
p = reflectiveTrolley();
EXPECT_TRUE(p.validate(error)) << error;
}
TEST(LegPatternMatcher, MatchBeforeConfigureSaysSo)
{
LegPatternMatcher matcher;