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:
32
README.md
32
README.md
@@ -165,6 +165,28 @@ tường) và bề rộng biểu kiến → giả thuyết từ cặp chân kh
|
||||
phải đứng thì loại) → lòng xe trống → chọn cạnh vào (dải `entry`, prior, hoặc cạnh quay về cảm
|
||||
biến) → cổng prior → từ chối khi hai xe điểm sát nhau ở hai chỗ.
|
||||
|
||||
### Lõi phản quang bị loang và bias cự ly
|
||||
|
||||
Mặc định `tape_bloom_max_angle: 0` giữ nguyên hoàn toàn đường phân cụm hình học. Khi bật dải và đặt
|
||||
góc dương, matcher tìm các run tia sáng trước, đi ra ngoài lõi tối đa góc này để nhận diện vệt
|
||||
`bloom`, rồi loại lõi và vệt khỏi phân cụm hình học. Chỉ điểm lõi đi vào candidate; hai mép vẫn được
|
||||
xếp loại tự do / bị che / dính theo cùng luật và candidate đi qua các cổng chân hiện có.
|
||||
|
||||
`tape_min_intensity` là ngưỡng xác định lõi khi bộ lọc bloom bật. Với băng gương trên lidar KIT, lõi
|
||||
đo được khoảng 13k–27k, vùng chuyển bloom khoảng 2k–10k; ngưỡng khoảng 8000 giữ bề rộng lõi gần mặt
|
||||
chân thật. Hạ thấp quá sẽ đưa phần bloom vào lõi và làm chân rộng giả; tăng quá có thể bỏ mất tia lõi.
|
||||
Góc bloom quá lớn có thể hấp thụ một vật ở cạnh chân, vì vậy cần đo trên scan thật. Khi bật bộ lọc,
|
||||
run sáng có độ trải cự ly lớn hơn `maxApparentLegWidth()` bị loại: về vật lý một chân không thể trải
|
||||
theo tia xa hơn bề rộng biểu kiến của chính nó. Giới hạn này chỉ đúng khi ngưỡng cường độ nằm trên
|
||||
lõi chân. Trên lidar KIT, ở ngưỡng 8000 có 5501 lõi thật với độ trải tối đa khoảng 0.041 m; ở 4000
|
||||
lõi thật đã trải tới 0.068 m, nên KIT cần ngưỡng 8000.
|
||||
|
||||
`tape_range_bias_sigma` [m] bật ước lượng thêm một bias cự ly chung cho các điểm sáng trong fit 4-DoF
|
||||
(x, y, yaw, b). Fit chỉ dùng đường này khi có ít nhất một góc chứa điểm sáng và ít nhất một góc có
|
||||
toàn bộ điểm không sáng (chân trần); điểm sáng được hiệu chỉnh dọc theo tia, còn prior Gaussian giữ
|
||||
bias gần 0. `0` tắt ước lượng và dùng fit 3-DoF cũ. Fit từ chối kết quả nếu `|b| > 0.1 m` để tránh
|
||||
đưa hiệu chỉnh bất thường vào pose.
|
||||
|
||||
**Điểm tin** = ½ · (số chân thấy / 4) + ½ · clamp(2 − RMS chuẩn hoá, 0, 1). RMS chuẩn hoá chia phần dư
|
||||
cho `sqrt(σ_điểm² + (cart_dim_tolerance / 2)²)`: σ_điểm là nhiễu lidar (`range_sigma`), còn
|
||||
`cart_dim_tolerance / 2` là σ của mô hình, cho sai số gia công giữa các xe cùng loại. Phần σ mô hình
|
||||
@@ -185,6 +207,11 @@ thành "bị che". Bật khoá: mép kề tia 0 là mép tự do; tia 0 tại g
|
||||
— góc chỉ toàn tia 0 thì coi là không có thông tin, không loại xe; tia đi qua rồi trả về từ vật phía
|
||||
sau vẫn loại xe như cũ.
|
||||
|
||||
**Chân trần khuất sau chân dán**: ở chế độ `entry`, chân xa không dán nằm trong khoảng 3° phía sau
|
||||
một chân dán đầu vào có thể bị bloom của chân gần làm méo rồi mất. Thay đổi bloom chỉ xử lý đường
|
||||
lõi sáng, không sửa đường chân trần nên không khôi phục được ca này; robot nên hướng gần vuông góc
|
||||
với xe.
|
||||
|
||||
Quy ước đầu ra giống V/arc: `center_yaw` là pháp tuyến cạnh vào hướng về cảm biến; lật theo cách
|
||||
robot chui (tiến/lùi) là việc của handler. Plan đầy đủ: `Test/docs/trolley_detection_action/` của
|
||||
workspace phát triển.
|
||||
@@ -200,8 +227,9 @@ catkin_make --pkg dock_pattern_detector
|
||||
```
|
||||
|
||||
`test/data/kit_f_scan_trolley_occlusion.txt` là 6 scan thật của lidar KIT (28/10): một cột che lần lượt
|
||||
từng chân, chân xa-phải dính vật phía sau. Mốc so sánh trong file là kết quả dò của chính robot, không
|
||||
phải số đo thước.
|
||||
từng chân, chân xa-phải dính vật phía sau. `test/data/kit_f_scan_trolley_tape.txt` chứa ranges và
|
||||
intensities của các scan dán băng gương (29/10); mốc so sánh là replay tham chiếu, không phải số đo
|
||||
thước hay ground truth.
|
||||
|
||||
Test chạy đúng chuỗi mà handler sẽ chạy: dựng thế giới → chiếu tia → `robot_line_extraction` →
|
||||
matcher. Nhóm **từ chối** quan trọng hơn nhóm dò đúng, và mỗi ca từ chối khẳng định luôn *lý do* —
|
||||
|
||||
@@ -81,6 +81,9 @@ struct LegDetection
|
||||
double measured_opening = 0.0;
|
||||
double measured_depth = 0.0;
|
||||
|
||||
double tape_range_bias = 0.0; ///< [m] Bias đã bù cho tia sáng; 0 nếu không ước lượng.
|
||||
bool tape_range_bias_estimated = false;
|
||||
|
||||
/// Số tia sáng trên từng chân (thứ tự @ref LegCorner), 0 khi dải tắt hoặc chân không thấy.
|
||||
std::array<std::size_t, kLegCount> tape_bright_rays = { { 0, 0, 0, 0 } };
|
||||
|
||||
@@ -98,8 +101,9 @@ struct LegDetection
|
||||
*
|
||||
* ## Các bước
|
||||
*
|
||||
* 1. Lọc tia (range, quạt, cường độ) + lọc điểm kéo đuôi theo góc β.
|
||||
* 2. Tách cụm theo thứ tự tia.
|
||||
* 1. Lọc tia (range, quạt, cường độ) + lọc điểm kéo đuôi theo góc β. Khi bật dải và
|
||||
* `tape_bloom_max_angle > 0`, tách lõi sáng và vệt bloom trước phân cụm hình học.
|
||||
* 2. Tách cụm theo thứ tự tia còn lại; lõi dải và vệt bloom đã nhận diện không tham gia bước này.
|
||||
* 3. Phân loại cụm chân: mép tự do / bị che / dính; bề rộng biểu kiến; cắt điểm xa; dải `all`.
|
||||
* Cụm dính bị loại, trừ khi `accept_attached_legs`: khi đó nó là chân YẾU.
|
||||
* 4. Giả thuyết từ cặp chân SẠCH (không dính) khớp `cart_opening`, `cart_depth` hoặc đường chéo.
|
||||
@@ -159,6 +163,7 @@ private:
|
||||
bool in_fan = true; // trong quạt tìm kiếm
|
||||
bool shadow = false; // điểm kéo đuôi, đã bỏ
|
||||
bool bright = false; // cường độ ≥ ngưỡng (chỉ khi tape_enabled)
|
||||
bool tape_filtered = false; // lõi dải hoặc vệt bloom, không dùng để tách cụm hình học
|
||||
};
|
||||
|
||||
struct LegCandidate
|
||||
@@ -194,7 +199,7 @@ private:
|
||||
|
||||
bool usable(std::size_t i) const
|
||||
{
|
||||
return rays_[i].valid && rays_[i].in_fan && !rays_[i].shadow;
|
||||
return rays_[i].valid && rays_[i].in_fan && !rays_[i].shadow && !rays_[i].tape_filtered;
|
||||
}
|
||||
|
||||
void prepareRays(const robot_line_extraction::ScanGeometry& geometry,
|
||||
@@ -203,6 +208,10 @@ private:
|
||||
void filterShadows(double dtheta);
|
||||
void buildCandidates(double dtheta);
|
||||
void processCluster(double dtheta);
|
||||
void processPoints(const std::vector<std::size_t>& points, int left, int right, double dtheta);
|
||||
|
||||
/// @brief Đi từ lõi dải qua vệt bloom; mã mép trả về như @ref classifyEdge.
|
||||
int walkTapeBloom(std::size_t edge, int step, std::vector<std::size_t>& skirt) const;
|
||||
|
||||
/// @brief Phân loại mép bên ngoài tia @p edge theo hướng @p step (−1 / +1).
|
||||
/// @return 0 tự do, 1 bị che, 2 dính.
|
||||
@@ -241,6 +250,9 @@ private:
|
||||
// Buffer dùng lại giữa các scan.
|
||||
std::vector<Ray> rays_;
|
||||
std::vector<std::size_t> cluster_;
|
||||
std::vector<std::size_t> tape_run_;
|
||||
std::vector<std::size_t> left_skirt_;
|
||||
std::vector<std::size_t> right_skirt_;
|
||||
std::vector<LegCandidate> legs_;
|
||||
std::vector<Hypothesis> hypotheses_;
|
||||
std::vector<FitPoint> fit_points_;
|
||||
@@ -252,6 +264,7 @@ private:
|
||||
std::size_t rejected_width_ = 0;
|
||||
std::size_t rejected_points_ = 0;
|
||||
std::size_t rejected_tape_ = 0;
|
||||
std::size_t rejected_bloom_spread_ = 0;
|
||||
double width_miss_ = 0.0; // [m] bề rộng của cụm trượt cổng bề rộng sát nhất
|
||||
bool truncated_ = false;
|
||||
};
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <dock_pattern_detector/leg_profile.h>
|
||||
@@ -75,6 +76,7 @@ struct FitPoint
|
||||
double x = 0.0; ///< [m] frame của scan
|
||||
double y = 0.0; ///< [m]
|
||||
int corner = 0; ///< chân mà điểm thuộc về
|
||||
bool bright = false; ///< true nếu điểm đến từ lõi dải phản quang.
|
||||
};
|
||||
|
||||
/// @brief Kết quả khớp tư thế.
|
||||
@@ -87,6 +89,9 @@ struct PoseFitResult
|
||||
///< `FitNoise::model_sigma`
|
||||
int iterations = 0;
|
||||
bool converged = false;
|
||||
double bright_range_bias = 0.0; ///< [m] Sai lệch cự ly chung ước lượng cho điểm sáng.
|
||||
bool bright_range_bias_estimated = false;
|
||||
std::string reason; ///< Lý do fit không hợp lệ; rỗng khi fit hợp lệ hoặc dùng đường 3-DoF cũ.
|
||||
};
|
||||
|
||||
/// @brief Tham số mô hình nhiễu của lidar, dùng làm trọng số.
|
||||
@@ -104,6 +109,9 @@ struct FitNoise
|
||||
* như trước khi có khoá này.
|
||||
*/
|
||||
double model_sigma = 0.0;
|
||||
|
||||
/// [m] σ tiên nghiệm của bias cự ly chung cho điểm sáng; 0 = không ước lượng bias.
|
||||
double bright_range_bias_sigma = 0.0;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -114,7 +122,7 @@ bool procrustes2D(const std::vector<std::array<double, 2>>& model,
|
||||
const std::vector<std::array<double, 2>>& scan, Pose2D& out);
|
||||
|
||||
/**
|
||||
* @brief Khớp pose xe trên mọi điểm chân bằng Gauss-Newton 3 bậc tự do có trọng số + Huber.
|
||||
* @brief Khớp pose xe bằng Gauss-Newton có trọng số + Huber.
|
||||
*
|
||||
* Phần dư, với cảm biến ở gốc frame scan:
|
||||
* - tia của điểm CẮT chân dự đoán: cự ly dự đoán dọc tia − cự ly đo, σ = `range_sigma`. Đúng chiều
|
||||
@@ -124,8 +132,14 @@ bool procrustes2D(const std::vector<std::array<double, 2>>& model,
|
||||
* `σ² = (n·u)²·range_sigma² + (n·u⊥)²·(r·Δθ/√12)²`.
|
||||
*
|
||||
* Huber δ = 2σ để điểm lai còn sót không kéo lệch. Mỗi bước chỉ được nhận nếu làm giảm tổng mất
|
||||
* mát Huber (chia đôi bước tới 6 lần). Dừng khi bước < 0.1 mm và < 0.01°, hoặc không bước nào làm
|
||||
* giảm mất mát, tối đa @p max_iterations vòng. Tất định.
|
||||
* mát Huber (chia đôi bước tới 6 lần). Mặc định dùng 3 bậc tự do (x, y, yaw). Khi
|
||||
* `bright_range_bias_sigma > 0`, có ít nhất một góc chứa điểm sáng và ít nhất một góc có toàn bộ
|
||||
* điểm không sáng (chân trần), thêm bậc tự do b với prior Gaussian b/σ; điểm sáng được coi như đo ở
|
||||
* cự ly |p| + b. Nhánh 4-DoF cũng dùng Jacobian sai phân,
|
||||
* Huber và chia đôi bước. Hai nhánh dừng khi bước tịnh tiến nhỏ hơn 0.1 mm và bước yaw nhỏ hơn
|
||||
* 0.01°; nhánh 4-DoF còn yêu cầu |db| < 0.1 mm. Bias |b| > 0.1 m làm fit không hợp lệ. Nếu σ = 0
|
||||
* hoặc không có cả góc sáng lẫn góc chân trần, dùng đường 3-DoF cũ. Tất định; tối đa
|
||||
* @p max_iterations vòng.
|
||||
*/
|
||||
PoseFitResult fitCartPose(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Pose2D& initial, const FitNoise& noise, int max_iterations = 10);
|
||||
|
||||
@@ -153,6 +153,17 @@ struct LegProfile
|
||||
/// [tia] Số tia sáng tối thiểu để một chân được coi là có dải.
|
||||
std::size_t tape_min_points = 1;
|
||||
|
||||
/**
|
||||
* [rad] Góc tối đa đi từ lõi phản quang qua vệt bloom để nhận lại bề mặt chân. 0 = tắt, giữ
|
||||
* nguyên phân cụm hình học cũ.
|
||||
*/
|
||||
double tape_bloom_max_angle = 0.0;
|
||||
|
||||
/**
|
||||
* [m] Độ lệch chuẩn tiên nghiệm của sai lệch cự ly chung cho các tia sáng; 0 = không ước lượng.
|
||||
*/
|
||||
double tape_range_bias_sigma = 0.0;
|
||||
|
||||
// --- Prior, trong `prior_frame` của handler (thường là base_link) -------------------------------
|
||||
|
||||
bool has_prior = false;
|
||||
|
||||
@@ -224,6 +224,7 @@ int LegPatternMatcher::classifyEdge(std::size_t edge, int step) const
|
||||
}
|
||||
if (nb.shadow)
|
||||
{
|
||||
// Không bỏ tape_filtered: chân trần cạnh bloom vẫn phải so range bình thường.
|
||||
return 0; // còn kéo đuôi sau khi đã bỏ qua tối đa: một chuỗi điểm lai dẫn ra nền phía sau
|
||||
}
|
||||
if (nb.range >= r_edge + profile_.leg_min_depth_gap)
|
||||
@@ -239,10 +240,18 @@ int LegPatternMatcher::classifyEdge(std::size_t edge, int step) const
|
||||
|
||||
void LegPatternMatcher::processCluster(double dtheta)
|
||||
{
|
||||
++clusters_;
|
||||
if (cluster_.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
processPoints(cluster_, classifyEdge(cluster_.front(), -1), classifyEdge(cluster_.back(), +1),
|
||||
dtheta);
|
||||
}
|
||||
|
||||
const int left = classifyEdge(cluster_.front(), -1);
|
||||
const int right = classifyEdge(cluster_.back(), +1);
|
||||
void LegPatternMatcher::processPoints(const std::vector<std::size_t>& points, int left, int right,
|
||||
double dtheta)
|
||||
{
|
||||
++clusters_;
|
||||
const bool attached = (left == 2 || right == 2);
|
||||
if (attached && !profile_.accept_attached_legs)
|
||||
{
|
||||
@@ -255,7 +264,7 @@ void LegPatternMatcher::processCluster(double dtheta)
|
||||
const bool partial = (left == 1 || right == 1);
|
||||
|
||||
double r_min = std::numeric_limits<double>::infinity();
|
||||
for (std::size_t idx : cluster_)
|
||||
for (std::size_t idx : points)
|
||||
{
|
||||
r_min = std::min(r_min, rays_[idx].range);
|
||||
}
|
||||
@@ -267,7 +276,7 @@ void LegPatternMatcher::processCluster(double dtheta)
|
||||
leg.attached = attached;
|
||||
leg.r_min = r_min;
|
||||
const double depth_limit = r_min + profile_.maxApparentLegWidth();
|
||||
for (std::size_t idx : cluster_)
|
||||
for (std::size_t idx : points)
|
||||
{
|
||||
if (rays_[idx].range <= depth_limit)
|
||||
{
|
||||
@@ -331,6 +340,62 @@ void LegPatternMatcher::processCluster(double dtheta)
|
||||
legs_.push_back(std::move(leg));
|
||||
}
|
||||
|
||||
int LegPatternMatcher::walkTapeBloom(std::size_t edge, int step,
|
||||
std::vector<std::size_t>& skirt) const
|
||||
{
|
||||
const double r0 = rays_[edge].range;
|
||||
double previous = r0;
|
||||
const double requested_steps = profile_.tape_bloom_max_angle / std::fabs(angle_increment_);
|
||||
const std::size_t max_steps = !std::isfinite(requested_steps) ||
|
||||
requested_steps >= static_cast<double>(rays_.size())
|
||||
? rays_.size()
|
||||
: static_cast<std::size_t>(requested_steps);
|
||||
long j = static_cast<long>(edge) + step;
|
||||
const long n = static_cast<long>(rays_.size());
|
||||
for (std::size_t walked = 0; walked < max_steps; ++walked)
|
||||
{
|
||||
if (j < 0 || j >= n)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
const Ray& ray = rays_[j];
|
||||
if (!ray.valid)
|
||||
{
|
||||
if (zero_no_echo_ && ray.range <= 0.0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return std::isfinite(ray.range) && ray.range < min_range_ ? 1 : 0;
|
||||
}
|
||||
if (!ray.in_fan)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (ray.bright)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
if (ray.range >= r0 + profile_.leg_min_depth_gap)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (ray.range <= r0 - profile_.leg_min_depth_gap)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (ray.range < previous - 0.005)
|
||||
{
|
||||
return ray.range < r0 - 0.01 ? 1 : 2;
|
||||
}
|
||||
|
||||
skirt.push_back(static_cast<std::size_t>(j));
|
||||
previous = ray.range;
|
||||
j += step;
|
||||
}
|
||||
return 2; // Hết góc cho phép khi vẫn còn bề mặt ở gần lõi: mép dính.
|
||||
}
|
||||
|
||||
void LegPatternMatcher::buildCandidates(double dtheta)
|
||||
{
|
||||
legs_.clear();
|
||||
@@ -340,8 +405,89 @@ void LegPatternMatcher::buildCandidates(double dtheta)
|
||||
rejected_width_ = 0;
|
||||
rejected_points_ = 0;
|
||||
rejected_tape_ = 0;
|
||||
rejected_bloom_spread_ = 0;
|
||||
width_miss_ = std::numeric_limits<double>::infinity();
|
||||
truncated_ = false;
|
||||
tape_run_.clear();
|
||||
left_skirt_.clear();
|
||||
right_skirt_.clear();
|
||||
|
||||
if (profile_.tape_enabled && profile_.tape_bloom_max_angle > 0.0)
|
||||
{
|
||||
const auto consumeRun = [&](const std::vector<std::size_t>& bright_run) {
|
||||
double min_range = std::numeric_limits<double>::infinity();
|
||||
double max_range = -std::numeric_limits<double>::infinity();
|
||||
for (std::size_t idx : bright_run)
|
||||
{
|
||||
min_range = std::min(min_range, rays_[idx].range);
|
||||
max_range = std::max(max_range, rays_[idx].range);
|
||||
}
|
||||
// Giới hạn vật lý: một chân không thể có độ trải cự ly lớn hơn bề rộng biểu kiến của nó.
|
||||
// Cổng này cần ngưỡng nằm trên lõi: KIT ở 8000 có độ trải lõi thật tối đa 0.041 m (5501 lõi),
|
||||
// còn ở 4000 lõi thật đã tới 0.068 m; vì vậy cấu hình KIT cần 8000. Độ trải lớn hơn thường là
|
||||
// lõi bị bloom của gương gần hơn làm méo, không phải một chân thật.
|
||||
if (max_range - min_range > profile_.maxApparentLegWidth())
|
||||
{
|
||||
for (std::size_t idx : bright_run)
|
||||
{
|
||||
rays_[idx].tape_filtered = true;
|
||||
}
|
||||
++rejected_bloom_spread_;
|
||||
return;
|
||||
}
|
||||
|
||||
if (bright_run.size() < std::max(profile_.tape_min_points, profile_.min_leg_points))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
left_skirt_.clear();
|
||||
right_skirt_.clear();
|
||||
const int left = walkTapeBloom(bright_run.front(), -1, left_skirt_);
|
||||
const int right = walkTapeBloom(bright_run.back(), +1, right_skirt_);
|
||||
for (std::size_t idx : bright_run)
|
||||
{
|
||||
rays_[idx].tape_filtered = true;
|
||||
}
|
||||
for (std::size_t idx : left_skirt_)
|
||||
{
|
||||
rays_[idx].tape_filtered = true;
|
||||
}
|
||||
for (std::size_t idx : right_skirt_)
|
||||
{
|
||||
rays_[idx].tape_filtered = true;
|
||||
}
|
||||
processPoints(bright_run, left, right, dtheta);
|
||||
};
|
||||
|
||||
for (std::size_t i = 0; i < rays_.size(); ++i)
|
||||
{
|
||||
const Ray& ray = rays_[i];
|
||||
const bool bright_in_fan = ray.valid && ray.in_fan && ray.bright;
|
||||
if (!bright_in_fan)
|
||||
{
|
||||
if (!tape_run_.empty())
|
||||
{
|
||||
consumeRun(tape_run_);
|
||||
tape_run_.clear();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!tape_run_.empty() &&
|
||||
std::fabs(ray.range - rays_[tape_run_.back()].range) > profile_.leg_cluster_gap)
|
||||
{
|
||||
consumeRun(tape_run_);
|
||||
tape_run_.clear();
|
||||
}
|
||||
tape_run_.push_back(i);
|
||||
}
|
||||
if (!tape_run_.empty())
|
||||
{
|
||||
consumeRun(tape_run_);
|
||||
tape_run_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t gap = 0; // số tia không dùng được liên tiếp kể từ điểm cuối của cụm
|
||||
for (std::size_t i = 0; i < rays_.size(); ++i)
|
||||
@@ -400,6 +546,14 @@ std::string LegPatternMatcher::describeClusterRejections() const
|
||||
{
|
||||
text += ", " + std::to_string(rejected_tape_) + " without reflective tape";
|
||||
}
|
||||
if (profile_.tape_enabled && profile_.tape_bloom_max_angle > 0.0 &&
|
||||
rejected_bloom_spread_ > 0)
|
||||
{
|
||||
text += ", " + std::to_string(rejected_bloom_spread_) +
|
||||
" bright core(s) spread over more than " +
|
||||
format("%.3f", profile_.maxApparentLegWidth()) +
|
||||
" m in range (bloom from a nearer mirror)";
|
||||
}
|
||||
if (rejected_width_ > 0 && std::isfinite(width_miss_))
|
||||
{
|
||||
text += format(" (closest width miss %.3f m; leg_width %.3f m)", width_miss_,
|
||||
@@ -743,6 +897,7 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
FitNoise noise;
|
||||
noise.range_sigma = limits.range_sigma;
|
||||
noise.angle_increment = dtheta;
|
||||
noise.bright_range_bias_sigma = profile_.tape_enabled ? profile_.tape_range_bias_sigma : 0.0;
|
||||
// Xe cùng loại vẫn lệch vài cm do gia công: một cạnh lệch tối đa `cart_dim_tolerance` đẩy mỗi
|
||||
// chân lệch tối đa tolerance/2 khỏi góc của mô hình. Coi mức đó là 1σ của mô hình, để điểm tin
|
||||
// không trừ điểm xe nằm trong dung sai mà vẫn trừ điểm xe lệch xa hơn.
|
||||
@@ -769,6 +924,7 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
p.x = rays_[idx].x;
|
||||
p.y = rays_[idx].y;
|
||||
p.corner = k;
|
||||
p.bright = rays_[idx].bright;
|
||||
fit_points_.push_back(p);
|
||||
}
|
||||
}
|
||||
@@ -776,7 +932,7 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
if (!fit.valid)
|
||||
{
|
||||
h.stage = kStageFit;
|
||||
h.failure = "pose fit diverged";
|
||||
h.failure = fit.reason.empty() ? "pose fit diverged" : fit.reason;
|
||||
return false;
|
||||
}
|
||||
const std::array<int, kLegCount> before = h.legs;
|
||||
@@ -807,6 +963,23 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
return false;
|
||||
}
|
||||
|
||||
// Tạo tâm thô đã hiệu chỉnh theo giả thuyết hiện tại; legs_ dùng chung giữa các giả thuyết nên
|
||||
// không ghi bias trở lại LegCandidate.
|
||||
const auto adjustedRough = [&](int leg_index) {
|
||||
const LegCandidate& leg = legs_[leg_index];
|
||||
std::array<double, 2> centre = { leg.rough_x, leg.rough_y };
|
||||
if (fit.bright_range_bias_estimated && leg.bright >= profile_.tape_min_points)
|
||||
{
|
||||
const double range = std::hypot(centre[0], centre[1]);
|
||||
if (range > 1e-9)
|
||||
{
|
||||
centre[0] += fit.bright_range_bias * centre[0] / range;
|
||||
centre[1] += fit.bright_range_bias * centre[1] / range;
|
||||
}
|
||||
}
|
||||
return centre;
|
||||
};
|
||||
|
||||
// --- Khoảng cách giữa các chân đã gán ------------------------------------------------------------
|
||||
// Khớp cứng luôn ra MỘT pose, kể cả khi bốn "chân" là chân của hai xe kề nhau đứng lệch vài cm so
|
||||
// với mô hình: phần dư dàn đều, điểm tin vẫn có thể qua. Kiểm thẳng từng cặp chặn ca đó, cùng
|
||||
@@ -831,7 +1004,9 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
const double tol = profile_.cart_dim_tolerance + std::max(la.r_min, lb.r_min) * dtheta +
|
||||
((la.partial || la.attached) ? profile_.leg_width / 2.0 : 0.0) +
|
||||
((lb.partial || lb.attached) ? profile_.leg_width / 2.0 : 0.0);
|
||||
const double d = std::hypot(la.rough_x - lb.rough_x, la.rough_y - lb.rough_y);
|
||||
const std::array<double, 2> ca = adjustedRough(ia);
|
||||
const std::array<double, 2> cb = adjustedRough(ib);
|
||||
const double d = std::hypot(ca[0] - cb[0], ca[1] - cb[1]);
|
||||
if (std::fabs(d - expected) > tol)
|
||||
{
|
||||
h.stage = kStageFit;
|
||||
@@ -862,6 +1037,7 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
int inside = 0;
|
||||
for (const Ray& r : rays_)
|
||||
{
|
||||
// Bloom vẫn là phản hồi vật lý và tham gia cổng lòng xe; chỉ bỏ shadow như cũ.
|
||||
if (!r.valid || r.shadow)
|
||||
{
|
||||
continue;
|
||||
@@ -981,6 +1157,8 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
d.legs_out_of_view = out_of_view;
|
||||
d.fit_rms = fit.rms;
|
||||
d.fit_iterations = fit.iterations;
|
||||
d.tape_range_bias = fit.bright_range_bias;
|
||||
d.tape_range_bias_estimated = fit.bright_range_bias_estimated;
|
||||
|
||||
// Đích = tâm dịch theo offset, trong hệ của chính xe: x dọc pháp tuyến cạnh vào hướng ra phía
|
||||
// robot, y sang trái — cùng công thức V/arc.
|
||||
@@ -994,9 +1172,9 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
const auto measure = [&](int a, int b, double& sum, int& count) {
|
||||
if (h.legs[a] >= 0 && h.legs[b] >= 0)
|
||||
{
|
||||
const LegCandidate& la = legs_[h.legs[a]];
|
||||
const LegCandidate& lb = legs_[h.legs[b]];
|
||||
sum += std::hypot(la.rough_x - lb.rough_x, la.rough_y - lb.rough_y);
|
||||
const std::array<double, 2> ca = adjustedRough(h.legs[a]);
|
||||
const std::array<double, 2> cb = adjustedRough(h.legs[b]);
|
||||
sum += std::hypot(ca[0] - cb[0], ca[1] - cb[1]);
|
||||
++count;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <dock_pattern_detector/leg_pose_fit.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
@@ -203,6 +204,108 @@ double robustCost(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
}
|
||||
return cost;
|
||||
}
|
||||
|
||||
FitPoint correctedPoint(const FitPoint& point, double bias)
|
||||
{
|
||||
FitPoint corrected = point;
|
||||
if (point.bright)
|
||||
{
|
||||
const double range = std::hypot(point.x, point.y);
|
||||
if (range > 1e-9)
|
||||
{
|
||||
const double scale = (range + bias) / range;
|
||||
corrected.x *= scale;
|
||||
corrected.y *= scale;
|
||||
}
|
||||
}
|
||||
return corrected;
|
||||
}
|
||||
|
||||
double residualWithBias(const LegModel& model, const FitPoint& point, double x, double y,
|
||||
double yaw, double bias, bool& on_ray)
|
||||
{
|
||||
return residual(model, correctedPoint(point, bias), x, y, yaw, on_ray);
|
||||
}
|
||||
|
||||
struct PointTerm4
|
||||
{
|
||||
double r = 0.0; // [m] phần dư
|
||||
Eigen::Vector4d jacobian; // ∂r/∂(x, y, yaw, b)
|
||||
double sigma = kMinSigma; // [m]
|
||||
};
|
||||
|
||||
PointTerm4 evaluateWithBias(const LegModel& model, const FitPoint& point,
|
||||
const Eigen::Vector4d& state, const FitNoise& noise)
|
||||
{
|
||||
PointTerm4 term;
|
||||
bool on_ray = false;
|
||||
term.r = residualWithBias(model, point, state.x(), state.y(), state.z(), state.w(), on_ray);
|
||||
const auto probe = [&](double x, double y, double yaw, double bias) {
|
||||
bool ignored = false;
|
||||
return residualWithBias(model, point, x, y, yaw, bias, ignored);
|
||||
};
|
||||
term.jacobian.x() =
|
||||
(probe(state.x() + kStepTranslation, state.y(), state.z(), state.w()) -
|
||||
probe(state.x() - kStepTranslation, state.y(), state.z(), state.w())) /
|
||||
(2.0 * kStepTranslation);
|
||||
term.jacobian.y() =
|
||||
(probe(state.x(), state.y() + kStepTranslation, state.z(), state.w()) -
|
||||
probe(state.x(), state.y() - kStepTranslation, state.z(), state.w())) /
|
||||
(2.0 * kStepTranslation);
|
||||
term.jacobian.z() =
|
||||
(probe(state.x(), state.y(), state.z() + kStepRotation, state.w()) -
|
||||
probe(state.x(), state.y(), state.z() - kStepRotation, state.w())) /
|
||||
(2.0 * kStepRotation);
|
||||
term.jacobian.w() =
|
||||
(probe(state.x(), state.y(), state.z(), state.w() + kStepTranslation) -
|
||||
probe(state.x(), state.y(), state.z(), state.w() - kStepTranslation)) /
|
||||
(2.0 * kStepTranslation);
|
||||
|
||||
const FitPoint corrected = correctedPoint(point, state.w());
|
||||
if (on_ray)
|
||||
{
|
||||
term.sigma = std::max(kMinSigma, noise.range_sigma);
|
||||
return term;
|
||||
}
|
||||
|
||||
double nx = -term.jacobian.x();
|
||||
double ny = -term.jacobian.y();
|
||||
const double nn = std::hypot(nx, ny);
|
||||
const double range = std::hypot(corrected.x, corrected.y);
|
||||
if (nn > 1e-9 && range > 1e-9)
|
||||
{
|
||||
nx /= nn;
|
||||
ny /= nn;
|
||||
const double ux = corrected.x / range;
|
||||
const double uy = corrected.y / range;
|
||||
const double along = nx * ux + ny * uy;
|
||||
const double across = -nx * uy + ny * ux;
|
||||
const double sigma_across = range * noise.angle_increment / std::sqrt(12.0);
|
||||
const double var = along * along * noise.range_sigma * noise.range_sigma +
|
||||
across * across * sigma_across * sigma_across;
|
||||
term.sigma = std::max(kMinSigma, std::sqrt(var));
|
||||
}
|
||||
else
|
||||
{
|
||||
term.sigma = std::max(kMinSigma, noise.range_sigma);
|
||||
}
|
||||
return term;
|
||||
}
|
||||
|
||||
double robustCostWithBias(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Eigen::Vector4d& state, const FitNoise& noise)
|
||||
{
|
||||
double cost = 0.0;
|
||||
for (const FitPoint& point : points)
|
||||
{
|
||||
const PointTerm4 term = evaluateWithBias(model, point, state, noise);
|
||||
const double error = std::fabs(term.r) / term.sigma;
|
||||
cost += (error <= kHuberK) ? 0.5 * error * error
|
||||
: kHuberK * error - 0.5 * kHuberK * kHuberK;
|
||||
}
|
||||
const double prior = state.w() / noise.bright_range_bias_sigma;
|
||||
return cost + 0.5 * prior * prior;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
LegModel LegModel::fromProfile(const LegProfile& profile)
|
||||
@@ -306,8 +409,10 @@ bool procrustes2D(const std::vector<std::array<double, 2>>& model,
|
||||
return true;
|
||||
}
|
||||
|
||||
PoseFitResult fitCartPose(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Pose2D& initial, const FitNoise& noise, int max_iterations)
|
||||
namespace
|
||||
{
|
||||
PoseFitResult fitCartPose3D(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Pose2D& initial, const FitNoise& noise, int max_iterations)
|
||||
{
|
||||
PoseFitResult result;
|
||||
result.pose = initial;
|
||||
@@ -394,4 +499,132 @@ PoseFitResult fitCartPose(const LegModel& model, const std::vector<FitPoint>& po
|
||||
return result;
|
||||
}
|
||||
|
||||
PoseFitResult fitCartPose4D(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Pose2D& initial, const FitNoise& noise, int max_iterations)
|
||||
{
|
||||
PoseFitResult result;
|
||||
result.pose = initial;
|
||||
result.bright_range_bias_estimated = true;
|
||||
if (points.size() < 3)
|
||||
{
|
||||
result.reason = "4-DoF pose fit requires at least three points";
|
||||
return result;
|
||||
}
|
||||
|
||||
Eigen::Vector4d state(initial.x, initial.y, initial.yaw, 0.0);
|
||||
for (int iter = 0; iter < max_iterations; ++iter)
|
||||
{
|
||||
Eigen::Matrix4d normal = Eigen::Matrix4d::Zero();
|
||||
Eigen::Vector4d rhs = Eigen::Vector4d::Zero();
|
||||
for (const FitPoint& point : points)
|
||||
{
|
||||
const PointTerm4 term = evaluateWithBias(model, point, state, noise);
|
||||
const double error = std::fabs(term.r) / term.sigma;
|
||||
const double huber = (error <= kHuberK) ? 1.0 : kHuberK / error;
|
||||
const double weight = huber / (term.sigma * term.sigma);
|
||||
normal += weight * term.jacobian * term.jacobian.transpose();
|
||||
rhs -= weight * term.jacobian * term.r;
|
||||
}
|
||||
|
||||
const double prior_jacobian = 1.0 / noise.bright_range_bias_sigma;
|
||||
normal(3, 3) += prior_jacobian * prior_jacobian;
|
||||
rhs(3) -= state.w() * prior_jacobian * prior_jacobian;
|
||||
normal += 1e-9 * (1.0 + normal.trace()) * Eigen::Matrix4d::Identity();
|
||||
Eigen::Vector4d step = normal.ldlt().solve(rhs);
|
||||
if (!step.allFinite())
|
||||
{
|
||||
result.reason = "4-DoF pose fit diverged (non-finite step)";
|
||||
return result;
|
||||
}
|
||||
|
||||
const double cost_now = robustCostWithBias(model, points, state, noise);
|
||||
bool accepted = false;
|
||||
for (int halving = 0; halving <= kMaxHalvings; ++halving)
|
||||
{
|
||||
Eigen::Vector4d candidate = state + step;
|
||||
candidate.z() = wrapAngle(candidate.z());
|
||||
if (robustCostWithBias(model, points, candidate, noise) < cost_now)
|
||||
{
|
||||
state = candidate;
|
||||
accepted = true;
|
||||
break;
|
||||
}
|
||||
step *= 0.5;
|
||||
}
|
||||
result.iterations = iter + 1;
|
||||
if (!accepted)
|
||||
{
|
||||
result.converged = true;
|
||||
break;
|
||||
}
|
||||
if (std::hypot(step.x(), step.y()) < kStopTranslation &&
|
||||
std::fabs(step.z()) < kStopRotation && std::fabs(step.w()) < kStopTranslation)
|
||||
{
|
||||
result.converged = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const double model_var = noise.model_sigma > 0.0 ? noise.model_sigma * noise.model_sigma : 0.0;
|
||||
double sum_r2 = 0.0;
|
||||
double sum_e2 = 0.0;
|
||||
for (const FitPoint& point : points)
|
||||
{
|
||||
const PointTerm4 term = evaluateWithBias(model, point, state, noise);
|
||||
sum_r2 += term.r * term.r;
|
||||
sum_e2 += (term.r * term.r) / (term.sigma * term.sigma + model_var);
|
||||
}
|
||||
const double count = static_cast<double>(points.size());
|
||||
result.rms = std::sqrt(sum_r2 / count);
|
||||
result.normalized_rms = std::sqrt(sum_e2 / count);
|
||||
result.pose.x = state.x();
|
||||
result.pose.y = state.y();
|
||||
result.pose.yaw = state.z();
|
||||
result.bright_range_bias = state.w();
|
||||
if (std::fabs(state.w()) > 0.1)
|
||||
{
|
||||
result.reason = "estimated bright range bias " + std::to_string(state.w()) +
|
||||
" m exceeds the 0.1 m safety limit";
|
||||
return result;
|
||||
}
|
||||
result.valid = state.allFinite() && std::isfinite(result.rms);
|
||||
if (!result.valid)
|
||||
{
|
||||
result.reason = "4-DoF pose fit produced a non-finite result";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
PoseFitResult fitCartPose(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Pose2D& initial, const FitNoise& noise, int max_iterations)
|
||||
{
|
||||
std::array<bool, kLegCount> corner_seen = { { false, false, false, false } };
|
||||
std::array<bool, kLegCount> corner_has_bright = { { false, false, false, false } };
|
||||
std::array<bool, kLegCount> corner_all_non_bright = { { true, true, true, true } };
|
||||
for (const FitPoint& point : points)
|
||||
{
|
||||
if (point.corner < 0 || point.corner >= kLegCount)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
const std::size_t corner = static_cast<std::size_t>(point.corner);
|
||||
corner_seen[corner] = true;
|
||||
corner_has_bright[corner] = corner_has_bright[corner] || point.bright;
|
||||
corner_all_non_bright[corner] = corner_all_non_bright[corner] && !point.bright;
|
||||
}
|
||||
bool has_bright_corner = false;
|
||||
bool has_bare_corner = false;
|
||||
for (std::size_t corner = 0; corner < corner_seen.size(); ++corner)
|
||||
{
|
||||
has_bright_corner = has_bright_corner || corner_has_bright[corner];
|
||||
has_bare_corner = has_bare_corner || (corner_seen[corner] && corner_all_non_bright[corner]);
|
||||
}
|
||||
if (noise.bright_range_bias_sigma > 0.0 && has_bright_corner && has_bare_corner)
|
||||
{
|
||||
return fitCartPose4D(model, points, initial, noise, max_iterations);
|
||||
}
|
||||
return fitCartPose3D(model, points, initial, noise, max_iterations);
|
||||
}
|
||||
|
||||
} // namespace dock_pattern_detector
|
||||
|
||||
@@ -188,6 +188,23 @@ bool LegProfile::validate(std::string& error) const
|
||||
error = "tape_min_intensity must be finite and tape_min_points >= 1";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(tape_bloom_max_angle) || tape_bloom_max_angle < 0.0 ||
|
||||
tape_bloom_max_angle >= 0.5)
|
||||
{
|
||||
error = "tape_bloom_max_angle must be finite and in [0, 0.5) [rad]";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(tape_range_bias_sigma) || tape_range_bias_sigma < 0.0 ||
|
||||
tape_range_bias_sigma > 0.2)
|
||||
{
|
||||
error = "tape_range_bias_sigma must be finite and in [0, 0.2] [m]";
|
||||
return false;
|
||||
}
|
||||
if (!tape_enabled && (tape_bloom_max_angle > 0.0 || tape_range_bias_sigma > 0.0))
|
||||
{
|
||||
error = "non-zero tape_bloom_max_angle or tape_range_bias_sigma requires tape_enabled: true";
|
||||
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
|
||||
|
||||
46
test/data/kit_f_scan_trolley_tape.txt
Normal file
46
test/data/kit_f_scan_trolley_tape.txt
Normal file
File diff suppressed because one or more lines are too long
18
test/data/kit_f_scan_trolley_tape_all4.txt
Normal file
18
test/data/kit_f_scan_trolley_tape_all4.txt
Normal file
File diff suppressed because one or more lines are too long
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user