Decision Module  1.0
ICRA2020 AI Challenge Northwestern Polytechnical University Aoxiang Team Strategy Code
behavior_node.h
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2019 RoboMaster.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  ***************************************************************************/
17 
18 #ifndef ROBORTS_DECISION_BEHAVIOR_NODE_H
19 #define ROBORTS_DECISION_BEHAVIOR_NODE_H
20 
21 #include <chrono>
22 #include <thread>
23 #include <vector>
24 
25 #include "../blackboard/blackboard_raw.h"
26 #include "behavior_state.h"
27 #include "../strategy/strategy_execute.h"
28 
29 namespace roborts_decision{
33 enum class BehaviorType {
34  PARALLEL,
35  SELECTOR,
36  SEQUENCE,
37  ACTION,
38  PRECONDITION,
39 };
44 /*enum class AbortType {
45  NONE, ///<Do not abort anything
46  SELF, ///<Abort self, and any sub-trees running under this node
47  LOW_PRIORITY, ///<Abort any nodes to the right of this node
48  BOTH ///<Abort self, any sub-trees running under me, and any nodes to the right of this node
49 };*/
53 class BehaviorNode : public std::enable_shared_from_this<BehaviorNode>{
54  public:
55 
56  typedef std::shared_ptr<BehaviorNode> Ptr;
57  BehaviorNode(){}
64  BehaviorNode(std::string name, BehaviorType behavior_type, const Blackboard::Ptr &blackboard_ptr):
65  name_(name),
66  behavior_type_(behavior_type),
67  blackboard_ptr_(blackboard_ptr),
69  level_(0){}
70 
71  virtual ~BehaviorNode()= default;
81 
83  OnInitialize();
84  }
85 
87 
90  }
91 
92  return behavior_state_;
93  }
98  virtual void Reset(){
102  }
103  }
109  return behavior_type_;
110  }
116  return behavior_state_;
117  }
122  std::string GetName(){
123  return name_.c_str();
124  }
129  void SetParent(BehaviorNode::Ptr parent_node_ptr){
130  parent_node_ptr_ = parent_node_ptr;
131  }
138  }
143  virtual BehaviorNode::Ptr GetChild(){
144  return nullptr;
145  }
150  unsigned int GetLevel(){
151  return level_;
152  }
157  void SetLevel(unsigned int level){
158  level_ = level;
159  }
160  protected:
165  virtual BehaviorState Update() = 0;
169  virtual void OnInitialize() = 0;
174  virtual void OnTerminate(BehaviorState state) = 0;
175 
177  std::string name_;
179 // std::mutex behavior_state_mutex_;
188  unsigned int level_;
189 };
190 
194 class CompositeNode: public BehaviorNode{
195  public:
202  CompositeNode(std::string name, BehaviorType behavior_type, const Blackboard::Ptr &blackboard_ptr):
203  BehaviorNode::BehaviorNode(name, behavior_type, blackboard_ptr),
205  }
206 
207  virtual ~CompositeNode()= default;
212  virtual void AddChildren(const BehaviorNode::Ptr& child_node_ptr){
213  children_node_ptr_.push_back(child_node_ptr);
214  child_node_ptr->SetParent(shared_from_this());
215  child_node_ptr->SetLevel(level_+1);
216  }
221  virtual void AddChildren(std::initializer_list<BehaviorNode::Ptr> child_node_ptr_list){
222  for (auto child_node_ptr = child_node_ptr_list.begin(); child_node_ptr!=child_node_ptr_list.end(); child_node_ptr++) {
223  children_node_ptr_.push_back(*child_node_ptr);
224  (*child_node_ptr)->SetParent(shared_from_this());
225  (*child_node_ptr)->SetLevel(level_+1);
226  }
227  }
232  virtual BehaviorNode::Ptr GetChild(){
234  }
239  std::vector<BehaviorNode::Ptr>& GetChildren(){
240  return children_node_ptr_;
241  }
246  unsigned int GetChildrenIndex(){
248  }
253  unsigned int GetChildrenNum(){
254  return children_node_ptr_.size();
255  }
256 
257  protected:
262  virtual BehaviorState Update() = 0;
266  virtual void OnInitialize() = 0;
271  virtual void OnTerminate(BehaviorState state) = 0;
273  std::vector<BehaviorNode::Ptr> children_node_ptr_;
275  unsigned int children_node_index_;
276 
277 };
282  public:
288  SelectorNode(std::string name, const Blackboard::Ptr &blackboard_ptr):
289  CompositeNode::CompositeNode(name, BehaviorType::SELECTOR, blackboard_ptr) {
290  this->ptr_selector_node_ = nullptr;
291  }
292  virtual ~SelectorNode() = default;
293 
294 
295  virtual void AddChildren(std::initializer_list<std::shared_ptr<AbstractCommonStrategy>> child_node_ptr_list);
296 
297  protected:
298  void SetChildrenIndex(unsigned int children_node_index);
299 
303  virtual void OnInitialize(){
305  ROS_INFO("%s %s", name_.c_str(), __FUNCTION__);
306  }
307 
308  virtual BehaviorState Update();
309 
310  std::shared_ptr<AbstractCommonStrategy> GetAction();
311 
312  bool HasActionOnGoing();
313 
314  void ExitThisAction();
315 
316  std::shared_ptr<AbstractCommonStrategy> ChooseActionToExecute();
317 
319  private:
320  std::shared_ptr<AbstractCommonStrategy> ptr_selector_node_;
321  std::vector<std::shared_ptr<AbstractCommonStrategy>> children_node_ptr_vector_;
322 
323 
324 };
325 
326 
327 
331 class ParallelNode: public CompositeNode{
332  public:
339  ParallelNode(std::string name, const Blackboard::Ptr &blackboard_ptr,
340  unsigned int threshold):
341  CompositeNode::CompositeNode(name, BehaviorType::PARALLEL, blackboard_ptr),
342  threshold_(threshold),
343  success_count_(0),
344  failure_count_(0){
345 
346  }
347 
348  virtual ~ParallelNode() = default;
349  protected:
354  virtual void OnInitialize(){
355  failure_count_=0;
356  success_count_=0;
357  children_node_done_.clear();
358  children_node_done_.resize(children_node_ptr_.size(),false);
359  ROS_INFO("initialize start");
360  ROS_INFO("%s %s", name_.c_str(), __FUNCTION__);
361  }
366  virtual BehaviorState Update(){
367 
368  if (children_node_ptr_.empty()) {
370  }
371 
372  for (unsigned int index = 0; index!=children_node_ptr_.size(); index++) {
373  if (!children_node_done_.at(index)){
374  BehaviorState state = children_node_ptr_.at(index)->Run();
375 
376 /* if (state == BehaviorState::SUCCESS) {
377  children_node_done_.at(index) = true;
378  if (++success_count_ >= threshold_) {
379  return BehaviorState::SUCCESS;
380  }
381  }
382  else if (state == BehaviorState::FAILURE) {
383  children_node_done_.at(index) = true;
384  if (++failure_count_ >= children_node_ptr_.size()-threshold_) {
385  return BehaviorState::FAILURE;
386  }
387  }*/
388 
389  }
390  }
391  return BehaviorState::RUNNING;
392  }
397  virtual void OnTerminate(BehaviorState state){
398  switch (state){
399  case BehaviorState::IDLE:
400  ROS_INFO("%s %s IDLE!", name_.c_str(), __FUNCTION__);
401  break;
403  ROS_INFO("%s %s SUCCESS!", name_.c_str(), __FUNCTION__);
404  break;
406  ROS_INFO("%s %s FAILURE!", name_.c_str(), __FUNCTION__);
407  break;
408  default:
409  ROS_INFO("%s %s ERROR!", name_.c_str(), __FUNCTION__);
410  return;
411  }
412  //TODO: no matter what state, the node would reset all running children to terminate.
413  for (unsigned int index = 0; index!=children_node_ptr_.size(); index++) {
414  children_node_ptr_.at(index)->Reset();
415  }
416  }
417 
419  std::vector<bool> children_node_done_;
421  unsigned int success_count_;
423  unsigned int failure_count_;
425  unsigned int threshold_;
426 };
427 
428 
429 } //namespace roborts_decision
430 
431 
432 #endif //ROBORTS_DECISION_BEHAVIOR_NODE_H
roborts_decision::BehaviorType::PRECONDITION
@ PRECONDITION
Precondition Node.
roborts_decision::ParallelNode
Behavior tree parallel node inherited from CompositeNode.
Definition: behavior_node.h:346
roborts_decision::BehaviorNode
Behavior tree base node.
Definition: behavior_node.h:68
roborts_decision::ParallelNode::Update
virtual BehaviorState Update()
Tick the node and update the state of the behavior node.
Definition: behavior_node.h:381
roborts_decision::CompositeNode::~CompositeNode
virtual ~CompositeNode()=default
roborts_decision::BehaviorState::FAILURE
@ FAILURE
Failure state as result.
roborts_decision::CompositeNode::GetChild
virtual BehaviorNode::Ptr GetChild()
Get the child behavior node that it is turn to tick.
Definition: behavior_node.h:247
roborts_decision::BehaviorNode::SetLevel
void SetLevel(unsigned int level)
Set the level of the behavior node.
Definition: behavior_node.h:172
roborts_decision::BehaviorNode::GetName
std::string GetName()
Get the name of the behavior node.
Definition: behavior_node.h:137
roborts_decision::SelectorNode::Update
virtual BehaviorState Update()
Tick the node and update the state of the behavior node.
Definition: behavior_node.cpp:51
roborts_decision::ParallelNode::success_count_
unsigned int success_count_
the current number of child nodes with success behavior state
Definition: behavior_node.h:436
roborts_decision::Blackboard::Ptr
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:37
roborts_decision::ParallelNode::threshold_
unsigned int threshold_
the number of child nodes with success behavior state to determine success of the parallel node
Definition: behavior_node.h:440
roborts_decision::ParallelNode::failure_count_
unsigned int failure_count_
the current number of child nodes with failure behavior state
Definition: behavior_node.h:438
roborts_decision::BehaviorNode::Ptr
std::shared_ptr< BehaviorNode > Ptr
Definition: behavior_node.h:71
roborts_decision::SelectorNode::SelectorNode
SelectorNode(std::string name, const Blackboard::Ptr &blackboard_ptr)
Constructor of SelectorNode.
Definition: behavior_node.h:303
roborts_decision::CompositeNode::GetChildren
std::vector< BehaviorNode::Ptr > & GetChildren()
Get the list of child behavior nodes.
Definition: behavior_node.h:254
roborts_decision::CompositeNode::OnInitialize
virtual void OnInitialize()=0
Initialize something before starting to tick the node.
roborts_decision::BehaviorNode::GetBehaviorState
BehaviorState GetBehaviorState()
Get the state of the behavior node.
Definition: behavior_node.h:130
roborts_decision::BehaviorNode::SetParent
void SetParent(BehaviorNode::Ptr parent_node_ptr)
Set the parent of the behavior node.
Definition: behavior_node.h:144
roborts_decision::BehaviorState::IDLE
@ IDLE
Idle state, state as default or after cancellation.
roborts_decision::BehaviorNode::level_
unsigned int level_
Level of the tree.
Definition: behavior_node.h:203
roborts_decision::BehaviorNode::Update
virtual BehaviorState Update()=0
Tick the node and update the state of the behavior node.
roborts_decision::BehaviorNode::Run
BehaviorState Run()
Run the behavior node.
Definition: behavior_node.h:95
roborts_decision::BehaviorNode::blackboard_ptr_
Blackboard::Ptr blackboard_ptr_
Blackboard.
Definition: behavior_node.h:199
roborts_decision::BehaviorNode::GetChild
virtual BehaviorNode::Ptr GetChild()
Get the child node of the behavior node.
Definition: behavior_node.h:158
roborts_decision::CompositeNode::CompositeNode
CompositeNode(std::string name, BehaviorType behavior_type, const Blackboard::Ptr &blackboard_ptr)
Constructor of CompositeNode.
Definition: behavior_node.h:217
roborts_decision::BehaviorNode::GetParent
BehaviorNode::Ptr GetParent()
Get the parent node of the behavior node.
Definition: behavior_node.h:151
roborts_decision::BehaviorNode::OnTerminate
virtual void OnTerminate(BehaviorState state)=0
Recover or reset something After getting the result.
roborts_decision::SelectorNode::SetChildrenIndex
void SetChildrenIndex(unsigned int children_node_index)
Set the index of the child node to tick.
Definition: behavior_node.cpp:93
roborts_decision::BehaviorType::ACTION
@ ACTION
Action Node.
roborts_decision::BehaviorNode::OnInitialize
virtual void OnInitialize()=0
Initialize something before starting to tick the node.
roborts_decision::BehaviorState::RUNNING
@ RUNNING
Running state in process.
roborts_decision::SelectorNode::ExitThisAction
void ExitThisAction()
Definition: behavior_node.cpp:14
roborts_decision::CompositeNode::children_node_index_
unsigned int children_node_index_
the index of child nodes
Definition: behavior_node.h:290
roborts_decision::CompositeNode::OnTerminate
virtual void OnTerminate(BehaviorState state)=0
Recover or reset something After getting the result.
roborts_decision::CompositeNode::children_node_ptr_
std::vector< BehaviorNode::Ptr > children_node_ptr_
the list of child nodes
Definition: behavior_node.h:288
behavior_state.h
roborts_decision::BehaviorNode::GetBehaviorType
BehaviorType GetBehaviorType()
Get the type of the behavior node.
Definition: behavior_node.h:123
roborts_decision::BehaviorType::PARALLEL
@ PARALLEL
Parallel Composite Node.
roborts_decision
Definition: behavior_test_node.h:14
roborts_decision::CompositeNode
Behavior tree composite node inherited from BehaviorNode.
Definition: behavior_node.h:209
roborts_decision::ParallelNode::children_node_done_
std::vector< bool > children_node_done_
a list of result checker flags for each child node
Definition: behavior_node.h:434
roborts_decision::ParallelNode::ParallelNode
ParallelNode(std::string name, const Blackboard::Ptr &blackboard_ptr, unsigned int threshold)
Constructor of ParallelNode.
Definition: behavior_node.h:354
roborts_decision::BehaviorType::SELECTOR
@ SELECTOR
Selector Composite Node.
roborts_decision::SelectorNode::ptr_selector_node_
std::shared_ptr< AbstractCommonStrategy > ptr_selector_node_
Definition: behavior_node.h:335
roborts_decision::ParallelNode::OnInitialize
virtual void OnInitialize()
Initialize something before starting to tick the node.
Definition: behavior_node.h:369
roborts_decision::ParallelNode::OnTerminate
virtual void OnTerminate(BehaviorState state)
Recover or reset something After getting the result.
Definition: behavior_node.h:412
roborts_decision::CompositeNode::GetChildrenIndex
unsigned int GetChildrenIndex()
Get the index of the child behavior node that it is turn to tick.
Definition: behavior_node.h:261
roborts_decision::SelectorNode::children_node_ptr_vector_
std::vector< std::shared_ptr< AbstractCommonStrategy > > children_node_ptr_vector_
Definition: behavior_node.h:336
roborts_decision::BehaviorType
BehaviorType
Type of behavior tree node.
Definition: behavior_node.h:48
roborts_decision::BehaviorState::SUCCESS
@ SUCCESS
Success state as result.
roborts_decision::SelectorNode::GetAction
std::shared_ptr< AbstractCommonStrategy > GetAction()
Definition: behavior_node.cpp:19
roborts_decision::BehaviorState
BehaviorState
Behavior state.
Definition: behavior_state.h:11
roborts_decision::CompositeNode::GetChildrenNum
unsigned int GetChildrenNum()
Get the number of child behavior nodes.
Definition: behavior_node.h:268
roborts_decision::CompositeNode::AddChildren
virtual void AddChildren(const BehaviorNode::Ptr &child_node_ptr)
Add child behavior node to the composite node.
Definition: behavior_node.h:227
roborts_decision::CompositeNode::Update
virtual BehaviorState Update()=0
Tick the node and update the state of the behavior node.
roborts_decision::BehaviorNode::GetLevel
unsigned int GetLevel()
Get the level of the behavior node.
Definition: behavior_node.h:165
roborts_decision::BehaviorNode::BehaviorNode
BehaviorNode()
Definition: behavior_node.h:72
roborts_decision::SelectorNode::OnTerminate
void OnTerminate(BehaviorState state)
Recover or reset something After getting the result.
Definition: behavior_node.cpp:69
roborts_decision::BehaviorNode::Reset
virtual void Reset()
Reset the behavior node.
Definition: behavior_node.h:113
roborts_decision::BehaviorNode::behavior_state_
BehaviorState behavior_state_
State.
Definition: behavior_node.h:195
roborts_decision::BehaviorNode::parent_node_ptr_
BehaviorNode::Ptr parent_node_ptr_
Parent Node Pointer.
Definition: behavior_node.h:201
roborts_decision::SelectorNode::HasActionOnGoing
bool HasActionOnGoing()
Definition: behavior_node.cpp:10
roborts_decision::ParallelNode::~ParallelNode
virtual ~ParallelNode()=default
roborts_decision::SelectorNode::AddChildren
virtual void AddChildren(std::initializer_list< std::shared_ptr< AbstractCommonStrategy >> child_node_ptr_list)
Definition: behavior_node.cpp:42
roborts_decision::SelectorNode::OnInitialize
virtual void OnInitialize()
Initialize something before starting to tick the node.
Definition: behavior_node.h:318
roborts_decision::SelectorNode::ChooseActionToExecute
std::shared_ptr< AbstractCommonStrategy > ChooseActionToExecute()
Definition: behavior_node.cpp:33
roborts_decision::SelectorNode::~SelectorNode
virtual ~SelectorNode()=default
roborts_decision::BehaviorNode::name_
std::string name_
Node name.
Definition: behavior_node.h:192
roborts_decision::BehaviorNode::~BehaviorNode
virtual ~BehaviorNode()=default
roborts_decision::SelectorNode
Behavior tree selector node inherited from CompositeNode.
Definition: behavior_node.h:296
roborts_decision::BehaviorType::SEQUENCE
@ SEQUENCE
Sequence Composite Node.
roborts_decision::BehaviorNode::behavior_type_
BehaviorType behavior_type_
Type.
Definition: behavior_node.h:197