35 #include "dwb_critics/map_grid.hpp"
42 #include "dwb_core/exceptions.hpp"
43 #include "nav2_costmap_2d/cost_values.hpp"
44 #include "nav2_util/node_utils.hpp"
58 void MapGridCritic::onInit()
60 costmap_ = costmap_ros_->getCostmap();
61 queue_ = std::make_shared<MapGridQueue>(*costmap_, *
this);
64 stop_on_failure_ =
true;
66 auto node = node_.lock();
68 throw std::runtime_error{
"Failed to lock node"};
71 nav2_util::declare_parameter_if_not_declared(
73 dwb_plugin_name_ +
"." + name_ +
".aggregation_type",
74 rclcpp::ParameterValue(std::string(
"last")));
76 std::string aggro_str;
77 node->get_parameter(dwb_plugin_name_ +
"." + name_ +
".aggregation_type", aggro_str);
78 std::transform(aggro_str.begin(), aggro_str.end(), aggro_str.begin(), ::tolower);
79 if (aggro_str ==
"last") {
80 aggregationType_ = ScoreAggregationType::Last;
81 }
else if (aggro_str ==
"sum") {
82 aggregationType_ = ScoreAggregationType::Sum;
83 }
else if (aggro_str ==
"product") {
84 aggregationType_ = ScoreAggregationType::Product;
88 "MapGridCritic"),
"aggregation_type parameter \"%s\" invalid. Using Last.",
90 aggregationType_ = ScoreAggregationType::Last;
96 cell_values_[index] = obstacle_score_;
103 obstacle_score_ =
static_cast<double>(cell_values_.size());
110 while (!queue_->isEmpty()) {
112 cell_values_[cell.index_] = CellData::absolute_difference(cell.src_x_, cell.x_) +
113 CellData::absolute_difference(cell.src_y_, cell.y_);
120 unsigned int start_index = 0;
121 if (aggregationType_ == ScoreAggregationType::Product) {
123 }
else if (aggregationType_ == ScoreAggregationType::Last && !stop_on_failure_) {
124 start_index = traj.poses.size() - 1;
128 for (
unsigned int i = start_index; i < traj.poses.size(); ++i) {
130 if (stop_on_failure_) {
131 if (grid_dist == obstacle_score_) {
133 IllegalTrajectoryException(name_,
"Trajectory Hits Obstacle.");
136 IllegalTrajectoryException(name_,
"Trajectory Hits Unreachable Area.");
140 switch (aggregationType_) {
141 case ScoreAggregationType::Last:
144 case ScoreAggregationType::Sum:
147 case ScoreAggregationType::Product:
160 unsigned int cell_x, cell_y;
162 if (!costmap_->
worldToMap(pose.x, pose.y, cell_x, cell_y)) {
164 IllegalTrajectoryException(name_,
"Trajectory Goes Off Grid.");
170 std::vector<std::pair<std::string, std::vector<float>>> & cost_channels)
172 std::pair<std::string, std::vector<float>> grid_scores;
173 grid_scores.first = name_;
178 grid_scores.second.resize(size_x * size_y);
180 for (
unsigned int cy = 0; cy < size_y; cy++) {
181 for (
unsigned int cx = 0; cx < size_x; cx++) {
182 grid_scores.second[i] =
getScore(cx, cy);
186 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...
virtual double scorePose(const geometry_msgs::msg::Pose2D &pose)
Retrieve the score for a single pose.
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.
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.