This commit is contained in:
2026-08-06 15:17:59 +07:00
parent 701d25f952
commit 5375a9da9e
33 changed files with 3199 additions and 137 deletions

194
src/plan_inspector.cpp Normal file
View File

@@ -0,0 +1,194 @@
/*********************************************************************
*
* 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 <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 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;
}
PlanBlockResult checkPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
std::size_t from_index, double horizon,
const robot_costmap_2d::Costmap2D& grid,
const PlanarTransform& plan_to_grid)
{
PlanBlockResult result;
if (plan.empty() || from_index >= plan.size() || !(horizon > 0.0))
{
return result;
}
double travelled = 0.0; // [m]
for (std::size_t i = from_index; i < plan.size(); ++i)
{
if (i > from_index)
{
travelled += std::hypot(plan[i].pose.position.x - plan[i - 1].pose.position.x,
plan[i].pose.position.y - plan[i - 1].pose.position.y);
if (travelled > horizon)
{
break;
}
}
double gx = 0.0;
double gy = 0.0;
plan_to_grid.apply(plan[i].pose.position.x, plan[i].pose.position.y, gx, gy);
unsigned int mx = 0;
unsigned int my = 0;
if (!grid.worldToMap(gx, gy, mx, my))
{
// Ra ngoài cửa sổ lưới: phần còn lại của tuyến cũng nằm ngoài (lưới lăn theo robot), nên
// dừng hẳn thay vì quét tiếp cho tới hết horizon.
break;
}
++result.scanned;
const unsigned char cost = grid.getCost(mx, my);
if (cost == robot_costmap_2d::NO_INFORMATION)
{
continue;
}
if (cost >= robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE)
{
result.blocked = true;
result.index = i;
result.distance = travelled;
return result;
}
}
return result;
}
} // namespace move_base2