fix(leg_pose_fit): estimate the taped range bias when every leg is taped, gated by its per-leg posterior std
This commit is contained in:
@@ -193,12 +193,14 @@ LegProfile mirrorTapeTrolley(bool enable_fixes = true,
|
||||
}
|
||||
|
||||
LegDetection detectMirrorTape(const RecordedScan& scan, bool enable_fixes = true,
|
||||
TapeLegs tape_legs = TapeLegs::kEntry, double plateau_fraction = 0.0)
|
||||
TapeLegs tape_legs = TapeLegs::kEntry, double plateau_fraction = 0.0,
|
||||
double all_max_std = 0.0)
|
||||
{
|
||||
LegPatternMatcher matcher;
|
||||
std::string error;
|
||||
LegProfile profile = mirrorTapeTrolley(enable_fixes, tape_legs);
|
||||
profile.tape_plateau_min_fraction = plateau_fraction;
|
||||
profile.tape_range_bias_all_max_std = all_max_std;
|
||||
EXPECT_TRUE(matcher.configure(profile, error)) << error;
|
||||
ScanLimits limits;
|
||||
limits.min_range = 0.10;
|
||||
@@ -478,6 +480,80 @@ TEST(LegPatternMatcherKit, RejectsFarBrightCoreCorruptedByNearMirrorBloom)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(LegPatternMatcherKit, AllTapedBiasNearAndFar)
|
||||
{
|
||||
const auto scans = loadTapeScans("kit_f_scan_trolley_tape_all_near.txt");
|
||||
ASSERT_EQ(scans.size(), 15u);
|
||||
for (const auto& scan : scans)
|
||||
{
|
||||
const auto old = detectMirrorTape(scan, true, TapeLegs::kAll, 0.65, 0.0);
|
||||
const auto corrected = detectMirrorTape(scan, true, TapeLegs::kAll, 0.65, 0.02);
|
||||
if (scan.name.find("near_fail_run6_") == 0)
|
||||
{
|
||||
EXPECT_FALSE(old.valid) << scan.name;
|
||||
EXPECT_NE(old.reason.find("two legs"), std::string::npos) << old.reason;
|
||||
ASSERT_TRUE(corrected.valid) << scan.name << " " << corrected.reason;
|
||||
EXPECT_TRUE(corrected.tape_range_bias_all_bright);
|
||||
EXPECT_TRUE(corrected.tape_range_bias_estimated);
|
||||
EXPECT_GE(corrected.tape_range_bias, 0.015);
|
||||
EXPECT_LE(corrected.tape_range_bias, 0.05);
|
||||
EXPECT_NEAR(corrected.measured_opening, 0.762, 0.04);
|
||||
EXPECT_NEAR(corrected.measured_depth, 0.480, 0.04);
|
||||
}
|
||||
else if (scan.name.find("far_ok_run17_m5_") == 0)
|
||||
{
|
||||
ASSERT_TRUE(old.valid) << scan.name << " " << old.reason;
|
||||
ASSERT_TRUE(corrected.valid) << scan.name << " " << corrected.reason;
|
||||
std::cout << scan.name << ": bias std=" << corrected.tape_range_bias_std << " m"
|
||||
<< " all-bright=" << corrected.tape_range_bias_all_bright << std::endl;
|
||||
EXPECT_LE(std::fabs(corrected.tape_range_bias), 0.05);
|
||||
if (!corrected.tape_range_bias_all_bright)
|
||||
{
|
||||
EXPECT_EQ(old.center_x, corrected.center_x);
|
||||
EXPECT_EQ(old.center_y, corrected.center_y);
|
||||
EXPECT_EQ(old.center_yaw, corrected.center_yaw);
|
||||
}
|
||||
else
|
||||
{
|
||||
const double dx = corrected.center_x - old.center_x;
|
||||
const double dy = corrected.center_y - old.center_y;
|
||||
EXPECT_GT(dx * old.center_x + dy * old.center_y, 0.0);
|
||||
EXPECT_LE(std::hypot(dx, dy), 0.04);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT_EQ(scan.name.find("near_ok_run17_m15_"), 0u);
|
||||
ASSERT_TRUE(old.valid) << old.reason;
|
||||
ASSERT_TRUE(corrected.valid) << corrected.reason;
|
||||
const double dx = corrected.center_x - old.center_x;
|
||||
const double dy = corrected.center_y - old.center_y;
|
||||
EXPECT_GT(dx * old.center_x + dy * old.center_y, 0.0);
|
||||
EXPECT_GE(std::hypot(dx, dy), 0.01);
|
||||
EXPECT_LE(std::hypot(dx, dy), 0.05);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(LegPatternMatcherKit, EntryTapeUnchangedWithAllBiasEnabled)
|
||||
{
|
||||
const auto scans = loadTapeScans("kit_f_scan_trolley_tape.txt");
|
||||
ASSERT_FALSE(scans.empty());
|
||||
for (const auto& scan : scans)
|
||||
{
|
||||
const auto a = detectMirrorTape(scan, true, TapeLegs::kEntry, 0.65, 0.0);
|
||||
const auto b = detectMirrorTape(scan, true, TapeLegs::kEntry, 0.65, 0.02);
|
||||
EXPECT_EQ(a.valid, b.valid) << scan.name;
|
||||
EXPECT_EQ(a.reason, 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.tape_range_bias, b.tape_range_bias);
|
||||
EXPECT_EQ(a.measured_opening, b.measured_opening);
|
||||
EXPECT_EQ(a.measured_depth, b.measured_depth);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(LegPatternMatcherKitReplay, BagReplayComparesPlateauOnAndOff)
|
||||
{
|
||||
const char* files_env = std::getenv("LEG_REPLAY_FILES");
|
||||
@@ -573,6 +649,139 @@ TEST(LegPatternMatcherKitReplay, BagReplayComparesPlateauOnAndOff)
|
||||
ASSERT_TRUE(csv.good()) << "Could not write replay CSV: " << output_path;
|
||||
}
|
||||
|
||||
TEST(LegPatternMatcherKitReplay, BagReplayComparesAllTapedBias)
|
||||
{
|
||||
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_BIAS to replay recorded scans.";
|
||||
}
|
||||
const char* output_path = std::getenv("LEG_REPLAY_OUT_BIAS");
|
||||
ASSERT_TRUE(output_path != nullptr && *output_path != '\0')
|
||||
<< "LEG_REPLAY_OUT_BIAS is required when LEG_REPLAY_FILES is set";
|
||||
std::ofstream csv(output_path);
|
||||
ASSERT_TRUE(csv.is_open()) << "Cannot open LEG_REPLAY_OUT_BIAS: " << output_path;
|
||||
csv << "label,stamp,config,valid,legs_seen,center_x,center_y,center_yaw,"
|
||||
"measured_opening,measured_depth,bias_estimated,bias,bias_std,bias_all_bright,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;
|
||||
};
|
||||
const auto printStdPercentiles = [](const char* label, std::vector<double>& samples) {
|
||||
std::cout << " " << label << " n=" << samples.size() << " std p10/p50/p90=";
|
||||
if (samples.empty())
|
||||
{
|
||||
std::cout << "n/a";
|
||||
return;
|
||||
}
|
||||
std::sort(samples.begin(), samples.end());
|
||||
const auto percentile = [&](double fraction) {
|
||||
const std::size_t rank = static_cast<std::size_t>(std::ceil(fraction * samples.size()));
|
||||
return samples[std::max<std::size_t>(1, rank) - 1];
|
||||
};
|
||||
std::cout << percentile(0.10) << "/" << percentile(0.50) << "/" << percentile(0.90);
|
||||
};
|
||||
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, signed_shift_sum = 0.0;
|
||||
std::size_t both = 0, valid_all_bright = 0;
|
||||
std::vector<double> accepted_std, rejected_std;
|
||||
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.65, 0.0);
|
||||
const LegDetection b = detectMirrorTape(scan, true, tape_legs, 0.65, 0.02);
|
||||
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 << ',' << detection.tape_range_bias_std << ','
|
||||
<< detection.tape_range_bias_all_bright << ',' << csvSafe(detection.reason) << '\n';
|
||||
};
|
||||
writeRow("A", a);
|
||||
writeRow("B", b);
|
||||
valid_a += a.valid;
|
||||
valid_b += b.valid;
|
||||
valid_all_bright += b.valid && b.tape_range_bias_all_bright;
|
||||
// The matcher exposes one selected (or furthest rejected) hypothesis per scan. A zero
|
||||
// means no all-bright attempt; infinity denotes a failed covariance or invalid fit.
|
||||
if (b.tape_range_bias_std > 0.0)
|
||||
{
|
||||
(b.tape_range_bias_all_bright ? accepted_std : rejected_std).push_back(b.tape_range_bias_std);
|
||||
}
|
||||
only_a += a.valid && !b.valid;
|
||||
only_b += b.valid && !a.valid;
|
||||
if (a.valid && b.valid)
|
||||
{
|
||||
++both;
|
||||
const double range = std::hypot(a.center_x, a.center_y);
|
||||
if (range > 0.0)
|
||||
{
|
||||
signed_shift_sum += ((b.center_x - a.center_x) * a.center_x +
|
||||
(b.center_y - a.center_y) * a.center_y) / range;
|
||||
}
|
||||
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
|
||||
<< " mean signed radial shift=" << (both ? signed_shift_sum / both : 0.0)
|
||||
<< " B-valid all-bright=" << valid_all_bright << "/" << valid_b
|
||||
<< " (fraction=" << (valid_b ? static_cast<double>(valid_all_bright) / valid_b : 0.0)
|
||||
<< ")";
|
||||
printStdPercentiles("selected accepted", accepted_std);
|
||||
printStdPercentiles("selected rejected", rejected_std);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
csv.flush();
|
||||
ASSERT_TRUE(csv.good()) << "Could not write replay CSV: " << output_path;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv)
|
||||
|
||||
Reference in New Issue
Block a user