Files
costmap_2d/include/robot_costmap_2d/layered_costmap.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

226 lines
7.2 KiB
C++
Executable File

/*********************************************************************
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, 2013, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Author: Eitan Marder-Eppstein
* David V. Lu!!
*********************************************************************/
#ifndef ROBOT_COSTMAP_2D_LAYERED_COSTMAP_H_
#define ROBOT_COSTMAP_2D_LAYERED_COSTMAP_H_
#include <robot_costmap_2d/direction_field.h>
#include <robot_costmap_2d/cost_values.h>
#include <robot_costmap_2d/layer.h>
#include <robot_costmap_2d/costmap_2d.h>
#include <vector>
#include <string>
#include <chrono>
#include <cstdint>
namespace robot_costmap_2d
{
class Layer;
/**
* @class LayeredCostmap
* @brief Instantiates different layer plugins and aggregates them into one score
*/
class LayeredCostmap
{
public:
/**
* @brief Constructor for a costmap
*/
LayeredCostmap(std::string global_frame, bool rolling_window, bool track_unknown);
/**
* @brief Destructor
*/
~LayeredCostmap();
/**
* @brief Update the underlying costmap with new data.
* If you want to update the map outside of the update loop that runs, you can call this.
*/
void updateMap(double robot_x, double robot_y, double robot_yaw);
void setPerformanceMetrics(bool enabled, double reporting_period_seconds);
/** @brief Debug verbosity for the periodic performance report.
* 0 keeps the legacy output untouched; 1 adds lock-wait/hold, update-window
* size and empty-cycle counters; 2 adds per-layer bounds-growth lines.
* Only effective while performance metrics are enabled. */
void setDebugLevel(int level);
inline const std::string& getGlobalFrameID() const noexcept
{
return global_frame_;
}
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y,
bool size_locked = false);
void getUpdatedBounds(double& minx, double& miny, double& maxx, double& maxy)
{
minx = minx_;
miny = miny_;
maxx = maxx_;
maxy = maxy_;
}
bool isCurrent();
/** Consistent snapshot; plugin list must be fixed before concurrent reads. */
DirectionFieldSnapshot getDirectionFieldSnapshot() const;
Costmap2D* getCostmap()
{
return &costmap_;
}
bool isRolling()
{
return rolling_window_;
}
bool isTrackingUnknown()
{
return costmap_.getDefaultValue() == robot_costmap_2d::NO_INFORMATION;
}
std::vector<boost::shared_ptr<Layer> >* getPlugins()
{
return &plugins_;
}
void addPlugin(boost::shared_ptr<Layer> plugin)
{
plugins_.push_back(plugin);
}
bool isSizeLocked()
{
return size_locked_;
}
void getBounds(unsigned int* x0, unsigned int* xn, unsigned int* y0, unsigned int* yn)
{
*x0 = bx0_;
*xn = bxn_;
*y0 = by0_;
*yn = byn_;
}
bool isInitialized()
{
return initialized_;
}
/** @brief Updates the stored footprint, updates the circumscribed
* and inscribed radii, and calls onFootprintChanged() in all
* layers. */
void setFootprint(const std::vector<robot_geometry_msgs::Point>& footprint_spec);
/** @brief Returns the latest footprint stored with setFootprint(). */
const std::vector<robot_geometry_msgs::Point>& getFootprint() { return footprint_; }
/** @brief The radius of a circle centered at the origin of the
* robot which just surrounds all points on the robot's
* footprint.
*
* This is updated by setFootprint(). */
double getCircumscribedRadius() { return circumscribed_radius_; }
/** @brief The radius of a circle centered at the origin of the
* robot which is just within all points and edges of the robot's
* footprint.
*
* This is updated by setFootprint(). */
double getInscribedRadius() { return inscribed_radius_; }
private:
struct LayerPerformance
{
std::uint64_t bounds_nanoseconds = 0;
std::uint64_t costs_nanoseconds = 0;
std::uint64_t bounds_calls = 0;
std::uint64_t costs_calls = 0;
// debug_level >= 2: how much this layer grew the update rect [m^2],
// clamped to the map extent so the inflation full-map request stays finite
double bounds_grow_m2_sum = 0.0;
double bounds_grow_m2_max = 0.0;
};
void resetPerformanceMetrics();
void maybeReportPerformance();
Costmap2D costmap_;
std::string global_frame_;
bool rolling_window_; /// < @brief Whether or not the costmap should roll with the robot
bool current_;
double minx_, miny_, maxx_, maxy_;
unsigned int bx0_, bxn_, by0_, byn_;
std::vector<boost::shared_ptr<Layer> > plugins_;
bool initialized_;
bool size_locked_;
double circumscribed_radius_, inscribed_radius_;
std::vector<robot_geometry_msgs::Point> footprint_;
bool performance_metrics_enabled_ = false;
double performance_metrics_period_seconds_ = 5.0;
int debug_level_ = 0;
std::chrono::steady_clock::time_point performance_window_start_;
std::uint64_t performance_cycle_nanoseconds_ = 0;
std::uint64_t performance_reset_nanoseconds_ = 0;
std::uint64_t performance_cycles_ = 0;
std::vector<std::uint64_t> performance_cycle_samples_;
std::vector<LayerPerformance> layer_performance_;
// debug_level >= 1: master-mutex acquisition time is the contention signal
// (planners hold the same mutex while reading costs), hold time is our own
// cost; empty cycles are updates that found no dirty bounds and did no work.
std::uint64_t lock_wait_nanoseconds_ = 0;
std::uint64_t lock_wait_max_nanoseconds_ = 0;
std::uint64_t lock_hold_nanoseconds_ = 0;
std::uint64_t window_cells_sum_ = 0;
std::uint64_t window_cells_max_ = 0;
std::uint64_t performance_empty_cycles_ = 0;
};
} // namespace robot_costmap_2d
#endif // ROBOT_COSTMAP_2D_LAYERED_COSTMAP_H_