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

@@ -22,10 +22,13 @@ chỉ là adapter: kho cảm biến, TF, chọn profile, lọc nhiều scan, ghi
Lidar của `scan_topic` phải nhìn đúng hướng với `entry_direction`; lệch thì action fail ngay ở scan
đầu (khai lệch là goal ngược 180°).
**Lidar gắn lộn ngược** (hook_150: roll π) tự xử lý từ TF `prior_frame` ← frame scan, không có khoá
**Lidar gắn lộn ngược** (hook_150: roll π) tự xử lý từ TF `robot_base_frame` của context ← frame scan, không có khoá
cấu hình: trục z của frame scan chỉ xuống thì góc tia được đảo dấu trước khi dò. Driver không lật dữ
liệu; nếu sau này driver lật thì URDF phải bỏ roll π (costmap cũng đọc TF đó). Mặt quét nghiêng quá
5° so với `prior_frame` → action fail (mô hình chân là 2D).
5° so với `robot_base_frame` của context → action fail (mô hình chân là 2D).
Prior và quạt tìm kiếm cũng được khai trong frame này. Khóa cũ `prior_frame` bằng
`robot_base_frame` chỉ được chấp nhận kèm cảnh báo để xóa khóa; khác frame hoặc
`robot_base_frame` rỗng làm cấu hình thất bại.
## Các bước dò (mỗi scan)

View File

@@ -274,16 +274,16 @@ Mặc định **tắt**: robot đứng ở đâu cũng dò được, miễn th
lấy xe điểm tin cao nhất. Hai xe điểm sát nhau ở hai chỗ thì từ chối cả lượt (`ambiguous`), không
đoán. Xe đỗ thành dãy sát nhau trong tầm lidar thì cần prior.
Prior khai trong `prior_frame` (mặc định `base_link`): xe **kỳ vọng** ở đâu khi robot đứng ở điểm dò.
Prior khai trong frame thân robot `robot_base_frame` của move_base2 (`base_link` trên robot này): xe **kỳ vọng** ở đâu khi robot đứng ở điểm dò.
| Khoá | Đơn vị | Ý nghĩa |
|---|---|---|
| `has_prior` | bool | Bật prior. |
| `prior_x`, `prior_y` | m | Tâm xe kỳ vọng trong `base_link`. Lùi vào: `prior_x` âm. |
| `prior_x`, `prior_y` | m | Tâm xe kỳ vọng trong frame thân robot. Lùi vào: `prior_x` âm. |
| `prior_yaw` | rad | Hướng pháp tuyến cạnh vào **trỏ về robot**. Tiến vào ≈ 3.14159, lùi vào ≈ 0. |
| `prior_yaw_tolerance` | rad | Dung sai quanh `prior_yaw`. < π/2 thì còn dùng để chọn cạnh vào. π = không dùng yaw. |
| `search_radius` | m | Tâm xe xa prior hơn ngần này thì loại. |
| `search_fov` | rad | Nửa góc quạt quanh trục x của `prior_frame`. 0 = không giới hạn. |
| `search_fov` | rad | Nửa góc quạt quanh trục x của `robot_base_frame`. 0 = không giới hạn. |
**Cách đo prior**: lái robot tới đúng điểm dò trên server, để `has_prior: false`, `debug/detect: true`,
dò một lần. Log cho hai dòng:
@@ -309,12 +309,12 @@ thoả thì phải đặt điểm dò chính xác hơn.
## 8. Khoá theo cảm biến và bộ lọc
Các khoá này nằm ngay dưới action, áp cho mọi profile.
Các khoá này nằm ngay dưới action, áp cho mọi profile. `prior_frame` là khóa cũ: nếu bằng
`robot_base_frame` thì handler cảnh báo để xóa khóa; nếu khác frame thì cấu hình thất bại.
| Khoá | Đơn vị | Mặc định code | Gợi ý chui gầm | Dùng khi |
|---|---|---|---|---|
| `scan_topic` | — | — | `/f_scan` | Topic `LaserScan` có trong sensor store của nav core; sai tên thì boot báo lỗi kèm danh sách topic. |
| `prior_frame` | — | `base_link` | `base_link` | Frame của `prior_*` và `search_fov`. |
| `min_range` | m | 0.10 | 0.10 | Bỏ tia trúng chính thân robot. |
| `max_range` | m | 3.0 | 3.0 | Tia xa hơn bị bỏ. Phải ≥ khoảng cách lidar → chân xa ở điểm dò xa nhất. Tăng không làm dò xa hơn (mục 2), chỉ thêm vật nhiễu. |
| `range_sigma` | m | 0.01 | đo | σ nhiễu range, làm trọng số khớp và mẫu số của điểm tin. 0.01 là số của S300 sim: lidar thật thường 0.015–0.03, **phải đo** (bên dưới). Đặt thấp hơn nhiễu thật thì điểm tin tụt dù xe đúng. |
@@ -394,7 +394,7 @@ quả khi tăng/giảm.
Sau khi sửa YAML, khởi động lại nav rồi dò một lần. Log của handler:
```text
[trolley_detection] 'detect_trolley': 1 profile(s), default 'tci_trolley', scan '/f_scan', entry_direction forward, prior_frame 'base_link', range [0.10, 3.00] m, sigma 0.010 m, zero range = blocked, debug/detect on.
[trolley_detection] 'detect_trolley': 1 profile(s), default 'tci_trolley', scan '/f_scan', entry_direction forward, robot_base_frame 'base_link', range [0.10, 3.00] m, sigma 0.010 m, zero range = blocked, debug/detect on.
[trolley_detection][debug] 'detect_trolley': detecting profile 'tci_trolley' (cart 0.517 x 0.720 m, leg box 0.030 m, min_legs 3, attached legs off, tape off/all, bloom 0.000 rad, range bias sigma 0.000 m, offset_yaw 0.000, prior off in 'base_link') on '/f_scan', entry forward -> 'dock_target'.
[trolley_detection][debug] 'detect_trolley': scan frame 'front_laser_link' in 'base_link': (0.454, 0.000) yaw 0.000 rad, tilt 0.000 rad, mounted upside down: bearings mirrored.
[trolley_detection][debug] 'detect_trolley': cart at (1.218, 0.010) yaw -3.091 in 'front_laser_link' (upright view), legs seen 4 (partial 0, attached 0, occluded 0, out of view 0), measured 0.516 x 0.731 m, fit rms 0.0078 m, confidence 1.00.

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.

View File

@@ -12,7 +12,6 @@ actions_trolley:
output_frame: dock_target
scan_topic: /f_scan
entry_direction: forward
prior_frame: base_link
settle_delay: 0.0 # [s] test tự điều khiển đồng hồ
timeout: 5.0 # [s]
default_profile: tci_trolley
@@ -87,7 +86,6 @@ actions_trolley:
output_frame: dock_target
scan_topic: /b_scan
entry_direction: backward
prior_frame: base_link
settle_delay: 0.0
timeout: 5.0
default_profile: tci_trolley
@@ -108,7 +106,6 @@ actions_trolley:
output_frame: dock_target
scan_topic: /b_scan
entry_direction: forward
prior_frame: base_link
settle_delay: 0.0
timeout: 5.0
default_profile: tci_trolley

View File

@@ -210,8 +210,9 @@ double yawError(double a, double b)
class Rig
{
public:
Rig()
explicit Rig(const std::string& base_frame = kBaseFrame)
{
ctx.robot_base_frame = base_frame;
robot_sensor_ports::SensorStoreConfig store_config;
for (const char* topic : { kFrontTopic, kBackTopic })
{
@@ -245,7 +246,7 @@ public:
const double cy = std::cos(yaw * 0.5), sy = std::sin(yaw * 0.5);
tf3::TransformStampedMsg t;
t.header.stamp = tf3::Time();
t.header.frame_id = kBaseFrame;
t.header.frame_id = ctx.robot_base_frame;
t.child_frame_id = child;
t.transform.translation.x = x;
t.transform.translation.z = z;
@@ -261,7 +262,7 @@ public:
tf3::TransformStampedMsg t;
t.header.stamp = toTf3Time(stamp_s);
t.header.frame_id = kGlobalFrame;
t.child_frame_id = kBaseFrame;
t.child_frame_id = ctx.robot_base_frame;
t.transform.translation.x = x;
t.transform.translation.y = y;
t.transform.rotation.z = std::sin(yaw * 0.5);
@@ -319,6 +320,60 @@ public:
// 1. Đường chạy đủ + heading theo entry_direction
// ================================================================================================
TEST(TrolleyDetectionAction, AbsentPriorFrameDetectsInContextBaseFrame)
{
Rig rig("custom_robot_base");
ASSERT_TRUE(rig.store_ok);
ASSERT_FALSE(rig.nh.hasParam("actions_trolley/detect_trolley/prior_frame"));
ASSERT_TRUE(rig.loaded);
ActionHandler* h = rig.registry.find("DetectTrolley");
ASSERT_NE(h, nullptr);
ASSERT_TRUE(h->start(makeAction("DetectTrolley", "tci_trolley_prior"), robot::Time(10.0)));
EXPECT_EQ(rig.feed(h, cartLegs(kCartInLaser, 0.0, M_PI), kFrontTopic, kFrontFrame, 10),
ActionStatus::kSucceeded) << rig.last_message;
EXPECT_NEAR(rig.goal().transform.translation.x, kLaserX + kCartInLaser, 0.01);
}
TEST(TrolleyDetectionAction, LegacyPriorFrameEqualToContextBaseLoads)
{
Rig rig("custom_robot_base");
const std::string root = "actions_trolley/detect_trolley";
YAML::Node current;
ASSERT_TRUE(rig.nh.getParam(root, current));
const YAML::Node original = YAML::Clone(current);
rig.nh.setParam(root + "/prior_frame", "custom_robot_base");
ActionRegistry registry;
registry.setContext(rig.ctx);
EXPECT_TRUE(registry.loadFromConfig(rig.nh, kNamespace));
EXPECT_NE(registry.find("DetectTrolley"), nullptr);
rig.nh.setParam(root, original);
}
TEST(TrolleyDetectionAction, LegacyPriorFrameDifferentFromContextBaseFails)
{
Rig rig;
const std::string root = "actions_trolley/detect_trolley";
YAML::Node current;
ASSERT_TRUE(rig.nh.getParam(root, current));
const YAML::Node original = YAML::Clone(current);
rig.nh.setParam(root + "/prior_frame", "other_frame");
ActionRegistry registry;
registry.setContext(rig.ctx);
EXPECT_FALSE(registry.loadFromConfig(rig.nh, kNamespace));
EXPECT_EQ(registry.find("DetectTrolley"), nullptr);
rig.nh.setParam(root, original);
}
TEST(TrolleyDetectionAction, EmptyContextBaseFrameFails)
{
Rig rig;
rig.ctx.robot_base_frame.clear();
ActionRegistry registry;
registry.setContext(rig.ctx);
EXPECT_FALSE(registry.loadFromConfig(rig.nh, kNamespace));
EXPECT_EQ(registry.find("DetectTrolley"), nullptr);
}
TEST(TrolleyDetectionAction, ForwardEntryGoalPointsIntoTheCart)
{
Rig rig;