-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
map_grid.cpp
189 lines (169 loc) · 6.16 KB
/
map_grid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2017, Locus Robotics
* 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 the copyright holder 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 HOLDER 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.
*/
#include "dwb_critics/map_grid.hpp"
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <memory>
#include "dwb_core/exceptions.hpp"
#include "nav2_costmap_2d/cost_values.hpp"
#include "nav2_util/node_utils.hpp"
using std::abs;
using costmap_queue::CellData;
namespace dwb_critics
{
// Customization of the CostmapQueue validCellToQueue method
bool MapGridCritic::MapGridQueue::validCellToQueue(const costmap_queue::CellData & /*cell*/)
{
return true;
}
void MapGridCritic::onInit()
{
costmap_ = costmap_ros_->getCostmap();
queue_ = std::make_shared<MapGridQueue>(*costmap_, *this);
// Always set to true, but can be overridden by subclasses
stop_on_failure_ = true;
auto node = node_.lock();
if (!node) {
throw std::runtime_error{"Failed to lock node"};
}
nav2_util::declare_parameter_if_not_declared(
node,
dwb_plugin_name_ + "." + name_ + ".aggregation_type",
rclcpp::ParameterValue(std::string("last")));
std::string aggro_str;
node->get_parameter(dwb_plugin_name_ + "." + name_ + ".aggregation_type", aggro_str);
std::transform(aggro_str.begin(), aggro_str.end(), aggro_str.begin(), ::tolower);
if (aggro_str == "last") {
aggregationType_ = ScoreAggregationType::Last;
} else if (aggro_str == "sum") {
aggregationType_ = ScoreAggregationType::Sum;
} else if (aggro_str == "product") {
aggregationType_ = ScoreAggregationType::Product;
} else {
RCLCPP_ERROR(
rclcpp::get_logger(
"MapGridCritic"), "aggregation_type parameter \"%s\" invalid. Using Last.",
aggro_str.c_str());
aggregationType_ = ScoreAggregationType::Last;
}
}
void MapGridCritic::setAsObstacle(unsigned int index)
{
cell_values_[index] = obstacle_score_;
}
void MapGridCritic::reset()
{
queue_->reset();
cell_values_.resize(costmap_->getSizeInCellsX() * costmap_->getSizeInCellsY());
obstacle_score_ = static_cast<double>(cell_values_.size());
unreachable_score_ = obstacle_score_ + 1.0;
std::fill(cell_values_.begin(), cell_values_.end(), unreachable_score_);
}
void MapGridCritic::propagateManhattanDistances()
{
while (!queue_->isEmpty()) {
costmap_queue::CellData cell = queue_->getNextCell();
cell_values_[cell.index_] = CellData::absolute_difference(cell.src_x_, cell.x_) +
CellData::absolute_difference(cell.src_y_, cell.y_);
}
}
double MapGridCritic::scoreTrajectory(const dwb_msgs::msg::Trajectory2D & traj)
{
double score = 0.0;
unsigned int start_index = 0;
if (aggregationType_ == ScoreAggregationType::Product) {
score = 1.0;
} else if (aggregationType_ == ScoreAggregationType::Last && !stop_on_failure_) {
start_index = traj.poses.size() - 1;
}
double grid_dist;
for (unsigned int i = start_index; i < traj.poses.size(); ++i) {
grid_dist = scorePose(traj.poses[i]);
if (stop_on_failure_) {
if (grid_dist == obstacle_score_) {
throw dwb_core::
IllegalTrajectoryException(name_, "Trajectory Hits Obstacle.");
} else if (grid_dist == unreachable_score_) {
throw dwb_core::
IllegalTrajectoryException(name_, "Trajectory Hits Unreachable Area.");
}
}
switch (aggregationType_) {
case ScoreAggregationType::Last:
score = grid_dist;
break;
case ScoreAggregationType::Sum:
score += grid_dist;
break;
case ScoreAggregationType::Product:
if (score > 0) {
score *= grid_dist;
}
break;
}
}
return score;
}
double MapGridCritic::scorePose(const geometry_msgs::msg::Pose2D & pose)
{
unsigned int cell_x, cell_y;
// we won't allow trajectories that go off the map... shouldn't happen that often anyways
if (!costmap_->worldToMap(pose.x, pose.y, cell_x, cell_y)) {
throw dwb_core::
IllegalTrajectoryException(name_, "Trajectory Goes Off Grid.");
}
return getScore(cell_x, cell_y);
}
void MapGridCritic::addCriticVisualization(
std::vector<std::pair<std::string, std::vector<float>>> & cost_channels)
{
std::pair<std::string, std::vector<float>> grid_scores;
grid_scores.first = name_;
nav2_costmap_2d::Costmap2D * costmap = costmap_ros_->getCostmap();
unsigned int size_x = costmap->getSizeInCellsX();
unsigned int size_y = costmap->getSizeInCellsY();
grid_scores.second.resize(size_x * size_y);
unsigned int i = 0;
for (unsigned int cy = 0; cy < size_y; cy++) {
for (unsigned int cx = 0; cx < size_x; cx++) {
grid_scores.second[i] = getScore(cx, cy);
i++;
}
}
cost_channels.push_back(grid_scores);
}
} // namespace dwb_critics