feat(costmap_2d): background reference graph, shared keys under root:
- 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.
This commit is contained in:
@@ -79,6 +79,14 @@ namespace robot_costmap_2d
|
||||
class Costmap2DROBOT
|
||||
{
|
||||
public:
|
||||
/** @brief Select the namespace containing shared costmap parameters. */
|
||||
static robot::NodeHandle commonParametersHandle(robot::NodeHandle& tree_nh);
|
||||
|
||||
/** @brief Fill missing costmap layer parameters from the shared layer block. */
|
||||
static void copyParentParameters(const std::string& costmap_name, const std::string& plugin_name,
|
||||
const std::string& plugin_type, robot::NodeHandle& source_nh,
|
||||
robot::NodeHandle& target_nh);
|
||||
|
||||
/**
|
||||
* @brief Constructor for the wrapper
|
||||
* @param name The name for this costmap
|
||||
@@ -295,7 +303,6 @@ protected:
|
||||
double transform_tolerance_; ///< timeout before transform errors
|
||||
|
||||
private:
|
||||
void copyParentParameters(const std::string& costmap_name, const std::string& plugin_name, const std::string& plugin_type, robot::NodeHandle& nh);
|
||||
/** @brief Set the footprint from the new_config object.
|
||||
*
|
||||
* If the values of footprint and robot_radius are the same in
|
||||
|
||||
@@ -39,9 +39,13 @@
|
||||
#define ROBOT_COSTMAP_2D_LAYERED_COSTMAP_H_
|
||||
|
||||
#include <robot_costmap_2d/direction_field.h>
|
||||
#include <robot_costmap_2d/reference_graph.h>
|
||||
#include <robot_costmap_2d/cost_values.h>
|
||||
#include <robot_costmap_2d/layer.h>
|
||||
#include <robot_costmap_2d/costmap_2d.h>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
@@ -50,6 +54,8 @@
|
||||
namespace robot_costmap_2d
|
||||
{
|
||||
class Layer;
|
||||
class ReferenceGraphWorker;
|
||||
enum class LayerType; // layer.h includes this header before it defines the enum.
|
||||
|
||||
/**
|
||||
* @class LayeredCostmap
|
||||
@@ -103,6 +109,23 @@ public:
|
||||
/** Consistent snapshot; plugin list must be fixed before concurrent reads. */
|
||||
DirectionFieldSnapshot getDirectionFieldSnapshot() const;
|
||||
|
||||
/** @brief Start computing the coarse graph in the background. Call once, after the plugins are loaded.
|
||||
* The first computation starts by itself, so a map that arrived earlier still counts.
|
||||
* @return False, with an error logged, on a rolling costmap, bad parameters or a second call. */
|
||||
bool enableReferenceGraph(const ReferenceGraphParams& params);
|
||||
|
||||
/** @brief Consistent snapshot, safe to retain on a planner thread. Never takes the master mutex. */
|
||||
ReferenceGraphSnapshot getReferenceGraphSnapshot() const;
|
||||
|
||||
/** @brief Counts map changes per layer type, e.g. PREFERRED_LAYER, so a planner knows when to rebuild what it derives. */
|
||||
uint64_t getStaticMapRevision(LayerType type) const;
|
||||
|
||||
/** @brief Called by the StaticLayer family under the master mutex. Only STATIC_LAYER reruns steps 1 and 2. */
|
||||
void notifyStaticMapChanged(LayerType type);
|
||||
|
||||
/** @brief Called by DirectionalLayer whenever it publishes. Reruns step 2 only. */
|
||||
void notifyDirectionFieldChanged();
|
||||
|
||||
Costmap2D* getCostmap()
|
||||
{
|
||||
return &costmap_;
|
||||
@@ -218,6 +241,10 @@ private:
|
||||
std::uint64_t window_cells_sum_ = 0;
|
||||
std::uint64_t window_cells_max_ = 0;
|
||||
std::uint64_t performance_empty_cycles_ = 0;
|
||||
|
||||
static constexpr std::size_t kMaxLayerTypes = 16; ///< Checked against LayerType in layered_costmap.cpp.
|
||||
std::array<std::atomic<std::uint64_t>, kMaxLayerTypes> static_map_revisions_{};
|
||||
std::unique_ptr<ReferenceGraphWorker> reference_graph_worker_; ///< Null when reference_graph_enabled is false: no thread at all.
|
||||
};
|
||||
|
||||
} // namespace robot_costmap_2d
|
||||
|
||||
157
include/robot_costmap_2d/reference_graph.h
Normal file
157
include/robot_costmap_2d/reference_graph.h
Normal file
@@ -0,0 +1,157 @@
|
||||
#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_
|
||||
133
include/robot_costmap_2d/reference_graph_worker.h
Normal file
133
include/robot_costmap_2d/reference_graph_worker.h
Normal file
@@ -0,0 +1,133 @@
|
||||
#ifndef ROBOT_COSTMAP_2D_REFERENCE_GRAPH_WORKER_H_
|
||||
#define ROBOT_COSTMAP_2D_REFERENCE_GRAPH_WORKER_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <robot_geometry_msgs/Point.h>
|
||||
|
||||
#include <robot_costmap_2d/direction_field.h>
|
||||
#include <robot_costmap_2d/reference_graph.h>
|
||||
|
||||
namespace robot_costmap_2d
|
||||
{
|
||||
|
||||
/** @brief Everything the worker reads from the costmap, taken in one short lock. */
|
||||
struct ReferenceGraphInput
|
||||
{
|
||||
DirectionGeometry geometry{}; ///< Master grid layout.
|
||||
std::vector<std::vector<uint8_t>> static_layers; ///< One width*height copy per enabled static layer.
|
||||
std::vector<robot_geometry_msgs::Point> footprint; ///< [m] Robot footprint.
|
||||
};
|
||||
|
||||
enum class ReferenceCaptureStatus
|
||||
{
|
||||
kReady, ///< The input is complete.
|
||||
kWait, ///< A static layer has not written its map yet. Try again later.
|
||||
kNoStaticLayer ///< Nothing to build a graph from: a configuration error.
|
||||
};
|
||||
|
||||
/** @brief Merge static layer copies into the grid the coarse graph is built from.
|
||||
*
|
||||
* A cell is lethal when any layer says so, because virtual walls and forbidden zones are static layers too. It is
|
||||
* unknown only when every layer says so. Everything else is free: soft values are read from the master at query time.
|
||||
* @throws std::invalid_argument when the layers differ in size.
|
||||
*/
|
||||
std::vector<uint8_t> mergeStaticLayers(const std::vector<std::vector<uint8_t>>& layers);
|
||||
|
||||
/** @brief Counters of finished work. */
|
||||
struct ReferenceGraphWorkerStats
|
||||
{
|
||||
uint64_t step1_runs = 0; ///< Finished runs of step 1.
|
||||
uint64_t step2_runs = 0; ///< Finished runs of step 2.
|
||||
uint64_t published = 0; ///< Graphs that reached kReady.
|
||||
uint64_t errors = 0; ///< Computations that threw.
|
||||
double capture_ms_max = 0.0; ///< [ms] Longest wait inside the capture callback, which holds the master mutex.
|
||||
double step1_ms_last = 0.0; ///< [ms] Duration of the last finished step 1.
|
||||
double step2_ms_last = 0.0; ///< [ms] Duration of the last finished step 2.
|
||||
};
|
||||
|
||||
/** @brief Background thread that keeps the coarse graph in step with the map, the zones and the footprint.
|
||||
*
|
||||
* Lock order is master mutex, then the mutex of DirectionalLayer, then the mutex of this class. The notify functions
|
||||
* may therefore be called while holding the first two. The thread never holds its own mutex while calling a callback.
|
||||
*/
|
||||
class ReferenceGraphWorker
|
||||
{
|
||||
public:
|
||||
struct Source
|
||||
{
|
||||
/** Called on the worker thread. Takes the master mutex itself and holds it only for the copy. */
|
||||
std::function<ReferenceCaptureStatus(bool with_grid, ReferenceGraphInput* input)> capture;
|
||||
/** Called on the worker thread, without the master mutex. */
|
||||
std::function<DirectionFieldSnapshot()> direction;
|
||||
};
|
||||
|
||||
/** @brief Starts the thread. The first computation starts by itself, so a map that arrived earlier counts.
|
||||
* @throws std::invalid_argument on bad parameters or a missing callback. */
|
||||
ReferenceGraphWorker(Source source, const ReferenceGraphParams& params);
|
||||
|
||||
/** @brief Cancels any computation and joins the thread. */
|
||||
~ReferenceGraphWorker();
|
||||
|
||||
ReferenceGraphWorker(const ReferenceGraphWorker&) = delete;
|
||||
ReferenceGraphWorker& operator=(const ReferenceGraphWorker&) = delete;
|
||||
|
||||
/** @brief The static map changed: reruns steps 1 and 2. */
|
||||
void notifyStaticChanged();
|
||||
/** @brief The direction grid changed: reruns step 2 only. */
|
||||
void notifyDirectionChanged();
|
||||
/** @brief The footprint changed: reruns step 2 only. */
|
||||
void notifyFootprintChanged();
|
||||
|
||||
/** @brief Consistent snapshot, safe to retain. Takes only this class's mutex, so a planner never waits for the costmap. */
|
||||
ReferenceGraphSnapshot snapshot() const;
|
||||
|
||||
ReferenceGraphWorkerStats stats() const;
|
||||
|
||||
private:
|
||||
enum class Outcome
|
||||
{
|
||||
kDone, ///< graph holds a new result.
|
||||
kWait, ///< An input is not ready yet.
|
||||
kCancelled, ///< An input changed during the computation.
|
||||
kFailed ///< It threw. Stay pending until the next change: the same input would throw again.
|
||||
};
|
||||
|
||||
void run();
|
||||
Outcome compute(uint64_t static_revision, ReferenceGraph* graph);
|
||||
void markPendingLocked();
|
||||
|
||||
const Source source_;
|
||||
const ReferenceGraphParams params_;
|
||||
|
||||
mutable std::mutex mutex_;
|
||||
std::condition_variable wake_;
|
||||
bool stop_ = false;
|
||||
uint64_t static_revision_ = 1; // Starts ahead of handled_, which starts the first computation.
|
||||
uint64_t step2_revision_ = 1;
|
||||
uint64_t handled_static_ = 0;
|
||||
uint64_t handled_step2_ = 0;
|
||||
ReferenceGraphSnapshot snapshot_;
|
||||
ReferenceGraphWorkerStats stats_;
|
||||
std::atomic<bool> cancel_{ false };
|
||||
|
||||
// Used only by the worker thread.
|
||||
std::vector<uint8_t> static_cost_;
|
||||
DirectionGeometry geometry_{};
|
||||
VoronoiSkeleton skeleton_;
|
||||
bool skeleton_valid_ = false;
|
||||
uint64_t skeleton_revision_ = 0;
|
||||
bool no_static_logged_ = false;
|
||||
|
||||
std::thread thread_; // Last member: it starts after everything above exists.
|
||||
};
|
||||
|
||||
} // namespace robot_costmap_2d
|
||||
|
||||
#endif // ROBOT_COSTMAP_2D_REFERENCE_GRAPH_WORKER_H_
|
||||
Reference in New Issue
Block a user