134 lines
4.1 KiB
Markdown
134 lines
4.1 KiB
Markdown
# Plugin Loader Helper - Hướng dẫn sử dụng
|
|
|
|
## Tổng quan
|
|
|
|
`PluginLoaderHelper` là một utility class giúp tìm đường dẫn library file (.so) từ tên symbol (export name) được sử dụng với `BOOST_DLL_ALIAS`. Tương tự như `plugins.xml` trong ROS pluginlib, nhưng dành cho boost::dll.
|
|
|
|
## Cách hoạt động
|
|
|
|
1. **File config YAML** (`boost_dll_plugins.yaml`): Map tên symbol → đường dẫn library
|
|
2. **PluginLoaderHelper class**: Đọc config và cung cấp hàm `findLibraryPath()`
|
|
|
|
## Cài đặt
|
|
|
|
### 1. Thêm file vào CMakeLists.txt
|
|
|
|
Thêm `plugin_loader_helper.cpp` vào build:
|
|
|
|
```cmake
|
|
add_library(${PROJECT_NAME} SHARED
|
|
src/console.cpp
|
|
src/node_handle.cpp
|
|
src/plugin_loader_helper.cpp # <-- Thêm dòng này
|
|
)
|
|
```
|
|
|
|
### 2. Cấu hình file YAML
|
|
|
|
File `config/boost_dll_plugins.yaml` đã được tạo với các mapping mặc định. Bạn có thể chỉnh sửa để thêm/bớt plugins.
|
|
|
|
## Cách sử dụng
|
|
|
|
### Ví dụ 1: Tìm library path từ symbol name
|
|
|
|
```cpp
|
|
#include <robot/plugin_loader_helper.h>
|
|
|
|
// Tạo helper instance
|
|
robot::PluginLoaderHelper loader;
|
|
|
|
// Tìm library path từ symbol name
|
|
std::string lib_path = loader.findLibraryPath("CustomPlanner");
|
|
if (!lib_path.empty()) {
|
|
// Sử dụng với boost::dll::import_alias
|
|
auto planner_loader = boost::dll::import_alias<robot_nav_core2::BaseGlobalPlanner::Ptr()>(
|
|
lib_path, "CustomPlanner", boost::dll::load_mode::append_decorations);
|
|
planner_ = planner_loader();
|
|
}
|
|
```
|
|
|
|
### Ví dụ 2: Sử dụng trong move_base.cpp
|
|
|
|
Thay vì hardcode đường dẫn:
|
|
|
|
```cpp
|
|
// CŨ (hardcode):
|
|
std::string path_file_so = "/home/robotics/AGV/Diff_Wheel_Prj/pnkx_robot_nav_core2/build/libcustom_planner.so";
|
|
|
|
// MỚI (từ config):
|
|
robot::PluginLoaderHelper loader;
|
|
std::string path_file_so = loader.findLibraryPath(global_planner);
|
|
if (path_file_so.empty()) {
|
|
// Fallback nếu không tìm thấy
|
|
path_file_so = "/home/robotics/AGV/Diff_Wheel_Prj/pnkx_robot_nav_core2/build/libcustom_planner.so";
|
|
}
|
|
```
|
|
|
|
### Ví dụ 3: Kiểm tra symbol có tồn tại không
|
|
|
|
```cpp
|
|
robot::PluginLoaderHelper loader;
|
|
if (loader.hasSymbol("CustomPlanner")) {
|
|
std::string lib_path = loader.findLibraryPath("CustomPlanner");
|
|
// ...
|
|
}
|
|
```
|
|
|
|
### Ví dụ 4: Reload config
|
|
|
|
```cpp
|
|
robot::PluginLoaderHelper loader;
|
|
// ... sử dụng ...
|
|
|
|
// Reload config nếu file thay đổi
|
|
loader.reloadConfig("boost_dll_plugins.yaml");
|
|
```
|
|
|
|
## Cấu trúc file config
|
|
|
|
File `config/boost_dll_plugins.yaml`:
|
|
|
|
```yaml
|
|
plugin_libraries:
|
|
CustomPlanner:
|
|
library_path: "/path/to/libcustom_planner.so"
|
|
|
|
PNKXLocalPlanner:
|
|
library_path: "/path/to/libpnkx_local_planner.so"
|
|
|
|
search_paths:
|
|
- "/home/robotics/AGV/Diff_Wheel_Prj/pnkx_robot_nav_core2/build"
|
|
- "/path/to/other/libraries"
|
|
```
|
|
|
|
## Tìm kiếm library
|
|
|
|
Helper sẽ tìm library theo thứ tự:
|
|
|
|
1. **Trong config file**: Tìm trong `plugin_libraries` section
|
|
2. **Resolve path**:
|
|
- Nếu absolute path → kiểm tra file có tồn tại
|
|
- Nếu relative path → tìm trong `search_paths`
|
|
3. **LD_LIBRARY_PATH**: Fallback nếu không tìm thấy trong config
|
|
4. **Return empty**: Nếu không tìm thấy
|
|
|
|
## Lợi ích
|
|
|
|
✅ **Không hardcode đường dẫn**: Tất cả đường dẫn trong file config
|
|
✅ **Dễ maintain**: Chỉ cần sửa file YAML khi thay đổi library
|
|
✅ **Tương tự plugins.xml**: Quen thuộc với ROS developers
|
|
✅ **Flexible**: Hỗ trợ cả absolute và relative paths
|
|
✅ **Fallback**: Tự động tìm trong LD_LIBRARY_PATH nếu không có trong config
|
|
|
|
## Lưu ý
|
|
|
|
- File config sẽ được tìm trong:
|
|
1. Đường dẫn trực tiếp (nếu absolute)
|
|
2. `$PNKX_NAV_CORE_CONFIG_DIR/boost_dll_plugins.yaml`
|
|
3. `$PNKX_NAV_CORE_DIR/config/boost_dll_plugins.yaml`
|
|
4. `/home/robotics/AGV/Diff_Wheel_Prj/pnkx_robot_nav_core2/config/boost_dll_plugins.yaml`
|
|
5. `../config/boost_dll_plugins.yaml` (relative paths)
|
|
|
|
- Symbol name có thể có namespace (ví dụ: `custom_planner::CustomPlanner`), helper sẽ tự động thử tìm cả tên đầy đủ và tên ngắn (`CustomPlanner`).
|
|
|