forked from tka-andrew/learning_BehaviourTreeCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial05.cpp
More file actions
89 lines (74 loc) · 2.98 KB
/
Copy pathtutorial05.cpp
File metadata and controls
89 lines (74 loc) · 2.98 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
#include "tutorial05_nodes.h"
#include "behaviortree_cpp_v3/bt_factory.h"
#include "behaviortree_cpp_v3/behavior_tree.h"
#include "behaviortree_cpp_v3/loggers/bt_cout_logger.h"
#include "behaviortree_cpp_v3/loggers/bt_minitrace_logger.h"
#include "behaviortree_cpp_v3/loggers/bt_file_logger.h"
#ifdef ZMQ_FOUND
#include "behaviortree_cpp_v3/loggers/bt_zmq_publisher.h"
#endif
static const char *xml_text = R"(
<root main_tree_to_execute = "MainTree">
<BehaviorTree ID="DoorClosed">
<Sequence name="door_closed_sequence">
<Inverter>
<IsDoorOpen/>
</Inverter>
<RetryUntilSuccessful num_attempts="4">
<OpenDoor/>
</RetryUntilSuccessful>
<PassThroughDoor/>
</Sequence>
</BehaviorTree>
<BehaviorTree ID="MainTree">
<Fallback name="root_Fallback">
<Sequence name="door_open_sequence">
<IsDoorOpen/>
<PassThroughDoor/>
</Sequence>
<SubTree ID="DoorClosed"/>
<PassThroughWindow/>
</Fallback>
</BehaviorTree>
</root>
)";
int main()
{
BT::BehaviorTreeFactory factory;
// register all the actions into the factory
// We don't show how these actions are implemented, since most of the
// times they just print a message on screen and return SUCCESS.
// See the code on Github for more details.
factory.registerSimpleCondition("IsDoorOpen", std::bind(T05DummyNodes::IsDoorOpen));
factory.registerSimpleAction("PassThroughDoor", std::bind(T05DummyNodes::PassThroughDoor));
factory.registerSimpleAction("PassThroughWindow", std::bind(T05DummyNodes::PassThroughWindow));
factory.registerSimpleAction("OpenDoor", std::bind(T05DummyNodes::OpenDoor));
factory.registerSimpleAction("CloseDoor", std::bind(T05DummyNodes::CloseDoor));
factory.registerSimpleCondition("IsDoorLocked", std::bind(T05DummyNodes::IsDoorLocked));
factory.registerSimpleAction("UnlockDoor", std::bind(T05DummyNodes::UnlockDoor));
// Load from text or file...
auto tree = factory.createTreeFromText(xml_text);
// This logger prints state changes on console
BT::StdCoutLogger logger_cout(tree);
// This logger saves state changes on file
BT::FileLogger logger_file(tree, "bt_trace.fbl");
// This logger stores the execution time of each node
BT::MinitraceLogger logger_minitrace(tree, "bt_trace.json");
#ifdef ZMQ_FOUND
// This logger publish status changes using ZeroMQ. Used by Groot
BT::PublisherZMQ publisher_zmq(tree);
#endif
printTreeRecursively(tree.rootNode());
//while (1)
{
BT::NodeStatus status = BT::NodeStatus::RUNNING;
// Keep on ticking until you get either a SUCCESS or FAILURE state
while (status == BT::NodeStatus::RUNNING)
{
status = tree.tickRoot();
T05DummyNodes::SleepMS(1); // optional sleep to avoid "busy loops"
}
T05DummyNodes::SleepMS(2000);
}
return 0;
}