Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
goal_manager.hpp
1 // Copyright (c) 2020, Samsung Research America
2 // Copyright (c) 2020, Applied Electric Vehicles Pty Ltd
3 // Copyright (c) 2024, Stevedan Ogochukwu Omodolor Omodia
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License. Reserved.
16 
17 #ifndef NAV2_SMAC_PLANNER__GOAL_MANAGER_HPP_
18 #define NAV2_SMAC_PLANNER__GOAL_MANAGER_HPP_
19 
20 #include <unordered_set>
21 #include <vector>
22 #include <functional>
23 
24 #include "nav2_smac_planner/types.hpp"
25 #include "nav2_smac_planner/node_2d.hpp"
26 #include "nav2_smac_planner/node_hybrid.hpp"
27 #include "nav2_smac_planner/node_lattice.hpp"
28 #include "nav2_smac_planner/node_basic.hpp"
29 #include "nav2_smac_planner/collision_checker.hpp"
30 
31 
32 namespace nav2_smac_planner
33 {
34 
39 template<typename NodeT>
41 {
42 public:
43  typedef NodeT * NodePtr;
44  typedef std::vector<NodePtr> NodeVector;
45  typedef std::unordered_set<NodePtr> NodeSet;
46  typedef std::vector<GoalState<NodeT>> GoalStateVector;
47  typedef typename NodeT::Coordinates Coordinates;
48  typedef typename NodeT::CoordinateVector CoordinateVector;
49 
54  : _goals_set(NodeSet()),
55  _goals_state(GoalStateVector()),
56  _goals_coordinate(CoordinateVector()),
57  _ref_goal_coord(Coordinates())
58  {
59  }
60 
64  ~GoalManager() = default;
65 
70  bool goalsIsEmpty()
71  {
72  return _goals_state.empty();
73  }
74 
79  void addGoal(NodePtr & goal)
80  {
81  _goals_state.push_back({goal, true});
82  }
83 
87  void clear()
88  {
89  _goals_set.clear();
90  _goals_state.clear();
91  _goals_coordinate.clear();
92  }
93 
101  NodeVector & coarse_check_goals, NodeVector & fine_check_goals,
102  int coarse_search_resolution)
103  {
104  for (unsigned int i = 0; i < _goals_state.size(); i++) {
105  if (_goals_state[i].is_valid) {
106  if (i % coarse_search_resolution == 0) {
107  coarse_check_goals.push_back(_goals_state[i].goal);
108  } else {
109  fine_check_goals.push_back(_goals_state[i].goal);
110  }
111  }
112  }
113  }
114 
125  const float & tolerance,
126  GridCollisionChecker * collision_checker,
127  const bool & traverse_unknown)
128  {
129  // Make sure that there was a goal clear before this was run
130  if (!_goals_set.empty() || !_goals_coordinate.empty()) {
131  throw std::runtime_error("Goal set should be cleared before calling "
132  "removeinvalidgoals");
133  }
134  for (unsigned int i = 0; i < _goals_state.size(); i++) {
135  if (_goals_state[i].goal->isNodeValid(traverse_unknown, collision_checker) ||
136  tolerance > 0.001)
137  {
138  _goals_state[i].is_valid = true;
139  _goals_set.insert(_goals_state[i].goal);
140  _goals_coordinate.push_back(_goals_state[i].goal->pose);
141  } else {
142  _goals_state[i].is_valid = false;
143  }
144  }
145  }
146 
152  inline bool isGoal(const NodePtr & node)
153  {
154  return _goals_set.find(node) != _goals_set.end();
155  }
156 
161  inline NodeSet & getGoalsSet()
162  {
163  return _goals_set;
164  }
165 
170  inline GoalStateVector & getGoalsState()
171  {
172  return _goals_state;
173  }
174 
179  inline CoordinateVector & getGoalsCoordinates()
180  {
181  return _goals_coordinate;
182  }
183 
188  inline void setRefGoalCoordinates(const Coordinates & coord)
189  {
190  _ref_goal_coord = coord;
191  }
192 
198  inline bool hasGoalChanged(const Coordinates & coord)
199  {
206  return _ref_goal_coord != coord;
207  }
208 
209 protected:
210  NodeSet _goals_set;
211  GoalStateVector _goals_state;
212  CoordinateVector _goals_coordinate;
213  Coordinates _ref_goal_coord;
214 };
215 
216 } // namespace nav2_smac_planner
217 
218 #endif // NAV2_SMAC_PLANNER__GOAL_MANAGER_HPP_
Responsible for managing multiple variables storing information on the goal.
GoalManager()
Constructor: Initializes empty goal state. sets and coordinate lists.
~GoalManager()=default
Destructor for the GoalManager.
void removeInvalidGoals(const float &tolerance, GridCollisionChecker *collision_checker, const bool &traverse_unknown)
Filters and marks invalid goals based on collision checking and tolerance thresholds.
NodeSet & getGoalsSet()
Get pointer reference to goals set vector.
bool hasGoalChanged(const Coordinates &coord)
Checks whether the Reference goal coordinate has changed.
void addGoal(NodePtr &goal)
Adds goal to the goal vector.
void prepareGoalsForAnalyticExpansion(NodeVector &coarse_check_goals, NodeVector &fine_check_goals, int coarse_search_resolution)
Populates coarse and fine goal lists for analytic expansion.
GoalStateVector & getGoalsState()
Get pointer reference to goals state.
void setRefGoalCoordinates(const Coordinates &coord)
Set the Reference goal coordinate.
void clear()
Clears all internal goal data, including goals, states, and coordinates.
bool goalsIsEmpty()
Checks if the goals set is empty.
bool isGoal(const NodePtr &node)
Check if a given node is part of the goal set.
CoordinateVector & getGoalsCoordinates()
Get pointer reference to goals coordinates.
A costmap grid collision checker.