|
12 | 12 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
13 | 13 |
|
14 | 14 | #include "behaviortree_ros2/bt_utils.hpp" |
| 15 | +#include "behaviortree_ros2/plugins.hpp" |
15 | 16 |
|
16 | 17 | namespace |
17 | 18 | { |
@@ -92,73 +93,69 @@ void LoadBehaviorTrees(BT::BehaviorTreeFactory& factory, |
92 | 93 | } |
93 | 94 | } |
94 | 95 |
|
95 | | -void LoadPlugins(BT::BehaviorTreeFactory& factory, const std::string& directory_path) |
96 | | -{ |
97 | | - using std::filesystem::directory_iterator; |
98 | | - for(const auto& entry : directory_iterator(directory_path)) |
99 | | - { |
100 | | - if(entry.path().extension() == ".so") |
101 | | - { |
102 | | - try |
103 | | - { |
104 | | - factory.registerFromPlugin(entry.path().string()); |
105 | | - RCLCPP_INFO(kLogger, "Loaded Plugin: %s", entry.path().filename().c_str()); |
106 | | - } |
107 | | - catch(const std::exception& e) |
108 | | - { |
109 | | - RCLCPP_ERROR(kLogger, "Failed to load Plugin: %s \n %s", |
110 | | - entry.path().filename().c_str(), e.what()); |
111 | | - } |
112 | | - } |
113 | | - } |
114 | | -} |
115 | | - |
116 | 96 | void LoadRosPlugins(BT::BehaviorTreeFactory& factory, const std::string& directory_path, |
117 | | - rclcpp::Node::SharedPtr node) |
| 97 | + BT::RosNodeParams params) |
118 | 98 | { |
119 | 99 | using std::filesystem::directory_iterator; |
120 | | - BT::RosNodeParams params; |
121 | | - params.nh = node; |
| 100 | + |
122 | 101 | for(const auto& entry : directory_iterator(directory_path)) |
123 | 102 | { |
| 103 | + const auto filename = entry.path().filename(); |
124 | 104 | if(entry.path().extension() == ".so") |
125 | 105 | { |
126 | 106 | try |
127 | 107 | { |
128 | | - RegisterRosNode(factory, entry.path().string(), params); |
129 | | - RCLCPP_INFO(kLogger, "Loaded ROS Plugin: %s", entry.path().filename().c_str()); |
| 108 | + BT::SharedLibrary loader(entry.path().string()); |
| 109 | + if(loader.hasSymbol(BT::PLUGIN_SYMBOL)) |
| 110 | + { |
| 111 | + typedef void (*Func)(BehaviorTreeFactory&); |
| 112 | + auto func = (Func)loader.getSymbol(BT::PLUGIN_SYMBOL); |
| 113 | + func(factory); |
| 114 | + } |
| 115 | + else if(loader.hasSymbol(BT::ROS_PLUGIN_SYMBOL)) |
| 116 | + { |
| 117 | + typedef void (*Func)(BT::BehaviorTreeFactory&, const BT::RosNodeParams&); |
| 118 | + auto func = (Func)loader.getSymbol(BT::ROS_PLUGIN_SYMBOL); |
| 119 | + func(factory, params); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + RCLCPP_ERROR(kLogger, "Failed to load Plugin from file: %s.", filename.c_str()); |
| 124 | + continue; |
| 125 | + } |
| 126 | + RCLCPP_INFO(kLogger, "Loaded ROS Plugin: %s", filename.c_str()); |
130 | 127 | } |
131 | 128 | catch(const std::exception& e) |
132 | 129 | { |
133 | | - RCLCPP_ERROR(kLogger, "Failed to load ROS Plugin: %s \n %s", |
134 | | - entry.path().filename().c_str(), e.what()); |
| 130 | + RCLCPP_ERROR(kLogger, "Failed to load ROS Plugin: %s \n %s", filename.c_str(), |
| 131 | + e.what()); |
135 | 132 | } |
136 | 133 | } |
137 | 134 | } |
138 | 135 | } |
139 | 136 |
|
140 | | -void RegisterBehaviorTrees(action_server_bt::Params& params, |
141 | | - BT::BehaviorTreeFactory& factory, rclcpp::Node::SharedPtr node) |
| 137 | +void RegisterBehaviorTrees(bt_server::Params& params, BT::BehaviorTreeFactory& factory, |
| 138 | + rclcpp::Node::SharedPtr node) |
142 | 139 | { |
143 | | - // clear the factory and load/reload it with the Behaviors and Trees specified by the user in their action_server_bt config yaml |
| 140 | + // clear the factory and load/reload it with the Behaviors and Trees specified by the user in their [bt_action_server] config yaml |
144 | 141 | factory.clearRegisteredBehaviorTrees(); |
145 | 142 |
|
| 143 | + BT::RosNodeParams ros_params; |
| 144 | + ros_params.nh = node; |
| 145 | + ros_params.server_timeout = std::chrono::milliseconds(params.ros_plugins_timeout); |
| 146 | + ros_params.wait_for_server_timeout = ros_params.server_timeout; |
| 147 | + |
146 | 148 | for(const auto& plugin : params.plugins) |
147 | 149 | { |
148 | 150 | const auto plugin_directory = GetDirectoryPath(plugin); |
149 | 151 | // skip invalid plugins directories |
150 | 152 | if(plugin_directory.empty()) |
| 153 | + { |
151 | 154 | continue; |
152 | | - LoadPlugins(factory, plugin_directory); |
153 | | - } |
154 | | - for(const auto& plugin : params.ros_plugins) |
155 | | - { |
156 | | - const auto plugin_directory = GetDirectoryPath(plugin); |
157 | | - // skip invalid plugins directories |
158 | | - if(plugin_directory.empty()) |
159 | | - continue; |
160 | | - LoadRosPlugins(factory, plugin_directory, node); |
| 155 | + } |
| 156 | + LoadRosPlugins(factory, plugin_directory, ros_params); |
161 | 157 | } |
| 158 | + |
162 | 159 | for(const auto& tree_dir : params.behavior_trees) |
163 | 160 | { |
164 | 161 | const auto tree_directory = GetDirectoryPath(tree_dir); |
|
0 commit comments