Skip to content

Commit 2b14fbb

Browse files
committed
add simple example to generate logs
1 parent 1ec2b50 commit 2b14fbb

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ CompileExample("t11_replace_rules")
3030

3131
if(BTCPP_GROOT_INTERFACE AND BTCPP_SQLITE_LOGGING)
3232
CompileExample("t12_groot_howto")
33+
CompileExample("generate_log")
3334
endif()
3435

3536
CompileExample("ex01_wrap_legacy")

examples/generate_log.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "behaviortree_cpp/bt_factory.h"
2+
#include "behaviortree_cpp/loggers/groot2_publisher.h"
3+
#include "behaviortree_cpp/loggers/bt_file_logger_v2.h"
4+
#include "behaviortree_cpp/loggers/bt_sqlite_logger.h"
5+
6+
// clang-format on
7+
8+
int main(int argc, char** argv)
9+
{
10+
if(argc < 2 || argc > 3) {
11+
std::cout << "Provide a XML file as first argument. "
12+
"Second argument might be the name of the tree to instantiate." << std::endl;
13+
return 1;
14+
}
15+
const std::string file = argv[1];
16+
17+
BT::BehaviorTreeFactory factory;
18+
19+
BT::Tree tree;
20+
21+
if(argc == 3) {
22+
factory.registerBehaviorTreeFromFile(file);
23+
tree = factory.createTree(argv[2]);
24+
}
25+
else {
26+
tree = factory.createTreeFromFile(file);
27+
}
28+
29+
BT::Groot2Publisher publisher(tree);
30+
BT::FileLogger2 file_logger(tree, "./generated_log.btlog");
31+
BT::SqliteLogger sqlite_logger(tree, "./generated_log.db3");
32+
33+
// helper function to print the tree
34+
BT::printTreeRecursively(tree.rootNode());
35+
36+
std::cout << "\nTree will run indefinitively. Press CTRL-C to stop\n";
37+
38+
while(true) {
39+
tree.tickWhileRunning();
40+
}
41+
}

examples/test_files/log_test.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<root BTCPP_format="4">
3+
<BehaviorTree ID="Main">
4+
<Sequence>
5+
<SubTree ID="Subtree" name="FirstSubtree"/>
6+
<SubTree ID="Subtree" name="SecondSubtree"/>
7+
</Sequence>
8+
</BehaviorTree>
9+
<BehaviorTree ID="Subtree">
10+
<Sequence name="SleepSequence">
11+
<Sleep msec="300"/>
12+
<AlwaysSuccess/>
13+
</Sequence>
14+
</BehaviorTree>
15+
<!-- Description of Node Models (used by Groot) -->
16+
<TreeNodesModel>
17+
<Action ID="Sleep" editable="true">
18+
<input_port name="msec"/>
19+
</Action>
20+
</TreeNodesModel>
21+
</root>

0 commit comments

Comments
 (0)