#ifndef ROBOT_COSTMAP_2D_REFERENCE_GRAPH_WORKER_H_ #define ROBOT_COSTMAP_2D_REFERENCE_GRAPH_WORKER_H_ #include #include #include #include #include #include #include #include #include #include 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> static_layers; ///< One width*height copy per enabled static layer. std::vector 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 mergeStaticLayers(const std::vector>& 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 capture; /** Called on the worker thread, without the master mutex. */ std::function 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 cancel_{ false }; // Used only by the worker thread. std::vector 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_