forked from BehaviorTree/btcpp_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop_sample.cpp
More file actions
163 lines (146 loc) · 4.38 KB
/
loop_sample.cpp
File metadata and controls
163 lines (146 loc) · 4.38 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "behaviortree_cpp/bt_factory.h"
#include "behaviortree_cpp/decorators/loop_node.h"
#include <list>
using namespace BT;
static const char* xml_tree = R"(
<root BTCPP_format="4">
<BehaviorTree ID="Untitled">
<Repeat num_cycles="3">
<Sequence>
<Fallback>
<isPointSelected/>
<SelectPoints points="{points}" selected="{selected}"/>
</Fallback>
<LoopPoints if_empty="SUCCESS"
queue="{points}"
value="{point}">
<TellMeWhichPoint point="{point}"/>
</LoopPoints>
<SaySomething message="I am done."/>
</Sequence>
</Repeat>
</BehaviorTree>
</root>
)";
struct Point
{
double x, y, z;
};
namespace BT
{
template<>
Point convertFromString(StringView key)
{
auto parts = splitString(key, ';');
if (parts.size() != 3)
{
throw RuntimeError("invalid input");
}
else
{
Point output;
output.x = convertFromString<double>(parts[0]);
output.y = convertFromString<double>(parts[0]);
output.z = convertFromString<double>(parts[0]);
return output;
}
}
}
class SelectPoints : public SyncActionNode
{
public:
SelectPoints(const std::string& name, const NodeConfiguration& config)
: SyncActionNode(name, config)
{}
NodeStatus tick() override
{
auto shared_queue = std::make_shared<std::deque<Point>>();
for (int i = 0; i < 10; i++)
{
shared_queue->push_back(Point{double(i), double(i+1), double(i+2)});
// Point p = (*shared_queue)[i];
// std::cout << "Point: " << p.x << ", " << p.y << ", " << p.z << std::endl;
}
setOutput("points", shared_queue);
setOutput("selected", true);
std::cout << "Selected points." << std::endl;
return NodeStatus::SUCCESS;
}
static PortsList providedPorts()
{
return {OutputPort<SharedQueue<Point>>("points"),
OutputPort<bool>("selected")};
}
};
class SaySomething : public SyncActionNode
{
public:
SaySomething(const std::string& name, const NodeConfiguration& config)
: SyncActionNode(name, config)
{}
NodeStatus tick() override
{
std::string msg;
getInput("message", msg);
std::cout << msg << std::endl;
return NodeStatus::SUCCESS;
}
static BT::PortsList providedPorts()
{
return {BT::InputPort<std::string>("message")};
}
};
class TellMeWhichPoint : public SyncActionNode
{
public:
TellMeWhichPoint(const std::string& name, const NodeConfiguration& config)
: SyncActionNode(name, config)
{}
NodeStatus tick() override
{
Point p;
getInput("point", p);
std::cout << "Point: " << p.x << ", " << p.y << ", " << p.z << std::endl;
return NodeStatus::SUCCESS;
}
static BT::PortsList providedPorts()
{
return {BT::InputPort<Point>("point")};
}
};
// NodeStatus isPointSelected(TreeNode& self)
// {
// auto p = std::make_shared<std::deque<Point>>();
// auto res = self.getInput<bool>("selected");
// if (!res)
// {
// std::cout << "Points are not selected." << std::endl;
// return NodeStatus::FAILURE;
// }
// std::cout << "Points are selected." << std::endl;
// return NodeStatus::SUCCESS;
// }
int main()
{
BehaviorTreeFactory factory;
// factory.registerNodeType<LoopNode<Point>>("LoopPoints");
// factory.registerSimpleCondition("isPointSelected", std::bind(isPointSelected, std::placeholders::_1));
factory.registerSimpleCondition("isPointSelected", [](BT::TreeNode& self) {
// auto p = std::make_shared<std::deque<Point>>();
auto res = self.getInput<bool>("selected");
if (!res)
{
std::cout << "Points are not selected." << std::endl;
return NodeStatus::FAILURE;
}
std::cout << "Points are selected." << std::endl;
return NodeStatus::SUCCESS;
});
factory.registerNodeType<LoopNode<Point>>("LoopPoints");
factory.registerNodeType<SelectPoints>("SelectPoints");
factory.registerNodeType<TellMeWhichPoint>("TellMeWhichPoint");
factory.registerNodeType<SaySomething>("SaySomething");
auto tree = factory.createTreeFromText(xml_tree);
tree.tickWhileRunning();
return 0;
}