|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. Reserved. |
14 | 14 |
|
| 15 | +#include <algorithm> |
| 16 | +#include <cmath> |
| 17 | +#include <unordered_set> |
| 18 | + |
| 19 | +#include "nav2_util/line_iterator.hpp" |
15 | 20 | #include "nav2_smac_planner/collision_checker.hpp" |
16 | 21 |
|
| 22 | + |
17 | 23 | namespace nav2_smac_planner |
18 | 24 | { |
19 | 25 |
|
@@ -182,6 +188,166 @@ bool GridCollisionChecker::inCollision( |
182 | 188 | return center_cost_ >= INSCRIBED_COST; |
183 | 189 | } |
184 | 190 |
|
| 191 | +bool GridCollisionChecker::inCollision( |
| 192 | + const float & x0, const float & y0, const float & theta0, |
| 193 | + const float & x1, const float & y1, const float & theta1, |
| 194 | + const bool & traverse_unknown, |
| 195 | + const float & min_turning_radius) |
| 196 | +{ |
| 197 | + // Convert angle bins to radians |
| 198 | + float start_theta = angles_[static_cast<unsigned int>(theta0)]; |
| 199 | + float end_theta = angles_[static_cast<unsigned int>(theta1)]; |
| 200 | + float dtheta = end_theta - start_theta; |
| 201 | + if (dtheta > M_PI) { |
| 202 | + dtheta -= 2.0f * static_cast<float>(M_PI); |
| 203 | + } else if (dtheta < -M_PI) { |
| 204 | + dtheta += 2.0f * static_cast<float>(M_PI); |
| 205 | + } |
| 206 | + |
| 207 | + // Quick checks to see if sweeping is necessary. If both poses are well |
| 208 | + // clear of obstacles, fall back to the single pose check on the child node |
| 209 | + const unsigned int size_x = costmap_->getSizeInCellsX(); |
| 210 | + const unsigned int size_y = costmap_->getSizeInCellsY(); |
| 211 | + |
| 212 | + if (outsideRange(size_x, x0) || outsideRange(size_y, y0) || |
| 213 | + outsideRange(size_x, x1) || outsideRange(size_y, y1)) |
| 214 | + { |
| 215 | + return true; |
| 216 | + } |
| 217 | + |
| 218 | + unsigned int mx0 = static_cast<unsigned int>(x0 + 0.5f); |
| 219 | + unsigned int my0 = static_cast<unsigned int>(y0 + 0.5f); |
| 220 | + unsigned int mx1 = static_cast<unsigned int>(x1 + 0.5f); |
| 221 | + unsigned int my1 = static_cast<unsigned int>(y1 + 0.5f); |
| 222 | + float start_cost = static_cast<float>(costmap_->getCost(mx0, my0)); |
| 223 | + float end_cost = static_cast<float>(costmap_->getCost(mx1, my1)); |
| 224 | + |
| 225 | + bool parent_safe = possible_collision_cost_ > 0.0f && |
| 226 | + start_cost < possible_collision_cost_; |
| 227 | + bool child_safe = possible_collision_cost_ > 0.0f && |
| 228 | + end_cost < possible_collision_cost_; |
| 229 | + |
| 230 | + if (parent_safe && child_safe) { |
| 231 | + return inCollision(x1, y1, theta1, traverse_unknown); |
| 232 | + } |
| 233 | + |
| 234 | + float dx = x1 - x0; |
| 235 | + float dy = y1 - y0; |
| 236 | + float distance = hypotf(dx, dy); |
| 237 | + float arc = fabsf(dtheta) * min_turning_radius; |
| 238 | + int steps = static_cast<int>(ceilf(std::max(std::max(distance, arc), 1.0f))); |
| 239 | + |
| 240 | + std::unordered_set<unsigned int> cells; |
| 241 | + float cx = 0.0f, cy = 0.0f; |
| 242 | + const bool use_arc = fabsf(dtheta) > 1e-3f; |
| 243 | + if (use_arc) { |
| 244 | + if (dtheta > 0.0f) { |
| 245 | + cx = x0 - min_turning_radius * sin(start_theta); |
| 246 | + cy = y0 + min_turning_radius * cos(start_theta); |
| 247 | + } else { |
| 248 | + cx = x0 + min_turning_radius * sin(start_theta); |
| 249 | + cy = y0 - min_turning_radius * cos(start_theta); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + for (int step = 0; step <= steps; ++step) { |
| 254 | + float t = static_cast<float>(step) / static_cast<float>(steps); |
| 255 | + float xi, yi, theta; |
| 256 | + if (use_arc) { |
| 257 | + theta = start_theta + t * dtheta; |
| 258 | + if (dtheta > 0.0f) { |
| 259 | + xi = cx + min_turning_radius * sin(theta); |
| 260 | + yi = cy - min_turning_radius * cos(theta); |
| 261 | + } else { |
| 262 | + xi = cx - min_turning_radius * sin(theta); |
| 263 | + yi = cy + min_turning_radius * cos(theta); |
| 264 | + } |
| 265 | + } else { |
| 266 | + xi = x0 + t * dx; |
| 267 | + yi = y0 + t * dy; |
| 268 | + theta = start_theta; |
| 269 | + } |
| 270 | + |
| 271 | + while (theta < 0.0f) { |
| 272 | + theta += 2.0f * static_cast<float>(M_PI); |
| 273 | + } |
| 274 | + while (theta >= 2.0f * static_cast<float>(M_PI)) { |
| 275 | + theta -= 2.0f * static_cast<float>(M_PI); |
| 276 | + } |
| 277 | + unsigned int angle_bin = static_cast<unsigned int>( |
| 278 | + theta / (2.0f * static_cast<float>(M_PI)) * angles_.size()); |
| 279 | + angle_bin %= angles_.size(); |
| 280 | + |
| 281 | + if (footprint_is_radius_) { |
| 282 | + if (outsideRange(costmap_->getSizeInCellsX(), xi) || |
| 283 | + outsideRange(costmap_->getSizeInCellsY(), yi)) |
| 284 | + { |
| 285 | + return true; |
| 286 | + } |
| 287 | + unsigned int mx = static_cast<unsigned int>(xi + 0.5f); |
| 288 | + unsigned int my = static_cast<unsigned int>(yi + 0.5f); |
| 289 | + cells.insert(my * size_x + mx); |
| 290 | + continue; |
| 291 | + } |
| 292 | + |
| 293 | + // Always include center cell |
| 294 | + if (outsideRange(costmap_->getSizeInCellsX(), xi) || |
| 295 | + outsideRange(costmap_->getSizeInCellsY(), yi)) |
| 296 | + { |
| 297 | + return true; |
| 298 | + } |
| 299 | + unsigned int mx = static_cast<unsigned int>(xi + 0.5f); |
| 300 | + unsigned int my = static_cast<unsigned int>(yi + 0.5f); |
| 301 | + cells.insert(my * size_x + mx); |
| 302 | + |
| 303 | + double wx, wy; |
| 304 | + costmap_->mapToWorld(static_cast<double>(xi), static_cast<double>(yi), wx, wy); |
| 305 | + const nav2_costmap_2d::Footprint & oriented = oriented_footprints_[angle_bin]; |
| 306 | + |
| 307 | + unsigned int x_prev, y_prev, x_curr, y_curr; |
| 308 | + if (!worldToMap(wx + oriented[0].x, wy + oriented[0].y, x_prev, y_prev)) { |
| 309 | + return true; |
| 310 | + } |
| 311 | + for (unsigned int i = 1; i < oriented.size(); ++i) { |
| 312 | + if (!worldToMap(wx + oriented[i].x, wy + oriented[i].y, x_curr, y_curr)) { |
| 313 | + return true; |
| 314 | + } |
| 315 | + for (nav2_util::LineIterator line(x_prev, y_prev, x_curr, y_curr); |
| 316 | + line.isValid(); line.advance()) |
| 317 | + { |
| 318 | + cells.insert(line.getY() * size_x + line.getX()); |
| 319 | + } |
| 320 | + x_prev = x_curr; |
| 321 | + y_prev = y_curr; |
| 322 | + } |
| 323 | + if (!worldToMap(wx + oriented[0].x, wy + oriented[0].y, x_curr, y_curr)) { |
| 324 | + return true; |
| 325 | + } |
| 326 | + for (nav2_util::LineIterator line(x_prev, y_prev, x_curr, y_curr); |
| 327 | + line.isValid(); line.advance()) |
| 328 | + { |
| 329 | + cells.insert(line.getY() * size_x + line.getX()); |
| 330 | + } |
| 331 | + } |
| 332 | + |
| 333 | + float max_cost = 0.0f; |
| 334 | + for (auto index : cells) { |
| 335 | + center_cost_ = static_cast<float>(costmap_->getCost(index)); |
| 336 | + if (center_cost_ == UNKNOWN_COST && traverse_unknown) { |
| 337 | + continue; |
| 338 | + } |
| 339 | + if (center_cost_ >= INSCRIBED_COST) { |
| 340 | + return true; |
| 341 | + } |
| 342 | + if (center_cost_ > max_cost) { |
| 343 | + max_cost = center_cost_; |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + center_cost_ = max_cost; |
| 348 | + return false; |
| 349 | +} |
| 350 | + |
185 | 351 | float GridCollisionChecker::getCost() |
186 | 352 | { |
187 | 353 | // Assumes inCollision called prior |
|
0 commit comments