|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <behaviortree_cpp/bt_factory.h> |
| 4 | +#include <iostream> |
| 5 | +#include <memory> |
| 6 | + |
| 7 | +// interface |
| 8 | +struct IMotor |
| 9 | +{ |
| 10 | + virtual void doMove() = 0; |
| 11 | +}; |
| 12 | + |
| 13 | +// implementation |
| 14 | +struct LinearMotor : public IMotor |
| 15 | +{ |
| 16 | + void doMove() override |
| 17 | + { |
| 18 | + std::cout << ">> doMove" << std::endl; |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +static const char* xml_text = R"( |
| 23 | +<root BTCPP_format="4"> |
| 24 | + <BehaviorTree ID="MainTree"> |
| 25 | + <Sequence name="root_sequence"> |
| 26 | + <PathFollow/> |
| 27 | + </Sequence> |
| 28 | + </BehaviorTree> |
| 29 | +</root> |
| 30 | +)"; |
| 31 | + |
| 32 | +// node using interface |
| 33 | +class PathFollow : public BT::StatefulActionNode |
| 34 | +{ |
| 35 | +public: |
| 36 | + PathFollow(const std::string& name, const BT::NodeConfig& config, IMotor& motor) |
| 37 | + : BT::StatefulActionNode(name, config), imotor_(motor) |
| 38 | + {} |
| 39 | + static BT::PortsList providedPorts() |
| 40 | + { |
| 41 | + return {}; |
| 42 | + } |
| 43 | + BT::NodeStatus onStart() override |
| 44 | + { |
| 45 | + std::cout << "onStart" << std::endl; |
| 46 | + imotor_.doMove(); |
| 47 | + return BT::NodeStatus::RUNNING; |
| 48 | + } |
| 49 | + BT::NodeStatus onRunning() override |
| 50 | + { |
| 51 | + std::cout << "onRunning" << std::endl; |
| 52 | + imotor_.doMove(); |
| 53 | + return BT::NodeStatus::SUCCESS; |
| 54 | + } |
| 55 | + void onHalted() override |
| 56 | + {} |
| 57 | + |
| 58 | +private: |
| 59 | + IMotor& imotor_; |
| 60 | +}; |
| 61 | + |
| 62 | +TEST(Factory, VirtualInterface_Issue_945) |
| 63 | +{ |
| 64 | + LinearMotor motor; |
| 65 | + BT::BehaviorTreeFactory factory; |
| 66 | + factory.registerNodeType<PathFollow>("PathFollow", std::ref(motor)); |
| 67 | + |
| 68 | + auto tree = factory.createTreeFromText(xml_text); |
| 69 | + tree.tickWhileRunning(); |
| 70 | +} |
0 commit comments