feat: take the detection frame from robot_base_frame, drop prior_frame

The search fan, prior and mount checks are now expressed in the context's
robot_base_frame, like DockPatternActionHandler. A leftover prior_frame equal
to it loads with one warning; a different one fails init.
This commit is contained in:
2026-11-12 17:01:54 +07:00
parent 775c16820b
commit 641932b601
5 changed files with 115 additions and 44 deletions

View File

@@ -51,7 +51,7 @@ constexpr double kDefaultRangeSigma = 0.01; // [m]
/// chậm chính vòng điều khiển. 1 Hz đủ để đọc xem đang hỏng ở cổng nào.
constexpr double kReasonLogPeriod = 1.0; // [s]
/// Mặt phẳng quét được nghiêng tối đa bao nhiêu so với mặt sàn của `prior_frame`. Bộ dò coi scan là
/// Mặt phẳng quét được nghiêng tối đa bao nhiêu so với mặt sàn của `robot_base_frame`. Bộ dò coi scan là
/// 2D: nghiêng t làm cự ly ngang sai r·(1 − cos t) — 5° ở 2.3 m là 9 mm, đã sát D6 (±1.5 cm). Quá
/// ngưỡng nghĩa là URDF sai hoặc lidar gắn lệch; dừng thay vì ghi một đích lệch.
constexpr double kMaxScanTilt = 0.0873; // [rad] 5°
@@ -106,7 +106,7 @@ const char* toString(EntryDirection d)
*
* ## Lidar gắn lộn ngược
*
* Tự suy từ TF `prior_frame` <- frame scan, không có khoá cấu hình: trục z của frame scan chỉ xuống
* Tự suy từ TF `robot_base_frame` <- frame scan, không có khoá cấu hình: trục z của frame scan chỉ xuống
* (hook_150: roll π) thì tia ở góc θ trong frame lidar nằm ở góc −θ khi nhìn từ trên xuống. Handler
* đảo dấu `angle_min`/`angle_increment` trước khi dò, nên matcher làm việc trong frame "đứng thẳng"
* cùng gốc, cùng trục x với lidar. Driver không lật dữ liệu; nếu sau này driver lật thì URDF phải bỏ
@@ -117,7 +117,7 @@ const char* toString(EntryDirection d)
*
* - `action_types`, `output_frame`, `scan_topic`, `settle_delay`, `timeout`, `default_profile`,
* `profile_param_key` — cùng nghĩa với `dock_pattern`.
* - `prior_frame` [string] frame của `prior_*` và `search_fov` (mặc định `base_link`)
* - `prior_*` và `search_fov` được khai trong `robot_base_frame` của context
* - `entry_direction` [string] `forward` / `backward`
* - `min_range`, `max_range` [m], `range_sigma` [m]
* - `zero_range_is_no_echo` [bool] driver trả range 0 cho tia không phản hồi (mặc định false,
@@ -165,7 +165,29 @@ public:
nh.param("timeout", timeout_, kDefaultTimeout);
nh.param("default_profile", default_profile_, std::string());
nh.param("profile_param_key", profile_param_key_, std::string("trolley_profile"));
nh.param("prior_frame", prior_frame_, std::string("base_link"));
if (ctx_.robot_base_frame.empty())
{
robot::log_error("[trolley_detection] '%s': robot_base_frame is empty.", name_.c_str());
return false;
}
if (nh.hasParam("prior_frame"))
{
std::string legacy_frame;
if (!nh.getParam("prior_frame", legacy_frame))
{
robot::log_error("[trolley_detection] '%s': prior_frame must be a string.", name_.c_str());
return false;
}
if (legacy_frame != ctx_.robot_base_frame)
{
robot::log_error("[trolley_detection] '%s': prior_frame '%s' differs from robot_base_frame '%s'.",
name_.c_str(), legacy_frame.c_str(), ctx_.robot_base_frame.c_str());
return false;
}
robot::log_warning("[trolley_detection] '%s': prior_frame is obsolete and ignored; "
"detector uses robot_base_frame '%s'; remove prior_frame.",
name_.c_str(), ctx_.robot_base_frame.c_str());
}
nh.param("min_range", limits_.min_range, kDefaultMinRange);
nh.param("max_range", limits_.max_range, kDefaultMaxRange);
nh.param("range_sigma", limits_.range_sigma, kDefaultRangeSigma);
@@ -195,11 +217,6 @@ public:
robot::log_error("[trolley_detection] '%s': scan_topic is empty.", name_.c_str());
return false;
}
if (prior_frame_.empty())
{
robot::log_error("[trolley_detection] '%s': prior_frame is empty.", name_.c_str());
return false;
}
if (direction == "forward")
{
entry_direction_ = EntryDirection::kForward;
@@ -260,10 +277,10 @@ public:
// Dòng tóm tắt lúc boot: chỗ duy nhất xác nhận được giá trị đã nạp mà không mở YAML trên robot.
robot::log_info("[trolley_detection] '%s': %zu profile(s), default '%s', scan '%s', "
"entry_direction %s, prior_frame '%s', range [%.2f, %.2f] m, sigma %.3f m, "
"entry_direction %s, robot_base_frame '%s', range [%.2f, %.2f] m, sigma %.3f m, "
"zero range %s, debug/detect %s.",
name_.c_str(), profiles_.size(), default_profile_.c_str(),
scan_topic_.c_str(), toString(entry_direction_), prior_frame_.c_str(),
scan_topic_.c_str(), toString(entry_direction_), ctx_.robot_base_frame.c_str(),
limits_.min_range, limits_.max_range, limits_.range_sigma,
limits_.zero_range_is_no_echo ? "= no echo" : "= blocked",
debug_detect_ ? "on" : "off");
@@ -343,7 +360,7 @@ public:
p.tape_enabled ? "on" : "off", dock_pattern_detector::toString(p.tape_legs),
p.tape_bloom_max_angle, p.tape_plateau_min_fraction, p.tape_range_bias_sigma,
p.tape_range_bias_all_max_std, p.offset_yaw,
p.has_prior ? "on" : "off", prior_frame_.c_str(), scan_topic_.c_str(),
p.has_prior ? "on" : "off", ctx_.robot_base_frame.c_str(), scan_topic_.c_str(),
toString(entry_direction_), output_frame_.c_str());
}
return true;
@@ -580,7 +597,7 @@ private:
// ==============================================================================================
/**
* @brief Pose 2D của frame scan trong `prior_frame`, tra một lần mỗi lượt rồi cache.
* @brief Pose 2D của frame scan trong `robot_base_frame`, tra một lần mỗi lượt rồi cache.
*
* Lidar gắn cứng vào thân robot nên phép biến đổi này không đổi trong một lượt; tra lại mỗi scan
* chỉ tốn công. Đồng thời suy lidar có lộn ngược không, kiểm mặt quét nằm ngang và hướng lidar
@@ -599,22 +616,22 @@ private:
tf3::TransformStampedMsg tf;
try
{
tf = ctx_.tf->lookupTransform(prior_frame_, scan_frame, tf3::Time());
tf = ctx_.tf->lookupTransform(ctx_.robot_base_frame, scan_frame, tf3::Time());
}
catch (const std::exception& ex)
{
noteReason(now, "no transform '" + prior_frame_ + "' <- '" + scan_frame + "' yet: " +
noteReason(now, "no transform '" + ctx_.robot_base_frame + "' <- '" + scan_frame + "' yet: " +
ex.what());
return false;
}
// Yaw = hướng trục x của lidar, giống nhau ở frame lidar và frame "đứng thẳng" của nó, nên
// `scan_in_prior_` dùng được cho cả hai.
// `scan_in_base_` dùng được cho cả hai.
const double up = upAxisZ(tf.transform.rotation);
const double tilt = std::acos(std::min(1.0, std::fabs(up))); // [rad]
scan_in_prior_.x = tf.transform.translation.x;
scan_in_prior_.y = tf.transform.translation.y;
scan_in_prior_.yaw = yawOf(tf.transform.rotation);
scan_in_base_.x = tf.transform.translation.x;
scan_in_base_.y = tf.transform.translation.y;
scan_in_base_.yaw = yawOf(tf.transform.rotation);
scan_flipped_ = up < 0.0;
prior_tf_frame_ = scan_frame;
prior_tf_valid_ = true;
@@ -623,8 +640,8 @@ private:
{
robot::log_info("[trolley_detection][debug] '%s': scan frame '%s' in '%s': (%.3f, %.3f) yaw "
"%.3f rad, tilt %.3f rad%s.",
name_.c_str(), scan_frame.c_str(), prior_frame_.c_str(), scan_in_prior_.x,
scan_in_prior_.y, scan_in_prior_.yaw, tilt,
name_.c_str(), scan_frame.c_str(), ctx_.robot_base_frame.c_str(), scan_in_base_.x,
scan_in_base_.y, scan_in_base_.yaw, tilt,
scan_flipped_ ? ", mounted upside down: bearings mirrored" : "");
}
@@ -634,7 +651,7 @@ private:
std::snprintf(buffer, sizeof(buffer),
"scan plane of '%s' (frame '%s') is tilted %.3f rad from '%s' (limit %.3f); "
"the leg model is 2D, check the lidar mount in the URDF",
scan_topic_.c_str(), scan_frame.c_str(), tilt, prior_frame_.c_str(),
scan_topic_.c_str(), scan_frame.c_str(), tilt, ctx_.robot_base_frame.c_str(),
kMaxScanTilt);
mount_error_ = buffer;
robot::log_error("[trolley_detection] '%s': %s.", name_.c_str(), buffer);
@@ -643,7 +660,7 @@ private:
// Lidar nhìn về trước (cos > 0) thì chỉ dò được xe phía trước: phải là `forward`, và ngược lại.
// Lidar góc của T800 (±π/4, ±3π/4) vẫn qua đúng phép kiểm này.
const bool looks_forward = std::cos(scan_in_prior_.yaw) > 0.0;
const bool looks_forward = std::cos(scan_in_base_.yaw) > 0.0;
const bool wants_forward = entry_direction_ == EntryDirection::kForward;
if (looks_forward != wants_forward)
{
@@ -651,8 +668,8 @@ private:
"entry_direction %s but '%s' (frame '%s') faces %s in '%s' (yaw %.2f rad); "
"the goal heading would be reversed",
toString(entry_direction_), scan_topic_.c_str(), scan_frame.c_str(),
looks_forward ? "forward" : "backward", prior_frame_.c_str(),
scan_in_prior_.yaw);
looks_forward ? "forward" : "backward", ctx_.robot_base_frame.c_str(),
scan_in_base_.yaw);
mount_error_ = buffer;
robot::log_error("[trolley_detection] '%s': %s.", name_.c_str(), buffer);
return false;
@@ -701,7 +718,7 @@ private:
ranges_.assign(scan->ranges.begin(), scan->ranges.end());
const dock_pattern_detector::LegDetection detection =
matcher_.match(geometry, ranges_, scan->intensities, limits_, scan_in_prior_);
matcher_.match(geometry, ranges_, scan->intensities, limits_, scan_in_base_);
if (!detection.valid)
{
noteReason(now, detection.reason);
@@ -893,7 +910,6 @@ private:
std::vector<std::string> action_types_;
std::string output_frame_;
std::string scan_topic_;
std::string prior_frame_ = "base_link";
EntryDirection entry_direction_ = EntryDirection::kForward;
double settle_delay_ = kDefaultSettleDelay;
double timeout_ = kDefaultTimeout;
@@ -913,12 +929,12 @@ private:
std::string action_id_;
std::string profile_name_;
/// Pose của frame scan trong prior_frame, cache cho cả lượt.
dock_pattern_detector::Pose2D scan_in_prior_;
/// Pose của frame scan trong robot_base_frame, cache cho cả lượt.
dock_pattern_detector::Pose2D scan_in_base_;
std::string prior_tf_frame_;
bool prior_tf_valid_ = false;
/// Trục z của frame scan chỉ xuống: góc tia phải đảo dấu trước khi dò. Suy từ TF, cùng lúc với
/// scan_in_prior_.
/// scan_in_base_.
bool scan_flipped_ = false;
/// Rỗng = ổn; khác rỗng = lidar nhìn ngược `entry_direction` hoặc mặt quét nghiêng, lượt này phải
/// fail.