Decision Module  1.0
ICRA2020 AI Challenge Northwestern Polytechnical University Aoxiang Team Strategy Code
behavior_tree.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_TREE_H
19 #define ROBORTS_DECISION_BEHAVIOR_TREE_H
20 
21 #include <chrono>
22 
23 #include <ros/ros.h>
24 
25 #include "behavior_node.h"
26 
27 
28 namespace roborts_decision{
32 class BehaviorTree {
33  public:
39  BehaviorTree(BehaviorNode::Ptr root_node, int cycle_duration) :
40  root_node_(root_node),
41  cycle_duration_(cycle_duration) {}
45  void Run() {
46 
47  unsigned int frame = 0;
48  while (ros::ok() ) {
49 
50  std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
51  // Update the blackboard data
52  ros::spinOnce();
53  ROS_INFO("Frame : %d", frame);
54  root_node_->Run();
55 
56  std::chrono::steady_clock::time_point end_time = std::chrono::steady_clock::now();
57  std::chrono::milliseconds execution_duration =
58  std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
59  std::chrono::milliseconds sleep_time = cycle_duration_ - execution_duration;
60 
61  if (sleep_time > std::chrono::microseconds(0)) {
62 
63  std::this_thread::sleep_for(sleep_time);
64  ROS_INFO("Excution Duration: %ld / %ld ms", cycle_duration_.count(), cycle_duration_.count());
65 
66  } else {
67 
68  ROS_WARN("The time tick once is %ld beyond the expected time %ld", execution_duration.count(), cycle_duration_.count());
69 
70  }
71  ROS_INFO("----------------------------------");
72  frame++;
73  }
74  }
75  private:
79  std::chrono::milliseconds cycle_duration_;
80 
81 };
82 
83 }//namespace roborts_decision
84 
85 #endif //ROBORTS_DECISION_BEHAVIOR_TREE_H
roborts_decision::BehaviorTree::cycle_duration_
std::chrono::milliseconds cycle_duration_
tick duration of the behavior tree (unit ms)
Definition: behavior_tree.h:109
roborts_decision::BehaviorNode::Ptr
std::shared_ptr< BehaviorNode > Ptr
Definition: behavior_node.h:71
roborts_decision::BehaviorTree::root_node_
BehaviorNode::Ptr root_node_
root node of the behavior tree
Definition: behavior_tree.h:107
roborts_decision
Definition: behavior_test_node.h:14
roborts_decision::BehaviorTree::BehaviorTree
BehaviorTree(BehaviorNode::Ptr root_node, int cycle_duration)
Constructor of BehaviorTree.
Definition: behavior_tree.h:69
roborts_decision::BehaviorTree::Run
void Run()
Loop to tick the behavior tree.
Definition: behavior_tree.h:75
behavior_node.h