35 #include "dwb_critics/map_grid.hpp"
42 #include "dwb_core/exceptions.hpp"
43 #include "nav2_costmap_2d/cost_values.hpp"
57 void MapGridCritic::onInit()
59 costmap_ = costmap_ros_->getCostmap();
60 queue_ = std::make_shared<MapGridQueue>(*costmap_, *
this);
63 stop_on_failure_ =
true;
65 auto node = node_.lock();
67 throw std::runtime_error{
"Failed to lock node"};
70 std::string aggro_str = node->declare_or_get_parameter(
71 dwb_plugin_name_ +
"." + name_ +
".aggregation_type",
73 std::transform(aggro_str.begin(), aggro_str.end(), aggro_str.begin(), ::tolower);
74 if (aggro_str ==
"last") {
75 aggregationType_ = ScoreAggregationType::Last;
76 }
else if (aggro_str ==
"sum") {
77 aggregationType_ = ScoreAggregationType::Sum;
78 }
else if (aggro_str ==
"product") {
79 aggregationType_ = ScoreAggregationType::Product;
83 "MapGridCritic"),
"aggregation_type parameter \"%s\" invalid. Using Last.",
85 aggregationType_ = ScoreAggregationType::Last;
91 cell_values_[index] = obstacle_score_;
98 obstacle_score_ =
static_cast<double>(cell_values_.size());
105 while (!queue_->isEmpty()) {
107 cell_values_[cell.index_] = CellData::absolute_difference(cell.src_x_, cell.x_) +
108 CellData::absolute_difference(cell.src_y_, cell.y_);
115 unsigned int start_index = 0;
116 if (aggregationType_ == ScoreAggregationType::Product) {
118 }
else if (aggregationType_ == ScoreAggregationType::Last && !stop_on_failure_) {
119 start_index = traj.poses.size() - 1;
123 for (
unsigned int i = start_index; i < traj.poses.size(); ++i) {
125 if (stop_on_failure_) {
126 if (grid_dist == obstacle_score_) {
128 IllegalTrajectoryException(name_,
"Trajectory Hits Obstacle.");
131 IllegalTrajectoryException(name_,
"Trajectory Hits Unreachable Area.");
135 switch (aggregationType_) {
136 case ScoreAggregationType::Last:
139 case ScoreAggregationType::Sum:
142 case ScoreAggregationType::Product:
155 unsigned int cell_x, cell_y;
157 if (!costmap_->
worldToMap(pose.position.x, pose.position.y, cell_x, cell_y)) {
159 IllegalTrajectoryException(name_,
"Trajectory Goes Off Grid.");
165 std::vector<std::pair<std::string, std::vector<float>>> & cost_channels)
167 std::pair<std::string, std::vector<float>> grid_scores;
168 grid_scores.first = name_;
173 grid_scores.second.resize(size_x * size_y);
175 for (
unsigned int cy = 0; cy < size_y; cy++) {
176 for (
unsigned int cx = 0; cx < size_x; cx++) {
177 grid_scores.second[i] =
getScore(cx, cy);
181 cost_channels.push_back(grid_scores);
Storage for cell information used during queue expansion.
bool validCellToQueue(const costmap_queue::CellData &cell) override
Check to see if we should add this cell to the queue. Always true unless overridden.
double scoreTrajectory(const dwb_msgs::msg::Trajectory2D &traj) override
Return a raw score for the given trajectory.
void addCriticVisualization(std::vector< std::pair< std::string, std::vector< float >>> &cost_channels) override
Add information to the given pointcloud for debugging costmap-grid based scores.
void reset() override
Clear the queueDWB_CRITICS_MAP_GRID_He and set cell_values_ to the appropriate number of unreachableC...
void setAsObstacle(unsigned int index)
Sets the score of a particular cell to the obstacle cost.
double getScore(unsigned int x, unsigned int y)
Retrieve the score for a particular cell of the costmap.
double unreachable_score_
Special cell_values.
void propagateManhattanDistances()
Go through the queue and set the cells to the Manhattan distance from their parents.
virtual double scorePose(const geometry_msgs::msg::Pose &pose)
Retrieve the score for a single pose.
A 2D costmap provides a mapping between points in the world and their associated "costs".
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.