Files
move_base2/src/plan_inspector.cpp
duongtd c0a04b29ad refactor: xoá PathMonitor và PathCheckPort, EnvMonitor là bộ giám sát duy nhất; gộp refreshSnapshot
Xoá hạ tầng giám sát cũ đã tắt từ 5/9 (path_monitor/enabled: false), −2947 dòng:
- ControlLoop: PathMonitorConfig, updatePathMonitor / updateProgressMonitor / applyCollisionGuard /
  planIsBlocked / resetPathMonitor / rebuildPlanArcLength, toàn bộ member bốn phép đo, luật "plan
  mới bẩn thì giữ plan cũ" trong collectPlannerResult. Bốn cổng StateMachineInput giữ nguyên
  (off_path không còn nguồn, đặt false).
- ports/path_check_port.h, CostmapPathCheck trong NavigationRuntime.
- plan_inspector: bỏ checkPlan / checkMotion / PlanBlockResult / MotionCheckParams /
  MotionBlockResult; giữ planarTransform, nearestIndex, trackedNearestIndex, crossTrackError.
- Config: readPathMonitor, MoveBase2Config::path_monitor, validate; xoá
  config/runtime/path_monitor_params.yaml; comment YAML/state_machine đổi sang tên verdict EnvMonitor.
- Test: FakePathCheckPort, 30 test WalkingSkeletonPathMonitor/CollisionGuard, 13 test
  PlanInspectorCheck/Motion, 2 test validate thân guard.

P0(b): ControlLoop::step() gọi EnvironmentPort::refresh() một lần đầu cycle (không phụ thuộc
env_monitor/enabled); ControllerRunner::setSnapshotRefreshedByLoop(true) làm ba refreshSnapshot()
trong runner thành no-op — monitor và controller đọc cùng một lưới, một bản copy mỗi cycle.

Test: env_monitor 43, grid_analysis 33, plan_inspector 11, walking_skeleton 57, config_validation 32,
scenario 12, state_machine 100, navigation_server 56, controller_runner 28, velocity_arbiter 29,
planner_runner 31 — pass.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017xXjFjr2DQ6rhmhHSW4fk4
2026-09-06 10:55:15 +07:00

182 lines
4.9 KiB
C++

/*********************************************************************
*
* Software License Agreement (BSD License)
*
* move_base2 — cài đặt plan_inspector.
*
* Author: DuongTD
*********************************************************************/
#include <move_base2/core/plan_inspector.h>
#include <algorithm>
#include <cmath>
#include <limits>
#include <exception>
#include <robot_costmap_2d/costmap_2d.h>
#include <robot_costmap_2d/cost_values.h>
#include <tf3/buffer_core.h>
namespace move_base2
{
namespace
{
/// [m] Dưới ngưỡng này thì một đoạn plan coi như suy biến thành điểm.
constexpr double kDegenerateSegment = 1e-9;
/// @brief [m] Khoảng cách từ điểm (px,py) tới đoạn thẳng (ax,ay)-(bx,by).
double pointToSegment(double px, double py, double ax, double ay, double bx, double by)
{
const double dx = bx - ax;
const double dy = by - ay;
const double len_sq = dx * dx + dy * dy;
if (len_sq <= kDegenerateSegment)
{
return std::hypot(px - ax, py - ay);
}
double t = ((px - ax) * dx + (py - ay) * dy) / len_sq;
t = std::max(0.0, std::min(1.0, t));
return std::hypot(px - (ax + t * dx), py - (ay + t * dy));
}
} // namespace
bool planarTransform(const tf3::BufferCore& tf, const std::string& from, const std::string& to,
PlanarTransform& out)
{
out = PlanarTransform();
if (from.empty() || to.empty())
{
return false;
}
if (from == to)
{
return true; // Identity — không cần chạm tới TF.
}
try
{
const tf3::TransformStampedMsg tf_msg =
const_cast<tf3::BufferCore&>(tf).lookupTransform(to, from, tf3::Time());
const auto& q = tf_msg.transform.rotation;
const double yaw =
std::atan2(2.0 * (q.w * q.z + q.x * q.y), 1.0 - 2.0 * (q.y * q.y + q.z * q.z)); // [rad]
out.x = tf_msg.transform.translation.x;
out.y = tf_msg.transform.translation.y;
out.cos_yaw = std::cos(yaw);
out.sin_yaw = std::sin(yaw);
return true;
}
catch (const std::exception&)
{
// Không log ở đây: hàm này chạy theo nhịp monitor. Bên gọi quyết định có báo hay không.
return false;
}
}
std::size_t trackedNearestIndex(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
const robot_geometry_msgs::PoseStamped& pose, std::size_t hint,
double back_m, double ahead_m)
{
if (plan.size() < 2)
{
return 0;
}
const std::size_t last_seg = plan.size() - 2;
hint = std::min(hint, last_seg);
auto seg_len = [&](std::size_t i) {
return std::hypot(plan[i + 1].pose.position.x - plan[i].pose.position.x,
plan[i + 1].pose.position.y - plan[i].pose.position.y);
};
std::size_t lo = hint;
for (double walked = 0.0; lo > 0 && walked < back_m; --lo)
{
walked += seg_len(lo - 1);
}
std::size_t hi = hint;
for (double walked = 0.0; hi < last_seg && walked < ahead_m; ++hi)
{
walked += seg_len(hi);
}
const double px = pose.pose.position.x;
const double py = pose.pose.position.y;
std::size_t best = hint;
double best_distance = std::numeric_limits<double>::max();
for (std::size_t i = lo; i <= hi; ++i)
{
const double distance =
pointToSegment(px, py, plan[i].pose.position.x, plan[i].pose.position.y,
plan[i + 1].pose.position.x, plan[i + 1].pose.position.y);
if (distance < best_distance)
{
best_distance = distance;
best = i;
}
}
return best;
}
std::size_t nearestIndex(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
const robot_geometry_msgs::PoseStamped& pose)
{
if (plan.size() < 2)
{
return 0;
}
const double px = pose.pose.position.x;
const double py = pose.pose.position.y;
std::size_t best = 0;
double best_distance = std::numeric_limits<double>::max();
for (std::size_t i = 0; i + 1 < plan.size(); ++i)
{
const double distance =
pointToSegment(px, py, plan[i].pose.position.x, plan[i].pose.position.y,
plan[i + 1].pose.position.x, plan[i + 1].pose.position.y);
if (distance < best_distance)
{
best_distance = distance;
best = i;
}
}
return best;
}
double crossTrackError(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
const robot_geometry_msgs::PoseStamped& pose)
{
if (plan.empty())
{
return 0.0;
}
const double px = pose.pose.position.x;
const double py = pose.pose.position.y;
if (plan.size() == 1)
{
return std::hypot(px - plan.front().pose.position.x, py - plan.front().pose.position.y);
}
double best = std::numeric_limits<double>::max();
for (std::size_t i = 0; i + 1 < plan.size(); ++i)
{
best = std::min(best, pointToSegment(px, py, plan[i].pose.position.x, plan[i].pose.position.y,
plan[i + 1].pose.position.x, plan[i + 1].pose.position.y));
}
return best;
}
} // namespace move_base2