Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
node_basic.hpp
1 // Copyright (c) 2020, Samsung Research America
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. Reserved.
14 
15 #ifndef NAV2_SMAC_PLANNER__NODE_BASIC_HPP_
16 #define NAV2_SMAC_PLANNER__NODE_BASIC_HPP_
17 
18 #include <type_traits>
19 
20 #include "nav2_smac_planner/constants.hpp"
21 #include "nav2_smac_planner/node_hybrid.hpp"
22 #include "nav2_smac_planner/node_lattice.hpp"
23 #include "nav2_smac_planner/node_2d.hpp"
24 #include "nav2_smac_planner/types.hpp"
25 #include "nav2_smac_planner/collision_checker.hpp"
26 
27 namespace nav2_smac_planner
28 {
29 
34 template<typename NodeT>
35 class NodeBasic
36 {
37 public:
42  explicit NodeBasic(const uint64_t new_index)
43  : graph_node_ptr(nullptr),
44  index(new_index)
45  {
46  }
47 
53  void populateSearchNode(NodeT * & node)
54  {
55  if constexpr (std::is_base_of_v<NodeLattice, NodeT>) {
56  // NodeLattice or derived: cache pose and primitive
57  this->pose = node->pose;
58  this->graph_node_ptr = node;
59  this->prim_ptr = node->getMotionPrimitive();
60  this->backward = node->isBackward();
61  } else if constexpr (std::is_base_of_v<NodeHybrid, NodeT>) {
62  // NodeHybrid or derived: cache pose and motion info
63  this->pose = node->pose;
64  this->graph_node_ptr = node;
65  this->motion_index = node->getMotionPrimitiveIndex();
66  this->turn_dir = node->getTurnDirection();
67  } else if constexpr (std::is_base_of_v<Node2D, NodeT>) {
68  // Node2D or derived: only set graph_node_ptr
69  this->graph_node_ptr = node;
70  } else {
71  // Unknown node type - set basics
72  this->graph_node_ptr = node;
73  }
74  }
75 
83  {
84  if constexpr (std::is_base_of_v<NodeLattice, NodeT>) {
85  // NodeLattice or derived: update pose and motion primitive
86  // We only want to override the node's pose/primitive if it has not yet been visited
87  // to prevent the case that a node has been queued multiple times and
88  // a new branch is overriding one of lower cost already visited.
89  if (!this->graph_node_ptr->wasVisited()) {
90  this->graph_node_ptr->pose = this->pose;
91  this->graph_node_ptr->setMotionPrimitive(this->prim_ptr);
92  this->graph_node_ptr->backwards(this->backward);
93  }
94  } else if constexpr (std::is_base_of_v<NodeHybrid, NodeT>) {
95  // NodeHybrid or derived: update pose and motion primitive index
96  // We only want to override the node's pose if it has not yet been visited
97  // to prevent the case that a node has been queued multiple times and
98  // a new branch is overriding one of lower cost already visited.
99  if (!this->graph_node_ptr->wasVisited()) {
100  this->graph_node_ptr->pose = this->pose;
101  this->graph_node_ptr->setMotionPrimitiveIndex(this->motion_index, this->turn_dir);
102  }
103  } else if constexpr (std::is_base_of_v<Node2D, NodeT>) {
104  // Node2D or derived: no-op
105  } else {
106  // Unknown node type: no-op
107  }
108  }
109 
110  typename NodeT::Coordinates pose; // Used by NodeHybrid and NodeLattice
111  NodeT * graph_node_ptr;
112  MotionPrimitive * prim_ptr; // Used by NodeLattice
113  uint64_t index;
114  unsigned int motion_index;
115  bool backward;
116  TurnDirection turn_dir;
117 };
118 
119 } // namespace nav2_smac_planner
120 
121 #endif // NAV2_SMAC_PLANNER__NODE_BASIC_HPP_
NodeBasic implementation for priority queue insertion.
Definition: node_basic.hpp:36
NodeBasic(const uint64_t new_index)
A constructor for nav2_smac_planner::NodeBasic.
Definition: node_basic.hpp:42
void populateSearchNode(NodeT *&node)
Take a NodeBasic and populate it with any necessary state cached in the queue for NodeT.
Definition: node_basic.hpp:53
void processSearchNode()
Take a NodeBasic and populate it with any necessary state cached in the queue for NodeTs.
Definition: node_basic.hpp:82
A struct of all motion primitive data.
Definition: types.hpp:169