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)
54  : BT::SyncActionNode(name, 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)
76  : BT::StatefulActionNode(name, 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
static BT::PortsList providedPorts()
Definition: dummy_nodes.h:79
std::chrono::system_clock::time_point deadline_
Definition: dummy_nodes.h:121
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:364
static BT::PortsList providedPorts()
Definition: dummy_nodes.h:62
BT::NodeStatus SaySomethingSimple(BT::TreeNode &self)
Definition: dummy_nodes.cpp:63
NodeStatus onRunning() override
method invoked by an action in the RUNNING state.
Definition: dummy_nodes.h:103
BT::NodeStatus SayHello()
Definition: dummy_nodes.cpp:25
The ActionNode is the prefered way to implement asynchronous Actions. It is actually easier to use co...
Definition: action_node.h:153
void RegisterNodes(BT::BehaviorTreeFactory &factory)
Definition: dummy_nodes.h:124
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn&#39;t require a...
Definition: action_node.h:52
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:251
NodeStatus onStart() override
Definition: dummy_nodes.h:85
ApproachObject(const std::string &name)
Definition: dummy_nodes.h:39
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
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:55
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:333
SleepNode(const std::string &name, const BT::NodeConfiguration &config)
Definition: dummy_nodes.h:75
BT::NodeStatus CheckBattery()
Definition: dummy_nodes.cpp:13
NodeStatus
Definition: basic_types.h:35
void onHalted() override
Definition: dummy_nodes.h:114
BT::NodeStatus CheckTemperature()
Definition: dummy_nodes.cpp:19
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
SaySomething(const std::string &name, const BT::NodeConfiguration &config)
Definition: dummy_nodes.h:53


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14