|
| 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 | +} |
0 commit comments