Files
costmap_2d/include/robot_costmap_2d/directional_layer.h
duongtd 40488b7632 feat(costmap_2d): direction zones and map readiness
- 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.
2026-11-04 13:24:16 +07:00

74 lines
3.3 KiB
C++

#ifndef ROBOT_COSTMAP_2D_DIRECTIONAL_LAYER_H_
#define ROBOT_COSTMAP_2D_DIRECTIONAL_LAYER_H_
#include <chrono>
#include <mutex>
#include <string>
#include <robot_costmap_2d/direction_field.h>
#include <robot_costmap_2d/layer.h>
namespace robot_costmap_2d
{
/** @brief Publish immutable direction snapshots without modifying master costs.
* The host thread feeds map and direction messages through dataCallBack(); the
* costmap update thread calls updateBounds()/updateCosts(); planner threads use
* directionSnapshot() and may retain a snapshot for an entire search.
* When both mutexes are required, acquire the master costmap mutex before the
* layer mutex, never the reverse. Snapshot readers hold only the layer mutex
* briefly. Layer::isCurrent() still reads the legacy non-atomic current_ flag.
*/
class DirectionalLayer : public Layer, public DirectionFieldProvider
{
public:
DirectionalLayer();
~DirectionalLayer() override = default;
/** @brief Identify this plugin as a directional layer. */
LayerType getType() const override { return LayerType::DIRECTIONAL_LAYER; }
/** @brief Copy a consistent immutable snapshot for a planner thread. */
DirectionFieldSnapshot directionSnapshot() const override;
/** @brief Update thread: leave bounds unchanged and report a stuck pending state.
* @param robot_x Robot x [m] in the global frame.
* @param robot_y Robot y [m] in the global frame.
* @param robot_yaw Robot yaw [rad] in the global frame.
* @param min_x Lower x bound [m] in the global frame.
* @param min_y Lower y bound [m] in the global frame.
* @param max_x Upper x bound [m] in the global frame.
* @param max_y Upper y bound [m] in the global frame.
*/
void updateBounds(double robot_x, double robot_y, double robot_yaw, double* min_x, double* min_y,
double* max_x, double* max_y) override;
/** @brief Update thread: leave master costs unchanged.
* @param master Master costmap in the global frame.
* @param min_i Lower x cell index relative to the grid origin.
* @param min_j Lower y cell index relative to the grid origin.
* @param max_i Upper x cell index relative to the grid origin.
* @param max_j Upper y cell index relative to the grid origin.
*/
void updateCosts(Costmap2D& /*master*/, int /*min_i*/, int /*min_j*/, int /*max_i*/, int /*max_j*/) override {}
/** @brief Resize path: invalidate an enabled layer's snapshot. */
void matchSize() override;
/** @brief Retain a ready snapshot if master geometry [m, cells, frame] is unchanged. */
void reset() override;
protected:
/** @brief Load plugin configuration and establish its initial state. */
void onInitialize() override;
/** @brief Host thread: process static-map and direction-grid callbacks. */
void handleImpl(const void* data, const std::type_info& type, const std::string& topic) override;
private:
DirectionGeometry geometry() const;
void publishLocked(DirectionFieldState state, std::shared_ptr<const DirectionField> field = {});
mutable std::mutex mutex_;
DirectionFieldSnapshot snapshot_;
bool seen_map_;
bool debug_state_;
bool rolling_;
double max_deviation_rad_; ///< Maximum angular deviation [rad].
std::string map_topic_, direction_topic_;
std::chrono::steady_clock::time_point pending_since_, last_pending_log_;
};
} // namespace robot_costmap_2d
#endif // ROBOT_COSTMAP_2D_DIRECTIONAL_LAYER_H_