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

253 lines
8.7 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/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>
#include <cstdint>
namespace robot_costmap_2d
{
class Layer;
class ReferenceGraphWorker;
enum class LayerType; // layer.h includes this header before it defines the enum.
/**
* @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;
/** @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_;
}
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;
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
#endif // ROBOT_COSTMAP_2D_LAYERED_COSTMAP_H_