Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
types.hpp
1 // Copyright (c) 2025 Open Navigation LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <queue>
16 #include <string>
17 #include <any>
18 #include <vector>
19 #include <unordered_map>
20 #include <utility>
21 #include <limits>
22 #include <geometry_msgs/msg/pose_stamped.hpp>
23 
24 #ifndef NAV2_ROUTE__TYPES_HPP_
25 #define NAV2_ROUTE__TYPES_HPP_
26 
27 namespace nav2_route
28 {
29 
34 struct Metadata
35 {
36  Metadata() {}
37 
38  // For retrieving metadata at run-time via plugins
39  template<typename T>
40  T getValue(const std::string & key, T & default_val) const
41  {
42  auto it = data.find(key);
43  if (it == data.end()) {
44  return default_val;
45  }
46  return std::any_cast<T>(it->second);
47  }
48 
49  // For populating metadata from file
50  template<typename T>
51  void setValue(const std::string & key, T & value)
52  {
53  data[key] = value;
54  }
55 
56  std::unordered_map<std::string, std::any> data;
57 };
58 
59 struct Node;
60 typedef Node * NodePtr;
61 typedef std::vector<Node> NodeVector;
62 typedef NodeVector Graph;
63 typedef std::unordered_map<unsigned int, unsigned int> GraphToIDMap;
64 typedef std::unordered_map<unsigned int, std::vector<unsigned int>> GraphToIncomingEdgesMap;
65 typedef std::vector<NodePtr> NodePtrVector;
66 typedef std::pair<float, NodePtr> NodeElement;
67 typedef std::pair<unsigned int, unsigned int> NodeExtents;
68 
74 {
75  bool operator()(const NodeElement & a, const NodeElement & b) const
76  {
77  return a.first > b.first;
78  }
79 };
80 
81 typedef std::priority_queue<NodeElement, std::vector<NodeElement>, NodeComparator> NodeQueue;
82 
87 struct EdgeCost
88 {
89  float cost{0.0};
90  bool overridable{true}; // If overridable, may use plugin edge cost scorers
91 };
92 
97 enum class OperationTrigger
98 {
99  NODE = 0,
100  ON_ENTER = 1,
101  ON_EXIT = 2
102 };
103 
108 struct Operation
109 {
110  std::string type;
111  OperationTrigger trigger;
112  Metadata metadata;
113 };
114 
115 typedef std::vector<Operation> Operations;
116 typedef std::vector<Operation *> OperationPtrs;
117 
123 {
124  std::vector<std::string> operations_triggered;
125  bool reroute{false};
126  std::vector<unsigned int> blocked_ids;
127 };
128 
134 {
135  unsigned int edgeid; // Edge identifier
136  NodePtr start{nullptr}; // Ptr to starting node of edge
137  NodePtr end{nullptr}; // Ptr to ending node of edge
138  EdgeCost edge_cost; // Cost information associated with edge
139  Metadata metadata; // Any metadata stored in the graph file of interest
140  Operations operations; // Operations to perform related to the edge
141 
142  float getEdgeLength();
143 };
144 
145 typedef DirectionalEdge * EdgePtr;
146 typedef std::vector<DirectionalEdge> EdgeVector;
147 typedef std::vector<EdgePtr> EdgePtrVector;
148 
155 {
156  EdgePtr parent_edge{nullptr};
157  float integrated_cost{std::numeric_limits<float>::max()};
158  float traversal_cost{std::numeric_limits<float>::max()};
159 
160  void reset()
161  {
162  integrated_cost = std::numeric_limits<float>::max();
163  traversal_cost = std::numeric_limits<float>::max();
164  parent_edge = nullptr;
165  }
166 };
167 
173 {
174  std::string frame_id{"map"};
175  float x{0.0}, y{0.0};
176 };
177 
182 struct Node
183 {
184  unsigned int nodeid; // Node identifier
185  Coordinates coords; // Coordinates of node
186  EdgeVector neighbors; // Directed neighbors and edges of the node
187  Metadata metadata; // Any metadata stored in the graph file of interest
188  Operations operations; // Operations to perform related to the node
189  SearchState search_state; // State maintained by route search algorithm
190 
191  void addEdge(
192  EdgeCost & cost, NodePtr node, unsigned int edgeid, Metadata meta_data = {},
193  Operations operations_data = {})
194  {
195  neighbors.push_back({edgeid, this, node, cost, meta_data, operations_data});
196  }
197 };
198 
199 inline float DirectionalEdge::getEdgeLength()
200 {
201  return hypotf(
202  end->coords.x - start->coords.x,
203  end->coords.y - start->coords.y);
204 }
205 
210 struct Route
211 {
212  NodePtr start_node;
213  EdgePtrVector edges;
214  float route_cost{0.0};
215 };
216 
224 {
225  unsigned int start_nodeid; // node id of start node
226  unsigned int goal_nodeid; // node id of goal node
227  geometry_msgs::msg::PoseStamped start_pose; // pose of start
228  geometry_msgs::msg::PoseStamped goal_pose; // pose of goal
229  bool use_poses; // whether the start and goal poses are used
230 };
231 
236 enum class TrackerResult
237 {
238  EXITED = 0,
239  INTERRUPTED = 1,
240  COMPLETED = 2
241 };
242 
248 {
249  NodePtr last_node{nullptr}, next_node{nullptr};
250  EdgePtr current_edge{nullptr};
251  int route_edges_idx{-1};
252  bool within_radius{false};
253 };
254 
264 {
265  // Communicate edges identified as blocked by the operational plugins like collision checkers.
266  // This is fully managed by the route tracker when populated.
267  std::vector<unsigned int> blocked_ids;
268 
269  // Used to determine if this is the first planning iteration in the goal intent extractor
270  // to bypass pruning. Fully managed in the goal intent extractor.
271  bool first_time{true};
272 
273  // Used to mark current edge being tracked by the route, if progress was made before rerouting.
274  // It is reset in the goal intent extractor if the previous progressed edge is different from
275  // the new edge from planning. Otherwise, used in the path converter to create new dense path
276  // with partial progress information and in the tracker to seed the state to continue.
277  // It is managed by both the goal intent extractor and the route tracker.
278  EdgePtr curr_edge{nullptr};
279  Coordinates closest_pt_on_edge;
280 
281  // Used to mark the route tracking state before rerouting was requested.
282  // When route tracking made some progress, the Start ID and pose are populated
283  // and used by the goal intent extractor to override the initial request's
284  // start, current pose along the edge, and pruning criteria. Otherwise, the initial request
285  // information is used. This is managed by the route tracker but used by goal intent extractor.
286  unsigned int rerouting_start_id{std::numeric_limits<unsigned int>::max()};
287  geometry_msgs::msg::PoseStamped rerouting_start_pose;
288 
289  void reset()
290  {
291  rerouting_start_id = std::numeric_limits<unsigned int>::max();
292  blocked_ids.clear();
293  first_time = true;
294  curr_edge = nullptr;
295  closest_pt_on_edge = Coordinates();
296  rerouting_start_pose = geometry_msgs::msg::PoseStamped();
297  }
298 };
299 
304 enum class EdgeType
305 {
306  NONE = 0,
307  START = 1,
308  END = 2
309 };
310 
311 } // namespace nav2_route
312 
313 #endif // NAV2_ROUTE__TYPES_HPP_
An object to store Node coordinates in different frames.
Definition: types.hpp:173
An object representing edges between nodes.
Definition: types.hpp:134
An object to store edge cost or cost metadata for scoring.
Definition: types.hpp:88
An object to store arbitrary metadata regarding nodes from the graph file.
Definition: types.hpp:35
Node comparison for priority queue sorting.
Definition: types.hpp:74
An object to store the nodes in the graph file.
Definition: types.hpp:183
An object to store operations to perform on events with types and metadata.
Definition: types.hpp:109
Result information from the operations manager.
Definition: types.hpp:123
State shared to objects to communicate important rerouting data to avoid rerouting over blocked edges...
Definition: types.hpp:264
An object to store salient features of the route request including its start and goal node ids,...
Definition: types.hpp:224
Current state management of route tracking class.
Definition: types.hpp:248
An ordered set of nodes and edges corresponding to the planned route.
Definition: types.hpp:211
An object to store state related to graph searching of nodes This is an internal class users should n...
Definition: types.hpp:155