Skip to content

Commit b0ffa24

Browse files
committed
fix groot2 publisher
1 parent 7b88aea commit b0ffa24

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

examples/t12_groot_howto.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,28 @@
1212
static const char* xml_text = R"(
1313
<root BTCPP_format="4">
1414
15-
<BehaviorTree ID="MainTree">
16-
<Sequence>
17-
<Fallback>
18-
<Inverter>
19-
<IsDoorClosed/>
20-
</Inverter>
21-
<SubTree ID="DoorClosed"/>
22-
</Fallback>
23-
<PassThroughDoor/>
24-
</Sequence>
25-
</BehaviorTree>
26-
27-
<BehaviorTree ID="DoorClosed">
28-
<Fallback>
29-
<OpenDoor/>
30-
<RetryUntilSuccessful num_attempts="5">
31-
<PickLock/>
32-
</RetryUntilSuccessful>
33-
<SmashDoor/>
34-
</Fallback>
35-
</BehaviorTree>
15+
<BehaviorTree ID="MainTree">
16+
<Sequence>
17+
<Script code="door_open:=false" />
18+
<Fallback>
19+
<Inverter>
20+
<IsDoorClosed/>
21+
</Inverter>
22+
<SubTree ID="DoorClosed" _autoremap="true" door_open="{door_open}"/>
23+
</Fallback>
24+
<PassThroughDoor/>
25+
</Sequence>
26+
</BehaviorTree>
27+
28+
<BehaviorTree ID="DoorClosed">
29+
<Fallback name="tryOpen" _onSuccess="door_open:=true">
30+
<OpenDoor/>
31+
<RetryUntilSuccessful num_attempts="5">
32+
<PickLock/>
33+
</RetryUntilSuccessful>
34+
<SmashDoor/>
35+
</Fallback>
36+
</BehaviorTree>
3637
3738
</root>
3839
)";
@@ -51,13 +52,14 @@ int main()
5152
// generated using this command and imported.
5253

5354
std::string xml_models = BT::writeTreeNodesModelXML(factory);
54-
std::cout << " ---------- XML file containing models ----------\n"
55-
<< xml_models
56-
<< "-------------------------------------------------\n";
5755

5856
factory.registerBehaviorTreeFromText(xml_text);
5957
auto tree = factory.createTree("MainTree");
6058

59+
std::cout << " ---------- XML file ----------\n"
60+
<< BT::WriteTreeToXML(tree, false)
61+
<< "--------------------------------\n";
62+
6163
// Connect the Groot2Publisher. This will allow Groot2 to
6264
// get the tree and poll status updates.
6365
BT::Groot2Publisher publisher(tree);

src/loggers/groot2_publisher.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ Groot2Publisher::Groot2Publisher(const BT::Tree& tree,
8989

9090
for(const auto& subtree: tree.subtrees)
9191
{
92-
subtrees_.insert( {subtree->instance_name, subtree} );
92+
auto name = subtree->instance_name.empty() ? subtree->tree_ID : subtree->instance_name;
93+
subtrees_.insert( {name, subtree} );
9394

9495
for(const auto& node: subtree->nodes)
9596
{

tests/gtest_subtree.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,32 @@ TEST(SubTree, SubtreePlusD)
254254
auto status = tree.tickWhileRunning();
255255
ASSERT_EQ(status, BT::NodeStatus::SUCCESS);
256256
}
257+
258+
// TODO: only explicit remapping work, autoremapping fails
259+
TEST(SubTree, ScriptRemap)
260+
{
261+
static const char* xml_text = R"(
262+
263+
<root BTCPP_format="4" >
264+
265+
<BehaviorTree ID="MainTree">
266+
<Sequence>
267+
<Script code = "value:=0" />
268+
<SubTree ID="mySubtree" value="{value}" />
269+
</Sequence>
270+
</BehaviorTree>
271+
272+
<BehaviorTree ID="mySubtree">
273+
<Script code = "value:=1" />
274+
</BehaviorTree>
275+
</root> )";
276+
277+
BT::BehaviorTreeFactory factory;
278+
factory.registerBehaviorTreeFromText(xml_text);
279+
280+
Tree tree = factory.createTree("MainTree");
281+
tree.tickOnce();
282+
283+
ASSERT_EQ(tree.subtrees[1]->blackboard->get<int>("value"), 1);
284+
ASSERT_EQ(tree.subtrees[0]->blackboard->get<int>("value"), 1);
285+
}

0 commit comments

Comments
 (0)