feat: add entry_direction and derive an upside-down lidar from TF
The charger goal heading always pointed out of the station, so only robots that reverse onto the charger were supported. Front-charging robots now set `entry_direction: forward` (goal yaw + pi, position unchanged) together with a front `scan_topic`. The default stays `backward`, so existing configs keep their behaviour. Once per run the handler looks up base_link <- scan frame and: - fails the action if the scan plane is tilted more than 5 deg; - fails the action if the lidar faces the side opposite to entry_direction, instead of writing a reversed goal; - mirrors the bearings when the lidar is mounted upside down (roll pi), as the trolley handler does. No config key is needed for that. Tests: forward vs backward, direction mismatch, upside-down V / taped V / arc, tilted scan plane, invalid entry_direction.
This commit is contained in:
@@ -37,6 +37,7 @@ namespace
|
||||
{
|
||||
constexpr double kDefaultSettleDelay = 0.4; // [s]
|
||||
constexpr double kDefaultTimeout = 15.0; // [s]
|
||||
constexpr double kMaxScanTilt = 0.0873; // [rad] approximately 5 degrees
|
||||
|
||||
/// Chu kỳ in lý do chưa dò ra. Control loop ~30 Hz; log mỗi cycle sẽ ngập log và làm chậm chính
|
||||
/// vòng điều khiển. 1 Hz đủ để đọc xem đang hỏng ở cổng nào.
|
||||
@@ -46,6 +47,17 @@ double yawOf(const tf3::QuaternionMsg& q)
|
||||
{
|
||||
return std::atan2(2.0 * (q.w * q.z + q.x * q.y), 1.0 - 2.0 * (q.y * q.y + q.z * q.z));
|
||||
}
|
||||
double upAxisZ(const tf3::QuaternionMsg& q)
|
||||
{
|
||||
return 1.0 - 2.0 * (q.x * q.x + q.y * q.y);
|
||||
}
|
||||
|
||||
enum class EntryDirection { kForward, kBackward };
|
||||
|
||||
const char* toString(EntryDirection direction)
|
||||
{
|
||||
return direction == EntryDirection::kForward ? "forward" : "backward";
|
||||
}
|
||||
} // namespace
|
||||
|
||||
/**
|
||||
@@ -122,6 +134,8 @@ public:
|
||||
nh.param("action_types", action_types_, std::vector<std::string>{});
|
||||
nh.param("output_frame", output_frame_, std::string());
|
||||
nh.param("scan_topic", scan_topic_, std::string());
|
||||
std::string direction;
|
||||
nh.param("entry_direction", direction, std::string("backward"));
|
||||
nh.param("settle_delay", settle_delay_, kDefaultSettleDelay);
|
||||
nh.param("timeout", timeout_, kDefaultTimeout);
|
||||
nh.param("default_profile", default_profile_, std::string());
|
||||
@@ -144,6 +158,21 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (direction == "forward")
|
||||
{
|
||||
entry_direction_ = EntryDirection::kForward;
|
||||
}
|
||||
else if (direction == "backward")
|
||||
{
|
||||
entry_direction_ = EntryDirection::kBackward;
|
||||
}
|
||||
else
|
||||
{
|
||||
robot::log_error("[dock_pattern] '%s': entry_direction '%s' is not 'forward' or 'backward'.",
|
||||
name_.c_str(), direction.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Gõ sai tên topic là lỗi phổ biến nhất, và triệu chứng của nó GIỐNG HỆT cảm biến chết: dò mãi
|
||||
// không ra. Bắt lúc boot và in ra danh sách topic có thật.
|
||||
if (!robot_sensor_ports::has<robot_sensor_msgs::LaserScan>(*ctx_.sensors, scan_topic_))
|
||||
@@ -172,6 +201,9 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
robot::log_info("[dock_pattern] '%s': scan_topic='%s', entry_direction=%s, output_frame='%s'.",
|
||||
name_.c_str(), scan_topic_.c_str(), toString(entry_direction_),
|
||||
output_frame_.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -191,6 +223,9 @@ public:
|
||||
have_reason_log_ = false;
|
||||
detections_ = 0;
|
||||
tape_reported_ = false;
|
||||
mount_tf_valid_ = false;
|
||||
mount_error_.clear();
|
||||
scan_flipped_ = false;
|
||||
|
||||
std::string profile_name = default_profile_;
|
||||
const auto it = std::find_if(action.actionParameters.begin(), action.actionParameters.end(),
|
||||
@@ -263,6 +298,13 @@ public:
|
||||
processNewScan(now);
|
||||
}
|
||||
|
||||
if (!mount_error_.empty())
|
||||
{
|
||||
tick.status = ActionStatus::kFailed;
|
||||
tick.message = "action '" + action_id_ + "': " + mount_error_;
|
||||
return tick;
|
||||
}
|
||||
|
||||
if (filter_.holdSatisfied(now))
|
||||
{
|
||||
if (!publish(filter_.holdPose(), now))
|
||||
@@ -489,6 +531,59 @@ private:
|
||||
// Vòng dò
|
||||
// ==============================================================================================
|
||||
|
||||
// The rigid mount is checked once per run (or again if the scan frame changes).
|
||||
bool ensureMountTransform(const std::string& frame, const robot::Time& now)
|
||||
{
|
||||
if (mount_tf_valid_ && mount_tf_frame_ == frame)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
tf3::TransformStampedMsg tf;
|
||||
try
|
||||
{
|
||||
tf = ctx_.tf->lookupTransform(ctx_.robot_base_frame, frame, tf3::Time());
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
noteReason(now, "no transform '" + ctx_.robot_base_frame + "' <- '" + frame +
|
||||
"' yet: " + ex.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
const double up = upAxisZ(tf.transform.rotation);
|
||||
const double tilt = std::acos(std::min(1.0, std::fabs(up))); // [rad]
|
||||
const double yaw = yawOf(tf.transform.rotation); // heading of the scan x axis
|
||||
char buffer[512];
|
||||
if (tilt > kMaxScanTilt)
|
||||
{
|
||||
std::snprintf(buffer, sizeof(buffer),
|
||||
"scan plane of '%s' (frame '%s') is tilted %.3f rad from '%s' "
|
||||
"(limit %.3f); check the lidar mount in the URDF",
|
||||
scan_topic_.c_str(), frame.c_str(), tilt, ctx_.robot_base_frame.c_str(),
|
||||
kMaxScanTilt);
|
||||
mount_error_ = buffer;
|
||||
}
|
||||
else if ((std::cos(yaw) > 0.0) != (entry_direction_ == EntryDirection::kForward))
|
||||
{
|
||||
std::snprintf(buffer, sizeof(buffer),
|
||||
"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(), frame.c_str(),
|
||||
std::cos(yaw) > 0.0 ? "forward" : "backward",
|
||||
ctx_.robot_base_frame.c_str(), yaw);
|
||||
mount_error_ = buffer;
|
||||
}
|
||||
if (!mount_error_.empty())
|
||||
{
|
||||
robot::log_error("[dock_pattern] '%s': %s.", name_.c_str(), mount_error_.c_str());
|
||||
return false;
|
||||
}
|
||||
scan_flipped_ = up < 0.0;
|
||||
mount_tf_frame_ = frame;
|
||||
mount_tf_valid_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Lấy scan MỚI (nếu có), dò, quy sang global frame, đẩy vào bộ lọc.
|
||||
*
|
||||
@@ -512,9 +607,14 @@ private:
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ensureMountTransform(scan->header.frame_id, now))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
robot_line_extraction::ScanGeometry geometry;
|
||||
geometry.angle_min = scan->angle_min;
|
||||
geometry.angle_increment = scan->angle_increment;
|
||||
geometry.angle_min = scan_flipped_ ? -scan->angle_min : scan->angle_min;
|
||||
geometry.angle_increment = scan_flipped_ ? -scan->angle_increment : scan->angle_increment;
|
||||
geometry.size = scan->ranges.size();
|
||||
|
||||
// `ranges` là float trong message, double trong lõi. Đổi một lần ở đây, dùng lại buffer để
|
||||
@@ -601,6 +701,7 @@ private:
|
||||
return false;
|
||||
}
|
||||
|
||||
// Bearings were mirrored into the upright scan view; its x axis has this same yaw.
|
||||
const double base_yaw = yawOf(sensor_to_global.transform.rotation);
|
||||
const double c = std::cos(base_yaw);
|
||||
const double s = std::sin(base_yaw);
|
||||
@@ -608,7 +709,9 @@ private:
|
||||
PoseSample sample;
|
||||
sample.x = sensor_to_global.transform.translation.x + c * pending_.x - s * pending_.y;
|
||||
sample.y = sensor_to_global.transform.translation.y + s * pending_.x + c * pending_.y;
|
||||
sample.yaw = std::atan2(std::sin(base_yaw + pending_.yaw), std::cos(base_yaw + pending_.yaw));
|
||||
const double goal_yaw = base_yaw + pending_.yaw +
|
||||
(entry_direction_ == EntryDirection::kForward ? M_PI : 0.0);
|
||||
sample.yaw = std::atan2(std::sin(goal_yaw), std::cos(goal_yaw));
|
||||
sample.taken_at = now;
|
||||
|
||||
++detections_;
|
||||
@@ -704,6 +807,7 @@ private:
|
||||
std::vector<std::string> action_types_;
|
||||
std::string output_frame_;
|
||||
std::string scan_topic_;
|
||||
EntryDirection entry_direction_ = EntryDirection::kBackward;
|
||||
double settle_delay_ = kDefaultSettleDelay;
|
||||
double timeout_ = kDefaultTimeout;
|
||||
|
||||
@@ -733,6 +837,10 @@ private:
|
||||
tf3::Time pending_stamp_;
|
||||
bool pending_valid_ = false;
|
||||
bool tape_reported_ = false;
|
||||
bool mount_tf_valid_ = false;
|
||||
bool scan_flipped_ = false;
|
||||
std::string mount_tf_frame_;
|
||||
std::string mount_error_;
|
||||
|
||||
std::string last_reason_;
|
||||
robot::Time last_reason_log_;
|
||||
|
||||
Reference in New Issue
Block a user