dummy_nodes.h
Go to the documentation of this file.
1 #ifndef SIMPLE_BT_NODES_H
2 #define SIMPLE_BT_NODES_H
3 
6 
7 namespace DummyNodes
8 {
9 
10 using BT::NodeStatus;
11 
13 
16 
18 {
19  public:
21  {
22  }
23 
24  NodeStatus open();
25 
26  NodeStatus close();
27 
28  private:
29  bool _opened;
30 };
31 
32 //--------------------------------------
33 
34 // Example of custom SyncActionNode (synchronous action)
35 // without ports.
37 {
38  public:
39  ApproachObject(const std::string& name) :
40  BT::SyncActionNode(name, {})
41  {
42  }
43 
44  // You must override the virtual function tick()
45  NodeStatus tick() override;
46 };
47 
48 // Example of custom SyncActionNode (synchronous action)
49 // with an input port.
51 {
52  public:
53  SaySomething(const std::string& name, const BT::NodeConfiguration& config)
55  {
56  }
57 
58  // You must override the virtual function tick()
59  NodeStatus tick() override;
60 
61  // It is mandatory to define this static method.
63  {
64  return{ BT::InputPort<std::string>("message") };
65  }
66 };
67 
68 //Same as class SaySomething, but to be registered with SimpleActionNode
70 
71 // Example os Asynchronous node that use StatefulActionNode as base class
73 {
74  public:
75  SleepNode(const std::string& name, const BT::NodeConfiguration& config)
77  {}
78 
80  {
81  // amount of milliseconds that we want to sleep
82  return{ BT::InputPort<int>("msec") };
83  }
84 
85  NodeStatus onStart() override
86  {
87  int msec = 0;
88  getInput("msec", msec);
89  if( msec <= 0 )
90  {
91  // no need to go into the RUNNING state
92  return NodeStatus::SUCCESS;
93  }
94  else {
95  using namespace std::chrono;
96  // once the deadline is reached, we will return SUCCESS.
97  deadline_ = system_clock::now() + milliseconds(msec);
98  return NodeStatus::RUNNING;
99  }
100  }
101 
104  {
105  if ( std::chrono::system_clock::now() >= deadline_ )
106  {
107  return NodeStatus::SUCCESS;
108  }
109  else {
110  return NodeStatus::RUNNING;
111  }
112  }
113 
114  void onHalted() override
115  {
116  // nothing to do here...
117  std::cout << "SleepNode interrupted" << std::endl;
118  }
119 
120  private:
121  std::chrono::system_clock::time_point deadline_;
122 };
123 
125 {
126  static GripperInterface grip_singleton;
127 
128  factory.registerSimpleCondition("CheckBattery", std::bind(CheckBattery));
129  factory.registerSimpleCondition("CheckTemperature", std::bind(CheckTemperature));
130  factory.registerSimpleAction("SayHello", std::bind(SayHello));
131  factory.registerSimpleAction("OpenGripper", std::bind(&GripperInterface::open, &grip_singleton));
132  factory.registerSimpleAction("CloseGripper", std::bind(&GripperInterface::close, &grip_singleton));
133  factory.registerNodeType<ApproachObject>("ApproachObject");
134  factory.registerNodeType<SaySomething>("SaySomething");
135 }
136 
137 } // end namespace
138 
139 #endif // SIMPLE_BT_NODES_H
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:232
BT
Definition: ex01_wrap_legacy.cpp:29
BT::SyncActionNode::SyncActionNode
SyncActionNode(const std::string &name, const NodeConfiguration &config)
Definition: action_node.cpp:50
DummyNodes::SleepNode::providedPorts
static BT::PortsList providedPorts()
Definition: dummy_nodes.h:79
DummyNodes::GripperInterface::open
NodeStatus open()
Definition: dummy_nodes.cpp:31
DummyNodes::ApproachObject::ApproachObject
ApproachObject(const std::string &name)
Definition: dummy_nodes.h:39
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:364
DummyNodes::SleepNode::deadline_
std::chrono::system_clock::time_point deadline_
Definition: dummy_nodes.h:121
DummyNodes::SleepNode::onRunning
NodeStatus onRunning() override
method invoked by an action in the RUNNING state.
Definition: dummy_nodes.h:103
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:55
DummyNodes::CheckBattery
BT::NodeStatus CheckBattery()
Definition: dummy_nodes.cpp:13
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:342
bt_factory.h
DummyNodes::SaySomethingSimple
BT::NodeStatus SaySomethingSimple(BT::TreeNode &self)
Definition: dummy_nodes.cpp:63
DummyNodes
Definition: dummy_nodes.cpp:10
BT::TreeNode::config
const NodeConfiguration & config() const
Definition: tree_node.cpp:127
BT::NodeConfiguration
Definition: tree_node.h:44
BT::StatefulActionNode::StatefulActionNode
StatefulActionNode(const std::string &name, const NodeConfiguration &config)
Definition: action_node.h:156
DummyNodes::SaySomething::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: dummy_nodes.cpp:51
DummyNodes::ApproachObject::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: dummy_nodes.cpp:45
DummyNodes::ApproachObject
Definition: dummy_nodes.h:36
DummyNodes::GripperInterface
Definition: dummy_nodes.h:17
DummyNodes::RegisterNodes
void RegisterNodes(BT::BehaviorTreeFactory &factory)
Definition: dummy_nodes.h:124
DummyNodes::GripperInterface::close
NodeStatus close()
Definition: dummy_nodes.cpp:38
DummyNodes::GripperInterface::_opened
bool _opened
Definition: dummy_nodes.h:29
DummyNodes::SleepNode::onStart
NodeStatus onStart() override
Definition: dummy_nodes.h:85
behavior_tree.h
DummyNodes::SaySomething
Definition: dummy_nodes.h:50
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:251
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
DummyNodes::SayHello
BT::NodeStatus SayHello()
Definition: dummy_nodes.cpp:25
DummyNodes::SleepNode
Definition: dummy_nodes.h:72
DummyNodes::SleepNode::SleepNode
SleepNode(const std::string &name, const BT::NodeConfiguration &config)
Definition: dummy_nodes.h:75
DummyNodes::SleepNode::onHalted
void onHalted() override
Definition: dummy_nodes.h:114
DummyNodes::SaySomething::SaySomething
SaySomething(const std::string &name, const BT::NodeConfiguration &config)
Definition: dummy_nodes.h:53
BT::BehaviorTreeFactory::registerSimpleCondition
void registerSimpleCondition(const std::string &ID, const SimpleConditionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleCondition help you register nodes of type SimpleConditionNode.
Definition: bt_factory.cpp:104
DummyNodes::SaySomething::providedPorts
static BT::PortsList providedPorts()
Definition: dummy_nodes.h:62
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
DummyNodes::CheckTemperature
BT::NodeStatus CheckTemperature()
Definition: dummy_nodes.cpp:19
BT::NodeStatus
NodeStatus
Definition: basic_types.h:35
BT::BehaviorTreeFactory::registerSimpleAction
void registerSimpleAction(const std::string &ID, const SimpleActionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleAction help you register nodes of type SimpleActionNode.
Definition: bt_factory.cpp:117
DummyNodes::GripperInterface::GripperInterface
GripperInterface()
Definition: dummy_nodes.h:20
BT::StatefulActionNode
The ActionNode is the prefered way to implement asynchronous Actions. It is actually easier to use co...
Definition: action_node.h:153


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19