Grouping segments into an arc candidate keyed only on ray index, and that is not the same question as whether they lie on one surface: the next ray along can land on something several metres away. Found in Gazebo, not in a synthesized scene. The four segments of a cradle sat two rays away from a 6.6 m wall behind it, so the wall was absorbed into the group; the circle fitted over all of it came out at radius 3.2 m with a 0.11 m residual, and the real cradle was rejected because of something that was not part of it. The regression test reproduces the same signature - radius 1.38 m, residual 0.20 m - and passes once the gap is checked. arc_max_point_gap is the euclidean distance between the two facing endpoints, and it belongs at or below the extractor's max_line_gap. Set it higher and the matcher rejoins exactly what the extractor just separated, which it separated for a reason.
412 lines
14 KiB
C++
412 lines
14 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* Kiểm khớp máng cong LÕM bằng scan TỔNG HỢP — dựng hình trước, sinh tia sau, biết trước đáp án.
|
|
*
|
|
* Bốn nhóm:
|
|
* 1. dò đúng — điểm sâu nhất, hướng, offset, cung bị che một phần;
|
|
* 2. TỪ CHỐI — đây mới là phần quan trọng: góc tường, mặt phẳng, trụ LỒI, bán kính sai;
|
|
* 3. cổng tìm kiếm;
|
|
* 4. config hỏng.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cmath>
|
|
#include <limits>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <robot_line_extraction/line_extractor.h>
|
|
|
|
#include <dock_pattern_detector/arc_pattern_matcher.h>
|
|
|
|
namespace
|
|
{
|
|
using dock_pattern_detector::ArcPatternMatcher;
|
|
using dock_pattern_detector::Detection;
|
|
using dock_pattern_detector::DockProfile;
|
|
using dock_pattern_detector::PatternType;
|
|
using robot_line_extraction::Line;
|
|
using robot_line_extraction::LineExtractionParams;
|
|
using robot_line_extraction::LineExtractor;
|
|
using robot_line_extraction::ScanGeometry;
|
|
|
|
constexpr double kInf = std::numeric_limits<double>::infinity();
|
|
|
|
struct Segment
|
|
{
|
|
double ax, ay, bx, by; // [m]
|
|
};
|
|
|
|
ScanGeometry geometry(std::size_t rays = 690, double angle_min = -0.9, double angle_max = 0.9)
|
|
{
|
|
ScanGeometry g;
|
|
g.angle_min = angle_min;
|
|
g.angle_increment = (angle_max - angle_min) / static_cast<double>(rays - 1);
|
|
g.size = rays;
|
|
return g;
|
|
}
|
|
|
|
/// @brief Xấp xỉ một cung tròn bằng nhiều đoạn thẳng rất ngắn, để bộ chiếu tia dùng chung một mã.
|
|
std::vector<Segment> arcSegments(double cx, double cy, double r, double a0, double span,
|
|
std::size_t facets = 400)
|
|
{
|
|
std::vector<Segment> out;
|
|
for (std::size_t i = 0; i + 1 < facets; ++i)
|
|
{
|
|
const double t0 = a0 + span * static_cast<double>(i) / static_cast<double>(facets - 1);
|
|
const double t1 = a0 + span * static_cast<double>(i + 1) / static_cast<double>(facets - 1);
|
|
out.push_back({ cx + r * std::cos(t0), cy + r * std::sin(t0), cx + r * std::cos(t1),
|
|
cy + r * std::sin(t1) });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* @brief Máng LÕM ôm cảm biến: tâm ở @p center_x trên trục x, mặt cong ở XA hơn tâm.
|
|
*
|
|
* Cung được vẽ về phía +x tính từ tâm, nên mọi điểm mặt đều xa cảm biến hơn tâm — đúng định nghĩa
|
|
* lõm mà matcher kiểm.
|
|
*/
|
|
std::vector<Segment> concaveCradle(double center_x, double r, double span)
|
|
{
|
|
return arcSegments(center_x, 0.0, r, -span / 2.0, span);
|
|
}
|
|
|
|
/// @brief Trụ tròn LỒI: tâm ở @p center_x, mặt cong quay về phía cảm biến, tức GẦN hơn tâm.
|
|
std::vector<Segment> convexCylinder(double center_x, double r, double span)
|
|
{
|
|
return arcSegments(center_x, 0.0, r, M_PI - span / 2.0, span);
|
|
}
|
|
|
|
std::vector<double> rayCast(const ScanGeometry& g, const std::vector<Segment>& world)
|
|
{
|
|
std::vector<double> ranges(g.size, kInf);
|
|
for (std::size_t i = 0; i < g.size; ++i)
|
|
{
|
|
const double bearing = g.angle_min + static_cast<double>(i) * g.angle_increment;
|
|
const double dx = std::cos(bearing);
|
|
const double dy = std::sin(bearing);
|
|
for (const Segment& s : world)
|
|
{
|
|
const double ex = s.bx - s.ax;
|
|
const double ey = s.by - s.ay;
|
|
const double det = dx * (-ey) - dy * (-ex);
|
|
if (std::fabs(det) < 1e-12)
|
|
{
|
|
continue;
|
|
}
|
|
const double t = (s.ax * (-ey) - s.ay * (-ex)) / det;
|
|
const double u = (dx * s.ay - dy * s.ax) / det;
|
|
if (t > 0.0 && u >= 0.0 && u <= 1.0 && t < ranges[i])
|
|
{
|
|
ranges[i] = t;
|
|
}
|
|
}
|
|
}
|
|
return ranges;
|
|
}
|
|
|
|
DockProfile arcProfile()
|
|
{
|
|
DockProfile p;
|
|
p.pattern = PatternType::kArc;
|
|
p.arc_radius = 0.40; // [m]
|
|
p.arc_radius_tolerance = 0.06; // [m]
|
|
p.arc_span = 2.2; // [rad] toàn phần của máng
|
|
p.arc_min_span = 0.52; // [rad]
|
|
p.arc_max_rms = 0.010; // [m]
|
|
p.min_arc_points = 8;
|
|
p.min_confidence = 0.3;
|
|
return p;
|
|
}
|
|
|
|
LineExtractionParams extractorParams()
|
|
{
|
|
LineExtractionParams p;
|
|
p.min_split_dist = 0.02; // [m]
|
|
p.max_line_gap = 0.08; // [m]
|
|
p.min_line_length = 0.05; // [m]
|
|
p.min_line_points = 5;
|
|
p.outlier_dist = 0.10; // [m]
|
|
return p;
|
|
}
|
|
|
|
class Rig
|
|
{
|
|
public:
|
|
explicit Rig(const DockProfile& profile = arcProfile())
|
|
{
|
|
std::string error;
|
|
ok_ = extractor_.configure(extractorParams(), error) && matcher_.configure(profile, error);
|
|
error_ = error;
|
|
}
|
|
|
|
Detection run(const std::vector<Segment>& world, const ScanGeometry& g = geometry())
|
|
{
|
|
std::string error;
|
|
if (!extractor_.extract(g, rayCast(g, world), lines_, error))
|
|
{
|
|
Detection d;
|
|
d.reason = error;
|
|
return d;
|
|
}
|
|
return matcher_.match(lines_);
|
|
}
|
|
|
|
bool ok_ = false;
|
|
std::string error_;
|
|
LineExtractor extractor_;
|
|
ArcPatternMatcher matcher_;
|
|
std::vector<Line> lines_;
|
|
};
|
|
|
|
// ================================================================================================
|
|
// 1. Dò đúng
|
|
// ================================================================================================
|
|
|
|
TEST(ArcPatternMatcher, FindsTheDeepestPointOfACradle)
|
|
{
|
|
Rig rig;
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
// Tâm ở x = 1.60, bán kính 0.40 -> điểm sâu nhất ở x = 2.00.
|
|
const Detection d = rig.run(concaveCradle(1.60, 0.40, 2.0));
|
|
ASSERT_TRUE(d.valid) << d.reason;
|
|
EXPECT_NEAR(d.apex_x, 2.00, 0.02);
|
|
EXPECT_NEAR(d.apex_y, 0.00, 0.02);
|
|
EXPECT_NEAR(d.measured_arc_radius, 0.40, 0.02);
|
|
EXPECT_NEAR(d.arc_center_x, 1.60, 0.02);
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, YawPointsBackAtTheSensor)
|
|
{
|
|
// Lệch 180 độ ở đây là robot lùi vào máng thay vì tiến vào, nên hướng phải canh riêng.
|
|
Rig rig;
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const Detection d = rig.run(concaveCradle(1.60, 0.40, 2.0));
|
|
ASSERT_TRUE(d.valid) << d.reason;
|
|
EXPECT_NEAR(d.yaw, M_PI, 0.05); // tam o +x, nen huong ve cam bien la -x
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, TheOffsetMovesTheTargetOutTowardsTheRobot)
|
|
{
|
|
DockProfile p = arcProfile();
|
|
p.offset_x = 0.90; // [m]
|
|
Rig rig(p);
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const Detection d = rig.run(concaveCradle(1.60, 0.40, 2.0));
|
|
ASSERT_TRUE(d.valid) << d.reason;
|
|
EXPECT_NEAR(d.apex_x, 2.00, 0.02);
|
|
EXPECT_NEAR(d.x, 2.00 - 0.90, 0.03) << "offset phai keo dich VE PHIA robot";
|
|
EXPECT_NEAR(d.y, 0.00, 0.03);
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, APartlyOccludedArcStillGivesTheSameDeepestPoint)
|
|
{
|
|
// Đây là lý do điểm đích được định nghĩa từ TÂM và BÁN KÍNH chứ không phải trung điểm cung quan
|
|
// sát được: che một bên thì trung điểm dịch ngay, còn tâm thì không.
|
|
Rig rig;
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const Detection full = rig.run(concaveCradle(1.60, 0.40, 2.0));
|
|
ASSERT_TRUE(full.valid) << full.reason;
|
|
|
|
// Chỉ còn nửa bên phải của cung.
|
|
const Detection half = rig.run(arcSegments(1.60, 0.0, 0.40, 0.0, 1.0));
|
|
ASSERT_TRUE(half.valid) << half.reason;
|
|
|
|
EXPECT_NEAR(half.apex_x, full.apex_x, 0.03);
|
|
EXPECT_NEAR(half.apex_y, full.apex_y, 0.03);
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, AWallJustPastTheArcIsNotAbsorbedIntoIt)
|
|
{
|
|
// Hồi quy cho một lỗi CHỈ SIM MỚI LỘ. Ban đầu phép gom chỉ xét khoảng cách theo SỐ TIA, và trong
|
|
// world Gazebo bức tường ngay sau máng chỉ cách 2 tia — lọt ngưỡng. Bốn đoạn của máng bị gom
|
|
// cùng bức tường 6.6 m, khớp tròn trên cả cụm cho bán kính 3.2 m và sai số 0.11 m, nên cái máng
|
|
// thật bị loại vì một thứ không thuộc về nó.
|
|
//
|
|
// Liền nhau theo số tia KHÔNG có nghĩa là liền nhau trong không gian: tia kế tiếp có thể rơi
|
|
// xuống một mặt cách đó vài mét.
|
|
Rig rig;
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
// Máng ở 2 m; mép của nó nằm quanh bearing +-0.17 rad.
|
|
std::vector<Segment> world = concaveCradle(1.60, 0.40, 2.0);
|
|
|
|
// Tường ở 4 m, bắt đầu ngay sát mép máng theo GÓC nhưng cách xa theo KHÔNG GIAN.
|
|
const double a0 = 0.18;
|
|
const double a1 = 0.95;
|
|
world.push_back({ 4.0 * std::cos(a0), 4.0 * std::sin(a0), 4.0 * std::cos(a1),
|
|
4.0 * std::sin(a1) });
|
|
|
|
const Detection d = rig.run(world);
|
|
ASSERT_TRUE(d.valid) << d.reason;
|
|
EXPECT_NEAR(d.apex_x, 2.00, 0.03);
|
|
EXPECT_NEAR(d.measured_arc_radius, 0.40, 0.03);
|
|
EXPECT_LT(d.measured_arc_rms, 0.010) << "sai so khop no ra = da nuot ca buc tuong";
|
|
}
|
|
|
|
// ================================================================================================
|
|
// 2. Từ chối — phần quan trọng nhất
|
|
// ================================================================================================
|
|
|
|
TEST(ArcPatternMatcher, RefusesARightAngledCorner)
|
|
{
|
|
// Mọi tập điểm đều khớp được vào một đường tròn NÀO ĐÓ. Cổng duy nhất phân biệt là sai số khớp.
|
|
Rig rig;
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const std::vector<Segment> corner = { { 2.0, -0.4, 2.0, 0.0 }, { 2.0, 0.0, 2.4, 0.0 } };
|
|
const Detection d = rig.run(corner);
|
|
EXPECT_FALSE(d.valid) << "goc tuong bi nhan la mang cong";
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, RefusesAFlatWall)
|
|
{
|
|
Rig rig;
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const Detection d = rig.run({ { 2.0, -1.0, 2.0, 1.0 } });
|
|
EXPECT_FALSE(d.valid) << "tuong phang bi nhan la mang cong";
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, RefusesAConvexCylinder)
|
|
{
|
|
// Cùng bán kính, cùng chỗ, chỉ khác chiều cong. Nhận nhầm là pose đích rơi ra SAU mặt trụ, tức
|
|
// robot được lệnh lái vào trong vật.
|
|
Rig rig;
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const Detection d = rig.run(convexCylinder(2.40, 0.40, 2.0));
|
|
EXPECT_FALSE(d.valid) << d.reason;
|
|
EXPECT_NE(d.reason.find("WRONG WAY"), std::string::npos) << d.reason;
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, RefusesAnArcOfTheWrongRadius)
|
|
{
|
|
Rig rig; // profile doi 0.40 m
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const Detection d = rig.run(concaveCradle(1.20, 0.80, 2.0));
|
|
EXPECT_FALSE(d.valid) << d.reason;
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, RefusesAnArcThatShowsTooLittleOfItself)
|
|
{
|
|
// Cổng SỐ HỌC, không phải hình học: dưới ngưỡng này bán kính khớp bắt đầu lệch nhanh, nên một
|
|
// mẩu cung 15 độ không được phép quyết định robot đỗ ở đâu.
|
|
Rig rig;
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const Detection d = rig.run(arcSegments(1.60, 0.0, 0.40, -0.13, 0.26)); // ~15 do
|
|
EXPECT_FALSE(d.valid) << d.reason;
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, RefusesAnArcThatSpansMoreThanTheProfileSays)
|
|
{
|
|
DockProfile p = arcProfile();
|
|
p.arc_span = 1.0; // [rad] mang that chi mo 1.0 rad
|
|
Rig rig(p);
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const Detection d = rig.run(concaveCradle(1.60, 0.40, 2.6)); // gan tron nua duong tron
|
|
EXPECT_FALSE(d.valid) << d.reason;
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, RefusesAScanWithNoSegments)
|
|
{
|
|
Rig rig;
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const Detection d = rig.run({});
|
|
EXPECT_FALSE(d.valid);
|
|
EXPECT_NE(d.reason.find("no line segments"), std::string::npos) << d.reason;
|
|
}
|
|
|
|
// ================================================================================================
|
|
// 3. Cổng tìm kiếm
|
|
// ================================================================================================
|
|
|
|
TEST(ArcPatternMatcher, ThePriorGateRejectsACradleInTheWrongPlace)
|
|
{
|
|
DockProfile p = arcProfile();
|
|
p.has_prior = true;
|
|
p.prior_x = 5.0; // [m] cho doi mang o cho khac han
|
|
p.prior_y = 0.0;
|
|
p.search_radius = 0.5; // [m]
|
|
Rig rig(p);
|
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
|
|
|
const Detection d = rig.run(concaveCradle(1.60, 0.40, 2.0));
|
|
EXPECT_FALSE(d.valid) << d.reason;
|
|
EXPECT_NE(d.reason.find("search gate"), std::string::npos) << d.reason;
|
|
}
|
|
|
|
// ================================================================================================
|
|
// 4. Config hỏng
|
|
// ================================================================================================
|
|
|
|
TEST(ArcPatternMatcher, RefusesAProfileThatCanNeverMatch)
|
|
{
|
|
std::string error;
|
|
ArcPatternMatcher m;
|
|
|
|
DockProfile p = arcProfile();
|
|
p.arc_radius_tolerance = p.arc_radius; // dung sai nuot tron phep do
|
|
EXPECT_FALSE(m.configure(p, error));
|
|
EXPECT_NE(error.find("arc_radius_tolerance"), std::string::npos) << error;
|
|
|
|
p = arcProfile();
|
|
p.arc_min_span = p.arc_span + 0.1; // san cao hon tran: khong cung nao lot
|
|
EXPECT_FALSE(m.configure(p, error));
|
|
EXPECT_NE(error.find("arc_min_span"), std::string::npos) << error;
|
|
|
|
p = arcProfile();
|
|
p.arc_max_rms = 0.0; // doi khop hoan hao tuyet doi
|
|
EXPECT_FALSE(m.configure(p, error));
|
|
EXPECT_NE(error.find("arc_max_rms"), std::string::npos) << error;
|
|
|
|
p = arcProfile();
|
|
p.min_arc_points = 2;
|
|
EXPECT_FALSE(m.configure(p, error));
|
|
EXPECT_NE(error.find("min_arc_points"), std::string::npos) << error;
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, RefusesAProfileForADifferentPattern)
|
|
{
|
|
std::string error;
|
|
ArcPatternMatcher m;
|
|
|
|
DockProfile p = arcProfile();
|
|
p.pattern = PatternType::kV;
|
|
EXPECT_FALSE(m.configure(p, error)) << "matcher cung nhan profile chu V";
|
|
}
|
|
|
|
TEST(ArcPatternMatcher, RefusesTapeOnAnArcProfileInsteadOfSilentlyIgnoringIt)
|
|
{
|
|
// findTape quét theo chỉ số tia của MỘT ĐOẠN THẲNG; cung tròn không sinh ra đoạn nào để quét.
|
|
// Nhận cờ rồi lặng lẽ bỏ qua là để người vận hành tưởng cổng phản quang đang bảo vệ mình.
|
|
std::string error;
|
|
ArcPatternMatcher m;
|
|
|
|
DockProfile p = arcProfile();
|
|
p.tape_enabled = true;
|
|
EXPECT_FALSE(m.configure(p, error));
|
|
EXPECT_NE(error.find("tape_enabled"), std::string::npos) << error;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|