|
1 |
| -#include <behaviortree_cpp/behavior_tree.h> |
2 |
| -#include <memory> |
3 |
| -#include <pybind11/chrono.h> |
4 |
| -#include <pybind11/functional.h> |
5 |
| -#include <pybind11/pybind11.h> |
6 |
| -#include <pybind11/stl.h> |
7 |
| - |
8 | 1 | #include <behaviortree_cpp/actions/always_success_node.h>
|
9 | 2 | #include <behaviortree_cpp/basic_types.h>
|
| 3 | +#include <behaviortree_cpp/behavior_tree.h> |
10 | 4 | #include <behaviortree_cpp/bt_factory.h>
|
11 | 5 | #include <behaviortree_cpp/controls/fallback_node.h>
|
12 | 6 | #include <behaviortree_cpp/controls/parallel_node.h>
|
|
16 | 10 | #include <behaviortree_cpp/loggers/bt_cout_logger.h>
|
17 | 11 | #include <behaviortree_cpp/loggers/groot2_publisher.h>
|
18 | 12 | #include <behaviortree_cpp/tree_node.h>
|
| 13 | +#include <fmt/args.h> |
| 14 | +#include <pybind11/chrono.h> |
| 15 | +#include <pybind11/functional.h> |
| 16 | +#include <pybind11/pybind11.h> |
| 17 | +#include <pybind11/stl.h> |
| 18 | + |
19 | 19 | #include <behaviortree_py/behaviortree_py.hpp>
|
| 20 | +#include <memory> |
| 21 | +#include <utility> |
20 | 22 |
|
21 | 23 | namespace py = pybind11;
|
22 |
| -using namespace BT; |
23 | 24 |
|
24 | 25 | PYBIND11_MODULE(behaviortree_py, m) {
|
25 | 26 | m.doc() = "Python wrapper for BehaviorTree.CPP";
|
26 | 27 |
|
27 |
| - py::register_exception<nonstd::bad_expected_access<std::string>>( |
28 |
| - m, "BadExpectedAccess"); |
| 28 | + py::register_exception<nonstd::bad_expected_access<std::string>>(m, "BadExpectedAccess"); |
29 | 29 |
|
30 |
| - py::enum_<NodeStatus>(m, "NodeStatus") |
31 |
| - .value("SUCCESS", NodeStatus::SUCCESS) |
32 |
| - .value("FAILURE", NodeStatus::FAILURE) |
33 |
| - .value("RUNNING", NodeStatus::RUNNING) |
34 |
| - .value("IDLE", NodeStatus::IDLE) |
| 30 | + py::enum_<BT::NodeStatus>(m, "NodeStatus") |
| 31 | + .value("SUCCESS", BT::NodeStatus::SUCCESS) |
| 32 | + .value("FAILURE", BT::NodeStatus::FAILURE) |
| 33 | + .value("RUNNING", BT::NodeStatus::RUNNING) |
| 34 | + .value("IDLE", BT::NodeStatus::IDLE) |
35 | 35 | .export_values();
|
36 | 36 |
|
37 |
| - py::class_<BT::Result>(m, "Result") |
38 |
| - .def("__bool__", &BT::Result::operator bool) |
39 |
| - .def("error", [](BT::Result *self) { return self->error(); }); |
| 37 | + py::class_<BT::Result>(m, "Result").def("__bool__", &BT::Result::operator bool).def("error", [](BT::Result* self) { |
| 38 | + return self->error(); |
| 39 | + }); |
40 | 40 |
|
41 |
| - py::class_<Blackboard, std::shared_ptr<Blackboard>>(m, "Blackboard") |
42 |
| - .def_static("create", &Blackboard::create); |
| 41 | + py::class_<BT::Blackboard, std::shared_ptr<BT::Blackboard>>(m, "Blackboard") |
| 42 | + .def_static("create", &BT::Blackboard::create); |
43 | 43 |
|
44 |
| - py::class_<Tree>(m, "Tree") |
45 |
| - .def("root_node", &Tree::rootNode, |
46 |
| - py::return_value_policy::reference_internal) |
47 |
| - .def("tick_while_running", &Tree::tickWhileRunning, |
48 |
| - py::arg("sleep_time") = std::chrono::milliseconds(10)); |
| 44 | + py::class_<BT::Tree>(m, "Tree") |
| 45 | + .def("root_node", &BT::Tree::rootNode, py::return_value_policy::reference_internal) |
| 46 | + .def("tick_while_running", &BT::Tree::tickWhileRunning, py::arg("sleep_time") = std::chrono::milliseconds(10)); |
49 | 47 |
|
50 |
| - py::class_<TreeNode, std::shared_ptr<TreeNode>>(m, "TreeNode") |
51 |
| - .def("name", &TreeNode::name) |
52 |
| - .def("status", &TreeNode::status) |
53 |
| - .def("type", &TreeNode::type); |
| 48 | + py::class_<BT::TreeNode, std::shared_ptr<BT::TreeNode>>(m, "TreeNode") |
| 49 | + .def("name", &BT::TreeNode::name) |
| 50 | + .def("status", &BT::TreeNode::status) |
| 51 | + .def("type", &BT::TreeNode::type); |
54 | 52 |
|
55 |
| - py::class_<BehaviorTreeFactory>(m, "BehaviorTreeFactory") |
| 53 | + py::class_<BT::BehaviorTreeFactory>(m, "BehaviorTreeFactory") |
56 | 54 | .def(py::init<>())
|
57 |
| - .def("create_tree_from_text", &BehaviorTreeFactory::createTreeFromText, |
58 |
| - py::arg("xml_text"), py::arg("blackboard") = Blackboard::create()) |
59 |
| - .def("create_tree", &BehaviorTreeFactory::createTree, |
60 |
| - py::arg("tree_name"), py::arg("blackboard") = Blackboard::create()) |
| 55 | + .def("create_tree_from_text", |
| 56 | + &BT::BehaviorTreeFactory::createTreeFromText, |
| 57 | + py::arg("xml_text"), |
| 58 | + py::arg("blackboard") = BT::Blackboard::create()) |
| 59 | + .def("create_tree", |
| 60 | + &BT::BehaviorTreeFactory::createTree, |
| 61 | + py::arg("tree_name"), |
| 62 | + py::arg("blackboard") = BT::Blackboard::create()) |
61 | 63 | .def("register_behavior_tree_from_text",
|
62 |
| - &BehaviorTreeFactory::registerBehaviorTreeFromText, |
| 64 | + &BT::BehaviorTreeFactory::registerBehaviorTreeFromText, |
63 | 65 | py::arg("xml_text"))
|
64 |
| - .def("register_scripting_enum", |
65 |
| - &BehaviorTreeFactory::registerScriptingEnum) |
| 66 | + .def("register_scripting_enum", &BT::BehaviorTreeFactory::registerScriptingEnum) |
66 | 67 | .def(
|
67 | 68 | "register_simple_action",
|
68 |
| - [](BehaviorTreeFactory *self, const std::string &id, |
69 |
| - std::function<NodeStatus(TreeNode *)> tick_functor, |
70 |
| - PortsList ports) { |
| 69 | + [](BT::BehaviorTreeFactory* self, |
| 70 | + const std::string& id, |
| 71 | + std::function<BT::NodeStatus(BT::TreeNode*)> tick_functor, |
| 72 | + BT::PortsList ports) { |
71 | 73 | self->registerSimpleAction(
|
72 |
| - id, |
73 |
| - [tick_functor](TreeNode &tree_node) { |
74 |
| - return tick_functor(&tree_node); |
75 |
| - }, |
76 |
| - ports); |
| 74 | + id, [tick_functor](BT::TreeNode& tree_node) { return tick_functor(&tree_node); }, std::move(ports)); |
77 | 75 | },
|
78 |
| - py::arg("id"), py::arg("tick_functor"), |
79 |
| - py::arg("ports") = PortsList()) |
| 76 | + py::arg("id"), |
| 77 | + py::arg("tick_functor"), |
| 78 | + py::arg("ports") = BT::PortsList()) |
80 | 79 | .def(
|
81 | 80 | "register_simple_decorator",
|
82 | 81 | // We need to use a pointer cause pybind11 does copy for references
|
83 | 82 | // https://github.com/pybind/pybind11/issues/1123
|
84 |
| - [](BehaviorTreeFactory *self, const std::string &id, |
85 |
| - std::function<NodeStatus(NodeStatus, TreeNode *)> tick_functor, |
86 |
| - PortsList ports) { |
| 83 | + [](BT::BehaviorTreeFactory* self, |
| 84 | + const std::string& id, |
| 85 | + std::function<BT::NodeStatus(BT::NodeStatus, BT::TreeNode*)> tick_functor, |
| 86 | + BT::PortsList ports) { |
87 | 87 | self->registerSimpleDecorator(
|
88 | 88 | id,
|
89 |
| - [tick_functor](NodeStatus node_status, TreeNode &tree_node) { |
| 89 | + [tick_functor](BT::NodeStatus node_status, BT::TreeNode& tree_node) { |
90 | 90 | return tick_functor(node_status, &tree_node);
|
91 | 91 | },
|
92 |
| - ports); |
| 92 | + std::move(ports)); |
93 | 93 | },
|
94 |
| - py::arg("id"), py::arg("tick_functor"), |
95 |
| - py::arg("ports") = PortsList()) |
| 94 | + py::arg("id"), |
| 95 | + py::arg("tick_functor"), |
| 96 | + py::arg("ports") = BT::PortsList()) |
96 | 97 | .def(
|
97 | 98 | "register_simple_condition",
|
98 |
| - [](BehaviorTreeFactory *self, const std::string &id, |
99 |
| - std::function<NodeStatus(TreeNode *)> tick_functor, |
100 |
| - PortsList ports) { |
| 99 | + [](BT::BehaviorTreeFactory* self, |
| 100 | + const std::string& id, |
| 101 | + std::function<BT::NodeStatus(BT::TreeNode*)> tick_functor, |
| 102 | + BT::PortsList ports) { |
101 | 103 | self->registerSimpleCondition(
|
102 |
| - id, |
103 |
| - [tick_functor](TreeNode &tree_node) { |
104 |
| - return tick_functor(&tree_node); |
105 |
| - }, |
106 |
| - ports); |
| 104 | + id, [tick_functor](BT::TreeNode& tree_node) { return tick_functor(&tree_node); }, std::move(ports)); |
107 | 105 | },
|
108 |
| - py::arg("id"), py::arg("tick_functor"), |
109 |
| - py::arg("ports") = PortsList()); |
| 106 | + py::arg("id"), |
| 107 | + py::arg("tick_functor"), |
| 108 | + py::arg("ports") = BT::PortsList()); |
110 | 109 |
|
111 |
| - py::class_<StdCoutLogger>(m, "StdCoutLogger") |
112 |
| - .def(py::init<const BT::Tree &>(), py::arg("tree")); |
| 110 | + py::class_<BT::StdCoutLogger>(m, "StdCoutLogger").def(py::init<const BT::Tree&>(), py::arg("tree")); |
113 | 111 |
|
114 | 112 | py::class_<BT::Groot2Publisher>(m, "Groot2Publisher")
|
115 |
| - .def(py::init<const BT::Tree &, unsigned>(), py::arg("tree"), |
116 |
| - py::arg("port") = 1667); |
| 113 | + .def(py::init<const BT::Tree&, unsigned>(), py::arg("tree"), py::arg("port") = 1667); |
117 | 114 |
|
118 |
| - py::class_<BT::PortInfo>(m, "PortInfo"); |
| 115 | + py::class_<BT::PortInfo> give_me_a_name(m, "PortInfo"); |
119 | 116 |
|
120 | 117 | m.def(
|
121 | 118 | "get_tree_recursively",
|
122 |
| - [](const TreeNode *root_node) { |
| 119 | + [](const BT::TreeNode* root_node) { |
123 | 120 | std::ostringstream s;
|
124 | 121 | BT::printTreeRecursively(root_node, s);
|
125 | 122 | return s.str();
|
|
0 commit comments