- DirectionalLayer rewritten as a DirectionFieldProvider. It no longer derives from StaticLayer and never writes the master grid. It takes an OccupancyGrid of direction codes (0 = no constraint, 1..8 = direction_index + 1) on direction_topic, validates it against the master geometry and global frame, and publishes an immutable DirectionField snapshot with a DISABLED / PENDING / READY state and a generation counter. Every static map on map_topic returns the layer to PENDING until a grid for that map arrives. - direction_field.h is the core API for planners: grid validation, bounds-checked codeAt(), a half-open-cell DDA (traverseGridSegment), allowedDirectionMask() and checkSegment(). LayeredCostmap::getDirectionFieldSnapshot() exposes the snapshot without adding data members. - max_deviation_deg (default 90, valid 0 < x < 180). The field carries the value so every planner uses the same rule; 90 keeps the previous masks bit for bit. - StaticLayer ignores an OccupancyGrid addressed to another layer's topic instead of logging two unthrottled errors per push. - StaticLayer and its subclasses report current only after updateCosts has written the newest map (markMapWritten, also used by CriticalLayer). Readiness flags are written under the master mutex; invalid partial updates are rejected with a throttled error. Full history: branch feature/direction-zone.
178 lines
7.1 KiB
C++
178 lines
7.1 KiB
C++
#include <robot_costmap_2d/direction_field.h>
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <sstream>
|
|
|
|
namespace robot_costmap_2d
|
|
{
|
|
namespace
|
|
{
|
|
constexpr double kTolerance = 1e-6;
|
|
|
|
std::string frameName(const std::string& frame)
|
|
{
|
|
return !frame.empty() && frame[0] == '/' ? frame.substr(1) : frame;
|
|
}
|
|
|
|
DirectionValidationResult fail(DirectionValidationError error, const std::string& message)
|
|
{
|
|
return {error, message};
|
|
}
|
|
|
|
template<typename T>
|
|
std::string value(const char* name, T actual)
|
|
{
|
|
std::ostringstream out;
|
|
out << name << "=" << actual;
|
|
return out.str();
|
|
}
|
|
} // namespace
|
|
|
|
DirectionValidationResult validateDirectionGrid(const robot_nav_msgs::OccupancyGrid& grid,
|
|
const DirectionGeometry& expected)
|
|
{
|
|
const auto& info = grid.info;
|
|
const uint64_t count = uint64_t(info.width) * uint64_t(info.height);
|
|
if (count > std::vector<uint8_t>().max_size())
|
|
return fail(DirectionValidationError::kOverflow, value("cell count", count));
|
|
if (frameName(grid.header.frame_id).empty() || frameName(grid.header.frame_id) != frameName(expected.frame_id))
|
|
return fail(DirectionValidationError::kFrame,
|
|
"frame_id=" + grid.header.frame_id + " expected=" + expected.frame_id);
|
|
if (info.width != expected.width || info.height != expected.height)
|
|
return fail(DirectionValidationError::kGeometry,
|
|
value("width", info.width) + " " + value("height", info.height));
|
|
|
|
const auto close = [](double a, double b)
|
|
{
|
|
return std::isfinite(a) && std::isfinite(b) && std::abs(a - b) <= kTolerance;
|
|
};
|
|
if (!close(info.resolution, expected.resolution) || info.resolution <= 0.0 || expected.resolution <= 0.0)
|
|
return fail(DirectionValidationError::kGeometry, value("resolution", info.resolution));
|
|
if (!close(info.origin.position.x, expected.origin_x) || !close(info.origin.position.y, expected.origin_y) ||
|
|
!std::isfinite(info.origin.position.z))
|
|
return fail(DirectionValidationError::kGeometry,
|
|
value("origin.x", info.origin.position.x) + " " + value("origin.y", info.origin.position.y) +
|
|
" " + value("origin.z", info.origin.position.z));
|
|
|
|
const auto& q = info.origin.orientation;
|
|
// A default-constructed Pose has a zero quaternion; it denotes no rotation here.
|
|
if (!std::isfinite(q.x) || !std::isfinite(q.y) || !std::isfinite(q.z) || !std::isfinite(q.w) ||
|
|
std::abs(q.x) > kTolerance || std::abs(q.y) > kTolerance || std::abs(q.z) > kTolerance ||
|
|
!(std::abs(std::abs(q.w) - 1.0) <= kTolerance || std::abs(q.w) <= kTolerance))
|
|
return fail(DirectionValidationError::kOrientation,
|
|
value("orientation.x", q.x) + " " + value("y", q.y) + " " + value("z", q.z) +
|
|
" " + value("w", q.w));
|
|
if (!grid.data.empty() && grid.data.size() != count)
|
|
return fail(DirectionValidationError::kSize, value("data.size", grid.data.size()) + " " + value("expected", count));
|
|
for (size_t i = 0; i < grid.data.size(); ++i)
|
|
{
|
|
if (grid.data[i] < 0 || grid.data[i] > 8)
|
|
return fail(DirectionValidationError::kCode, value("data index", i) + " " + value("code", int(grid.data[i])));
|
|
}
|
|
return {DirectionValidationError::kNone, "valid"};
|
|
}
|
|
|
|
uint8_t allowedDirectionMask(double dx, double dy, double max_deviation_rad)
|
|
{
|
|
if (!std::isfinite(dx) || !std::isfinite(dy))
|
|
return 0;
|
|
constexpr int vx[] = {1, 1, 0, -1, -1, -1, 0, 1};
|
|
constexpr int vy[] = {0, 1, 1, 1, 0, -1, -1, -1};
|
|
const long double x = dx, y = dy;
|
|
const bool legacy_right_angle = max_deviation_rad == M_PI / 2.0;
|
|
const long double length = std::hypot(x, y);
|
|
const long double threshold = (std::cos(max_deviation_rad) - 1e-9L) * length;
|
|
const long double legacy_tolerance = 1e-9L * (std::abs(x) + std::abs(y));
|
|
constexpr long double kInverseSqrtTwo = 0.707106781186547524400844362104849L;
|
|
uint8_t mask = 0;
|
|
for (int k = 0; k < 8; ++k)
|
|
{
|
|
const long double dot = vx[k] * x + vy[k] * y;
|
|
// Preserve the exact legacy tolerance at 90 degrees, including near-boundary inputs.
|
|
if (legacy_right_angle ? dot >= -legacy_tolerance :
|
|
dot * (k % 2 ? kInverseSqrtTwo : 1.0L) >= threshold)
|
|
mask |= uint8_t(1u << k);
|
|
}
|
|
return mask;
|
|
}
|
|
|
|
DirectionField::DirectionField(const robot_nav_msgs::OccupancyGrid& grid, const DirectionGeometry& geometry,
|
|
double max_deviation_rad)
|
|
: geometry_(geometry), codes_(grid.data.begin(), grid.data.end()),
|
|
max_deviation_rad_(max_deviation_rad),
|
|
has_constraints_(std::any_of(codes_.begin(), codes_.end(), [](uint8_t c) { return c != 0; }))
|
|
{
|
|
}
|
|
|
|
std::shared_ptr<const DirectionField> DirectionField::create(const robot_nav_msgs::OccupancyGrid& grid,
|
|
const DirectionGeometry& expected,
|
|
double max_deviation_rad,
|
|
DirectionValidationResult* result)
|
|
{
|
|
auto validation = validateDirectionGrid(grid, expected);
|
|
if (result)
|
|
*result = validation;
|
|
if (!validation)
|
|
return {};
|
|
return std::shared_ptr<const DirectionField>(new DirectionField(grid, expected, max_deviation_rad));
|
|
}
|
|
|
|
uint8_t DirectionField::code(uint32_t x, uint32_t y) const
|
|
{
|
|
assert(x < geometry_.width && y < geometry_.height);
|
|
return codes_.empty() ? 0 : codes_[size_t(y) * geometry_.width + x];
|
|
}
|
|
|
|
bool DirectionField::codeAt(int64_t x, int64_t y, uint8_t* code_out) const
|
|
{
|
|
if (!code_out || x < 0 || y < 0 || x >= geometry_.width || y >= geometry_.height)
|
|
return false;
|
|
*code_out = code(static_cast<uint32_t>(x), static_cast<uint32_t>(y));
|
|
return true;
|
|
}
|
|
|
|
SegmentResult DirectionField::checkSegment(double wx0, double wy0, double wx1, double wy1,
|
|
SegmentViolation* first) const
|
|
{
|
|
const auto gx = [this](double x) { return (x - geometry_.origin_x) / geometry_.resolution; };
|
|
const auto gy = [this](double y) { return (y - geometry_.origin_y) / geometry_.resolution; };
|
|
const double x0 = gx(wx0);
|
|
const double y0 = gy(wy0);
|
|
const double x1 = gx(wx1);
|
|
const double y1 = gy(wy1);
|
|
const auto inside = [this](double x, double y)
|
|
{
|
|
return std::isfinite(x) && std::isfinite(y) && x >= 0 && y >= 0 &&
|
|
x < geometry_.width && y < geometry_.height;
|
|
};
|
|
if (!inside(x0, y0) || !inside(x1, y1))
|
|
return SegmentResult::kOutOfGrid;
|
|
const double dx = wx1 - wx0;
|
|
const double dy = wy1 - wy0;
|
|
if (!std::isfinite(dx) || !std::isfinite(dy))
|
|
return SegmentResult::kOutOfGrid;
|
|
|
|
const uint8_t mask = allowedDirectionMask(dx, dy, max_deviation_rad_);
|
|
SegmentResult result = SegmentResult::kAllowed;
|
|
const bool completed = traverseGridSegment(x0, y0, x1, y1, [&](int64_t x, int64_t y)
|
|
{
|
|
uint8_t cell_code;
|
|
if (!codeAt(x, y, &cell_code))
|
|
{
|
|
result = SegmentResult::kOutOfGrid;
|
|
return false;
|
|
}
|
|
if (cell_code && !(mask & (1u << (cell_code - 1))))
|
|
{
|
|
if (first)
|
|
*first = {uint32_t(x), uint32_t(y), cell_code, dx, dy};
|
|
result = SegmentResult::kViolation;
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
return completed || result != SegmentResult::kAllowed ? result : SegmentResult::kOutOfGrid;
|
|
}
|
|
} // namespace robot_costmap_2d
|