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  NodeStatus open();
24 
25  NodeStatus close();
26 
27 private:
28  bool _opened;
29 };
30 
31 //--------------------------------------
32 
33 // Example of custom SyncActionNode (synchronous action)
34 // without ports.
36 {
37 public:
38  ApproachObject(const std::string& name) : BT::SyncActionNode(name, {})
39  {}
40 
41  // You must override the virtual function tick()
42  NodeStatus tick() override;
43 };
44 
45 // Example of custom SyncActionNode (synchronous action)
46 // with an input port.
48 {
49 public:
50  SaySomething(const std::string& name, const BT::NodeConfig& config)
52  {}
53 
54  // You must override the virtual function tick()
55  NodeStatus tick() override;
56 
57  // It is mandatory to define this static method.
59  {
60  return { BT::InputPort<std::string>("message") };
61  }
62 };
63 
64 //Same as class SaySomething, but to be registered with SimpleActionNode
66 
67 // Example os Asynchronous node that use StatefulActionNode as base class
69 {
70 public:
71  SleepNode(const std::string& name, const BT::NodeConfig& config)
73  {}
74 
76  {
77  // amount of milliseconds that we want to sleep
78  return { BT::InputPort<int>("msec") };
79  }
80 
81  NodeStatus onStart() override
82  {
83  int msec = 0;
84  getInput("msec", msec);
85  if(msec <= 0)
86  {
87  // no need to go into the RUNNING state
88  return NodeStatus::SUCCESS;
89  }
90  else
91  {
92  using namespace std::chrono;
93  // once the deadline is reached, we will return SUCCESS.
94  deadline_ = system_clock::now() + milliseconds(msec);
95  return NodeStatus::RUNNING;
96  }
97  }
98 
101  {
102  if(std::chrono::system_clock::now() >= deadline_)
103  {
104  return NodeStatus::SUCCESS;
105  }
106  else
107  {
108  return NodeStatus::RUNNING;
109  }
110  }
111 
112  void onHalted() override
113  {
114  // nothing to do here...
115  std::cout << "SleepNode interrupted" << std::endl;
116  }
117 
118 private:
119  std::chrono::system_clock::time_point deadline_;
120 };
121 
123 {
124  static GripperInterface grip_singleton;
125 
126  factory.registerSimpleCondition("CheckBattery", std::bind(CheckBattery));
127  factory.registerSimpleCondition("CheckTemperature", std::bind(CheckTemperature));
128  factory.registerSimpleAction("SayHello", std::bind(SayHello));
129  factory.registerSimpleAction("OpenGripper",
130  std::bind(&GripperInterface::open, &grip_singleton));
131  factory.registerSimpleAction("CloseGripper",
132  std::bind(&GripperInterface::close, &grip_singleton));
133  factory.registerNodeType<ApproachObject>("ApproachObject");
134  factory.registerNodeType<SaySomething>("SaySomething");
135 }
136 
137 } // namespace DummyNodes
138 
139 #endif // SIMPLE_BT_NODES_H
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:547
BT
Definition: ex01_wrap_legacy.cpp:29
DummyNodes::SleepNode::providedPorts
static BT::PortsList providedPorts()
Definition: dummy_nodes.h:75
DummyNodes::GripperInterface::open
NodeStatus open()
Definition: dummy_nodes.cpp:31
DummyNodes::ApproachObject::ApproachObject
ApproachObject(const std::string &name)
Definition: dummy_nodes.h:38
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
DummyNodes::SleepNode::deadline_
std::chrono::system_clock::time_point deadline_
Definition: dummy_nodes.h:119
BT::StatefulActionNode::StatefulActionNode
StatefulActionNode(const std::string &name, const NodeConfig &config)
Definition: action_node.h:162
DummyNodes::SleepNode::onRunning
NodeStatus onRunning() override
method invoked by an action in the RUNNING state.
Definition: dummy_nodes.h:100
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
DummyNodes::CheckBattery
BT::NodeStatus CheckBattery()
Definition: dummy_nodes.cpp:13
bt_factory.h
DummyNodes::SaySomethingSimple
BT::NodeStatus SaySomethingSimple(BT::TreeNode &self)
Definition: dummy_nodes.cpp:63
DummyNodes
Definition: dummy_nodes.cpp:10
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
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
lexy::bind
constexpr auto bind(Callback &&callback, BoundArgs &&... args)
Binds the operator() of the callback with pre-defined/remapped values.
Definition: bind.hpp:321
DummyNodes::ApproachObject
Definition: dummy_nodes.h:35
DummyNodes::SleepNode::SleepNode
SleepNode(const std::string &name, const BT::NodeConfig &config)
Definition: dummy_nodes.h:71
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:326
DummyNodes::GripperInterface
Definition: dummy_nodes.h:17
DummyNodes::RegisterNodes
void RegisterNodes(BT::BehaviorTreeFactory &factory)
Definition: dummy_nodes.h:122
BT::SyncActionNode::SyncActionNode
SyncActionNode(const std::string &name, const NodeConfig &config)
Definition: action_node.cpp:52
DummyNodes::GripperInterface::close
NodeStatus close()
Definition: dummy_nodes.cpp:38
DummyNodes::GripperInterface::_opened
bool _opened
Definition: dummy_nodes.h:28
DummyNodes::SleepNode::onStart
NodeStatus onStart() override
Definition: dummy_nodes.h:81
behavior_tree.h
DummyNodes::SaySomething
Definition: dummy_nodes.h:47
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:209
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
DummyNodes::SayHello
BT::NodeStatus SayHello()
Definition: dummy_nodes.cpp:25
DummyNodes::SleepNode
Definition: dummy_nodes.h:68
DummyNodes::SleepNode::onHalted
void onHalted() override
Definition: dummy_nodes.h:112
BT::NodeConfig
Definition: tree_node.h:73
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:153
DummyNodes::SaySomething::providedPorts
static BT::PortsList providedPorts()
Definition: dummy_nodes.h:58
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:33
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:166
DummyNodes::GripperInterface::GripperInterface
GripperInterface()
Definition: dummy_nodes.h:20
BT::StatefulActionNode
The StatefulActionNode is the preferred way to implement asynchronous Actions. It is actually easier ...
Definition: action_node.h:159
DummyNodes::SaySomething::SaySomething
SaySomething(const std::string &name, const BT::NodeConfig &config)
Definition: dummy_nodes.h:50


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16