- ReferenceGraph / ReferenceGraphWorker: Voronoi ridges and one-way lanes are computed on a low-priority thread when the map changes (reference_graph_* keys, off with rolling_window), with unit tests. - Shared costmap keys can live in a root: map or flat at the top of the YAML; costmap_common_root_test covers both layouts. - Static and directional layers report map changes to the layered costmap.
158 lines
8.0 KiB
C++
158 lines
8.0 KiB
C++
#ifndef ROBOT_COSTMAP_2D_REFERENCE_GRAPH_H_
|
|
#define ROBOT_COSTMAP_2D_REFERENCE_GRAPH_H_
|
|
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <robot_geometry_msgs/Point.h>
|
|
|
|
#include <robot_costmap_2d/direction_field.h>
|
|
|
|
namespace robot_costmap_2d
|
|
{
|
|
|
|
/** @brief Inputs of the coarse graph. All lengths are absolute, none is derived from the footprint. */
|
|
struct ReferenceGraphParams
|
|
{
|
|
double min_clearance_m = 0.335; ///< [m] Ridge cells closer to a lethal cell than this are dropped.
|
|
double ridge_min_separation_m = 0.7; ///< [m] Two nearest obstacles closer than this count as one.
|
|
double spur_min_length_m = 1.0; ///< [m] Dead-end branches shorter than this are pruned.
|
|
double lane_offset_step_m = 0.5; ///< [m] Spacing of the parallel lanes inside one zone.
|
|
bool debug_log = true; ///< One [debug] line per finished computation. Errors always log.
|
|
};
|
|
|
|
/** @brief Return an empty string for valid parameters, otherwise a description of the first bad value. */
|
|
std::string validateReferenceGraphParams(const ReferenceGraphParams& params);
|
|
|
|
/** @brief Point [m] in ReferenceGraph::geometry.frame_id. */
|
|
struct ReferencePoint
|
|
{
|
|
double x, y;
|
|
};
|
|
|
|
/** @brief Undirected ridge segment, split at zone boundaries. Direction rules belong to the planner. */
|
|
struct VoronoiEdge
|
|
{
|
|
uint32_t from, to; ///< Indices into ReferenceGraph::nodes.
|
|
std::vector<ReferencePoint> points; ///< [m] Every fifth skeleton cell plus the exact end, from -> to.
|
|
double length_m; ///< [m] Length of the polyline through points.
|
|
double min_clearance_m; ///< [m] Smallest clearance at points. Infinity when no clearance grid was given.
|
|
};
|
|
|
|
/** @brief Straight one-way lane inside one zone, in travel direction. */
|
|
struct ReferenceLane
|
|
{
|
|
uint32_t zone_id; ///< Connected region of equal direction code. Unique inside one graph.
|
|
uint8_t direction_index; ///< 0..7 CCW from +X, as in direction_field.h.
|
|
ReferencePoint entry, exit; ///< [m]
|
|
double length_m; ///< [m]
|
|
double offset_from_centre_m; ///< [m] Absolute distance from the zone's main lane. The planner turns it into a penalty.
|
|
};
|
|
|
|
/** @brief Plain grid input. A window crop passes the full-map clearance so obstacles just outside still count. */
|
|
struct ReferenceGridView
|
|
{
|
|
const uint8_t* cost = nullptr; ///< width*height, row-major, cost_values.h.
|
|
const uint16_t* clearance_mm = nullptr; ///< [mm] May be null: computed from `cost`.
|
|
const uint8_t* direction_codes = nullptr; ///< May be null: no zones. 0 unconstrained, 1..8 as in direction_field.h.
|
|
DirectionGeometry geometry{};
|
|
};
|
|
|
|
/** @brief Output of step 1. Kept by the worker so a zone or footprint change reruns step 2 only. */
|
|
struct VoronoiSkeleton
|
|
{
|
|
DirectionGeometry geometry{};
|
|
std::vector<uint8_t> mask; ///< width*height, row-major, 1 on a skeleton cell. Empty when cancelled.
|
|
std::vector<uint16_t> clearance_mm; ///< [mm] width*height. Distance to the nearest lethal cell, capped at 65535.
|
|
};
|
|
|
|
/** @brief Coarse graph of one map and one direction grid. */
|
|
struct ReferenceGraph
|
|
{
|
|
DirectionGeometry geometry{}; ///< Same grid layout as the master costmap.
|
|
ReferenceGraphParams params; ///< Parameters this graph was built with.
|
|
uint64_t direction_generation = 0; ///< DirectionFieldSnapshot::generation the lanes came from. Set by the worker.
|
|
std::vector<ReferencePoint> nodes; ///< [m] Junctions, dead ends and zone-boundary points.
|
|
std::vector<VoronoiEdge> edges; ///< Green lines in the review image.
|
|
std::vector<ReferenceLane> lanes; ///< Red lines in the review image, before the lead extension.
|
|
std::vector<uint8_t> static_cost; ///< width*height, row-major, cost_values.h. Empty when cancelled.
|
|
std::vector<uint16_t> clearance_mm; ///< [mm] width*height.
|
|
};
|
|
|
|
enum class ReferenceGraphState
|
|
{
|
|
kDisabled, ///< reference_graph_enabled is false.
|
|
kPending, ///< A map, zone or footprint change is being processed.
|
|
kReady ///< graph matches the current map and direction grid.
|
|
};
|
|
|
|
/** @brief Consistent view of the graph, safe to retain on a planner thread. */
|
|
struct ReferenceGraphSnapshot
|
|
{
|
|
ReferenceGraphState state = ReferenceGraphState::kDisabled;
|
|
std::shared_ptr<const ReferenceGraph> graph; ///< Non-null only in kReady.
|
|
uint64_t generation = 0; ///< Changes whenever state or graph changes.
|
|
};
|
|
|
|
/** @brief Step 1: distance transform, ridge cells, thinning and spur pruning.
|
|
*
|
|
* Only LETHAL_OBSTACLE cells are obstacles. Ridge cells must have cost below
|
|
* INSCRIBED_INFLATED_OBSTACLE, so unknown and inflated cells never join the skeleton.
|
|
* @param grid Input grid. `direction_codes` is ignored here.
|
|
* @param params Graph parameters.
|
|
* @param cancel When non-null and set, returns early with an empty mask.
|
|
* @throws std::invalid_argument on bad parameters, geometry or a null cost pointer.
|
|
*/
|
|
VoronoiSkeleton computeVoronoiSkeleton(const ReferenceGridView& grid, const ReferenceGraphParams& params,
|
|
const std::atomic<bool>* cancel = nullptr);
|
|
|
|
/** @brief Centre line of a cell mask of any width, used by the planner for the `preferred` region in step 3.
|
|
* @param mask width*height, row-major, nonzero inside the region.
|
|
* @param geometry Grid layout of the mask.
|
|
* @param params Only spur_min_length_m is used: the region's own boundary replaces the obstacle thresholds.
|
|
* @return width*height mask of the centre line.
|
|
* @throws std::invalid_argument on bad parameters or a size mismatch.
|
|
*/
|
|
std::vector<uint8_t> computeRegionCentreLine(const std::vector<uint8_t>& mask, const DirectionGeometry& geometry,
|
|
const ReferenceGraphParams& params);
|
|
|
|
/** @brief Nodes and edges of a skeleton, split where the direction code changes. Part of step 2.
|
|
*
|
|
* A closed ring without a junction yields no node and no edge, and neither does a branch that
|
|
* returns to the node it left. The graph search could not enter them through a node anyway.
|
|
* @param skeleton_mask width*height, row-major, nonzero on a skeleton cell.
|
|
* @param direction_codes May be null: no zones.
|
|
* @param geometry Grid layout of the mask.
|
|
* @param clearance_mm May be null: VoronoiEdge::min_clearance_m is then infinity.
|
|
* @param nodes Output, replaced. Must not be null.
|
|
* @param edges Output, replaced. Must not be null.
|
|
* @param cancel When non-null and set, returns early with both outputs empty.
|
|
* @throws std::invalid_argument on a size mismatch or a null output.
|
|
*/
|
|
void extractVoronoiEdges(const std::vector<uint8_t>& skeleton_mask, const uint8_t* direction_codes,
|
|
const DirectionGeometry& geometry, const std::vector<uint16_t>* clearance_mm,
|
|
std::vector<ReferencePoint>* nodes, std::vector<VoronoiEdge>* edges,
|
|
const std::atomic<bool>* cancel = nullptr);
|
|
|
|
/** @brief Step 2: Voronoi edges split at zone boundaries, plus straight lanes from the direction grid.
|
|
*
|
|
* Lanes use the rectangle that bounds the footprint, centred on the robot origin, so an off-centre
|
|
* or non-rectangular footprint is checked conservatively.
|
|
* @param grid Input grid, the same one the skeleton came from.
|
|
* @param skeleton Output of computeVoronoiSkeleton.
|
|
* @param footprint Robot footprint [m]. Needs at least 3 points when `grid.direction_codes` is non-null.
|
|
* @param params Graph parameters.
|
|
* @param cancel When non-null and set, returns early with an empty graph.
|
|
* @throws std::invalid_argument on bad parameters, a skeleton of another size or a bad footprint.
|
|
*/
|
|
ReferenceGraph buildReferenceGraph(const ReferenceGridView& grid, const VoronoiSkeleton& skeleton,
|
|
const std::vector<robot_geometry_msgs::Point>& footprint,
|
|
const ReferenceGraphParams& params, const std::atomic<bool>* cancel = nullptr);
|
|
|
|
} // namespace robot_costmap_2d
|
|
|
|
#endif // ROBOT_COSTMAP_2D_REFERENCE_GRAPH_H_
|