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
This commit is contained in:
@@ -178,284 +178,4 @@ double crossTrackError(const std::vector<robot_geometry_msgs::PoseStamped>& plan
|
||||
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
|
||||
{
|
||||
/// [m] Quãng đầu cung bị bỏ qua — chỗ robot đang đứng. Xem @ref checkMotion.
|
||||
constexpr double kSkipOwnFootprintDist = 0.05;
|
||||
|
||||
/// Một điểm dò: ra ngoài lưới hay chưa có thông tin đều là KHÔNG chạm.
|
||||
bool probeHits(const robot_costmap_2d::Costmap2D& grid, double x, double y,
|
||||
unsigned char threshold, const std::vector<unsigned int>& baseline)
|
||||
{
|
||||
unsigned int mx = 0;
|
||||
unsigned int my = 0;
|
||||
if (!grid.worldToMap(x, y, mx, my))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
const unsigned char cost = grid.getCost(mx, my);
|
||||
if (cost == robot_costmap_2d::NO_INFORMATION || cost < threshold)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// Ô đã nằm dưới thân tại pose hiện tại: lệnh này không "đưa thêm" nó vào thân.
|
||||
return baseline.empty() || !std::binary_search(baseline.begin(), baseline.end(), grid.getIndex(mx, my));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gom các ô >= @p threshold đang nằm dưới hình chữ nhật thân đệm tại pose hiện tại.
|
||||
*
|
||||
* Duyệt DIỆN TÍCH chứ không phải chu vi: ô nằm sâu trong thân mà chỉ lấy chu vi thì mẫu sau quét
|
||||
* qua nó và tính oan là "mới". Ô được coi là dưới thân khi TÂM ô nằm trong hình chữ nhật nới thêm
|
||||
* nửa ô — cùng độ rộng với những gì probe chu vi (bước nửa ô) có thể chạm tới ở s = 0. Kết quả
|
||||
* được sort để probe tra bằng binary_search. ~600 ô ở lưới 0.05 m; kiểm cost trước khi quy đổi
|
||||
* hình học nên phần lớn ô thoát sớm.
|
||||
*/
|
||||
void collectBaseline(const robot_costmap_2d::Costmap2D& grid, double x, double y, double yaw,
|
||||
double front, double rear, double side, unsigned char threshold,
|
||||
std::vector<unsigned int>& out, MotionBlockResult& result)
|
||||
{
|
||||
out.clear();
|
||||
result.baseline_listed = 0;
|
||||
const double c = std::cos(yaw);
|
||||
const double sn = std::sin(yaw);
|
||||
const double corners_x[4] = { front, front, -rear, -rear };
|
||||
const double corners_y[4] = { side, -side, side, -side };
|
||||
double min_wx = std::numeric_limits<double>::max();
|
||||
double min_wy = std::numeric_limits<double>::max();
|
||||
double max_wx = std::numeric_limits<double>::lowest();
|
||||
double max_wy = std::numeric_limits<double>::lowest();
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
const double wx = x + corners_x[i] * c - corners_y[i] * sn;
|
||||
const double wy = y + corners_x[i] * sn + corners_y[i] * c;
|
||||
min_wx = std::min(min_wx, wx);
|
||||
max_wx = std::max(max_wx, wx);
|
||||
min_wy = std::min(min_wy, wy);
|
||||
max_wy = std::max(max_wy, wy);
|
||||
}
|
||||
int mx0 = 0;
|
||||
int my0 = 0;
|
||||
int mx1 = 0;
|
||||
int my1 = 0;
|
||||
grid.worldToMapEnforceBounds(min_wx, min_wy, mx0, my0);
|
||||
grid.worldToMapEnforceBounds(max_wx, max_wy, mx1, my1);
|
||||
const double slack = 0.5 * grid.getResolution();
|
||||
for (int my = my0; my <= my1; ++my)
|
||||
{
|
||||
for (int mx = mx0; mx <= mx1; ++mx)
|
||||
{
|
||||
const unsigned char cost = grid.getCost(static_cast<unsigned int>(mx), static_cast<unsigned int>(my));
|
||||
if (cost == robot_costmap_2d::NO_INFORMATION || cost < threshold)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
double wx = 0.0;
|
||||
double wy = 0.0;
|
||||
grid.mapToWorld(static_cast<unsigned int>(mx), static_cast<unsigned int>(my), wx, wy);
|
||||
const double dx = wx - x;
|
||||
const double dy = wy - y;
|
||||
const double bx = dx * c + dy * sn;
|
||||
const double by = -dx * sn + dy * c;
|
||||
if (bx < -rear - slack || bx > front + slack || std::fabs(by) > side + slack)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
out.push_back(grid.getIndex(static_cast<unsigned int>(mx), static_cast<unsigned int>(my)));
|
||||
if (result.baseline_listed < MotionBlockResult::kMaxListedBaseline)
|
||||
{
|
||||
result.baseline_xy[result.baseline_listed][0] = bx;
|
||||
result.baseline_xy[result.baseline_listed][1] = by;
|
||||
++result.baseline_listed;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::sort(out.begin(), out.end());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
MotionBlockResult checkMotion(double x, double y, double yaw, double v, double omega,
|
||||
const MotionCheckParams& params,
|
||||
const robot_costmap_2d::Costmap2D& grid,
|
||||
std::vector<unsigned int>* baseline_scratch)
|
||||
{
|
||||
MotionBlockResult result;
|
||||
if (std::fabs(v) < params.min_speed || !(params.lookahead > 0.0) || params.max_samples < 1)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
const double direction = v < 0.0 ? -1.0 : 1.0;
|
||||
// Độ cong của cung: omega / v. Lùi thì cung lật chiều cùng với v — công thức giữ nguyên dấu.
|
||||
const double kappa = omega / v;
|
||||
// Bước dò không được lớn hơn một ô lưới: với bước 0.13 m mũi robot ở mẫu này còn cách tường
|
||||
// mỏng 1 ô, mẫu sau đã ở BÊN KIA tường — cả hai đều sạch, robot đâm xuyên. max_samples chỉ là
|
||||
// trần thưa; lưới mịn hơn thì dò dày hơn, tối đa lookahead/resolution mẫu (16 ở 0.8 m / 0.05).
|
||||
double step = params.lookahead / params.max_samples;
|
||||
const double resolution = grid.getResolution();
|
||||
if (resolution > 0.0 && step > resolution)
|
||||
{
|
||||
step = resolution;
|
||||
}
|
||||
const int samples = static_cast<int>(std::ceil(params.lookahead / step - 1.0e-9));
|
||||
const double front = params.front_offset + params.padding;
|
||||
const double rear = params.rear_offset + params.padding;
|
||||
const double side = params.side_offset + params.padding;
|
||||
const double sin0 = std::sin(yaw);
|
||||
const double cos0 = std::cos(yaw);
|
||||
|
||||
std::vector<unsigned int> local_baseline;
|
||||
std::vector<unsigned int>& baseline = baseline_scratch != nullptr ? *baseline_scratch : local_baseline;
|
||||
baseline.clear();
|
||||
if (params.exclude_baseline)
|
||||
{
|
||||
collectBaseline(grid, x, y, yaw, front, rear, side, params.cost_threshold, baseline, result);
|
||||
}
|
||||
result.baseline_cells = static_cast<int>(baseline.size());
|
||||
|
||||
for (int i = 1; i <= samples; ++i)
|
||||
{
|
||||
const double walk = std::min(step * i, params.lookahead); // [m] dọc cung, luôn dương
|
||||
if (walk < kSkipOwnFootprintDist)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (std::fabs(kappa) * walk > params.lookahead_angle)
|
||||
{
|
||||
break; // quay quá góc này thì lệnh đã đổi — phần cung còn lại không phải tương lai thật
|
||||
}
|
||||
// Vị trí trên cung sau quãng `walk` theo chiều `direction`.
|
||||
double px = 0.0;
|
||||
double py = 0.0;
|
||||
double pyaw = yaw;
|
||||
const double s = direction * walk; // quãng có dấu
|
||||
if (std::fabs(kappa) < 1.0e-6)
|
||||
{
|
||||
px = x + s * cos0;
|
||||
py = y + s * sin0;
|
||||
}
|
||||
else
|
||||
{
|
||||
pyaw = yaw + kappa * s;
|
||||
px = x + (std::sin(pyaw) - sin0) / kappa;
|
||||
py = y - (std::cos(pyaw) - cos0) / kappa;
|
||||
}
|
||||
++result.scanned;
|
||||
|
||||
// Dò TOÀN BỘ CHU VI footprint đã đệm tại mẫu này, không phải 5 điểm rời. Năm điểm rời kéo dọc
|
||||
// cung là năm đường quét, giữa chúng hở tới nửa bề rộng thân: hai trụ cách nhau 0.45 m nằm lọt
|
||||
// giữa đường quét mũi (ngang 0) và đường quét hông (ngang ±0.53) — không đường nào chạm, robot
|
||||
// ôm cả hai trụ mới dừng (đo 4/9: near=0.36 m @ +70° = góc trước-trái thân). Bỏ inflation thì
|
||||
// trụ chỉ còn 2-3 ô, dò điểm rời chắc chắn lọt; "dò vật thật" bắt buộc đi kèm "dò kín thân".
|
||||
// Chu vi lấy mẫu dày hơn một ô, và bước dọc cung không quá một ô, nên một ô vật cản muốn vào
|
||||
// trong footprint bắt buộc phải cắt chu vi ở một mẫu nào đó — không còn khe hình học.
|
||||
const double c = std::cos(pyaw);
|
||||
const double sn = std::sin(pyaw);
|
||||
const double spacing = resolution > 0.0 ? 0.5 * resolution : 0.025;
|
||||
// Bốn đỉnh trong khung thân: x dọc heading, y sang trái.
|
||||
const double x_min = -rear;
|
||||
const double x_max = front;
|
||||
const double y_max = side; // side đã gồm padding; side_offset = 0 -> dải rộng 2*padding
|
||||
const double y_min = -side;
|
||||
auto probe_body = [&](double bx, double by) {
|
||||
const double wx = px + bx * c - by * sn;
|
||||
const double wy = py + bx * sn + by * c;
|
||||
if (!probeHits(grid, wx, wy, params.cost_threshold, baseline))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// Ghi ô chạm theo frame robot HIỆN TẠI (không phải thân đã dịch tới mẫu s) để log đọc được.
|
||||
const double dx = wx - x;
|
||||
const double dy = wy - y;
|
||||
result.hit_x = dx * cos0 + dy * sin0;
|
||||
result.hit_y = -dx * sin0 + dy * cos0;
|
||||
return true;
|
||||
};
|
||||
auto probe_edge = [&](int edge, double x0, double y0, double x1, double y1) {
|
||||
const double len = std::hypot(x1 - x0, y1 - y0);
|
||||
const int n = std::max(1, static_cast<int>(std::ceil(len / spacing)));
|
||||
for (int e = 0; e <= n; ++e)
|
||||
{
|
||||
const double t = static_cast<double>(e) / n;
|
||||
if (probe_body(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t))
|
||||
{
|
||||
result.hit_edge = edge;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
const bool hit = probe_edge(0, x_min, y_min, x_max, y_min) || // cạnh phải
|
||||
probe_edge(1, x_max, y_min, x_max, y_max) || // cạnh mũi
|
||||
probe_edge(2, x_max, y_max, x_min, y_max) || // cạnh trái
|
||||
probe_edge(3, x_min, y_max, x_min, y_min); // cạnh đuôi
|
||||
if (hit)
|
||||
{
|
||||
result.blocked = true;
|
||||
result.sample = i;
|
||||
result.distance = walk;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace move_base2
|
||||
|
||||
Reference in New Issue
Block a user