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. The tape core
threshold and plateau fraction keys are optional.
This commit is contained in:
2026-11-12 17:01:54 +07:00
parent b5da961449
commit 48e1e9a0ed
3 changed files with 140 additions and 44 deletions

View File

@@ -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<robot_sensor_msgs::LaserScan>(*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_;