diff --git a/nav2_smac_planner/include/nav2_smac_planner/a_star.hpp b/nav2_smac_planner/include/nav2_smac_planner/a_star.hpp index a4720b3c5b2..631f4bee0b7 100644 --- a/nav2_smac_planner/include/nav2_smac_planner/a_star.hpp +++ b/nav2_smac_planner/include/nav2_smac_planner/a_star.hpp @@ -238,6 +238,13 @@ class AStarAlgorithm */ inline bool areInputsValid(); + /** + * @brief Get the closest path within tolerance if available + * @param path Vector of coordinates to fill with path + * @return if a valid path was found within tolerance + */ + inline bool getClosestPathWithinTolerance(CoordinateVector & path); + /** * @brief Clear heuristic queue of nodes to search */ diff --git a/nav2_smac_planner/src/a_star.cpp b/nav2_smac_planner/src/a_star.cpp index b8d12e7941f..6e4b8d00d3b 100644 --- a/nav2_smac_planner/src/a_star.cpp +++ b/nav2_smac_planner/src/a_star.cpp @@ -322,6 +322,17 @@ bool AStarAlgorithm::areInputsValid() return true; } +template +bool AStarAlgorithm::getClosestPathWithinTolerance(CoordinateVector & path) +{ + if (_best_heuristic_node.first < getToleranceHeuristic()) { + _graph.at(_best_heuristic_node.second).backtracePath(path); + return true; + } + + return false; +} + template bool AStarAlgorithm::createPath( CoordinateVector & path, int & iterations, @@ -381,7 +392,8 @@ bool AStarAlgorithm::createPath( std::chrono::duration planning_duration = std::chrono::duration_cast>(steady_clock::now() - start_time); if (static_cast(planning_duration.count()) >= _max_planning_time) { - return false; + // In case of timeout, return the path that is closest, if within tolerance. + return getClosestPathWithinTolerance(path); } } @@ -448,12 +460,8 @@ bool AStarAlgorithm::createPath( } } - if (_best_heuristic_node.first < getToleranceHeuristic()) { - // If we run out of search options, return the path that is closest, if within tolerance. - return _graph.at(_best_heuristic_node.second).backtracePath(path); - } - - return false; + // If we run out of search options, return the path that is closest, if within tolerance. + return getClosestPathWithinTolerance(path); } template