diff --git a/README.md b/README.md index 54486d5..3655ce7 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ tĩnh cho trạm dừng. Gói không phát vận tốc. Cấu hình và model P3 - `scan_topic` phải được khai trong sensor store và đưa đủ `ranges`, `intensities`, frame và stamp của cùng một scan. Ngưỡng cường độ và kích thước tape phải đo trên đúng LiDAR và tường thực. -- `prior_frame` là frame thân robot để khai ROI/prior. TF gắn lidar được tra để xác định flip, +- Frame thân robot `robot_base_frame` của context là frame để khai ROI/prior. TF gắn lidar được tra để xác định flip, hướng trước/sau và độ nghiêng; quá 0.0873 rad hoặc nhìn ngược `entry_direction` thì fail. Không - có khóa lật lidar. P1 trả marker trong `prior_frame`; plugin tra `global_frame <- prior_frame` + có khóa lật lidar. P1 trả marker trong `robot_base_frame`; plugin tra `global_frame <- robot_base_frame` tại stamp scan, không thay bằng TF mới nhất. - `marker_frame` mặc định `wall_reflector_frame`: gốc ở tâm cặp, +X hướng từ tường về sensor. `output_frame` mặc định `wall_reflector_target`: `C + offset_x*n + offset_y*t`, @@ -32,6 +32,9 @@ tĩnh cho trạm dừng. Gói không phát vận tốc. Cấu hình và model P3 profile lạ hoặc lỗi ghi TF báo lỗi. Các lý do P1 như `wall_support`, `width`, `spacing`, `ambiguous`, `capacity_*` được chuyển vào thông điệp chờ/timeout. +Cấu hình cũ có `prior_frame` bằng `robot_base_frame` vẫn được 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. + ## Khóa handler Các khóa đặt dưới namespace của handler; cấu hình runtime P3 dùng một profile SIM-ONLY: @@ -41,7 +44,7 @@ Các khóa đặt dưới namespace của handler; cấu hình runtime P3 dùng | `action_types` | `[DetectWallReflectors]` | danh sách chuỗi không rỗng, không trùng | | `scan_topic`, `default_profile` | bắt buộc | topic LaserScan trong store; profile tồn tại | | `output_frame`, `marker_frame` | `wall_reflector_target`, `wall_reflector_frame` | marker rỗng để tắt; output bắt buộc, các frame không trùng nhau/global/base | -| `prior_frame`, `profile_param_key` | `base_link`, `reflector_profile` | chuỗi không rỗng | +| `profile_param_key` | `reflector_profile` | chuỗi không rỗng | | `entry_direction` | `forward` | `forward` hoặc `backward` | | `min_range`, `max_range` | 0.1, 3.0 | [m] 0 ≤ min < max; giao với range limits trong scan | | `zero_range_is_no_echo` | true | bool; P1 loại 0 ở cả hai chế độ | @@ -55,14 +58,17 @@ Các khóa đặt dưới namespace của handler; cấu hình runtime P3 dùng | `debug/state`, `debug/detect`, `debug/skip` | true | bool; log lỗi không tắt được | Mỗi `profiles/` nạp các khóa sau vào `ReflectorPairProfile`. Các giá trị **bắt buộc** không có -mặc định dùng được và được `validate()` kiểm lúc configure: +mặc định dùng được và được `validate()` kiểm lúc configure. Hai khóa core/plateau chỉ cần khai +riêng khi lidar có bloom nằm giữa hai ngưỡng: | Nhóm khóa | Default / kiểm tra | |---|---| | `tape_width`, `width_tolerance`, `tape_spacing`, `spacing_tolerance` | bắt buộc [m]; width >0, spacing > width, dung sai >0 và nhỏ hơn đại lượng tương ứng | -| `tape_min_intensity`, `tape_core_intensity` | bắt buộc [đơn vị driver]; ≥0 và core ≥ min | +| `tape_min_intensity` | bắt buộc [đơn vị driver]; ≥0 | +| `tape_core_intensity` | tùy chọn [đơn vị driver]; mặc định bằng `tape_min_intensity`, nếu khai phải ≥ min | | `tape_min_points` | 3 tia; ≥3 | -| `tape_bloom_max_angle`, `tape_plateau_min_fraction` | bắt buộc; [rad] [0,π), tỉ lệ (0,1] | +| `tape_bloom_max_angle` | bắt buộc; [rad] [0,π) | +| `tape_plateau_min_fraction` | tùy chọn; mặc định 1.0, nếu khai phải thuộc (0,1] | | `edge_bias_bound`, `max_edge_uncertainty`, `min_ray_wall_cos` | bắt buộc [m] ≥0, bắt buộc [m] >0, 0.35 trong (0,1] | | `wall_min_points_each_side`, `wall_min_span` | 5 tia ≥3; span bắt buộc [m] > spacing | | `wall_max_rms`, `wall_max_residual`, `wall_max_gap`, `wall_merge_distance` | bắt buộc [m]; RMS >0, max ≥ RMS, gap ≥ tape width, merge >0 | diff --git a/plugins/wall_reflector_detection_action_handler.cpp b/plugins/wall_reflector_detection_action_handler.cpp index fd1bb24..982e420 100644 --- a/plugins/wall_reflector_detection_action_handler.cpp +++ b/plugins/wall_reflector_detection_action_handler.cpp @@ -51,7 +51,7 @@ void load(robot::NodeHandle& nh, const char* key, T& value) /** * @brief Adapter scan → marker → hai TF tĩnh, không điều khiển vận tốc. * - * Sensor và TF là cổng non-owning của context. Marker P1 nằm trong prior_frame; TF từ prior_frame + * Sensor và TF là cổng non-owning của context. Marker P1 nằm trong robot_base_frame; TF từ robot_base_frame * sang global_frame được tra đúng timestamp scan trước khi đưa marker vào PoseSampleFilter. */ class WallReflectorDetectionActionHandler final : public ActionHandler @@ -70,7 +70,7 @@ public: paused_ = false; name_ = name; ctx_ = context; - if (!ctx_.tf || !ctx_.sensors || ctx_.global_frame.empty() || ctx_.robot_base_frame.empty()) + if (!ctx_.tf || !ctx_.sensors || ctx_.global_frame.empty()) { robot::log_error("[wall_reflector] '%s': TF, sensor store and context frames are required.", name_.c_str()); @@ -80,7 +80,23 @@ public: nh.param("output_frame", output_frame_, std::string("wall_reflector_target")); nh.param("marker_frame", marker_frame_, std::string("wall_reflector_frame")); nh.param("scan_topic", scan_topic_, std::string()); - nh.param("prior_frame", prior_frame_, std::string("base_link")); + if (ctx_.robot_base_frame.empty()) + return configError("robot_base_frame is empty"); + if (nh.hasParam("prior_frame")) + { + std::string legacy_frame; + if (!nh.getParam("prior_frame", legacy_frame)) + return configError("prior_frame must be a string"); + if (legacy_frame != ctx_.robot_base_frame) + { + const std::string error = "prior_frame '" + legacy_frame + "' differs from robot_base_frame '" + + ctx_.robot_base_frame + "'"; + return configError(error.c_str()); + } + robot::log_warning("[wall_reflector] '%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("default_profile", default_profile_, std::string()); nh.param("profile_param_key", profile_param_key_, std::string("reflector_profile")); nh.param("settle_delay", settle_delay_, 0.4); @@ -112,14 +128,13 @@ public: if (std::adjacent_find(sorted.begin(), sorted.end()) != sorted.end()) return configError("action_types contains duplicates"); if (output_frame_.empty() || output_frame_ == ctx_.global_frame || - output_frame_ == ctx_.robot_base_frame || output_frame_ == prior_frame_ || + output_frame_ == ctx_.robot_base_frame || (!marker_frame_.empty() && (marker_frame_ == output_frame_ || - marker_frame_ == ctx_.global_frame || marker_frame_ == ctx_.robot_base_frame || - marker_frame_ == prior_frame_))) + marker_frame_ == ctx_.global_frame || marker_frame_ == ctx_.robot_base_frame))) return configError("output_frame/marker_frame conflict with each other or context frames"); - if (scan_topic_.empty() || prior_frame_.empty() || default_profile_.empty() || + if (scan_topic_.empty() || default_profile_.empty() || profile_param_key_.empty()) - return configError("scan_topic, prior_frame, default_profile and profile_param_key are required"); + return configError("scan_topic, default_profile and profile_param_key are required"); if (!robot_sensor_ports::has(*ctx_.sensors, scan_topic_)) return configError("scan_topic is not a LaserScan in sensor store"); if (direction != "forward" && direction != "backward") @@ -142,9 +157,9 @@ public: if (!loadProfiles(nh) || !loadFilter(nh)) return false; initialized_ = true; - robot::log_info("[wall_reflector] '%s': scan=%s prior=%s target=%s marker=%s direction=%s " + robot::log_info("[wall_reflector] '%s': scan=%s robot_base_frame=%s target=%s marker=%s direction=%s " "profiles=%zu default=%s debug/state=%s debug/detect=%s debug/skip=%s", - name_.c_str(), scan_topic_.c_str(), prior_frame_.c_str(), output_frame_.c_str(), + name_.c_str(), scan_topic_.c_str(), ctx_.robot_base_frame.c_str(), output_frame_.c_str(), marker_frame_.c_str(), direction.c_str(), profiles_.size(), default_profile_.c_str(), debug_state_ ? "on" : "off", debug_detect_ ? "on" : "off", debug_skip_ ? "on" : "off"); @@ -314,9 +329,13 @@ private: load(p, "tape_spacing", profile.tape_spacing); load(p, "spacing_tolerance", profile.spacing_tolerance); load(p, "tape_min_intensity", profile.tape_min_intensity); - load(p, "tape_core_intensity", profile.tape_core_intensity); + if (p.hasParam("tape_core_intensity") && + !p.getParam("tape_core_intensity", profile.tape_core_intensity)) + return configError("tape_core_intensity must be a number"); load(p, "tape_bloom_max_angle", profile.tape_bloom_max_angle); - load(p, "tape_plateau_min_fraction", profile.tape_plateau_min_fraction); + if (p.hasParam("tape_plateau_min_fraction") && + !p.getParam("tape_plateau_min_fraction", profile.tape_plateau_min_fraction)) + return configError("tape_plateau_min_fraction must be a number"); load(p, "edge_bias_bound", profile.edge_bias_bound); load(p, "max_edge_uncertainty", profile.max_edge_uncertainty); load(p, "min_ray_wall_cos", profile.min_ray_wall_cos); @@ -441,11 +460,9 @@ private: robot::log_error("[wall_reflector] '%s': %s", name_.c_str(), mount_error_.c_str()); return false; } - tf3::TransformStampedMsg prior_tf; tf3::TransformStampedMsg base_tf; try { - prior_tf = ctx_.tf->lookupTransform(prior_frame_, scan_frame, tf3::Time()); base_tf = ctx_.tf->lookupTransform(ctx_.robot_base_frame, scan_frame, tf3::Time()); } catch (const std::exception&) @@ -456,24 +473,18 @@ private: const auto& rotation = base_tf.transform.rotation; const double norm = rotation.x * rotation.x + rotation.y * rotation.y + rotation.z * rotation.z + rotation.w * rotation.w; - const auto& prior_rotation = prior_tf.transform.rotation; - const double prior_norm = prior_rotation.x * prior_rotation.x + prior_rotation.y * prior_rotation.y + - prior_rotation.z * prior_rotation.z + prior_rotation.w * prior_rotation.w; if (!std::isfinite(norm) || std::abs(norm - 1.0) > 0.01 || - !std::isfinite(prior_norm) || std::abs(prior_norm - 1.0) > 0.01 || - !std::isfinite(prior_tf.transform.translation.x) || - !std::isfinite(prior_tf.transform.translation.y)) + !std::isfinite(base_tf.transform.translation.x) || + !std::isfinite(base_tf.transform.translation.y)) { mount_error_ = "invalid mounting TF"; robot::log_error("[wall_reflector] '%s': %s", name_.c_str(), mount_error_.c_str()); return false; } const double up_base = 1.0 - 2.0 * (rotation.x * rotation.x + rotation.y * rotation.y); - const double up_prior = 1.0 - 2.0 * (prior_rotation.x * prior_rotation.x + prior_rotation.y * prior_rotation.y); const double tilt_base = std::acos(std::min(1.0, std::abs(up_base))); - const double tilt_prior = std::acos(std::min(1.0, std::abs(up_prior))); - if (tilt_base > kMaxTilt || tilt_prior > kMaxTilt || (up_base < 0.0) != (up_prior < 0.0)) - mount_error_ = "scan plane tilt exceeds limit or prior frame is inverted"; + if (tilt_base > kMaxTilt) + mount_error_ = "scan plane tilt exceeds limit"; else if ((std::cos(yawOf(rotation)) > 0.0) != forward_) mount_error_ = "lidar faces opposite entry_direction"; if (!mount_error_.empty()) @@ -481,15 +492,15 @@ private: robot::log_error("[wall_reflector] '%s': %s", name_.c_str(), mount_error_.c_str()); return false; } - scan_in_prior_.x = prior_tf.transform.translation.x; - scan_in_prior_.y = prior_tf.transform.translation.y; - scan_in_prior_.yaw = yawOf(prior_rotation); - flipped_ = up_prior < 0.0; + scan_in_base_.x = base_tf.transform.translation.x; + scan_in_base_.y = base_tf.transform.translation.y; + scan_in_base_.yaw = yawOf(rotation); + flipped_ = up_base < 0.0; scan_frame_ = scan_frame; mount_valid_ = true; if (debug_detect_) - robot::log_info("[wall_reflector][debug] '%s': scan=%s prior=%s flip=%s tilt=%.4f", - name_.c_str(), scan_frame.c_str(), prior_frame_.c_str(), + robot::log_info("[wall_reflector][debug] '%s': scan=%s robot_base_frame=%s flip=%s tilt=%.4f", + name_.c_str(), scan_frame.c_str(), ctx_.robot_base_frame.c_str(), flipped_ ? "yes" : "no", tilt_base); return true; } @@ -539,7 +550,7 @@ private: { ranges_.assign(scan->ranges.begin(), scan->ranges.end()); auto detection = matcher_.match(geometry, ranges_, scan->intensities, - effective_limits, scan_in_prior_); + effective_limits, scan_in_base_); if (!detection.valid) { if (detection.reason == "ambiguous") @@ -582,7 +593,7 @@ private: tf3::TransformStampedMsg transform; try { - transform = ctx_.tf->lookupTransform(ctx_.global_frame, prior_frame_, + transform = ctx_.tf->lookupTransform(ctx_.global_frame, ctx_.robot_base_frame, tf3::Time(pending_stamp_.sec, pending_stamp_.nsec)); } catch (const std::exception&) @@ -695,7 +706,6 @@ private: std::string output_frame_; std::string marker_frame_; std::string scan_topic_; - std::string prior_frame_; std::string default_profile_; std::string profile_param_key_; std::string action_id_; @@ -708,7 +718,7 @@ private: dock_pattern_detector::ReflectorPairProfile active_profile_; dock_pattern_detector::ReflectorPairLimits limits_; dock_pattern_detector::ReflectorPairMatcher matcher_; - dock_pattern_detector::ReflectorPose2D scan_in_prior_; + dock_pattern_detector::ReflectorPose2D scan_in_base_; dock_pattern_detector::ReflectorPose2D pending_marker_; PoseSampleFilter filter_; robot::Time started_at_; diff --git a/test/wall_reflector_detection_action_test.cpp b/test/wall_reflector_detection_action_test.cpp index a7fa01a..2d3bb8d 100644 --- a/test/wall_reflector_detection_action_test.cpp +++ b/test/wall_reflector_detection_action_test.cpp @@ -74,8 +74,9 @@ double angleError(double first, double second) class Rig { public: - Rig() + explicit Rig(const std::string& base_frame = "base_link") { + context.robot_base_frame = base_frame; robot_sensor_ports::SensorStoreConfig config; for (const char* topic : {"/f_scan", "/b_scan2"}) { @@ -95,7 +96,6 @@ public: context.tf = &tf; context.sensors = &store; context.global_frame = "map"; - context.robot_base_frame = "base_link"; registry.setContext(context); loaded = registry.loadFromConfig(nh, kNamespace); } @@ -106,7 +106,7 @@ public: const double cp = std::cos(pitch / 2), sp = std::sin(pitch / 2); const double cy = std::cos(yaw / 2), sy = std::sin(yaw / 2); tf3::TransformStampedMsg transform; - transform.header.frame_id = "base_link"; + transform.header.frame_id = context.robot_base_frame; transform.child_frame_id = child; transform.transform.rotation.w = cr * cp * cy + sr * sp * sy; transform.transform.rotation.x = sr * cp * cy - cr * sp * sy; @@ -121,7 +121,7 @@ public: tf3::TransformStampedMsg transform; transform.header.stamp = tf3::Time(time.sec, time.nsec); transform.header.frame_id = "map"; - transform.child_frame_id = "base_link"; + transform.child_frame_id = context.robot_base_frame; transform.transform.translation.x = x; transform.transform.rotation.w = 1.0; tf.setTransform(transform, "test", false); @@ -179,6 +179,86 @@ public: bool loaded = false; }; +TEST(WallReflectorAction, AbsentPriorFrameDetectsInContextBaseFrame) +{ + Rig rig("custom_robot_base"); + ASSERT_TRUE(rig.store_ok); + ASSERT_FALSE(rig.nh.hasParam("actions_wall/forward/prior_frame")); + ASSERT_TRUE(rig.loaded); + auto* handler = rig.registry.find("DetectWallReflectors"); + ASSERT_NE(handler, nullptr); + ASSERT_TRUE(handler->start(action("DetectWallReflectors"), robot::Time(10.0))); + const auto result = rig.happy(handler); + EXPECT_EQ(result.status, ActionStatus::kSucceeded) << result.message; + EXPECT_TRUE(rig.hasOutput(kTarget)); +} + +TEST(WallReflectorAction, LegacyPriorFrameEqualToContextBaseLoads) +{ + Rig rig("custom_robot_base"); + const std::string root = "actions_wall/forward"; + 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.context); + EXPECT_TRUE(registry.loadFromConfig(rig.nh, kNamespace)); + EXPECT_NE(registry.find("DetectWallReflectors"), nullptr); + rig.nh.setParam(root, original); +} + +TEST(WallReflectorAction, LegacyPriorFrameDifferentFromContextBaseFails) +{ + Rig rig; + const std::string root = "actions_wall/forward"; + 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.context); + EXPECT_FALSE(registry.loadFromConfig(rig.nh, kNamespace)); + EXPECT_EQ(registry.find("DetectWallReflectors"), nullptr); + rig.nh.setParam(root, original); +} + +TEST(WallReflectorAction, EmptyContextBaseFrameFails) +{ + Rig rig; + rig.context.robot_base_frame.clear(); + ActionRegistry registry; + registry.setContext(rig.context); + EXPECT_FALSE(registry.loadFromConfig(rig.nh, kNamespace)); + EXPECT_EQ(registry.find("DetectWallReflectors"), nullptr); +} + +TEST(WallReflectorAction, ProfileWithoutOptionalThresholdsLoadsAndDetects) +{ + Rig rig; + ASSERT_TRUE(rig.store_ok); + ASSERT_TRUE(rig.loaded); + const std::string key = "actions_wall/forward/profiles/standard"; + YAML::Node current; + ASSERT_TRUE(rig.nh.getParam(key, current)); + const YAML::Node original = YAML::Clone(current); + YAML::Node without_optional = YAML::Clone(current); + without_optional.remove("tape_core_intensity"); + without_optional.remove("tape_plateau_min_fraction"); + rig.nh.setParam(key, without_optional); + ActionRegistry registry; + registry.setContext(rig.context); + const bool loaded = registry.loadFromConfig(rig.nh, kNamespace); + rig.nh.setParam(key, original); + ASSERT_TRUE(loaded); + auto* handler = registry.find("DetectWallReflectors"); + ASSERT_NE(handler, nullptr); + ASSERT_TRUE(handler->start(action("DetectWallReflectors"), robot::Time(10.0))); + const auto result = rig.happy(handler); + ASSERT_EQ(result.status, ActionStatus::kSucceeded) << result.message; + EXPECT_NEAR(rig.output(kTarget).transform.translation.x, 0.4, 0.01); +} + TEST(WallReflectorAction, PluginLoadAndForwardTarget) { Rig rig;