Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a0721d4

Browse files
Merge pull request BehaviorTree#138 from HansRobo/fix/remerge_#53
Add BehaviorTree#53 content
2 parents a4a26e3 + 59eaab8 commit a0721d4

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

include/behaviortree_cpp_v3/bt_factory.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ class BehaviorTreeFactory
225225
*/
226226
void registerFromPlugin(const std::string &file_path);
227227

228+
/**
229+
* @brief registerFromROSPlugins finds all shared libraries that export ROS plugins for behaviortree_cpp, and calls registerFromPlugin for each library.
230+
* @throws If not compiled with ROS support or if the library cannot load for any reason
231+
*
232+
*/
233+
void registerFromROSPlugins();
234+
228235
/**
229236
* @brief instantiateTreeNode creates an instance of a previously registered TreeNode.
230237
*

src/bt_factory.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#include "behaviortree_cpp_v3/utils/shared_library.h"
1515
#include "behaviortree_cpp_v3/xml_parsing.h"
1616

17+
#ifdef USING_ROS
18+
#include "filesystem/path.h"
19+
#include <ros/package.h>
20+
#endif
21+
1722
namespace BT
1823
{
1924
BehaviorTreeFactory::BehaviorTreeFactory()
@@ -131,6 +136,66 @@ void BehaviorTreeFactory::registerFromPlugin(const std::string& file_path)
131136
}
132137
}
133138

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+
134199
std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
135200
const std::string& name,
136201
const std::string& ID,

0 commit comments

Comments
 (0)