feat(leg_pattern): add LegPatternMatcher for 4-leg trolleys

Finds a 4-leg trolley in a raw LaserScan without line extraction:
shadow filter, leg clusters in ray order with free/occluded/attached edges,
pair hypotheses from cart_opening/cart_depth/diagonal, then a Gauss-Newton
pose fit over every leg point with an along-ray residual, Huber weights and
backtracking. Gates: pair spacing after the fit, see-through leg positions,
optional empty interior, entry side (entry tape / prior yaw / facing the
sensor), optional prior, confidence, and ambiguity between two carts.

LegProfile validates every key at load. Two gtests (7 + 23) run on a
synthetic ray-cast scan (test/leg_scan_sim.h). Used by trolley_detection_action.
This commit is contained in:
2026-10-28 13:43:56 +07:00
parent edc6b30431
commit c568e90c53
11 changed files with 3324 additions and 0 deletions

237
src/leg_profile.cpp Normal file
View File

@@ -0,0 +1,237 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* dock_pattern_detector — kiểm miền của LegProfile.
*
* Author: DuongTD
*********************************************************************/
#include <dock_pattern_detector/leg_profile.h>
#include <cmath>
#include <string>
namespace dock_pattern_detector
{
namespace
{
bool positiveFinite(double value)
{
return std::isfinite(value) && value > 0.0;
}
} // namespace
const char* toString(LegShape shape)
{
switch (shape)
{
case LegShape::kBox:
return "box";
case LegShape::kRound:
return "round";
}
return "unknown";
}
bool legShapeFromString(const std::string& name, LegShape& out, std::string& error)
{
if (name == "box")
{
out = LegShape::kBox;
return true;
}
if (name == "round")
{
out = LegShape::kRound;
return true;
}
error = "unknown leg_shape '" + name + "'; known values are: box, round";
return false;
}
const char* toString(TapeLegs legs)
{
switch (legs)
{
case TapeLegs::kAll:
return "all";
case TapeLegs::kEntry:
return "entry";
}
return "unknown";
}
bool tapeLegsFromString(const std::string& name, TapeLegs& out, std::string& error)
{
if (name == "all")
{
out = TapeLegs::kAll;
return true;
}
if (name == "entry")
{
out = TapeLegs::kEntry;
return true;
}
error = "unknown tape_legs '" + name + "'; known values are: all, entry";
return false;
}
bool LegProfile::validate(std::string& error) const
{
if (!positiveFinite(cart_opening) || !positiveFinite(cart_depth))
{
error = "cart_opening and cart_depth must be finite and > 0 [m] (both are required)";
return false;
}
if (!positiveFinite(leg_width))
{
error = "leg_width must be finite and > 0 [m] (it is required)";
return false;
}
if (cart_opening <= leg_width + 0.2)
{
// Khe lọt giữa hai chân cạnh vào dưới 0.2 m: không robot nào chui được. Gần như chắc là khai
// nhầm đơn vị (mm thay vì m) hoặc nhầm hai khoá.
error = "cart_opening must exceed leg_width + 0.2 m; nothing fits through a narrower opening "
"(wrong unit?)";
return false;
}
if (!std::isfinite(cart_dim_tolerance) || cart_dim_tolerance <= 0.0 ||
cart_dim_tolerance >= std::min(cart_opening, cart_depth) / 4.0)
{
error = "cart_dim_tolerance must be > 0 and < min(cart_opening, cart_depth)/4 [m]";
return false;
}
if (!std::isfinite(leg_width_tolerance) || leg_width_tolerance < 0.0)
{
error = "leg_width_tolerance must be finite and >= 0 [m]";
return false;
}
if (!std::isfinite(leg_depth) || leg_depth < 0.0)
{
error = "leg_depth must be finite and >= 0 [m] (0 = same as leg_width)";
return false;
}
if (!std::isfinite(leg_yaw_offset))
{
error = "leg_yaw_offset must be finite [rad]";
return false;
}
if (leg_shape == LegShape::kRound && (leg_depth > 0.0 || leg_yaw_offset != 0.0))
{
// Nói thẳng thay vì nhận rồi lờ đi: chân tròn không có bề dày riêng hay góc xoay.
error = "leg_depth and leg_yaw_offset only apply to leg_shape 'box'";
return false;
}
if (min_leg_points < 1)
{
error = "min_leg_points must be >= 1";
return false;
}
if (!std::isfinite(shadow_min_angle) || shadow_min_angle < 0.0 ||
shadow_min_angle >= M_PI / 2.0)
{
error = "shadow_min_angle must be in [0, pi/2) [rad] (0 disables the shadow filter)";
return false;
}
if (!positiveFinite(leg_cluster_gap))
{
error = "leg_cluster_gap must be finite and > 0 [m]";
return false;
}
if (!positiveFinite(leg_min_depth_gap))
{
error = "leg_min_depth_gap must be finite and > 0 [m]";
return false;
}
if (min_legs < 2 || min_legs > 4)
{
error = "min_legs must be 2, 3 or 4";
return false;
}
if (min_legs == 2 && !has_prior)
{
// Hai chân có thể là cạnh vào, cạnh dọc hoặc đường chéo, mỗi loại hai phía. Không có prior thì
// không có gì chọn giữa các khả năng đó.
error = "min_legs 2 requires has_prior: two legs alone fit several cart poses";
return false;
}
if (!positiveFinite(leg_assoc_radius))
{
error = "leg_assoc_radius must be finite and > 0 [m]";
return false;
}
if (!std::isfinite(interior_margin) || interior_margin < 0.0 ||
2.0 * interior_margin >= std::min(cart_opening, cart_depth))
{
error = "interior_margin must be >= 0 and leave a non-empty interior [m]";
return false;
}
if (!std::isfinite(ambiguity_margin) || ambiguity_margin < 0.0 || ambiguity_margin >= 1.0)
{
error = "ambiguity_margin must be in [0, 1)";
return false;
}
if (!std::isfinite(min_confidence) || min_confidence < 0.0 || min_confidence > 1.0)
{
error = "min_confidence must be in [0, 1]";
return false;
}
if (tape_legs == TapeLegs::kEntry && !tape_enabled)
{
error = "tape_legs 'entry' requires tape_enabled: true (forgot to turn the tape on?)";
return false;
}
if (tape_enabled && (!std::isfinite(tape_min_intensity) || tape_min_points < 1))
{
error = "tape_min_intensity must be finite and tape_min_points >= 1";
return false;
}
if (std::fabs(cart_opening - cart_depth) < 2.0 * cart_dim_tolerance)
{
// Xe gần vuông: hình học không biết cạnh nào là cạnh vào. Dò ra cạnh bên là robot chui ngang
// vào chân xe — phải có thứ khác chọn hộ.
const bool yaw_prior = has_prior && prior_yaw_tolerance < M_PI / 4.0;
const bool entry_tape = tape_enabled && tape_legs == TapeLegs::kEntry;
if (!yaw_prior && !entry_tape)
{
error = "cart_opening and cart_depth differ by less than 2*cart_dim_tolerance, so the entry "
"side is ambiguous by 90 deg; set has_prior with prior_yaw_tolerance < pi/4, or "
"tape_legs: entry";
return false;
}
}
if (has_prior)
{
if (!std::isfinite(prior_x) || !std::isfinite(prior_y) || !std::isfinite(prior_yaw))
{
error = "prior_x, prior_y and prior_yaw must be finite";
return false;
}
if (!positiveFinite(search_radius))
{
error = "search_radius must be finite and > 0 [m] when has_prior is true";
return false;
}
if (!std::isfinite(prior_yaw_tolerance) || prior_yaw_tolerance <= 0.0 ||
prior_yaw_tolerance > M_PI)
{
error = "prior_yaw_tolerance must be in (0, pi] [rad]";
return false;
}
}
if (!std::isfinite(search_fov))
{
error = "search_fov must be finite [rad] (<= 0 disables the fan)";
return false;
}
if (!std::isfinite(offset_x) || !std::isfinite(offset_y) || !std::isfinite(offset_yaw))
{
error = "offset_x, offset_y and offset_yaw must be finite";
return false;
}
return true;
}
} // namespace dock_pattern_detector