forked from BehaviorTree/BehaviorTree.CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dummy_nodes.h
139 lines (113 loc) · 3.49 KB
/
dummy_nodes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef SIMPLE_BT_NODES_H
#define SIMPLE_BT_NODES_H
#include "behaviortree_cpp_v3/behavior_tree.h"
#include "behaviortree_cpp_v3/bt_factory.h"
namespace DummyNodes
{
using BT::NodeStatus;
NodeStatus CheckBattery();
NodeStatus CheckTemperature();
NodeStatus SayHello();
class GripperInterface
{
public:
GripperInterface() : _opened(true)
{
}
NodeStatus open();
NodeStatus close();
private:
bool _opened;
};
//--------------------------------------
// Example of custom SyncActionNode (synchronous action)
// without ports.
class ApproachObject : public BT::SyncActionNode
{
public:
ApproachObject(const std::string& name) :
BT::SyncActionNode(name, {})
{
}
// You must override the virtual function tick()
NodeStatus tick() override;
};
// Example of custom SyncActionNode (synchronous action)
// with an input port.
class SaySomething : public BT::SyncActionNode
{
public:
SaySomething(const std::string& name, const BT::NodeConfiguration& config)
: BT::SyncActionNode(name, config)
{
}
// You must override the virtual function tick()
NodeStatus tick() override;
// It is mandatory to define this static method.
static BT::PortsList providedPorts()
{
return{ BT::InputPort<std::string>("message") };
}
};
//Same as class SaySomething, but to be registered with SimpleActionNode
NodeStatus SaySomethingSimple(BT::TreeNode& self);
// Example os Asynchronous node that use StatefulActionNode as base class
class SleepNode : public BT::StatefulActionNode
{
public:
SleepNode(const std::string& name, const BT::NodeConfiguration& config)
: BT::StatefulActionNode(name, config)
{}
static BT::PortsList providedPorts()
{
// amount of milliseconds that we want to sleep
return{ BT::InputPort<int>("msec") };
}
NodeStatus onStart() override
{
int msec = 0;
getInput("msec", msec);
if( msec <= 0 )
{
// no need to go into the RUNNING state
return NodeStatus::SUCCESS;
}
else {
using namespace std::chrono;
// once the deadline is reached, we will return SUCCESS.
deadline_ = system_clock::now() + milliseconds(msec);
return NodeStatus::RUNNING;
}
}
/// method invoked by an action in the RUNNING state.
NodeStatus onRunning() override
{
if ( std::chrono::system_clock::now() >= deadline_ )
{
return NodeStatus::SUCCESS;
}
else {
return NodeStatus::RUNNING;
}
}
void onHalted() override
{
// nothing to do here...
std::cout << "SleepNode interrupted" << std::endl;
}
private:
std::chrono::system_clock::time_point deadline_;
};
inline void RegisterNodes(BT::BehaviorTreeFactory& factory)
{
static GripperInterface grip_singleton;
factory.registerSimpleCondition("CheckBattery", std::bind(CheckBattery));
factory.registerSimpleCondition("CheckTemperature", std::bind(CheckTemperature));
factory.registerSimpleAction("SayHello", std::bind(SayHello));
factory.registerSimpleAction("OpenGripper", std::bind(&GripperInterface::open, &grip_singleton));
factory.registerSimpleAction("CloseGripper", std::bind(&GripperInterface::close, &grip_singleton));
factory.registerNodeType<ApproachObject>("ApproachObject");
factory.registerNodeType<SaySomething>("SaySomething");
}
} // end namespace
#endif // SIMPLE_BT_NODES_H