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

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -18,6 +18,7 @@
#include <fstream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <dock_pattern_detector/leg_pattern_matcher.h>
@@ -30,6 +31,7 @@ using dock_pattern_detector::LegProfile;
using dock_pattern_detector::LegShape;
using dock_pattern_detector::Pose2D;
using dock_pattern_detector::ScanLimits;
using dock_pattern_detector::TapeLegs;
constexpr double kDeg = M_PI / 180.0;
@@ -38,6 +40,7 @@ struct RecordedScan
std::string name;
robot_line_extraction::ScanGeometry geometry;
std::vector<double> ranges;
std::vector<float> intensities;
double ref_x = 0.0; // [m]
double ref_y = 0.0; // [m]
double ref_yaw = 0.0; // [rad]
@@ -75,6 +78,48 @@ std::vector<RecordedScan> loadScans()
return scans;
}
std::vector<RecordedScan> loadTapeScans(const char* filename = "kit_f_scan_trolley_tape.txt")
{
std::vector<RecordedScan> scans;
std::ifstream in(std::string(DOCK_PATTERN_DETECTOR_TEST_DATA_DIR) +
"/" + filename);
std::string line;
while (std::getline(in, line))
{
if (line.empty() || line[0] == '#')
{
continue;
}
std::istringstream header(line);
std::string tag;
RecordedScan scan;
header >> tag >> scan.name >> scan.geometry.angle_min >> scan.geometry.angle_increment >>
scan.geometry.size >> scan.ref_x >> scan.ref_y >> scan.ref_yaw;
if (tag != "scan" || !std::getline(in, line))
{
break;
}
std::istringstream range_values(line);
double range = 0.0;
while (range_values >> range)
{
scan.ranges.push_back(range);
}
if (!std::getline(in, line))
{
break;
}
std::istringstream intensity_values(line);
float intensity = 0.0f;
while (intensity_values >> intensity)
{
scan.intensities.push_back(intensity);
}
scans.push_back(std::move(scan));
}
return scans;
}
const RecordedScan& scanNamed(const std::vector<RecordedScan>& scans, const std::string& name)
{
for (const RecordedScan& s : scans)
@@ -117,6 +162,42 @@ LegDetection detect(const RecordedScan& s, bool accept_attached_legs, bool zero_
return matcher.match(s.geometry, s.ranges, {}, limits, Pose2D());
}
LegProfile mirrorTapeTrolley(bool enable_fixes = true,
TapeLegs tape_legs = TapeLegs::kEntry)
{
LegProfile p;
p.cart_opening = 0.762;
p.cart_depth = 0.480;
p.cart_dim_tolerance = 0.04;
p.leg_shape = LegShape::kBox;
p.leg_width = 0.030;
p.leg_width_tolerance = 0.02;
p.min_legs = 3;
p.accept_attached_legs = true;
p.min_confidence = 0.5;
p.tape_enabled = true;
p.tape_legs = tape_legs;
p.tape_min_intensity = 8000.0;
p.tape_min_points = 2;
p.tape_bloom_max_angle = enable_fixes ? 0.10 : 0.0;
p.tape_range_bias_sigma = enable_fixes ? 0.05 : 0.0;
return p;
}
LegDetection detectMirrorTape(const RecordedScan& scan, bool enable_fixes = true,
TapeLegs tape_legs = TapeLegs::kEntry)
{
LegPatternMatcher matcher;
std::string error;
EXPECT_TRUE(matcher.configure(mirrorTapeTrolley(enable_fixes, tape_legs), error)) << error;
ScanLimits limits;
limits.min_range = 0.10;
limits.max_range = 3.0;
limits.range_sigma = 0.01;
limits.zero_range_is_no_echo = true;
return matcher.match(scan.geometry, scan.ranges, scan.intensities, limits, Pose2D());
}
double yawError(double a, double b)
{
return std::fabs(std::atan2(std::sin(a - b), std::cos(a - b)));
@@ -200,6 +281,127 @@ TEST(LegPatternMatcherKit, ZeroRangeAsNoEchoClearsTheFalsePartialFlags)
EXPECT_LT(no_echo.legs_partial, blocked.legs_partial);
}
TEST(LegPatternMatcherKit, EveryUntapedOcclusionScanKeepsLegacyResults)
{
const std::vector<RecordedScan> scans = loadScans();
ASSERT_EQ(scans.size(), 6u);
for (const RecordedScan& scan : scans)
{
if (scan.name == "far_right_leg_beside_post")
{
const LegDetection blocked = detect(scan, false, false);
ASSERT_TRUE(blocked.valid) << scan.name << ": " << blocked.reason;
EXPECT_EQ(blocked.legs_seen, 4) << scan.name;
EXPECT_NEAR(blocked.center_x, scan.ref_x, 0.02) << scan.name;
EXPECT_NEAR(blocked.center_y, scan.ref_y, 0.02) << scan.name;
EXPECT_LT(yawError(blocked.center_yaw, scan.ref_yaw), 1.5 * kDeg) << scan.name;
EXPECT_EQ(blocked.legs_partial, 4) << scan.name;
const LegDetection no_echo = detect(scan, true, true);
ASSERT_TRUE(no_echo.valid) << scan.name << ": " << no_echo.reason;
EXPECT_EQ(no_echo.legs_seen, 4) << scan.name;
EXPECT_NEAR(no_echo.center_x, scan.ref_x, 0.02) << scan.name;
EXPECT_NEAR(no_echo.center_y, scan.ref_y, 0.02) << scan.name;
EXPECT_LT(yawError(no_echo.center_yaw, scan.ref_yaw), 1.5 * kDeg) << scan.name;
EXPECT_LT(no_echo.legs_partial, blocked.legs_partial) << scan.name;
continue;
}
EXPECT_FALSE(detect(scan, false, false).valid) << scan.name;
const LegDetection attached = detect(scan, true, true);
ASSERT_TRUE(attached.valid) << scan.name << ": " << attached.reason;
EXPECT_EQ(attached.legs_seen, 3) << scan.name;
EXPECT_EQ(attached.legs_attached, 1) << scan.name;
EXPECT_EQ(attached.legs_occluded + attached.legs_distorted, 1) << scan.name;
EXPECT_NEAR(attached.center_x, scan.ref_x, 0.02) << scan.name;
EXPECT_NEAR(attached.center_y, scan.ref_y, 0.02) << scan.name;
EXPECT_LT(yawError(attached.center_yaw, scan.ref_yaw), 1.5 * kDeg) << scan.name;
}
}
TEST(LegPatternMatcherKit, MirrorTapeDataFileLoadsRangesAndIntensities)
{
const std::vector<RecordedScan> scans = loadTapeScans();
ASSERT_FALSE(scans.empty());
for (const RecordedScan& scan : scans)
{
EXPECT_EQ(scan.ranges.size(), scan.geometry.size) << scan.name;
EXPECT_EQ(scan.intensities.size(), scan.geometry.size) << scan.name;
}
}
TEST(LegPatternMatcherKit, EveryMirrorTapeScanMatchesItsReplayReference)
{
const std::vector<RecordedScan> scans = loadTapeScans();
ASSERT_FALSE(scans.empty());
for (const RecordedScan& scan : scans)
{
const LegDetection detection = detectMirrorTape(scan);
ASSERT_TRUE(detection.valid) << scan.name << ": " << detection.reason;
EXPECT_GE(detection.legs_seen, 3) << scan.name;
EXPECT_LE(std::hypot(detection.center_x - scan.ref_x, detection.center_y - scan.ref_y), 0.03)
<< scan.name;
EXPECT_LT(yawError(detection.center_yaw, scan.ref_yaw), 2.0 * kDeg) << scan.name;
}
}
TEST(LegPatternMatcherKit, OldMirrorTapePathStillFailsTheKnownBloomScans)
{
const std::vector<RecordedScan> scans = loadTapeScans();
bool checked_m5 = false;
bool checked_m7 = false;
bool checked_m11 = false;
for (const RecordedScan& scan : scans)
{
const bool regression_case = scan.name.rfind("m5_", 0) == 0 ||
scan.name.rfind("m7_", 0) == 0 ||
scan.name.rfind("m11_", 0) == 0;
if (!regression_case)
{
continue;
}
EXPECT_FALSE(detectMirrorTape(scan, false).valid) << scan.name;
checked_m5 = checked_m5 || scan.name.rfind("m5_", 0) == 0;
checked_m7 = checked_m7 || scan.name.rfind("m7_", 0) == 0;
checked_m11 = checked_m11 || scan.name.rfind("m11_", 0) == 0;
}
EXPECT_TRUE(checked_m5);
EXPECT_TRUE(checked_m7);
EXPECT_TRUE(checked_m11);
}
TEST(LegPatternMatcherKit, BiasCorrectsMeasuredDepthOnFourLegScans)
{
const std::vector<RecordedScan> scans = loadTapeScans();
for (const char* name : { "near_both_taped_4legs", "far_both_taped_4legs" })
{
const RecordedScan& scan = scanNamed(scans, name);
const LegDetection detection = detectMirrorTape(scan);
ASSERT_TRUE(detection.valid) << name << ": " << detection.reason;
ASSERT_EQ(detection.legs_seen, 4) << name;
EXPECT_TRUE(detection.tape_range_bias_estimated) << name;
EXPECT_GE(detection.tape_range_bias, 0.005) << name;
EXPECT_LE(detection.tape_range_bias, 0.045) << name;
EXPECT_NEAR(detection.measured_depth, 0.480, 0.015) << name;
}
}
TEST(LegPatternMatcherKit, RejectsFarBrightCoreCorruptedByNearMirrorBloom)
{
const std::vector<RecordedScan> scans = loadTapeScans("kit_f_scan_trolley_tape_all4.txt");
ASSERT_EQ(scans.size(), 2u);
for (const RecordedScan& scan : scans)
{
const LegDetection detection = detectMirrorTape(scan, true, TapeLegs::kAll);
ASSERT_TRUE(detection.valid) << scan.name << ": " << detection.reason;
EXPECT_EQ(detection.legs_seen, 3) << scan.name;
EXPECT_LE(std::hypot(detection.center_x - scan.ref_x, detection.center_y - scan.ref_y), 0.03)
<< scan.name;
EXPECT_LT(yawError(detection.center_yaw, scan.ref_yaw), 2.0 * kDeg) << scan.name;
EXPECT_FALSE(detection.tape_range_bias_estimated) << scan.name;
}
}
} // namespace
int main(int argc, char** argv)

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;

View File

@@ -9,6 +9,7 @@
*********************************************************************/
#include <gtest/gtest.h>
#include <array>
#include <cmath>
#include <vector>
@@ -212,6 +213,117 @@ TEST(LegPoseFit, ModelSigmaChangesTheScoreButNotThePose)
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();