forked from BehaviorTree/BehaviorTree.CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht02_basic_ports.cpp
More file actions
114 lines (95 loc) · 3.34 KB
/
t02_basic_ports.cpp
File metadata and controls
114 lines (95 loc) · 3.34 KB
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
#include "dummy_nodes.h"
#include "behaviortree_cpp/bt_factory.h"
using namespace BT;
/** This tutorial will teach you how basic input/output ports work.
*
* Ports are a mechanism to exchange information between Nodes using
* a key/value storage called "Blackboard".
* The type and number of ports of a Node is statically defined.
*
* Input Ports are like "argument" of a functions.
* Output ports are, conceptually, like "return values".
*
* In this example, a Sequence of 5 Actions is executed:
*
* - Actions 1 and 4 read the input "message" from a static string.
*
* - Actions 3 and 5 read the input "message" from an entry in the
* blackboard called "the_answer".
*
* - Action 2 writes something into the entry of the blackboard
* called "the_answer".
*/
// clang-format off
static const char* xml_text = R"(
<root BTCPP_format="4" >
<BehaviorTree ID="MainTree">
<Sequence name="root">
<SaySomething message="hello" />
<SaySomething2 message="this works too" />
<ThinkWhatToSay text="{the_answer}"/>
<SaySomething2 message="{the_answer}" />
</Sequence>
</BehaviorTree>
</root>
)";
// clang-format on
class ThinkWhatToSay : public BT::SyncActionNode
{
public:
ThinkWhatToSay(const std::string& name, const BT::NodeConfig& config)
: BT::SyncActionNode(name, config)
{}
// This Action simply write a value in the port "text"
BT::NodeStatus tick() override
{
setOutput("text", "The answer is 42");
return BT::NodeStatus::SUCCESS;
}
// A node having ports MUST implement this STATIC method
static BT::PortsList providedPorts()
{
return { BT::OutputPort<std::string>("text") };
}
};
int main()
{
using namespace DummyNodes;
BehaviorTreeFactory factory;
// The class SaySomething has a method called providedPorts() that define the INPUTS.
// In this case, it requires an input called "message"
factory.registerNodeType<SaySomething>("SaySomething");
// Similarly to SaySomething, ThinkWhatToSay has an OUTPUT port called "text"
// Both these ports are std::string, therefore they can connect to each other
factory.registerNodeType<ThinkWhatToSay>("ThinkWhatToSay");
// SimpleActionNodes can not define their own method providedPorts(), therefore
// we have to pass the PortsList explicitly if we want the Action to use getInput()
// or setOutput();
PortsList say_something_ports = { InputPort<std::string>("message") };
factory.registerSimpleAction("SaySomething2", SaySomethingSimple, say_something_ports);
/* An INPUT can be either a string, for instance:
*
* <SaySomething message="hello" />
*
* or contain a "pointer" to a type erased entry in the Blackboard,
* using this syntax: {name_of_entry}. Example:
*
* <SaySomething message="{the_answer}" />
*/
auto tree = factory.createTreeFromText(xml_text);
tree.tickWhileRunning();
/* Expected output:
*
Robot says: hello
Robot says: this works too
Robot says: The answer is 42
*
* The way we "connect" output ports to input ports is to "point" to the same
* Blackboard entry.
*
* This means that ThinkSomething will write into the entry with key "the_answer";
* SaySomething and SaySomething will read the message from the same entry.
*
*/
return 0;
}