Skip to content

Commit 0c20df2

Browse files
nav2_smac_planner: make A* return closest path found in case of timeout (#5578)
Until now, a path computation timeout would systematically lead to a nav2_core::PlannerTimedOut error, even though a path was found within the tolerance. This commit makes the A* return the best path found if it's within the tolerance. Signed-off-by: Dylan De Coeyer <dylan.decoeyer@quimesis.be>
1 parent f78bac5 commit 0c20df2

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

‎nav2_smac_planner/include/nav2_smac_planner/a_star.hpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ class AStarAlgorithm
238238
*/
239239
inline bool areInputsValid();
240240

241+
/**
242+
* @brief Get the closest path within tolerance if available
243+
* @param path Vector of coordinates to fill with path
244+
* @return if a valid path was found within tolerance
245+
*/
246+
inline bool getClosestPathWithinTolerance(CoordinateVector & path);
247+
241248
/**
242249
* @brief Clear heuristic queue of nodes to search
243250
*/

‎nav2_smac_planner/src/a_star.cpp‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,17 @@ bool AStarAlgorithm<NodeT>::areInputsValid()
322322
return true;
323323
}
324324

325+
template<typename NodeT>
326+
bool AStarAlgorithm<NodeT>::getClosestPathWithinTolerance(CoordinateVector & path)
327+
{
328+
if (_best_heuristic_node.first < getToleranceHeuristic()) {
329+
_graph.at(_best_heuristic_node.second).backtracePath(path);
330+
return true;
331+
}
332+
333+
return false;
334+
}
335+
325336
template<typename NodeT>
326337
bool AStarAlgorithm<NodeT>::createPath(
327338
CoordinateVector & path, int & iterations,
@@ -381,7 +392,8 @@ bool AStarAlgorithm<NodeT>::createPath(
381392
std::chrono::duration<double> planning_duration =
382393
std::chrono::duration_cast<std::chrono::duration<double>>(steady_clock::now() - start_time);
383394
if (static_cast<double>(planning_duration.count()) >= _max_planning_time) {
384-
return false;
395+
// In case of timeout, return the path that is closest, if within tolerance.
396+
return getClosestPathWithinTolerance(path);
385397
}
386398
}
387399

@@ -448,12 +460,8 @@ bool AStarAlgorithm<NodeT>::createPath(
448460
}
449461
}
450462

451-
if (_best_heuristic_node.first < getToleranceHeuristic()) {
452-
// If we run out of search options, return the path that is closest, if within tolerance.
453-
return _graph.at(_best_heuristic_node.second).backtracePath(path);
454-
}
455-
456-
return false;
463+
// If we run out of search options, return the path that is closest, if within tolerance.
464+
return getClosestPathWithinTolerance(path);
457465
}
458466

459467
template<typename NodeT>

0 commit comments

Comments
 (0)