22 #include "nav2_route/route_planner.hpp"
23 #include "nav2_ros_common/tf2_factories.hpp"
29 nav2::LifecycleNode::SharedPtr node,
30 const nav2::TransformBuffer::SharedPtr tf_buffer,
31 const std::shared_ptr<nav2_costmap_2d::CostmapSubscriber> costmap_subscriber)
33 max_iterations_ = node->declare_or_get_parameter(
"max_iterations", 0);
35 if (max_iterations_ <= 0) {
36 max_iterations_ = std::numeric_limits<int>::max();
39 edge_scorer_ = std::make_unique<EdgeScorer>(node, tf_buffer, costmap_subscriber);
43 Graph & graph,
unsigned int start_index,
unsigned int goal_index,
44 const std::vector<unsigned int> & blocked_ids,
54 const NodePtr & start_node = &graph.at(start_index);
55 const NodePtr & goal_node = &graph.at(goal_index);
58 EdgePtr & parent_edge = goal_node->search_state.parent_edge;
66 route.edges.push_back(parent_edge);
67 parent_edge = parent_edge->start->search_state.parent_edge;
70 std::reverse(route.edges.begin(), route.edges.end());
71 route.start_node = start_node;
72 route.route_cost = goal_node->search_state.integrated_cost;
80 for (
unsigned int i = 0; i != graph.size(); i++) {
81 graph[i].search_state.reset();
87 const std::vector<unsigned int> & blocked_ids,
92 start_id_ = start_node->nodeid;
93 goal_id_ = goal_node->nodeid;
94 start_node->search_state.integrated_cost = 0.0;
99 float potential_cost = 0.0, traversal_cost = 0.0;
101 while (!queue_.empty() && iterations < max_iterations_) {
108 if (curr_cost != node->search_state.integrated_cost) {
120 EdgeVector & edges =
getEdges(node);
121 for (
unsigned int edge_num = 0; edge_num != edges.size(); edge_num++) {
122 edge = &edges[edge_num];
123 neighbor = edge->end;
130 potential_cost = curr_cost + traversal_cost;
131 if (potential_cost < neighbor->search_state.integrated_cost) {
132 neighbor->search_state.parent_edge = edge;
133 neighbor->search_state.integrated_cost = potential_cost;
134 neighbor->search_state.traversal_cost = traversal_cost;
135 addNode(potential_cost, neighbor);
140 if (iterations == max_iterations_) {
148 const EdgePtr edge,
float & score,
const std::vector<unsigned int> & blocked_ids,
152 auto is_blocked = std::find_if(
153 blocked_ids.begin(), blocked_ids.end(),
154 [&](
unsigned int id) {return id == edge->edgeid || id == edge->end->nodeid;});
155 if (is_blocked != blocked_ids.end()) {
161 if (!edge->edge_cost.overridable || edge_scorer_->numPlugins() == 0) {
162 if (edge->edge_cost.cost <= 0.0) {
164 "Edge " + std::to_string(edge->edgeid) +
165 " doesn't contain and cannot compute a valid edge cost!");
167 score = edge->edge_cost.cost;
171 return edge_scorer_->score(edge, route_request,
classifyEdge(edge), score);
176 NodeElement data = queue_.top();
183 queue_.emplace(cost, node);
188 return node->neighbors;
194 std::swap(queue_, q);
199 return node->nodeid == goal_id_;
204 return node->nodeid == start_id_;
210 return EdgeType::START;
211 }
else if (
isGoal(edge->end)) {
212 return EdgeType::END;
214 return nav2_route::EdgeType::NONE;
void findShortestGraphTraversal(Graph &graph, const NodePtr start_node, const NodePtr goal_node, const std::vector< unsigned int > &blocked_ids, const RouteRequest &route_request)
Dikstra's algorithm search on the graph.
void configure(nav2::LifecycleNode::SharedPtr node, const nav2::TransformBuffer::SharedPtr tf_buffer, const std::shared_ptr< nav2_costmap_2d::CostmapSubscriber > costmap_subscriber)
Configure the route planner, get parameters.
nav2_route::EdgeType classifyEdge(const EdgePtr edge)
Checks edge is a start or end edge.
bool getTraversalCost(const EdgePtr edge, float &score, const std::vector< unsigned int > &blocked_ids, const RouteRequest &route_request)
Gets the traversal cost for an edge using edge scorers.
bool isGoal(const NodePtr node)
Checks if a given node is the goal node.
NodeElement getNextNode()
Gets the next node in the priority queue for search.
virtual Route findRoute(Graph &graph, unsigned int start_index, unsigned int goal_index, const std::vector< unsigned int > &blocked_ids, const RouteRequest &route_request)
Find the route from start to goal on the graph.
EdgeVector & getEdges(const NodePtr node)
Gets the edges from a given node.
void addNode(const float cost, const NodePtr node)
Adds a node to the priority queue for search.
void resetSearchStates(Graph &graph)
Reset the search state of the graph nodes.
bool isStart(const NodePtr node)
Checks if a given node is the start node.
void clearQueue()
Clears the priority queue.
An object representing edges between nodes.
An object to store the nodes in the graph file.
An object to store salient features of the route request including its start and goal node ids,...
An ordered set of nodes and edges corresponding to the planned route.