Describe the sample set when a detection is rejected as unstable

The spread number alone cannot separate two failures that need opposite
responses: a handful of samples landing far away while the rest agree, which
is a mis-detection at the source, versus every sample drifting, which means
the detection genuinely is not settled and the robot should re-detect or move
closer.

Four field runs hit this gate with spreads between 1.3 m and 3.2 m while the
detector itself reported the pallet to within a millimetre and the robot stood
still. Each run eliminated one theory and offered nothing to replace it,
because a single scalar cannot say where the samples went.

On failure the handler now logs the sample count, the filtered centre, the
deviation at p50, p90 and max, and the bounding box in the fixed frame.
Comparing p50 against p90 separates the two cases, and the box says where the
stray samples ended up - sitting on the robot's own position means something
very different from being scattered around the target.

Runs once per failed detection, never on the sampling path.
This commit is contained in:
2026-08-13 16:35:59 +07:00
parent 60413b2c1b
commit 643f63f906

View File

@@ -11,6 +11,7 @@
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <memory>
#include <string>
#include <vector>
@@ -314,6 +315,15 @@ public:
{
// Thấy, nhưng không đáng tin. Khác hẳn "không thấy gì" — và nguy hiểm hơn, vì trung bình của
// dữ liệu rác vẫn ra một con số trông hợp lý.
//
// Riêng ca này log thêm hình dạng tập mẫu, không chỉ một con số tổng hợp. Chỉ mình `spread`
// không phân biệt được hai nguyên nhân đòi hai cách xử lý trái ngược:
// - vài mẫu văng xa, phần còn lại chụm -> mẫu nhận nhầm, sửa ở nguồn publish frame;
// - mọi mẫu đều lệch -> dò thật sự không ổn định, phải dò lại/đứng gần hơn.
// Hộp bao cho biết các mẫu văng đi ĐÂU: trùng vị trí robot là một chuyện, rải đều là chuyện
// khác hẳn. Chỉ chạy đúng một lần mỗi lượt dò hỏng nên không đụng gì tới đường nóng.
robot::log_error("[action_core] '%s': %s\n", name_.c_str(), describeSamples(filtered).c_str());
tick.status = ActionStatus::kFailed;
tick.message = "action '" + action_id_ + "': detection unstable (spread xy=" +
std::to_string(spread_xy) + " m, yaw=" + std::to_string(spread_yaw) + " rad)";
@@ -397,6 +407,53 @@ private:
samples_.push_back(s);
}
/**
* @brief Mô tả hình dạng tập mẫu để chẩn đoán một lượt dò hỏng.
*
* Ba phân vị của độ lệch cộng hộp bao trong frame cố định. Đọc theo cặp:
* - `p50` nhỏ mà `p90` lớn: đa số mẫu chụm, một nhúm văng ra — mẫu nhận nhầm ở nguồn.
* - `p50` xấp xỉ `p90`: mọi mẫu đều lệch — phép dò thật sự không ổn định.
* Hộp bao chỉ ra các mẫu văng nằm ở đâu, thứ mà không con số tổng hợp nào nói được.
*/
std::string describeSamples(const Sample& centre) const
{
if (samples_.empty())
{
return "no sample to describe";
}
std::vector<double> dev;
dev.reserve(samples_.size());
double min_x = samples_.front().x;
double max_x = min_x;
double min_y = samples_.front().y;
double max_y = min_y;
for (const Sample& s : samples_)
{
dev.push_back(std::hypot(s.x - centre.x, s.y - centre.y));
min_x = std::min(min_x, s.x);
max_x = std::max(max_x, s.x);
min_y = std::min(min_y, s.y);
max_y = std::max(max_y, s.y);
}
std::sort(dev.begin(), dev.end());
const auto at = [&dev](double q) {
const std::size_t i =
std::min(dev.size() - 1, static_cast<std::size_t>(dev.size() * q));
return dev[i];
};
char buffer[320];
std::snprintf(buffer, sizeof(buffer),
"%zu sample(s) of '%s' in '%s': median=(%.3f, %.3f) yaw=%.4f rad, "
"deviation p50=%.3f p90=%.3f max=%.3f m, bbox x=[%.3f, %.3f] y=[%.3f, %.3f]",
samples_.size(), frame_.c_str(), ctx_.global_frame.c_str(), centre.x, centre.y,
centre.yaw, at(0.5), at(0.9), dev.back(), min_x, max_x, min_y, max_y);
return buffer;
}
void filter(Sample& out, double& spread_xy, double& spread_yaw) const
{
std::vector<double> xs, ys;