fix(leg_pattern): rescue a real taped leg's edge-ramp bright run from the bloom-spread gate

This commit is contained in:
2026-10-30 09:43:41 +07:00
parent 347896e252
commit 588ed58b6d
8 changed files with 387 additions and 18 deletions

View File

@@ -14,8 +14,12 @@
*********************************************************************/
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
@@ -78,11 +82,10 @@ std::vector<RecordedScan> loadScans()
return scans;
}
std::vector<RecordedScan> loadTapeScans(const char* filename = "kit_f_scan_trolley_tape.txt")
std::vector<RecordedScan> loadTapeScans(const std::string& path)
{
std::vector<RecordedScan> scans;
std::ifstream in(std::string(DOCK_PATTERN_DETECTOR_TEST_DATA_DIR) +
"/" + filename);
std::ifstream in(path);
std::string line;
while (std::getline(in, line))
{
@@ -120,6 +123,11 @@ std::vector<RecordedScan> loadTapeScans(const char* filename = "kit_f_scan_troll
return scans;
}
std::vector<RecordedScan> loadTapeScans(const char* filename = "kit_f_scan_trolley_tape.txt")
{
return loadTapeScans(std::string(DOCK_PATTERN_DETECTOR_TEST_DATA_DIR) + "/" + filename);
}
const RecordedScan& scanNamed(const std::vector<RecordedScan>& scans, const std::string& name)
{
for (const RecordedScan& s : scans)
@@ -185,11 +193,13 @@ LegProfile mirrorTapeTrolley(bool enable_fixes = true,
}
LegDetection detectMirrorTape(const RecordedScan& scan, bool enable_fixes = true,
TapeLegs tape_legs = TapeLegs::kEntry)
TapeLegs tape_legs = TapeLegs::kEntry, double plateau_fraction = 0.0)
{
LegPatternMatcher matcher;
std::string error;
EXPECT_TRUE(matcher.configure(mirrorTapeTrolley(enable_fixes, tape_legs), error)) << error;
LegProfile profile = mirrorTapeTrolley(enable_fixes, tape_legs);
profile.tape_plateau_min_fraction = plateau_fraction;
EXPECT_TRUE(matcher.configure(profile, error)) << error;
ScanLimits limits;
limits.min_range = 0.10;
limits.max_range = 3.0;
@@ -376,13 +386,73 @@ TEST(LegPatternMatcherKit, BiasCorrectsMeasuredDepthOnFourLegScans)
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;
for (double fraction : { 0.0, 0.65 })
{
const LegDetection detection = detectMirrorTape(scan, true, TapeLegs::kEntry, fraction);
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, ObliquePlateauRescuesRealLegAndPreservesBloomRejection)
{
const auto scans = loadTapeScans("kit_f_scan_trolley_tape_oblique.txt");
ASSERT_EQ(scans.size(), 10u);
std::size_t failed_cases = 0, bloom_cases = 0;
for (const RecordedScan& scan : scans)
{
ASSERT_EQ(scan.ranges.size(), scan.geometry.size) << scan.name;
ASSERT_EQ(scan.intensities.size(), scan.geometry.size) << scan.name;
const LegDetection baseline = detectMirrorTape(scan, true, TapeLegs::kAll);
const LegDetection plateau = detectMirrorTape(scan, true, TapeLegs::kAll, 0.65);
if (scan.name.rfind("oblique_el_fail_", 0) == 0)
{
++failed_cases;
EXPECT_FALSE(baseline.valid) << scan.name;
EXPECT_LT(baseline.legs_seen, 3) << scan.name;
ASSERT_TRUE(plateau.valid) << scan.name << ": " << plateau.reason;
EXPECT_GE(plateau.legs_seen, 3) << scan.name;
EXPECT_LE(std::hypot(plateau.center_x - scan.ref_x, plateau.center_y - scan.ref_y), 0.04)
<< scan.name;
EXPECT_LE(yawError(plateau.center_yaw, scan.ref_yaw), 0.06) << scan.name;
EXPECT_NEAR(plateau.measured_opening, 0.762, 0.04) << scan.name;
EXPECT_NEAR(plateau.measured_depth, 0.480, 0.04) << scan.name;
}
else
{
ASSERT_EQ(scan.name.rfind("bloom_fl_ok_", 0), 0u) << scan.name;
++bloom_cases;
ASSERT_TRUE(baseline.valid) << scan.name << ": " << baseline.reason;
ASSERT_TRUE(plateau.valid) << scan.name << ": " << plateau.reason;
EXPECT_EQ(plateau.legs_seen, baseline.legs_seen) << scan.name;
EXPECT_EQ(plateau.tape_bright_rays, baseline.tape_bright_rays) << scan.name;
EXPECT_EQ(plateau.tape_bright_rays[dock_pattern_detector::kFarLeft], 0u) << scan.name;
EXPECT_NEAR(plateau.center_x, baseline.center_x, 1e-9) << scan.name;
EXPECT_NEAR(plateau.center_y, baseline.center_y, 1e-9) << scan.name;
EXPECT_NEAR(plateau.center_yaw, baseline.center_yaw, 1e-9) << scan.name;
}
}
EXPECT_EQ(failed_cases, 6u);
EXPECT_EQ(bloom_cases, 4u);
}
TEST(LegPatternMatcherKit, MirrorTapeReferencesStillHoldWithPlateau)
{
const auto scans = loadTapeScans();
ASSERT_FALSE(scans.empty());
for (const RecordedScan& scan : scans)
{
const LegDetection detection = detectMirrorTape(scan, true, TapeLegs::kEntry, 0.65);
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;
}
}
@@ -392,8 +462,14 @@ TEST(LegPatternMatcherKit, RejectsFarBrightCoreCorruptedByNearMirrorBloom)
ASSERT_EQ(scans.size(), 2u);
for (const RecordedScan& scan : scans)
{
const LegDetection detection = detectMirrorTape(scan, true, TapeLegs::kAll);
const LegDetection baseline = detectMirrorTape(scan, true, TapeLegs::kAll);
const LegDetection detection = detectMirrorTape(scan, true, TapeLegs::kAll, 0.65);
ASSERT_TRUE(baseline.valid) << scan.name << ": " << baseline.reason;
ASSERT_TRUE(detection.valid) << scan.name << ": " << detection.reason;
EXPECT_EQ(detection.tape_bright_rays, baseline.tape_bright_rays) << scan.name;
EXPECT_NEAR(detection.center_x, baseline.center_x, 1e-9) << scan.name;
EXPECT_NEAR(detection.center_y, baseline.center_y, 1e-9) << scan.name;
EXPECT_NEAR(detection.center_yaw, baseline.center_yaw, 1e-9) << scan.name;
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;
@@ -402,6 +478,101 @@ TEST(LegPatternMatcherKit, RejectsFarBrightCoreCorruptedByNearMirrorBloom)
}
}
TEST(LegPatternMatcherKitReplay, BagReplayComparesPlateauOnAndOff)
{
const char* files_env = std::getenv("LEG_REPLAY_FILES");
if (files_env == nullptr || *files_env == '\0')
{
GTEST_SKIP() << "Set LEG_REPLAY_FILES and LEG_REPLAY_OUT to replay recorded scans.";
}
const char* output_path = std::getenv("LEG_REPLAY_OUT");
ASSERT_TRUE(output_path != nullptr && *output_path != '\0')
<< "LEG_REPLAY_OUT is required when LEG_REPLAY_FILES is set";
std::ofstream csv(output_path);
ASSERT_TRUE(csv.is_open()) << "Cannot open LEG_REPLAY_OUT: " << output_path;
csv << "label,stamp,config,valid,legs_seen,center_x,center_y,center_yaw,"
"measured_opening,measured_depth,bias_estimated,bias,reason\n";
csv << std::fixed << std::setprecision(6);
const auto csvSafe = [](std::string text) {
for (char& c : text)
{
if (c == ',' || c == '"' || c == '\n' || c == '\r')
{
c = ' ';
}
}
return text;
};
std::istringstream entries(files_env);
std::string entry;
while (std::getline(entries, entry, ','))
{
const std::size_t first = entry.find(':');
const std::size_t second = first == std::string::npos ? first : entry.find(':', first + 1);
ASSERT_TRUE(first != std::string::npos && first > 0 && second != std::string::npos &&
second + 1 < entry.size())
<< "LEG_REPLAY_FILES entry must be label:all|entry:absolute_path: " << entry;
const std::string label = entry.substr(0, first);
const std::string tape_name = entry.substr(first + 1, second - first - 1);
const std::string path = entry.substr(second + 1);
ASSERT_TRUE(tape_name == "all" || tape_name == "entry") << "Invalid tape_legs: " << tape_name;
ASSERT_EQ(path.front(), '/') << "Replay path must be absolute: " << path;
const TapeLegs tape_legs = tape_name == "all" ? TapeLegs::kAll : TapeLegs::kEntry;
std::ifstream input(path);
ASSERT_TRUE(input.is_open()) << "Cannot open replay file: " << path;
input.close();
const std::vector<RecordedScan> scans = loadTapeScans(path);
ASSERT_FALSE(scans.empty()) << "No scans loaded from " << path;
std::size_t valid_a = 0, valid_b = 0, only_b = 0, only_a = 0, changed = 0;
double max_dxy = 0.0, max_dyaw = 0.0;
for (const RecordedScan& scan : scans)
{
ASSERT_EQ(scan.ranges.size(), scan.geometry.size) << path << ": " << scan.name;
ASSERT_EQ(scan.intensities.size(), scan.geometry.size) << path << ": " << scan.name;
const std::size_t separator = scan.name.rfind('_');
ASSERT_NE(separator, std::string::npos) << "Missing timestamp in scan name: " << scan.name;
const std::string stamp_text = scan.name.substr(separator + 1);
char* stamp_end = nullptr;
const double stamp = std::strtod(stamp_text.c_str(), &stamp_end);
ASSERT_TRUE(stamp_end != stamp_text.c_str() && *stamp_end == '\0' && std::isfinite(stamp))
<< "Invalid timestamp in scan name: " << scan.name;
// detectMirrorTape configures a fresh matcher and the robot limits for each call.
const LegDetection a = detectMirrorTape(scan, true, tape_legs, 0.0);
const LegDetection b = detectMirrorTape(scan, true, tape_legs, 0.65);
const auto writeRow = [&](const char* config, const LegDetection& detection) {
csv << csvSafe(label) << ',' << stamp << ',' << config << ',' << detection.valid << ','
<< detection.legs_seen << ',' << detection.center_x << ',' << detection.center_y << ','
<< detection.center_yaw << ',' << detection.measured_opening << ','
<< detection.measured_depth << ',' << detection.tape_range_bias_estimated << ','
<< detection.tape_range_bias << ',' << csvSafe(detection.reason) << '\n';
};
writeRow("A", a);
writeRow("B", b);
valid_a += a.valid;
valid_b += b.valid;
only_a += a.valid && !b.valid;
only_b += b.valid && !a.valid;
if (a.valid && b.valid)
{
const double dxy = std::hypot(a.center_x - b.center_x, a.center_y - b.center_y);
const double dyaw = yawError(a.center_yaw, b.center_yaw);
changed += dxy > 1e-9 || dyaw > 1e-9;
max_dxy = std::max(max_dxy, dxy);
max_dyaw = std::max(max_dyaw, dyaw);
}
}
std::cout << label << ": scans=" << scans.size() << " valid A=" << valid_a
<< " valid B=" << valid_b << " only B=" << only_b << " only A=" << only_a
<< " both valid with pose changed=" << changed << std::fixed << std::setprecision(12)
<< " max |dxy|=" << max_dxy << " max |dyaw|=" << max_dyaw << std::endl;
}
csv.flush();
ASSERT_TRUE(csv.good()) << "Could not write replay CSV: " << output_path;
}
} // namespace
int main(int argc, char** argv)