|
14 | 14 | #include "behaviortree_cpp_v3/utils/shared_library.h"
|
15 | 15 | #include "behaviortree_cpp_v3/xml_parsing.h"
|
16 | 16 |
|
| 17 | +#ifdef USING_ROS |
| 18 | +#include "filesystem/path.h" |
| 19 | +#include <ros/package.h> |
| 20 | +#endif |
| 21 | + |
17 | 22 | namespace BT
|
18 | 23 | {
|
19 | 24 | BehaviorTreeFactory::BehaviorTreeFactory()
|
@@ -131,6 +136,66 @@ void BehaviorTreeFactory::registerFromPlugin(const std::string& file_path)
|
131 | 136 | }
|
132 | 137 | }
|
133 | 138 |
|
| 139 | +#ifdef USING_ROS |
| 140 | + |
| 141 | + #ifdef _WIN32 |
| 142 | +const char os_pathsep(';'); // NOLINT |
| 143 | +#else |
| 144 | +const char os_pathsep(':'); // NOLINT |
| 145 | +#endif |
| 146 | + |
| 147 | +// This function is a copy from the one in class_loader_imp.hpp in ROS pluginlib |
| 148 | +// package, licensed under BSD. |
| 149 | +// https://github.com/ros/pluginlib |
| 150 | +std::vector<std::string> getCatkinLibraryPaths() |
| 151 | +{ |
| 152 | + std::vector<std::string> lib_paths; |
| 153 | + const char* env = std::getenv("CMAKE_PREFIX_PATH"); |
| 154 | + if (env) |
| 155 | + { |
| 156 | + const std::string env_catkin_prefix_paths(env); |
| 157 | + std::vector<BT::StringView> catkin_prefix_paths = |
| 158 | + splitString(env_catkin_prefix_paths, os_pathsep); |
| 159 | + for (BT::StringView catkin_prefix_path : catkin_prefix_paths) |
| 160 | + { |
| 161 | + filesystem::path path(catkin_prefix_path.to_string()); |
| 162 | + filesystem::path lib("lib"); |
| 163 | + lib_paths.push_back((path / lib).str()); |
| 164 | + } |
| 165 | + } |
| 166 | + return lib_paths; |
| 167 | +} |
| 168 | + |
| 169 | +void BehaviorTreeFactory::registerFromROSPlugins() |
| 170 | +{ |
| 171 | + std::vector<std::string> plugins; |
| 172 | + ros::package::getPlugins("behaviortree_cpp", "bt_lib_plugin", plugins, true); |
| 173 | + std::vector<std::string> catkin_lib_paths = getCatkinLibraryPaths(); |
| 174 | + |
| 175 | + for (const auto& plugin : plugins) |
| 176 | + { |
| 177 | + auto filename = filesystem::path(plugin + BT::SharedLibrary::suffix()); |
| 178 | + for (const auto& lib_path : catkin_lib_paths) |
| 179 | + { |
| 180 | + const auto full_path = filesystem::path(lib_path) / filename; |
| 181 | + if (full_path.exists()) |
| 182 | + { |
| 183 | + std::cout << "Registering ROS plugins from " << full_path.str() << std::endl; |
| 184 | + registerFromPlugin(full_path.str()); |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | +#else |
| 191 | + |
| 192 | + void BehaviorTreeFactory::registerFromROSPlugins() |
| 193 | + { |
| 194 | + throw RuntimeError("Using attribute [ros_pkg] in <include>, but this library was compiled " |
| 195 | + "without ROS support. Recompile the BehaviorTree.CPP using catkin"); |
| 196 | + } |
| 197 | +#endif |
| 198 | + |
134 | 199 | std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
|
135 | 200 | const std::string& name,
|
136 | 201 | const std::string& ID,
|
|
0 commit comments