Files
costmap_2d/include/robot_costmap_2d/reference_graph_worker.h
duongtd 5034029c6e 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.
2026-11-10 16:37:55 +07:00

134 lines
5.0 KiB
C++

#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_