Skip to content

Commit f005384

Browse files
author
Davide Faconti
committed
Changes in the Loggers, motivated by the new tutorial_sequence_star.cpp
1 parent df2b878 commit f005384

File tree

11 files changed

+270
-199
lines changed

11 files changed

+270
-199
lines changed

CMakeLists.txt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ set(BT_Source
6161
src/behavior_tree.cpp
6262

6363
src/xml_parsing.cpp
64-
src/bt_file_logger.cpp
65-
3rdparty/tinyXML2/tinyxml2.cpp
6664

65+
src/loggers/bt_cout_logger.cpp
66+
src/loggers/bt_file_logger.cpp
67+
src/loggers/bt_minitrace_logger.cpp
68+
69+
3rdparty/tinyXML2/tinyxml2.cpp
6770
3rdparty/minitrace/minitrace.cpp
6871
)
6972
include_directories(include 3rdparty/)
@@ -75,7 +78,7 @@ find_package(ZMQ)
7578
if( ZMQ_FOUND )
7679
message(STATUS "ZeroMQ found.")
7780
add_definitions( -DZMQ_FOUND )
78-
set(BT_Source ${BT_Source} src/bt_zmq_publisher.cpp )
81+
set(BT_Source ${BT_Source} src/loggers/bt_zmq_publisher.cpp )
7982

8083
set(BEHAVIOR_TRE_LIBRARIES behavior_tree_core zmq)
8184
else()
@@ -133,18 +136,16 @@ if( ZMQ_FOUND )
133136
target_link_libraries(bt_recorder ${BEHAVIOR_TRE_LIBRARIES} )
134137
endif()
135138

136-
add_executable(simple_example examples/simple_example.cpp )
137-
target_link_libraries(simple_example ${BEHAVIOR_TRE_LIBRARIES} )
138-
139-
140-
add_executable(tutorial_blackboard examples/tutorial_blackboard.cpp )
141-
target_link_libraries(tutorial_blackboard ${BEHAVIOR_TRE_LIBRARIES} )
139+
function(BuildExample filename)
140+
add_executable(${filename} examples/${filename}.cpp )
141+
target_link_libraries(${filename} ${BEHAVIOR_TRE_LIBRARIES} )
142+
endfunction()
142143

143-
add_executable(tutorial_programmatic_tree examples/tutorial_programmatic_tree.cpp )
144-
target_link_libraries(tutorial_programmatic_tree ${BEHAVIOR_TRE_LIBRARIES} )
144+
BuildExample(tutorial_programmatic_tree)
145+
BuildExample(tutorial_factory_tree)
146+
BuildExample(tutorial_sequence_star)
147+
BuildExample(tutorial_blackboard)
145148

146-
add_executable(tutorial_factory_tree examples/tutorial_factory_tree.cpp )
147-
target_link_libraries(tutorial_factory_tree ${BEHAVIOR_TRE_LIBRARIES} )
148149

149150
######################################################
150151
# INSTALLATION OF LIBRARY AND EXECUTABLE

examples/simple_example.cpp

Lines changed: 0 additions & 109 deletions
This file was deleted.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include "behavior_tree_core/xml_parsing.h"
2+
#include "behavior_tree_logger/bt_cout_logger.h"
3+
#include "behavior_tree_logger/bt_file_logger.h"
4+
5+
using namespace BT;
6+
7+
8+
NodeStatus checkBattery()
9+
{
10+
std::cout << "[ Battery: OK ]" << std::endl;
11+
return NodeStatus::SUCCESS;
12+
}
13+
14+
NodeStatus CheckTemperature()
15+
{
16+
std::cout << "[ Temperature: OK ]" << std::endl;
17+
return NodeStatus::SUCCESS;
18+
}
19+
20+
// This is an asynchronous operation that will run in a separate thread
21+
class MoveAction: public ActionNode
22+
{
23+
public:
24+
MoveAction(const std::string& name): ActionNode(name) {}
25+
NodeStatus tick() override
26+
{
27+
std::cout << "[ Move: started ]" << std::endl;
28+
std::this_thread::sleep_for( std::chrono::milliseconds(80) );
29+
std::cout << "[ Move: finished ]" << std::endl;
30+
return NodeStatus::SUCCESS;
31+
}
32+
};
33+
34+
// clang-format off
35+
36+
const std::string xml_text_sequence = R"(
37+
38+
<root main_tree_to_execute = "MainTree" >
39+
40+
<BehaviorTree ID="MainTree">
41+
<Sequence name="root">
42+
<BatteryOK/>
43+
<TemperatureOK />
44+
<Move/>
45+
</Sequence>
46+
</BehaviorTree>
47+
48+
</root>
49+
)";
50+
51+
const std::string xml_text_sequence_star = R"(
52+
53+
<root main_tree_to_execute = "MainTree" >
54+
55+
<BehaviorTree ID="MainTree">
56+
<SequenceStar name="root">
57+
<BatteryOK/>
58+
<TemperatureOK />
59+
<Move/>
60+
</SequenceStar>
61+
</BehaviorTree>
62+
63+
</root>
64+
)";
65+
66+
// clang-format on
67+
68+
void Assert(bool condition)
69+
{
70+
if( !condition ) throw std::runtime_error("this is not what I expected");
71+
}
72+
73+
int main()
74+
{
75+
BehaviorTreeFactory factory;
76+
factory.registerSimpleCondition("TemperatureOK", std::bind( checkBattery ));
77+
factory.registerSimpleCondition("BatteryOK", std::bind( CheckTemperature ));
78+
factory.registerNodeType<MoveAction>("Move");
79+
80+
// Loog at the state transitions and messages using either
81+
// xml_text_sequence and xml_text_sequence_star
82+
83+
// The main difference that you should notice is that the
84+
// actions BatteryOK and TempearaturOK are executed at each tick()
85+
// is Sequence is used and only once if SequenceStar is used.
86+
87+
for(auto& xml_text: {xml_text_sequence, xml_text_sequence_star})
88+
{
89+
std::cout << "\n------------ BUILDING A NEW TREE ------------\n\n" << std::endl;
90+
91+
auto tree = buildTreeFromText(factory, xml_text);
92+
TreeNode::Ptr root_node = tree.first;
93+
94+
// This logger will show all the state transitions on console
95+
StdCoutLogger logger_cout(root_node.get());
96+
97+
// This other logger will save the state transition in a custom file format
98+
// simple_trace.fbl can be visualized using the command line tool [bt_log_cat]
99+
FileLogger file_file(root_node.get(), "simple_trace.fbl", 32);
100+
101+
NodeStatus status;
102+
103+
std::cout << "\n------- First executeTick() --------" << std::endl;
104+
status = root_node->executeTick();
105+
Assert( status == NodeStatus::RUNNING);
106+
107+
std::cout << "\n------- sleep --------" << std::endl;
108+
std::this_thread::sleep_for(std::chrono::milliseconds(50));
109+
110+
std::cout << "\n------- Second executeTick() --------" << std::endl;
111+
status = root_node->executeTick();
112+
Assert( status == NodeStatus::RUNNING);
113+
114+
std::cout << "\n------- sleep --------" << std::endl;
115+
std::this_thread::sleep_for(std::chrono::milliseconds(50));
116+
117+
std::cout << "\n------- Third executeTick() --------" << std::endl;
118+
status = root_node->executeTick();
119+
Assert( status == NodeStatus::SUCCESS);
120+
121+
std::cout << std::endl;
122+
123+
// The main difference that you should notice is that the
124+
// actions BatteryOK and TempearaturOK are executed at each tick()
125+
// is Sequence is used and only once if SequenceStar is used.
126+
}
127+
return 0;
128+
}

gtest/crossdoor_example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const std::string xml_text = R"(
4343

4444
// clang-format on
4545

46-
int main(int argc, char** argv)
46+
int main()
4747
{
4848
using namespace BT;
4949

include/behavior_tree_logger/bt_cout_logger.h

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,18 @@ namespace BT
1818

1919
class StdCoutLogger : public StatusChangeLogger
2020
{
21+
static std::atomic<bool> ref_count;
22+
2123
public:
22-
StdCoutLogger(TreeNode* root_node) : StatusChangeLogger(root_node)
23-
{
24-
static bool first_instance = true;
25-
if (first_instance)
26-
{
27-
first_instance = false;
28-
}
29-
else
30-
{
31-
throw std::logic_error("Only one instance of StdCoutLogger shall be created");
32-
}
33-
}
34-
35-
virtual void callback(TimePoint timestamp, const TreeNode& node, NodeStatus prev_status, NodeStatus status) override
36-
{
37-
using namespace std::chrono;
38-
39-
constexpr const char* whitespaces = " ";
40-
constexpr const size_t ws_count = 25;
41-
42-
double since_epoch = duration<double>(timestamp.time_since_epoch()).count();
43-
printf("[%.3f]: %s%s %s -> %s\n", since_epoch, node.name().c_str(),
44-
&whitespaces[std::min(ws_count, node.name().size())], toStr(prev_status, true), toStr(status, true));
45-
}
46-
47-
virtual void flush() override
48-
{
49-
std::cout << std::flush;
50-
}
24+
StdCoutLogger(TreeNode* root_node);
25+
~StdCoutLogger();
26+
27+
virtual void callback(TimePoint timestamp,
28+
const TreeNode& node,
29+
NodeStatus prev_status,
30+
NodeStatus status) override;
31+
32+
virtual void flush() override;
5133
};
5234

5335
} // end namespace

0 commit comments

Comments
 (0)