feat(plan_env): 可选矩形 footprint(SAT 检测 + 朝向保持)

grid_map 增加 rectangle_footprint / footprint_length / footprint_width /
footprint_margin / footprint_yaw_tolerance 参数,以及 getRectangleOccupancy()、
rotationIsFree()、setFootprintHeading()、getInflateOccupancy(pos, yaw) 重载。

圆形(单圆 + 横向裕度)与矩形两种模型并存,默认仍是圆形。
矩形模式下一个任务只持有一个车体朝向,与路径切线无关——这正是原先
「胶囊/圆」近似无法表达的部分:圆模型对 80 cm 方形四角的覆盖是过度的,
对窄道两侧判据又不足。
This commit is contained in:
walunt5
2026-11-05 11:59:08 +08:00
parent 729726e49d
commit 9073c370a5
3 changed files with 175 additions and 1 deletions
@@ -65,6 +65,9 @@ struct MappingParameters {
// 拆开后:车体尺寸只表达物理大小,安全裕度可单独调。 // 拆开后:车体尺寸只表达物理大小,安全裕度可单独调。
double obstacles_inflation_xy; double obstacles_inflation_xy;
double double_cylinder_radius_, double_cylinder_offset_; double double_cylinder_radius_, double_cylinder_offset_;
bool rectangle_footprint_{false};
double footprint_length_{0.8}, footprint_width_{0.8}, footprint_margin_{0.09};
double footprint_yaw_tolerance_{0.035}, footprint_yaw_{0.0};
bool map_sliding_en_; bool map_sliding_en_;
double map_sliding_thresh_; double map_sliding_thresh_;
int map_sliding_thresh_vox_; int map_sliding_thresh_vox_;
@@ -182,6 +185,16 @@ public:
inline bool isKnownOccupied(const Eigen::Vector3i& id); inline bool isKnownOccupied(const Eigen::Vector3i& id);
void initMap(rclcpp::Node* node); void initMap(rclcpp::Node* node);
bool usesRectangleFootprint() const { return mp_.rectangle_footprint_; }
double footprintHeading() const { return mp_.footprint_yaw_; }
double footprintYawTolerance() const { return mp_.footprint_yaw_tolerance_; }
double footprintLength() const { return mp_.footprint_length_; }
double footprintWidth() const { return mp_.footprint_width_; }
double footprintMargin() const { return mp_.footprint_margin_; }
void setFootprintHeading(double yaw);
int getRectangleOccupancy(Eigen::Vector3d pos, double yaw, double extra_margin = 0.0);
bool rotationIsFree(Eigen::Vector3d pos, double from, double to, double extra_margin = 0.0);
bool rectangleBufferInMap(Eigen::Vector3d pos);
void publishMap(); void publishMap();
void publishMapInflate(bool all_info = false); void publishMapInflate(bool all_info = false);
@@ -372,7 +385,12 @@ inline int GridMap::getOccupancy(Eigen::Vector3d pos) {
return md_.occupancy_buffer_[toAddress(id)] > mp_.min_occupancy_log_ ? 1 : 0; return md_.occupancy_buffer_[toAddress(id)] > mp_.min_occupancy_log_ ? 1 : 0;
} }
inline int GridMap::getInflateOccupancy(Eigen::Vector3d pos, double yaw) { inline int GridMap::getInflateOccupancy(Eigen::Vector3d pos, double yaw) {
if (mp_.rectangle_footprint_) {
// A task holds one body heading, independently of the path tangent.
if (!pos.allFinite() || !rectangleBufferInMap(pos)) return -1;
return getInflateOccupancyFromBuffer(pos, md_.occupancy_buffer_inflate_);
}
Eigen::Vector3d heading(std::cos(yaw), std::sin(yaw), 0.0); Eigen::Vector3d heading(std::cos(yaw), std::sin(yaw), 0.0);
Eigen::Vector3d front = pos + mp_.double_cylinder_offset_ * heading; Eigen::Vector3d front = pos + mp_.double_cylinder_offset_ * heading;
Eigen::Vector3d rear = pos - mp_.double_cylinder_offset_ * heading; Eigen::Vector3d rear = pos - mp_.double_cylinder_offset_ * heading;
@@ -0,0 +1,28 @@
#pragma once
#include <algorithm>
#include <cmath>
namespace footprint
{
// SAT for an oriented rectangle and a closed, axis-aligned square voxel.
// Touching is collision. Padding expands both local half dimensions.
inline bool intersects(double x, double y, double yaw, double half_length,
double half_width, double cell_half)
{
const double c = std::cos(yaw), s = std::sin(yaw);
const double ac = std::abs(c), as = std::abs(s);
return std::abs(x) <= half_length * ac + half_width * as + cell_half + 1e-10 &&
std::abs(y) <= half_length * as + half_width * ac + cell_half + 1e-10 &&
std::abs(c*x + s*y) <= half_length + cell_half*(ac+as) + 1e-10 &&
std::abs(-s*x + c*y) <= half_width + cell_half*(ac+as) + 1e-10;
}
inline double angleDifference(double to, double from)
{
return std::atan2(std::sin(to-from), std::cos(to-from));
}
// Every padded corner moves by at most this distance over the yaw interval.
inline double angularPadding(double radius, double angle)
{
return 2.0 * radius * std::sin(0.5 * std::min(std::abs(angle), M_PI));
}
} // namespace footprint
+128
View File
@@ -1,5 +1,8 @@
#include "plan_env/grid_map.h" #include "plan_env/grid_map.h"
#include <cmath> #include <cmath>
#include <stdexcept>
#include <unordered_set>
#include <plan_env/rectangular_footprint.h>
#include <limits> #include <limits>
#include <string> #include <string>
#include <geometry_msgs/msg/transform_stamped.hpp> #include <geometry_msgs/msg/transform_stamped.hpp>
@@ -34,6 +37,18 @@ void GridMap::initMap(rclcpp::Node *node)
load_parameter(node_, "grid_map.double_cylinder_radius", mp_.double_cylinder_radius_, -1.0); load_parameter(node_, "grid_map.double_cylinder_radius", mp_.double_cylinder_radius_, -1.0);
load_parameter(node_, "grid_map.obstacles_inflation_xy", mp_.obstacles_inflation_xy, 0.0); load_parameter(node_, "grid_map.obstacles_inflation_xy", mp_.obstacles_inflation_xy, 0.0);
load_parameter(node_, "grid_map.double_cylinder_offset", mp_.double_cylinder_offset_, 0.0); load_parameter(node_, "grid_map.double_cylinder_offset", mp_.double_cylinder_offset_, 0.0);
load_parameter(node_, "grid_map.rectangle_footprint", mp_.rectangle_footprint_, false);
load_parameter(node_, "grid_map.footprint_length", mp_.footprint_length_, 0.8);
load_parameter(node_, "grid_map.footprint_width", mp_.footprint_width_, 0.8);
load_parameter(node_, "grid_map.footprint_margin", mp_.footprint_margin_, 0.09);
load_parameter(node_, "grid_map.footprint_yaw_tolerance", mp_.footprint_yaw_tolerance_, 0.035);
if (mp_.rectangle_footprint_ &&
(!std::isfinite(mp_.footprint_length_) || mp_.footprint_length_ <= 0 ||
!std::isfinite(mp_.footprint_width_) || mp_.footprint_width_ <= 0 ||
!std::isfinite(mp_.footprint_margin_) || mp_.footprint_margin_ < 0 ||
!std::isfinite(mp_.footprint_yaw_tolerance_) || mp_.footprint_yaw_tolerance_ <= 0 ||
mp_.footprint_yaw_tolerance_ > 0.1 || mp_.double_cylinder_offset_ != 0.0))
throw std::runtime_error("Invalid rectangle footprint dimensions, margin, yaw tolerance or cylinder offset");
load_parameter(node_, "grid_map.map_sliding_en", mp_.map_sliding_en_, true); load_parameter(node_, "grid_map.map_sliding_en", mp_.map_sliding_en_, true);
load_parameter(node_, "grid_map.map_sliding_thresh", mp_.map_sliding_thresh_, mp_.resolution_); load_parameter(node_, "grid_map.map_sliding_thresh", mp_.map_sliding_thresh_, mp_.resolution_);
@@ -208,6 +223,24 @@ void GridMap::updateMapBoundaryFromIndex()
void GridMap::rebuildInflationOffsets() void GridMap::rebuildInflationOffsets()
{ {
if (mp_.rectangle_footprint_)
{
const double a = 0.5 * mp_.footprint_length_ + mp_.footprint_margin_;
const double b = 0.5 * mp_.footprint_width_ + mp_.footprint_margin_;
const double pad = footprint::angularPadding(std::hypot(a, b), mp_.footprint_yaw_tolerance_);
const int n = std::ceil((std::hypot(a+pad, b+pad) + mp_.resolution_) / mp_.resolution_);
const int up = std::ceil(mp_.obstacles_inflation_z_up / mp_.resolution_);
const int down = std::ceil(mp_.obstacles_inflation_z_down / mp_.resolution_);
md_.inflate_offsets_.clear();
for (int x = -n; x <= n; ++x)
for (int y = -n; y <= n; ++y)
// One full cell covers the obstacle voxel AND every subcell robot center.
if (footprint::intersects(x*mp_.resolution_, y*mp_.resolution_,
mp_.footprint_yaw_, a+pad, b+pad, mp_.resolution_))
for (int z = -down; z <= up; ++z)
md_.inflate_offsets_.emplace_back(x, y, z);
return;
}
// 膨胀半径 = 车体几何半径 + 额外安全裕度。 // 膨胀半径 = 车体几何半径 + 额外安全裕度。
// 拆开后:车体尺寸只描述物理大小,改裕度不必跟着改"车有多大"。 // 拆开后:车体尺寸只描述物理大小,改裕度不必跟着改"车有多大"。
const double robot_radius = std::max(0.0, mp_.double_cylinder_radius_); const double robot_radius = std::max(0.0, mp_.double_cylinder_radius_);
@@ -230,6 +263,72 @@ void GridMap::rebuildInflationOffsets()
} }
} }
void GridMap::setFootprintHeading(double yaw)
{
if (!mp_.rectangle_footprint_ || !std::isfinite(yaw)) return;
if (std::abs(footprint::angleDifference(yaw, mp_.footprint_yaw_)) < 1e-8) return;
mp_.footprint_yaw_ = yaw;
rebuildInflationOffsets();
std::fill(md_.occupancy_buffer_inflate_.begin(), md_.occupancy_buffer_inflate_.end(), 0);
std::fill(md_.occupancy_buffer_inflate_cnt_.begin(), md_.occupancy_buffer_inflate_cnt_.end(), 0);
for (size_t i = 0; i < md_.occupancy_buffer_.size(); ++i)
if (md_.occupancy_buffer_[i] > mp_.min_occupancy_log_) {
Eigen::Vector3i id;
hashIdToGlobalIndex(static_cast<int>(i), id);
updateInflation(id, 1);
}
}
bool GridMap::rectangleBufferInMap(Eigen::Vector3d pos)
{
const double a = 0.5*mp_.footprint_length_ + mp_.footprint_margin_;
const double b = 0.5*mp_.footprint_width_ + mp_.footprint_margin_;
const double pad = footprint::angularPadding(std::hypot(a,b), mp_.footprint_yaw_tolerance_);
const double c = std::abs(std::cos(mp_.footprint_yaw_)), s = std::abs(std::sin(mp_.footprint_yaw_));
const double x = (a+pad)*c + (b+pad)*s + mp_.resolution_;
const double y = (a+pad)*s + (b+pad)*c + mp_.resolution_;
return isInMap(Eigen::Vector3d(pos.x()-x, pos.y()-y, pos.z()-mp_.obstacles_inflation_z_up-mp_.resolution_)) &&
isInMap(Eigen::Vector3d(pos.x()+x, pos.y()+y, pos.z()+mp_.obstacles_inflation_z_down+mp_.resolution_));
}
int GridMap::getRectangleOccupancy(Eigen::Vector3d pos, double yaw, double extra_margin)
{
if (!pos.allFinite() || !std::isfinite(yaw) || !std::isfinite(extra_margin) || extra_margin < 0) return -1;
const double a = 0.5*mp_.footprint_length_ + mp_.footprint_margin_ + extra_margin;
const double b = 0.5*mp_.footprint_width_ + mp_.footprint_margin_ + extra_margin;
const double c = std::abs(std::cos(yaw)), s = std::abs(std::sin(yaw));
const double hx = a*c+b*s, hy = a*s+b*c;
Eigen::Vector3d lo(pos.x()-hx, pos.y()-hy, pos.z()-mp_.obstacles_inflation_z_up-mp_.resolution_);
Eigen::Vector3d hi(pos.x()+hx, pos.y()+hy, pos.z()+mp_.obstacles_inflation_z_down+mp_.resolution_);
if (!isInMap(lo) || !isInMap(hi)) return -1;
Eigen::Vector3i low, high, query;
posToIndex(lo, low); posToIndex(hi, high); posToIndex(pos, query);
// Same asymmetric vertical offsets as the cached dilation, inverted to source cells.
low.z() = query.z() - static_cast<int>(std::ceil(mp_.obstacles_inflation_z_up/mp_.resolution_));
high.z() = query.z() + static_cast<int>(std::ceil(mp_.obstacles_inflation_z_down/mp_.resolution_));
for (int x = low.x(); x <= high.x(); ++x)
for (int y = low.y(); y <= high.y(); ++y)
if (footprint::intersects((x+0.5)*mp_.resolution_-pos.x(), (y+0.5)*mp_.resolution_-pos.y(),
yaw, a, b, 0.5*mp_.resolution_))
for (int z = low.z(); z <= high.z(); ++z)
if (getOccupancy(Eigen::Vector3i(x,y,z)) != 0) return 1;
return 0;
}
bool GridMap::rotationIsFree(Eigen::Vector3d pos, double from, double to, double extra_margin)
{
if (!std::isfinite(from) || !std::isfinite(to)) return false;
const double angle = footprint::angleDifference(to, from);
const int steps = std::max(1, static_cast<int>(std::ceil(std::abs(angle)/0.05)));
const double radius = std::hypot(0.5*mp_.footprint_length_+mp_.footprint_margin_+extra_margin,
0.5*mp_.footprint_width_+mp_.footprint_margin_+extra_margin);
// Nearest sampled angle is at most half a step away. Cover all intervening corners.
const double pad = footprint::angularPadding(radius, std::abs(angle)/(2*steps));
for (int i = 0; i <= steps; ++i)
if (getRectangleOccupancy(pos, from+angle*i/steps, extra_margin+pad) != 0) return false;
return true;
}
void GridMap::resetAllMapData() void GridMap::resetAllMapData()
{ {
std::fill(md_.occupancy_buffer_.begin(), md_.occupancy_buffer_.end(), mp_.clamp_min_log_ - mp_.unknown_flag_); std::fill(md_.occupancy_buffer_.begin(), md_.occupancy_buffer_.end(), mp_.clamp_min_log_ - mp_.unknown_flag_);
@@ -421,6 +520,35 @@ void GridMap::updateSlidingMap(const Eigen::Vector3d& center)
mp_.map_origin_idx_ = new_origin_idx; mp_.map_origin_idx_ = new_origin_idx;
updateMapBoundaryFromIndex(); updateMapBoundaryFromIndex();
if (mp_.rectangle_footprint_)
{
// Retained obstacles must also inflate into newly entered circular-buffer
// slots. Their occupancy did not change, so no hit/miss transition does it.
std::vector<char> keep_mask(buffer_size, 1);
for (int addr : clear_addrs) keep_mask[addr] = 0;
Eigen::Vector3i offset_min = Eigen::Vector3i::Zero(), offset_max = Eigen::Vector3i::Zero();
for (const auto &offset : md_.inflate_offsets_) {
offset_min = offset_min.cwiseMin(offset);
offset_max = offset_max.cwiseMax(offset);
}
std::unordered_set<int> applied;
for (int dim = 0; dim < 3; ++dim) {
if (shift_num(dim) == 0) continue;
Eigen::Vector3i lo = mp_.map_bound_min_idx_, hi = mp_.map_bound_max_idx_;
if (shift_num(dim) > 0) lo(dim) = hi(dim)-shift_num(dim)+1;
else hi(dim) = lo(dim)-shift_num(dim)-1;
lo = (lo-offset_max).cwiseMax(mp_.map_bound_min_idx_);
hi = (hi-offset_min).cwiseMin(mp_.map_bound_max_idx_);
for (int x = lo.x(); x <= hi.x(); ++x)
for (int y = lo.y(); y <= hi.y(); ++y)
for (int z = lo.z(); z <= hi.z(); ++z) {
const Eigen::Vector3i id(x,y,z);
const int addr = toAddress(id);
if (md_.occupancy_buffer_[addr] > mp_.min_occupancy_log_ && applied.insert(addr).second)
updateInflation(id, 1, &keep_mask);
}
}
}
boundIndex(md_.local_bound_min_); boundIndex(md_.local_bound_min_);
boundIndex(md_.local_bound_max_); boundIndex(md_.local_bound_max_);
} }