first commit
This commit is contained in:
152
CMakeLists.txt
Normal file
152
CMakeLists.txt
Normal file
@@ -0,0 +1,152 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(sbpl_recovery)
|
||||
|
||||
# C++17 theo quy ước workspace cho code mới.
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(catkin REQUIRED COMPONENTS
|
||||
recovery_core
|
||||
robot_cpp
|
||||
robot_costmap_2d
|
||||
robot_nav_core
|
||||
robot_geometry_msgs
|
||||
robot_nav_msgs
|
||||
robot_tf3_geometry_msgs
|
||||
)
|
||||
|
||||
find_package(Boost REQUIRED COMPONENTS
|
||||
system
|
||||
filesystem
|
||||
thread
|
||||
)
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
# tf3 cài sẵn ngoài workspace (xem CLAUDE.md) — không phải package catkin.
|
||||
find_library(TF3_LIBRARY
|
||||
NAMES tf3
|
||||
|
||||
PATHS
|
||||
/usr/lib
|
||||
/usr/local/lib
|
||||
/usr/lib/x86_64-linux-gnu
|
||||
)
|
||||
|
||||
if(NOT TF3_LIBRARY)
|
||||
message(FATAL_ERROR
|
||||
"sbpl_recovery: libtf3 not found. Install tf3 (see the auto_setup.sh scripts under "
|
||||
"Controllers/, Devices/, Localizations/) or add its location to CMAKE_PREFIX_PATH.")
|
||||
endif()
|
||||
|
||||
catkin_package(
|
||||
INCLUDE_DIRS
|
||||
include
|
||||
|
||||
LIBRARIES
|
||||
sbpl_recovery
|
||||
|
||||
CATKIN_DEPENDS
|
||||
recovery_core
|
||||
robot_cpp
|
||||
robot_costmap_2d
|
||||
robot_nav_core
|
||||
robot_geometry_msgs
|
||||
robot_nav_msgs
|
||||
robot_tf3_geometry_msgs
|
||||
|
||||
DEPENDS
|
||||
Boost
|
||||
)
|
||||
|
||||
# ==================================================================================================
|
||||
# Plugin: libsbpl_recovery.so — export symbol `DetourPathRecovery` (Boost.DLL)
|
||||
# ==================================================================================================
|
||||
|
||||
add_library(sbpl_recovery SHARED
|
||||
plugins/detour_path_recovery.cpp
|
||||
)
|
||||
|
||||
target_compile_options(sbpl_recovery PRIVATE -Wall -Wextra)
|
||||
|
||||
# Header dependency vào dạng SYSTEM để -Wall -Wextra của chính gói không bị warning của
|
||||
# robot_cpp/costmap_2d nhấn chìm — cùng lý do với recovery_core.
|
||||
target_include_directories(sbpl_recovery SYSTEM
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
${catkin_INCLUDE_DIRS}
|
||||
${Boost_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_link_libraries(sbpl_recovery
|
||||
PRIVATE
|
||||
${catkin_LIBRARIES}
|
||||
Boost::system
|
||||
Boost::filesystem
|
||||
Boost::thread
|
||||
Threads::Threads
|
||||
${CMAKE_DL_LIBS}
|
||||
${TF3_LIBRARY}
|
||||
)
|
||||
|
||||
add_dependencies(sbpl_recovery ${catkin_EXPORTED_TARGETS})
|
||||
|
||||
install(TARGETS sbpl_recovery
|
||||
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
|
||||
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
|
||||
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
|
||||
)
|
||||
|
||||
# ==================================================================================================
|
||||
# Test
|
||||
# ==================================================================================================
|
||||
|
||||
if(CATKIN_ENABLE_TESTING)
|
||||
# Planner giả cho unit test — nạp qua đúng đường Boost.DLL mà runtime đi.
|
||||
add_library(sbpl_recovery_test_line_planner SHARED
|
||||
test/plugins/test_line_planner.cpp
|
||||
)
|
||||
target_compile_options(sbpl_recovery_test_line_planner PRIVATE -Wall -Wextra)
|
||||
target_include_directories(sbpl_recovery_test_line_planner SYSTEM
|
||||
PRIVATE
|
||||
${catkin_INCLUDE_DIRS}
|
||||
${Boost_INCLUDE_DIRS}
|
||||
)
|
||||
target_link_libraries(sbpl_recovery_test_line_planner
|
||||
PRIVATE
|
||||
${catkin_LIBRARIES}
|
||||
Boost::system
|
||||
Boost::filesystem
|
||||
Threads::Threads
|
||||
${CMAKE_DL_LIBS}
|
||||
)
|
||||
|
||||
catkin_add_gtest(detour_path_recovery_test test/detour_path_recovery_test.cpp)
|
||||
if(TARGET detour_path_recovery_test)
|
||||
target_compile_options(detour_path_recovery_test PRIVATE -Wall -Wextra)
|
||||
target_compile_definitions(detour_path_recovery_test PRIVATE
|
||||
SBPL_RECOVERY_TEST_CONFIG_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test/config"
|
||||
SBPL_RECOVERY_TEST_LIBRARY_DIR="${CATKIN_DEVEL_PREFIX}/lib"
|
||||
)
|
||||
target_include_directories(detour_path_recovery_test SYSTEM
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
${catkin_INCLUDE_DIRS}
|
||||
${Boost_INCLUDE_DIRS}
|
||||
)
|
||||
# catkin_add_gtest dùng target_link_libraries dạng plain, nên phần bổ sung cũng phải plain.
|
||||
target_link_libraries(detour_path_recovery_test
|
||||
${catkin_LIBRARIES}
|
||||
${Boost_LIBRARIES}
|
||||
pthread
|
||||
${CMAKE_DL_LIBS}
|
||||
${TF3_LIBRARY}
|
||||
)
|
||||
# Plugin KHÔNG link thẳng vào test: chúng được nạp qua RecoveryRegistry lúc chạy, đúng đường
|
||||
# runtime — nhưng phải build xong trước khi test chạy.
|
||||
add_dependencies(detour_path_recovery_test
|
||||
sbpl_recovery
|
||||
sbpl_recovery_test_line_planner
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
84
README.md
Normal file
84
README.md
Normal file
@@ -0,0 +1,84 @@
|
||||
# sbpl_recovery
|
||||
|
||||
Recovery behavior **họ path** cho `recovery_core`: khi local planner bế tắc (vật cản chắn tuyến),
|
||||
behavior lập một **đường vòng** bằng một global planner plugin — mặc định `SBPLLatticePlanner` —
|
||||
rồi **nối đường vòng vào phần còn lại của global plan hiện hành** và trả về cho runtime bám tiếp.
|
||||
|
||||
Port từ [`navigation_experimental/sbpl_recovery`](https://github.com/ros-planning/navigation_experimental)
|
||||
(BSD, Eitan Marder-Eppstein) sang hệ sinh thái `robot_*` (không roscpp).
|
||||
|
||||
## Khác bản gốc ở đâu
|
||||
|
||||
| Bản gốc (ROS) | Bản này |
|
||||
|---|---|
|
||||
| Tự lái: chạy `pose_follower` bên trong, publish thẳng `cmd_vel` | **Không lái.** Trả `RecoveryOutputType::kPath`; runtime áp path và local planner đang active bám theo — một nguồn vận tốc duy nhất |
|
||||
| Plan nhận qua topic `NavfnROS/plan` | Plan đến từ `RecoveryContext::plan` — chính plan control loop đang bám |
|
||||
| Sau recovery move_base replan nên chỉ cần lập tới điểm nối | **Splice**: path trả về = đường vòng + phần plan cũ sau điểm nối, vì runtime không replan sau recovery (contract 2026-08-04b) |
|
||||
| `pluginlib` | `boost::dll` + `BOOST_DLL_ALIAS(…, DetourPathRecovery)` + `library_path` trong YAML |
|
||||
|
||||
## Local costmap hay global costmap?
|
||||
|
||||
Mặc định **local costmap** (`use_global_costmap: false`):
|
||||
|
||||
- vật cản buộc phải vòng là thứ sensor vừa thấy — nằm tươi nhất trong local costmap;
|
||||
- cửa sổ lăn nhỏ nên một lượt SBPL bounded (~vài chục ms), chạy được trong một tick recovery.
|
||||
|
||||
Đổi lại hai ràng buộc:
|
||||
|
||||
- **điểm nối phải nằm trong cửa sổ local** — vật cản chắn dài hơn nửa cửa sổ thì behavior trả
|
||||
`kFailed` và nhường behavior kế tiếp trong route;
|
||||
- local costmap chạy ở frame `odom`, plan ở frame `map` → cần `RecoveryContext::tf` (runtime đã
|
||||
nối sẵn). Thiếu TF thì lượt chạy bị từ chối kèm lý do.
|
||||
|
||||
`use_global_costmap: true` khi cần vòng xa hơn cửa sổ (đổi lại phụ thuộc độ tươi của obstacle
|
||||
layer trên global costmap).
|
||||
|
||||
## Cấu hình
|
||||
|
||||
```yaml
|
||||
recovery:
|
||||
behaviors:
|
||||
- {name: detour_path, type: DetourPathRecovery}
|
||||
routes:
|
||||
# Thêm detour_path vào route nào cần đường vòng, ví dụ:
|
||||
# controlling_failed: [wait, detour_path]
|
||||
detour_path:
|
||||
planner_name: SBPLLatticePlanner # global planner gen-1, nạp qua library_path ở root config
|
||||
use_global_costmap: false
|
||||
planning_distance: 2.0 # [m] điểm nối cách robot tối thiểu chừng này dọc plan
|
||||
sample_step: 0.5 # [m] khoảng cách dọc plan giữa hai ứng viên điểm nối
|
||||
rejoin_clearance: 0.5 # [m] điểm nối cách POSE BẨN GẦN NHẤT tối thiểu chừng này
|
||||
attempts_per_run: 3 # số ứng viên thử tối đa một lượt (mỗi tick thử một ứng viên)
|
||||
timeout: 10.0 # [s] trần lượt, recovery_core cưỡng chế
|
||||
|
||||
DetourPathRecovery:
|
||||
library_path: libsbpl_recovery
|
||||
```
|
||||
|
||||
Planner đọc tham số từ namespace mang tên chính nó (`SBPLLatticePlanner/…`) — trùng tên với
|
||||
`backup_global_planner` thì dùng chung tuning, gồm cả `primitive_filename` (độ phân giải mprim
|
||||
phải khớp resolution của costmap được chọn).
|
||||
|
||||
Hai khoá `lock_costmap: false` và `costmap_frame_override` **chỉ dành cho unit test** với costmap
|
||||
placeholder — production không đặt.
|
||||
|
||||
## Chọn điểm nối
|
||||
|
||||
Ứng viên điểm nối phải **đặt được robot vào** (`RecoveryContext::collision`, footprint thật) và cách
|
||||
**pose bẩn gần nhất** dọc plan ít nhất `rejoin_clearance`. Chọn thuần theo khoảng cách — như bản gốc
|
||||
— cho ra ứng viên nằm ngay trên vật cản đang chắn tuyến: planner không bao giờ tới được, mà mỗi lượt
|
||||
thử hỏng vẫn giữ mutex costmap trọn `allocated_time` và làm đói thread cập nhật costmap.
|
||||
|
||||
Không có ứng viên nào đặt được → `start()` trả `false` **ngay**, không chạy lượt SBPL nào. Context
|
||||
không có collision checker → quay về luật thuần-khoảng-cách (không biết thì không loại).
|
||||
|
||||
## Verify
|
||||
|
||||
```bash
|
||||
catkin_make --pkg sbpl_recovery
|
||||
./devel/lib/sbpl_recovery/detour_path_recovery_test
|
||||
```
|
||||
|
||||
Test dùng planner giả (`TestLinePlanner`, nạp qua đúng đường Boost.DLL) — kiểm chọn điểm nối,
|
||||
splice, thử-lại theo ứng viên, quy đổi frame map↔odom qua tf3, mất pose giữa lượt, và từ chối khi
|
||||
thiếu TF. Đường chạy với SBPL thật thuộc test tích hợp trên sim.
|
||||
118
include/sbpl_recovery/merge_grids.h
Normal file
118
include/sbpl_recovery/merge_grids.h
Normal file
@@ -0,0 +1,118 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* sbpl_recovery — gộp lưới costmap: global làm nền, cửa sổ local đè lên.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#ifndef SBPL_RECOVERY_MERGE_GRIDS_H_
|
||||
#define SBPL_RECOVERY_MERGE_GRIDS_H_
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <robot_costmap_2d/cost_values.h>
|
||||
#include <robot_costmap_2d/costmap_2d.h>
|
||||
|
||||
namespace sbpl_recovery
|
||||
{
|
||||
|
||||
/**
|
||||
* @struct PlanarTransform2D
|
||||
* @brief Phép biến đổi cứng 2D (frame local -> frame lưới đích), tra TF MỘT lần rồi áp hàng chục
|
||||
* nghìn cell — không tra TF trong vòng lặp.
|
||||
*/
|
||||
struct PlanarTransform2D
|
||||
{
|
||||
double x = 0.0; ///< [m] tịnh tiến
|
||||
double y = 0.0; ///< [m]
|
||||
double cos_yaw = 1.0;
|
||||
double sin_yaw = 0.0;
|
||||
|
||||
void apply(double in_x, double in_y, double& out_x, double& out_y) const
|
||||
{
|
||||
out_x = x + cos_yaw * in_x - sin_yaw * in_y;
|
||||
out_y = y + sin_yaw * in_x + cos_yaw * in_y;
|
||||
}
|
||||
|
||||
/// @brief Chiều ngược của @ref apply (frame đích -> frame nguồn). Phép quay là trực giao nên
|
||||
/// nghịch đảo chỉ là chuyển vị — không cần dựng transform thứ hai.
|
||||
void applyInverse(double in_x, double in_y, double& out_x, double& out_y) const
|
||||
{
|
||||
const double dx = in_x - x;
|
||||
const double dy = in_y - y;
|
||||
out_x = cos_yaw * dx + sin_yaw * dy;
|
||||
out_y = -sin_yaw * dx + cos_yaw * dy;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Cost một cell sau khi gộp hai nguồn: một bên báo chắn là chắn.
|
||||
*
|
||||
* Quy tắc (chốt 2026-08-05, xem DETOUR_MERGED_GRID_PLAN.md):
|
||||
* - local `NO_INFORMATION` -> giữ giá trị global (local chưa quét thì tin mắt rộng);
|
||||
* - global `NO_INFORMATION` nhưng local ĐÃ biết -> lấy local (255 là giá trị lớn nhất, `max` mù
|
||||
* sẽ biến "chưa biết" thành "chắn");
|
||||
* - cả hai đều biết -> `max`: local FREE (60) KHÔNG xoá tường global — global của hệ này chỉ chứa
|
||||
* tĩnh, tường không tự đi.
|
||||
*/
|
||||
inline unsigned char mergeCellCost(unsigned char global_cost, unsigned char local_cost)
|
||||
{
|
||||
if (local_cost == robot_costmap_2d::NO_INFORMATION)
|
||||
{
|
||||
return global_cost;
|
||||
}
|
||||
if (global_cost == robot_costmap_2d::NO_INFORMATION)
|
||||
{
|
||||
return local_cost;
|
||||
}
|
||||
return std::max(global_cost, local_cost);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Đè cửa sổ @p local lên @p out (đã là bản copy của lưới global, CÙNG frame với đích).
|
||||
*
|
||||
* Duyệt theo cell của local (nhỏ hơn nhiều lần lưới đích); tâm cell local được quy sang frame đích
|
||||
* bằng @p local_to_out rồi ghi theo @ref mergeCellCost. Cell local rơi ngoài lưới đích bị bỏ qua.
|
||||
* Cả hai lưới đều là BẢN CHỤP của bên gọi — hàm này không đụng costmap sống, không cần mutex.
|
||||
*/
|
||||
inline void overlayLocalGrid(const robot_costmap_2d::Costmap2D& local,
|
||||
const PlanarTransform2D& local_to_out,
|
||||
robot_costmap_2d::Costmap2D& out)
|
||||
{
|
||||
const unsigned int size_x = local.getSizeInCellsX();
|
||||
const unsigned int size_y = local.getSizeInCellsY();
|
||||
|
||||
for (unsigned int ly = 0; ly < size_y; ++ly)
|
||||
{
|
||||
for (unsigned int lx = 0; lx < size_x; ++lx)
|
||||
{
|
||||
const unsigned char local_cost = local.getCost(lx, ly);
|
||||
if (local_cost == robot_costmap_2d::NO_INFORMATION)
|
||||
{
|
||||
continue; // Không đóng góp gì — giữ nền global, khỏi tính transform.
|
||||
}
|
||||
|
||||
double wx = 0.0;
|
||||
double wy = 0.0;
|
||||
local.mapToWorld(lx, ly, wx, wy);
|
||||
|
||||
double ox = 0.0;
|
||||
double oy = 0.0;
|
||||
local_to_out.apply(wx, wy, ox, oy);
|
||||
|
||||
unsigned int mx = 0;
|
||||
unsigned int my = 0;
|
||||
if (!out.worldToMap(ox, oy, mx, my))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
out.setCost(mx, my, mergeCellCost(out.getCost(mx, my), local_cost));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sbpl_recovery
|
||||
|
||||
#endif // SBPL_RECOVERY_MERGE_GRIDS_H_
|
||||
47
package.xml
Normal file
47
package.xml
Normal file
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0"?>
|
||||
<package>
|
||||
<name>sbpl_recovery</name>
|
||||
<version>0.1.0</version>
|
||||
<description>
|
||||
Recovery behavior họ path cho recovery_core: lập đường vòng quanh vật cản bằng một global
|
||||
planner plugin (mặc định SBPLLatticePlanner) rồi nối lại global plan hiện hành.
|
||||
|
||||
Port từ navigation_experimental/sbpl_recovery. Khác bản gốc: behavior KHÔNG tự lái — nó trả
|
||||
path (RecoveryOutputType::kPath) và runtime bám path đó bằng local planner đang active, giữ
|
||||
bất biến một nguồn vận tốc duy nhất. Mặc định lập đường vòng trên LOCAL costmap (vật cản tươi
|
||||
nhất, cửa sổ nhỏ nên một lượt SBPL bounded); dữ liệu plan ở frame map được quy đổi hai chiều
|
||||
qua tf3.
|
||||
|
||||
Planner nạp qua Boost.DLL theo khoá library_path — thay planner khác chỉ là đổi một dòng YAML.
|
||||
</description>
|
||||
<author>DuongTD</author>
|
||||
<maintainer email="tdd@gmail.com">DuongTD</maintainer>
|
||||
<license>BSD</license>
|
||||
|
||||
<buildtool_depend version_gte="0.5.68">catkin</buildtool_depend>
|
||||
|
||||
<build_depend>recovery_core</build_depend>
|
||||
<run_depend>recovery_core</run_depend>
|
||||
|
||||
<build_depend>robot_cpp</build_depend>
|
||||
<run_depend>robot_cpp</run_depend>
|
||||
|
||||
<build_depend>robot_costmap_2d</build_depend>
|
||||
<run_depend>robot_costmap_2d</run_depend>
|
||||
|
||||
<build_depend>robot_nav_core</build_depend>
|
||||
<run_depend>robot_nav_core</run_depend>
|
||||
|
||||
<build_depend>robot_geometry_msgs</build_depend>
|
||||
<run_depend>robot_geometry_msgs</run_depend>
|
||||
|
||||
<build_depend>robot_nav_msgs</build_depend>
|
||||
<run_depend>robot_nav_msgs</run_depend>
|
||||
|
||||
<build_depend>robot_tf3_geometry_msgs</build_depend>
|
||||
<run_depend>robot_tf3_geometry_msgs</run_depend>
|
||||
|
||||
<!-- Runtime: planner mặc định của config là SBPLLatticePlanner; không phải build_depend vì
|
||||
planner nạp động qua Boost.DLL theo library_path, đổi được bằng YAML. -->
|
||||
<run_depend>sbpl_lattice_planner</run_depend>
|
||||
</package>
|
||||
1277
plugins/detour_path_recovery.cpp
Normal file
1277
plugins/detour_path_recovery.cpp
Normal file
File diff suppressed because it is too large
Load Diff
94
test/config/recovery_behaviors_params.yaml
Normal file
94
test/config/recovery_behaviors_params.yaml
Normal file
@@ -0,0 +1,94 @@
|
||||
# Config CHỈ dành cho unit test của sbpl_recovery — được nạp qua PNKX_NAV_CORE_CONFIG_DIR do chính
|
||||
# binary test đặt. KHÔNG phải config runtime.
|
||||
#
|
||||
# `lock_costmap: false` và `costmap_frame_override` là test seam: costmap trong test là placeholder
|
||||
# không dereference được (không dựng nổi Costmap2DROBOT trong unit test — cần tf3 + cây config đầy
|
||||
# đủ). Production không bao giờ đặt hai khoá này.
|
||||
|
||||
recovery:
|
||||
behaviors:
|
||||
- {name: detour, type: DetourPathRecovery}
|
||||
- {name: detour_flaky, type: DetourPathRecovery}
|
||||
- {name: detour_dead, type: DetourPathRecovery}
|
||||
- {name: detour_map, type: DetourPathRecovery}
|
||||
- {name: detour_narrow, type: DetourPathRecovery}
|
||||
- {name: detour_alias, type: DetourPathRecovery}
|
||||
|
||||
detour:
|
||||
planner_name: TestLinePlanner
|
||||
planning_distance: 1.0 # [m]
|
||||
sample_step: 0.5 # [m]
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
|
||||
detour_flaky:
|
||||
planner_name: TestFlakyPlanner # hai ứng viên đầu bị "chắn", ứng viên ba mới thông
|
||||
planning_distance: 1.0
|
||||
sample_step: 0.5
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
|
||||
detour_dead:
|
||||
planner_name: TestDeadPlanner # không ứng viên nào thông -> behavior phải kFailed
|
||||
planning_distance: 1.0
|
||||
sample_step: 0.5
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
|
||||
detour_map:
|
||||
planner_name: TestLinePlanner # plan ở frame map, costmap ở odom -> ép đường quy đổi TF
|
||||
planning_distance: 1.0
|
||||
sample_step: 0.5
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
|
||||
# Tên instance KHÁC symbol: đọc param từ `DetourOnlyTuning`, nạp .so theo alias `TestLinePlanner`.
|
||||
# Đây là đường mà cấu hình runtime dùng để cho detour một instance SBPL riêng (allocated_time
|
||||
# ngắn) trong khi `libsbpl_lattice_planner` chỉ export đúng một symbol.
|
||||
detour_alias:
|
||||
planner_name: DetourOnlyTuning
|
||||
planner_symbol: TestLinePlanner
|
||||
planning_distance: 1.0
|
||||
sample_step: 0.5
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
|
||||
detour_narrow:
|
||||
planner_name: TestOffsetPlanner # planner giả luôn vẽ đường vòng lệch 1.0 m khỏi tuyến
|
||||
planning_distance: 1.0
|
||||
sample_step: 0.5
|
||||
attempts_per_run: 3
|
||||
max_deviation: 0.2 # [m] chặt hơn độ lệch của planner -> mọi ứng viên bị loại
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
|
||||
# Bảng symbol -> thư viện cho Boost.DLL. Ba symbol planner cùng một .so; param của planner đọc theo
|
||||
# namespace mang tên symbol.
|
||||
TestLinePlanner:
|
||||
library_path: libsbpl_recovery_test_line_planner
|
||||
|
||||
TestFlakyPlanner:
|
||||
library_path: libsbpl_recovery_test_line_planner
|
||||
fail_first_attempts: 2
|
||||
|
||||
TestDeadPlanner:
|
||||
library_path: libsbpl_recovery_test_line_planner
|
||||
fail_first_attempts: 1000
|
||||
|
||||
# Luôn vẽ đường vòng lệch 1.0 m khỏi đoạn thẳng start->goal: dùng để kiểm guard max_deviation.
|
||||
TestOffsetPlanner:
|
||||
library_path: libsbpl_recovery_test_line_planner
|
||||
lateral_offset: 1.0
|
||||
|
||||
# Namespace param THUẦN — cố ý KHÔNG có library_path: nó không phải symbol, việc nạp .so đi theo
|
||||
# `planner_symbol`. Tham số khác hẳn bản mặc định để test chứng minh param được đọc từ đây.
|
||||
DetourOnlyTuning:
|
||||
points: 9
|
||||
|
||||
DetourPathRecovery:
|
||||
library_path: libsbpl_recovery
|
||||
801
test/detour_path_recovery_test.cpp
Normal file
801
test/detour_path_recovery_test.cpp
Normal file
@@ -0,0 +1,801 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Kiểm DetourPathRecovery: chọn điểm nối, splice với plan cũ, thử-lại theo ứng viên, quy đổi frame
|
||||
* qua TF, và các đường từ chối. Planner là plugin giả (TestLinePlanner) nạp qua đúng đường
|
||||
* Boost.DLL mà runtime đi; costmap là placeholder — hai test seam `lock_costmap` và
|
||||
* `costmap_frame_override` giữ cho không có dereference nào chạm vào nó.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <robot/node_handle.h>
|
||||
#include <robot_costmap_2d/cost_values.h>
|
||||
#include <robot_costmap_2d/costmap_2d.h>
|
||||
#include <tf3/buffer_core.h>
|
||||
|
||||
#include <recovery_core/recovery_registry.h>
|
||||
|
||||
#include <sbpl_recovery/merge_grids.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using recovery_core::RecoveryBehavior;
|
||||
using recovery_core::RecoveryContext;
|
||||
using recovery_core::RecoveryGoal;
|
||||
using recovery_core::RecoveryRegistry;
|
||||
using recovery_core::RecoveryResult;
|
||||
using recovery_core::RecoveryStatus;
|
||||
using recovery_core::RecoveryTrigger;
|
||||
|
||||
/// Costmap placeholder: địa chỉ hợp lệ nhưng KHÔNG phải costmap — mọi đường dereference đã bị tắt
|
||||
/// qua test seam. Cùng khuôn với planner_runner_test của move_base2.
|
||||
robot_costmap_2d::Costmap2DROBOT* dummyCostmap()
|
||||
{
|
||||
static std::uintptr_t placeholder = 0;
|
||||
return reinterpret_cast<robot_costmap_2d::Costmap2DROBOT*>(&placeholder);
|
||||
}
|
||||
|
||||
class FakePoseProvider final : public recovery_core::PoseProvider
|
||||
{
|
||||
public:
|
||||
bool ok = true;
|
||||
robot_geometry_msgs::PoseStamped pose;
|
||||
|
||||
bool getRobotPose(robot_geometry_msgs::PoseStamped& out) const override
|
||||
{
|
||||
if (!ok)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
out = pose;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @class BandCollisionChecker
|
||||
* @brief Chặn một dải theo trục x: [from, to] không đặt được robot vào.
|
||||
*
|
||||
* Mô phỏng đúng tình huống sim: một vật cản nằm CHÍNH TRÊN tuyến, kéo dài vài mét — thứ khiến các
|
||||
* ứng viên điểm nối chọn thuần theo khoảng cách đều rơi vào vùng cấm.
|
||||
*/
|
||||
class BandCollisionChecker final : public recovery_core::CollisionChecker
|
||||
{
|
||||
public:
|
||||
void blockBand(double from_x, double to_x)
|
||||
{
|
||||
from_x_ = from_x;
|
||||
to_x_ = to_x;
|
||||
}
|
||||
|
||||
/// Vật cản THỨ HAI xa hơn trên tuyến — plan cũ không replan nên đuôi nó xuyên vật cản này mãi.
|
||||
void blockSecondBand(double from_x, double to_x)
|
||||
{
|
||||
second_from_x_ = from_x;
|
||||
second_to_x_ = to_x;
|
||||
}
|
||||
|
||||
/// Dải "chưa quan sát": costmap local lăn theo robot nên đây là phần lớn tuyến phía trước.
|
||||
void unknownBand(double from_x, double to_x)
|
||||
{
|
||||
unknown_from_x_ = from_x;
|
||||
unknown_to_x_ = to_x;
|
||||
}
|
||||
|
||||
/// Rìa lưới costmap: từ x này trở đi pose nằm NGOÀI lưới — quét phải dừng, không phải "chắn".
|
||||
void outsideFrom(double from_x)
|
||||
{
|
||||
outside_from_x_ = from_x;
|
||||
}
|
||||
|
||||
double footprintCost(double x, double y, double theta) const override
|
||||
{
|
||||
(void)y;
|
||||
(void)theta;
|
||||
++query_count_;
|
||||
if ((x >= from_x_ && x <= to_x_) || (x >= second_from_x_ && x <= second_to_x_))
|
||||
{
|
||||
return -1.0;
|
||||
}
|
||||
return (x >= unknown_from_x_ && x <= unknown_to_x_) ? -3.0 : 0.0;
|
||||
}
|
||||
|
||||
Placement classify(double x, double y, double theta) const override
|
||||
{
|
||||
if (x >= outside_from_x_)
|
||||
{
|
||||
return Placement::kOutside;
|
||||
}
|
||||
const double cost = footprintCost(x, y, theta);
|
||||
if (cost >= 0.0)
|
||||
{
|
||||
return Placement::kFree;
|
||||
}
|
||||
return cost == -3.0 ? Placement::kUnknown : Placement::kBlocked;
|
||||
}
|
||||
|
||||
std::size_t queryCount() const
|
||||
{
|
||||
return query_count_;
|
||||
}
|
||||
|
||||
private:
|
||||
double from_x_ = 1.0;
|
||||
double to_x_ = 0.0; // dải rỗng: mặc định không chặn gì
|
||||
double second_from_x_ = 1.0;
|
||||
double second_to_x_ = 0.0; // dải thứ hai, mặc định rỗng
|
||||
double unknown_from_x_ = 1.0;
|
||||
double unknown_to_x_ = 0.0;
|
||||
double outside_from_x_ = 1e9; // mặc định: lưới phủ hết
|
||||
mutable std::size_t query_count_ = 0;
|
||||
};
|
||||
|
||||
class FakePlanProvider final : public recovery_core::PlanProvider
|
||||
{
|
||||
public:
|
||||
std::vector<robot_geometry_msgs::PoseStamped> plan;
|
||||
|
||||
bool getGlobalPlan(std::vector<robot_geometry_msgs::PoseStamped>& out) const override
|
||||
{
|
||||
if (plan.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
out = plan;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
robot_geometry_msgs::PoseStamped makePose(const std::string& frame, double x, double y)
|
||||
{
|
||||
robot_geometry_msgs::PoseStamped pose;
|
||||
pose.header.frame_id = frame;
|
||||
pose.pose.position.x = x;
|
||||
pose.pose.position.y = y;
|
||||
pose.pose.orientation.w = 1.0;
|
||||
return pose;
|
||||
}
|
||||
|
||||
/// Plan thẳng dọc trục x: [from_x, to_x] bước @p step, y cố định.
|
||||
std::vector<robot_geometry_msgs::PoseStamped> makeLinePlan(const std::string& frame, double from_x,
|
||||
double to_x, double step, double y)
|
||||
{
|
||||
std::vector<robot_geometry_msgs::PoseStamped> plan;
|
||||
for (double x = from_x; x <= to_x + 1e-9; x += step)
|
||||
{
|
||||
plan.push_back(makePose(frame, x, y));
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
RecoveryBehavior* findBehavior(RecoveryRegistry& registry, const std::string& name)
|
||||
{
|
||||
for (std::size_t i = 0; i < registry.size(); ++i)
|
||||
{
|
||||
if (registry.nameAt(i) == name)
|
||||
{
|
||||
return registry.at(i);
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
RecoveryGoal controllingGoal()
|
||||
{
|
||||
RecoveryGoal goal;
|
||||
goal.trigger = RecoveryTrigger::kControllingFailed;
|
||||
return goal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fixture: registry thật + plugin thật (nạp qua Boost.DLL) trên cổng giả. TF có sẵn một transform
|
||||
* tĩnh map->odom (odom gốc tại (2, 1) trong map) cho các ca đổi frame.
|
||||
*/
|
||||
class DetourPathRecoveryTest : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
void SetUp() override
|
||||
{
|
||||
tf3::TransformStampedMsg map_to_odom;
|
||||
map_to_odom.header.frame_id = "map";
|
||||
map_to_odom.child_frame_id = "odom";
|
||||
map_to_odom.transform.translation.x = 2.0; // [m] p_map = p_odom + (2, 1)
|
||||
map_to_odom.transform.translation.y = 1.0; // [m]
|
||||
map_to_odom.transform.rotation.w = 1.0;
|
||||
ASSERT_TRUE(tf_.setTransform(map_to_odom, "test", /*is_static=*/true));
|
||||
|
||||
ctx_.pose = &pose_;
|
||||
ctx_.plan = &plan_;
|
||||
// Tuyến tham chiếu để RỖNG mặc định: phần lớn test kiểm hình học trên plan hiện hành, và
|
||||
// behavior phải tự quay về `plan` khi tham chiếu chưa có. Test nào cần thì tự nạp.
|
||||
ctx_.reference_plan = &reference_plan_;
|
||||
ctx_.collision = &collision_;
|
||||
ctx_.local_costmap = dummyCostmap();
|
||||
ctx_.tf = &tf_;
|
||||
|
||||
robot::NodeHandle nh;
|
||||
ASSERT_TRUE(registry_.loadFromConfig(nh, "recovery", ctx_));
|
||||
ASSERT_EQ(registry_.size(), 6u);
|
||||
}
|
||||
|
||||
FakePoseProvider pose_;
|
||||
FakePlanProvider plan_;
|
||||
FakePlanProvider reference_plan_;
|
||||
BandCollisionChecker collision_;
|
||||
tf3::BufferCore tf_;
|
||||
RecoveryContext ctx_;
|
||||
RecoveryRegistry registry_;
|
||||
robot::Time t0_{100, 0};
|
||||
};
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, StartRefusesWithoutAPlan)
|
||||
{
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan.clear(); // Không có plan thì không có gì để nối lại.
|
||||
|
||||
EXPECT_FALSE(detour->start(controllingGoal(), t0_));
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, SplicesTheDetourOntoTheRemainder)
|
||||
{
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
// Plan cùng frame với costmap (odom): không cần quy đổi. Robot ở gốc, plan 0..5 m bước 0.5.
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
EXPECT_EQ(path->header.frame_id, "odom");
|
||||
|
||||
// Ứng viên đầu tiên: pose đầu cách robot >= planning_distance (1.0 m) => x = 1.0 (index 2).
|
||||
// Đường vòng 5 điểm (0,0)->(1,0) + phần còn lại của plan x = 1.5..5.0 (8 pose) = 13 pose.
|
||||
ASSERT_EQ(path->poses.size(), 13u);
|
||||
EXPECT_NEAR(path->poses.front().pose.position.x, 0.0, 1e-9);
|
||||
EXPECT_NEAR(path->poses[4].pose.position.x, 1.0, 1e-9); // cuối đường vòng = điểm nối
|
||||
EXPECT_NEAR(path->poses[5].pose.position.x, 1.5, 1e-9); // pose đầu của phần plan cũ
|
||||
EXPECT_NEAR(path->poses.back().pose.position.x, 5.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, FlakyPlannerRetriesUntilACandidateWorks)
|
||||
{
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour_flaky");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
|
||||
// Hai ứng viên đầu (x = 1.0, 1.5) bị planner giả từ chối; ứng viên ba (x = 2.0) thành công.
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
||||
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
|
||||
// Cuối đường vòng phải là điểm nối thứ BA — bằng chứng cursor ứng viên có tiến.
|
||||
EXPECT_NEAR(path->poses[4].pose.position.x, 2.0, 1e-9);
|
||||
EXPECT_NEAR(path->poses[5].pose.position.x, 2.5, 1e-9);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, FailsAfterAllCandidates)
|
||||
{
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour_dead");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
||||
|
||||
// Hết attempts_per_run (3) ứng viên: kFailed để state machine chuyển behavior kế tiếp.
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kFailed);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, ConvertsFramesThroughTf)
|
||||
{
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour_map");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
// Plan ở MAP, costmap ở ODOM (odom gốc tại map (2,1)). Robot đứng ở odom (0,0) = map (2,1).
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("map", 2.0, 7.0, 0.5, 1.0);
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
|
||||
// Kết quả phải nằm TRỌN trong frame của plan: đường vòng (planner trả ở odom) đã được quy về map.
|
||||
EXPECT_EQ(path->header.frame_id, "map");
|
||||
EXPECT_NEAR(path->poses.front().pose.position.x, 2.0, 1e-9); // robot tại map (2,1)
|
||||
EXPECT_NEAR(path->poses.front().pose.position.y, 1.0, 1e-9);
|
||||
EXPECT_NEAR(path->poses[4].pose.position.x, 3.0, 1e-9); // điểm nối map x=3.0
|
||||
EXPECT_NEAR(path->poses[5].pose.position.x, 3.5, 1e-9); // plan cũ tiếp tục
|
||||
EXPECT_NEAR(path->poses.back().pose.position.x, 7.0, 1e-9);
|
||||
EXPECT_NEAR(path->poses.back().pose.position.y, 1.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, RejoinPointsSkipPastTheBlockedStretch)
|
||||
{
|
||||
// Lỗi sim 2026-08-05: ứng viên chọn thuần theo khoảng cách đều rơi vào chính vật cản đang chắn
|
||||
// tuyến, nên cả ba lượt SBPL đều "Solution not found" — mà mỗi lượt hỏng vẫn giữ mutex costmap.
|
||||
// Đúng: bỏ qua cả dải bẩn, điểm nối đầu tiên phải nằm sau nó + rejoin_clearance.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 6.0, 0.5, 0.0);
|
||||
collision_.blockBand(1.0, 2.4); // [m] vật cản trải dài trên tuyến
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
|
||||
// Đường vòng 5 điểm; điểm cuối của nó là điểm nối. Pose bẩn cuối cùng trên plan là x = 2.0
|
||||
// (2.5 đã ra ngoài dải), cộng `rejoin_clearance` 0.5 -> ứng viên đầu tiên đạt là x = 2.5.
|
||||
// KHÔNG phải x = 1.0 như luật chọn thuần-khoảng-cách cũ, vốn rơi thẳng vào vật cản.
|
||||
EXPECT_NEAR(path->poses[4].pose.position.x, 2.5, 1e-9);
|
||||
EXPECT_NEAR(path->poses[5].pose.position.x, 3.0, 1e-9);
|
||||
EXPECT_NEAR(path->poses.back().pose.position.x, 6.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, PosesBeyondReachStopTheScanInsteadOfCountingAsBlocked)
|
||||
{
|
||||
// Lỗi sim 2026-08-05 (lượt 2): pose ngoài cửa sổ costmap bị đọc thành "bẩn" — plan dài vài chục
|
||||
// mét trên cửa sổ 8x8 m nên phần xa làm mọi ứng viên bị loại. Ngữ nghĩa đúng: `kOutside` là HẾT
|
||||
// TẦM QUÉT — dừng lại, không phải vật cản.
|
||||
//
|
||||
// Mô phỏng: rìa lưới ở 3.5 m, plan dài 20 m, KHÔNG có vật cản thật trong lưới. Ứng viên phải
|
||||
// chọn bình thường trong tầm, không bị phần ngoài lưới làm hỏng.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 20.0, 0.5, 0.0);
|
||||
collision_.outsideFrom(3.5); // rìa lưới: quét dừng ở đây, không phải "chắn"
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
|
||||
// Ứng viên đầu tiên vẫn là x = 1.0 (planning_distance của instance `detour`), không phải một pose
|
||||
// nào đó bị phần ngoài lưới đẩy đi.
|
||||
EXPECT_NEAR(path->poses[4].pose.position.x, 1.0, 1e-9);
|
||||
EXPECT_NEAR(path->poses.back().pose.position.x, 20.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, RejoinsOntoTheReferenceRouteNotTheDriftedPlan)
|
||||
{
|
||||
// Contract 2026-08-05e: điểm nối nằm trên TUYẾN GỐC của chặng, không phải plan đang bám. Nối vào
|
||||
// plan đang bám thì mỗi lượt detour lấy kết quả lượt trước làm mốc — sim đo tuyến bị ăn mòn dần
|
||||
// (rejoin 106/305 -> 130/289 -> 132/273) và robot không bao giờ quay về tuyến order.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
// Plan ĐANG BÁM đã trôi sang y = 3.0 sau vài lượt detour trước đó.
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 6.0, 0.5, 3.0);
|
||||
// Tuyến GỐC vẫn là tuyến order ở y = 0.0.
|
||||
reference_plan_.plan = makeLinePlan("odom", 0.0, 6.0, 0.5, 0.0);
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
|
||||
// Đuôi splice phải là đuôi TUYẾN GỐC (y = 0), không phải plan đã trôi (y = 3): đó chính là thứ
|
||||
// kéo robot trở lại tuyến fleet đã duyệt.
|
||||
EXPECT_NEAR(path->poses.back().pose.position.y, 0.0, 1e-9);
|
||||
EXPECT_NEAR(path->poses.back().pose.position.x, 6.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, FallsBackToTheCurrentPlanWhenNoReferenceRouteExists)
|
||||
{
|
||||
// Runtime chưa lập được plan nào (hoặc không cấp tuyến tham chiếu): vẫn phải vòng được, chỉ mất
|
||||
// khả năng tự về tuyến. Im lặng bỏ cuộc thì tệ hơn nhiều.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 6.0, 0.5, 3.0);
|
||||
reference_plan_.plan.clear(); // chưa có tuyến gốc
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
EXPECT_NEAR(path->poses.back().pose.position.y, 3.0, 1e-9); // đuôi của plan đang bám
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, RejoinsPastEveryVisibleObstacleNotJustTheFirst)
|
||||
{
|
||||
// Luật chốt 2026-08-05f: điểm nối phải qua HẾT phần chắn còn NHÌN THẤY ĐƯỢC. Hai luật trước đều
|
||||
// sai ở PHẠM VI:
|
||||
// - "sau pose bẩn cuối toàn tầm quét": đuôi plan không bao giờ được replan nên nó cắt qua vật
|
||||
// cản ở xa; vết bẩn ở 7.96 m giết sạch ứng viên hợp lệ ngay sau vật cản đang chắn ở 2.5 m ->
|
||||
// refuse -> ABORT (log sim 11:00);
|
||||
// - "sau cụm chắn ĐẦU TIÊN": nối vào khe giữa hai vật cản cùng nhìn thấy, robot chạy ~0.5 m rồi
|
||||
// bị chắn lại -> detour lặp vô ích quanh đúng vật cản thứ nhất.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 10.0, 0.5, 0.0);
|
||||
collision_.blockBand(1.0, 2.0); // vật cản đang chắn tuyến
|
||||
collision_.blockSecondBand(4.0, 4.5); // vật cản thứ hai, CÙNG nhìn thấy -> phải vòng qua luôn
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
|
||||
// Pose bẩn cuối nhìn thấy được là x = 4.5; + rejoin_clearance 0.5 -> ứng viên đầu tiên x = 5.0.
|
||||
// KHÔNG phải x = 2.5 trong khe giữa hai vật cản như luật "cụm đầu tiên".
|
||||
EXPECT_NEAR(path->poses[4].pose.position.x, 5.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, NarrowGapBetweenObstaclesIsSkipped)
|
||||
{
|
||||
// Clearance tính CẢ HAI PHÍA: khoảng sạch giữa hai vật cản mà hẹp hơn 2*rejoin_clearance thì
|
||||
// điểm nối trong đó đặt robot sát mép vật cản kế tiếp — monitor sẽ bắn lại ngay sau một nhịp.
|
||||
// Đúng: nhảy qua, nối sau cụm thứ hai.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 10.0, 0.5, 0.0);
|
||||
collision_.blockBand(1.0, 2.0);
|
||||
collision_.blockSecondBand(2.5, 3.0); // khoảng giữa không còn pose sạch đủ clearance hai phía
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
|
||||
// Sau cụm thứ hai (x = 3.0) + clearance 0.5 -> ứng viên đầu tiên x = 3.5.
|
||||
EXPECT_NEAR(path->poses[4].pose.position.x, 3.5, 1e-9);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, UnobservedSpaceIsNotAnObstacle)
|
||||
{
|
||||
// Lỗi sim 2026-08-05 (lượt 3): costmap local lăn theo robot nên mọi ô ngoài tầm sensor là
|
||||
// NO_INFORMATION, và `footprintCost` trả âm cho cả ca đó. Detour đọc gần hết tuyến phía trước
|
||||
// thành vật cản -> "no placeable rejoin point" -> từ chối mọi lượt.
|
||||
//
|
||||
// Phải khớp hai bên còn lại: `checkPlan` bỏ qua ô unknown, SBPL chạy `allow_unknown: true`.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 20.0, 0.5, 0.0);
|
||||
collision_.unknownBand(0.6, 100.0); // gần như toàn bộ tuyến phía trước là "chưa quan sát"
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
ASSERT_NE(result.pathOut(), nullptr);
|
||||
EXPECT_NEAR(result.pathOut()->poses[4].pose.position.x, 1.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, RealObstacleStillBlocksEvenAmidUnobservedSpace)
|
||||
{
|
||||
// Nới unknown KHÔNG được nới luôn vật cản thật: vật cản nằm giữa vùng chưa quan sát vẫn phải đẩy
|
||||
// điểm nối ra sau nó.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 20.0, 0.5, 0.0);
|
||||
collision_.unknownBand(0.6, 100.0);
|
||||
collision_.blockBand(1.0, 1.6); // vật cản THẬT nằm trong vùng unknown
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
ASSERT_NE(result.pathOut(), nullptr);
|
||||
// Pose bẩn cuối là x = 1.5; cộng rejoin_clearance 0.5 -> điểm nối đầu tiên là x = 2.0.
|
||||
EXPECT_NEAR(result.pathOut()->poses[4].pose.position.x, 2.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, RefusesWhenTheOnlyCleanPosesAreBeforeTheObstacle)
|
||||
{
|
||||
// Đường lùi ghi TRƯỚC chỗ chắn phải bị vứt: nối lại vào pose nằm trước vật cản là đưa robot về
|
||||
// đúng chỗ vừa bị chặn, rồi monitor bắn lại ngay lượt sau và cạn route.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 20.0, 0.5, 0.0);
|
||||
// Sạch tới 1.4 m rồi chắn hết phần còn lại TRONG TẦM QUÉT (3.0 m).
|
||||
collision_.blockBand(1.5, 100.0);
|
||||
|
||||
EXPECT_FALSE(detour->start(controllingGoal(), t0_));
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, RefusesToStartWhenNoRejoinPointIsPlaceable)
|
||||
{
|
||||
// Toàn bộ phần còn lại của tuyến bị chắn: từ chối NGAY. Chạy đủ attempts_per_run lượt SBPL chắc
|
||||
// chắn hỏng chỉ để tới cùng kết luận, trong khi mỗi lượt giữ mutex costmap trọn allocated_time —
|
||||
// đó chính là thứ làm đói thread cập nhật costmap trên sim.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
collision_.blockBand(0.5, 100.0);
|
||||
|
||||
EXPECT_FALSE(detour->start(controllingGoal(), t0_));
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, NoCollisionCheckerFallsBackToDistanceOnlyCandidates)
|
||||
{
|
||||
// Context không có collision checker: "không biết" không được biến thành "loại hết" — hành vi
|
||||
// phải y như trước, planner là bên phán quyết cuối.
|
||||
FakePoseProvider pose;
|
||||
FakePlanProvider plan;
|
||||
RecoveryContext ctx;
|
||||
ctx.pose = &pose;
|
||||
ctx.plan = &plan;
|
||||
ctx.collision = nullptr;
|
||||
ctx.local_costmap = dummyCostmap();
|
||||
ctx.tf = &tf_;
|
||||
|
||||
robot::NodeHandle nh;
|
||||
RecoveryRegistry registry;
|
||||
ASSERT_TRUE(registry.loadFromConfig(nh, "recovery", ctx));
|
||||
|
||||
RecoveryBehavior* detour = findBehavior(registry, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose.pose = makePose("odom", 0.0, 0.0);
|
||||
plan.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
ASSERT_NE(result.pathOut(), nullptr);
|
||||
EXPECT_NEAR(result.pathOut()->poses[4].pose.position.x, 1.0, 1e-9); // ứng viên đầu như cũ
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, PlannerSymbolMayDifferFromTheInstanceName)
|
||||
{
|
||||
// Đường mà runtime dùng để cho detour một instance SBPL riêng: `planner_name` là namespace param
|
||||
// (`DetourOnlyTuning`, khai `points: 9`), `planner_symbol` là alias có thật trong .so.
|
||||
// `libsbpl_lattice_planner` chỉ export một symbol, nên nếu hai thứ này bị buộc phải trùng nhau
|
||||
// thì không thể có instance tuning riêng — đúng lỗi làm route path_blocked rỗng trên sim.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour_alias");
|
||||
ASSERT_NE(detour, nullptr) << "behavior bị loại lúc nạp: planner_symbol không resolve được";
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
const RecoveryResult result = detour->update(t0_);
|
||||
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
|
||||
// Param đọc từ `DetourOnlyTuning`: đường vòng 9 điểm (mặc định là 5) + 8 pose còn lại của plan.
|
||||
EXPECT_EQ(path->poses.size(), 17u);
|
||||
EXPECT_NEAR(path->poses[8].pose.position.x, 1.0, 1e-9); // điểm nối
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, RejectsDetoursThatStrayTooFarFromTheRoute)
|
||||
{
|
||||
// Guard lệch tuyến: order chỉ định tuyến, robot chỉ được rời khỏi nó trong hành lang fleet cho
|
||||
// phép. Planner giả luôn phình 1.0 m, ngưỡng đặt 0.2 m -> mọi ứng viên bị loại, behavior kFailed
|
||||
// để route chuyển sang cách xử lý khác.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour_narrow");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
|
||||
// Ba ứng viên, ứng viên nào cũng lập được đường nhưng đều vượt ngưỡng lệch.
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kFailed);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, AcceptsDetoursInsideTheDeviationLimit)
|
||||
{
|
||||
// Cùng ngưỡng 0.2 m nhưng planner mặc định (`detour`) không phình: đường vòng nằm trên tuyến nên
|
||||
// guard không được chặn nhầm.
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kSucceeded);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, FailsWhenPoseIsLostMidRun)
|
||||
{
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
|
||||
// Mất pose giữa lượt: không được lập đường vòng bằng pose cũ.
|
||||
pose_.ok = false;
|
||||
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kFailed);
|
||||
}
|
||||
|
||||
TEST(DetourPathRecoveryNoTfTest, RefusesWhenFramesDifferWithoutTf)
|
||||
{
|
||||
// Registry riêng với context KHÔNG có TF: plan (map) khác frame costmap (odom) phải bị từ chối
|
||||
// ngay tại start, kèm lý do — không đoán, không trộn frame.
|
||||
FakePoseProvider pose;
|
||||
FakePlanProvider plan;
|
||||
RecoveryContext ctx;
|
||||
ctx.pose = &pose;
|
||||
ctx.plan = &plan;
|
||||
ctx.local_costmap = dummyCostmap();
|
||||
ctx.tf = nullptr;
|
||||
|
||||
robot::NodeHandle nh;
|
||||
RecoveryRegistry registry;
|
||||
ASSERT_TRUE(registry.loadFromConfig(nh, "recovery", ctx));
|
||||
|
||||
RecoveryBehavior* detour = findBehavior(registry, "detour_map");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose.pose = makePose("odom", 0.0, 0.0);
|
||||
plan.plan = makeLinePlan("map", 2.0, 7.0, 0.5, 1.0);
|
||||
|
||||
EXPECT_FALSE(detour->start(controllingGoal(), robot::Time{100, 0}));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// ================================================================================================
|
||||
// merge_grids.h — ngữ nghĩa gộp lưới (thuần, lưới tay, không cần costmap sống)
|
||||
// ================================================================================================
|
||||
|
||||
TEST(MergeGrids, CellMergeSemantics)
|
||||
{
|
||||
using robot_costmap_2d::FREE_SPACE;
|
||||
using robot_costmap_2d::LETHAL_OBSTACLE;
|
||||
using robot_costmap_2d::NO_INFORMATION;
|
||||
|
||||
// local chưa quét -> tin mắt rộng.
|
||||
EXPECT_EQ(sbpl_recovery::mergeCellCost(LETHAL_OBSTACLE, NO_INFORMATION), LETHAL_OBSTACLE);
|
||||
EXPECT_EQ(sbpl_recovery::mergeCellCost(FREE_SPACE, NO_INFORMATION), FREE_SPACE);
|
||||
// global chưa biết nhưng local ĐÃ biết -> lấy local (max mù sẽ biến 255 thành "chắn").
|
||||
EXPECT_EQ(sbpl_recovery::mergeCellCost(NO_INFORMATION, FREE_SPACE), FREE_SPACE);
|
||||
EXPECT_EQ(sbpl_recovery::mergeCellCost(NO_INFORMATION, LETHAL_OBSTACLE), LETHAL_OBSTACLE);
|
||||
// Cả hai đều biết -> một bên chắn là chắn; local FREE không xoá tường global.
|
||||
EXPECT_EQ(sbpl_recovery::mergeCellCost(LETHAL_OBSTACLE, FREE_SPACE), LETHAL_OBSTACLE);
|
||||
EXPECT_EQ(sbpl_recovery::mergeCellCost(FREE_SPACE, LETHAL_OBSTACLE), LETHAL_OBSTACLE);
|
||||
EXPECT_EQ(sbpl_recovery::mergeCellCost(FREE_SPACE, FREE_SPACE), FREE_SPACE);
|
||||
}
|
||||
|
||||
TEST(MergeGrids, OverlayWritesLocalWindowOntoGlobalBase)
|
||||
{
|
||||
using robot_costmap_2d::Costmap2D;
|
||||
using robot_costmap_2d::FREE_SPACE;
|
||||
using robot_costmap_2d::LETHAL_OBSTACLE;
|
||||
using robot_costmap_2d::NO_INFORMATION;
|
||||
|
||||
// Nền global 10x10 @ 0.1 m, gốc (0,0): FREE, một bức tường tĩnh tại (0.55, 0.55).
|
||||
Costmap2D merged(10, 10, 0.1, 0.0, 0.0, FREE_SPACE);
|
||||
merged.setCost(5, 5, LETHAL_OBSTACLE);
|
||||
|
||||
// Cửa sổ local 4x4 @ 0.1 m, gốc (0.2, 0.2): một vật cản tươi, một cell FREE trùng tường global,
|
||||
// phần còn lại chưa quét (NO_INFORMATION).
|
||||
Costmap2D local(4, 4, 0.1, 0.2, 0.2, NO_INFORMATION);
|
||||
local.setCost(1, 1, LETHAL_OBSTACLE); // (0.35, 0.35): vật cản sensor
|
||||
local.setCost(3, 3, FREE_SPACE); // (0.55, 0.55): local nói sạch NGAY TRÊN tường global
|
||||
|
||||
sbpl_recovery::PlanarTransform2D identity;
|
||||
sbpl_recovery::overlayLocalGrid(local, identity, merged);
|
||||
|
||||
unsigned int mx = 0;
|
||||
unsigned int my = 0;
|
||||
ASSERT_TRUE(merged.worldToMap(0.35, 0.35, mx, my));
|
||||
EXPECT_EQ(merged.getCost(mx, my), LETHAL_OBSTACLE); // vật cản tươi được đè vào nền
|
||||
ASSERT_TRUE(merged.worldToMap(0.55, 0.55, mx, my));
|
||||
EXPECT_EQ(merged.getCost(mx, my), LETHAL_OBSTACLE); // tường global KHÔNG bị local FREE xoá
|
||||
ASSERT_TRUE(merged.worldToMap(0.25, 0.25, mx, my));
|
||||
EXPECT_EQ(merged.getCost(mx, my), FREE_SPACE); // local chưa quét -> giữ nền global
|
||||
ASSERT_TRUE(merged.worldToMap(0.95, 0.95, mx, my));
|
||||
EXPECT_EQ(merged.getCost(mx, my), FREE_SPACE); // ngoài cửa sổ local -> nền global nguyên vẹn
|
||||
}
|
||||
|
||||
TEST(MergeGrids, OverlayTransformsLocalFrameIntoGridFrame)
|
||||
{
|
||||
using robot_costmap_2d::Costmap2D;
|
||||
using robot_costmap_2d::FREE_SPACE;
|
||||
using robot_costmap_2d::LETHAL_OBSTACLE;
|
||||
using robot_costmap_2d::NO_INFORMATION;
|
||||
|
||||
Costmap2D merged(20, 20, 0.1, 0.0, 0.0, FREE_SPACE);
|
||||
|
||||
// Local frame (odom) lệch map: tịnh tiến (1.0, 0.5), quay 90 độ.
|
||||
Costmap2D local(4, 4, 0.1, 0.0, 0.0, NO_INFORMATION);
|
||||
local.setCost(2, 0, LETHAL_OBSTACLE); // tâm cell tại odom (0.25, 0.05)
|
||||
|
||||
sbpl_recovery::PlanarTransform2D odom_to_map;
|
||||
odom_to_map.x = 1.0;
|
||||
odom_to_map.y = 0.5;
|
||||
odom_to_map.cos_yaw = 0.0;
|
||||
odom_to_map.sin_yaw = 1.0; // yaw = +90 độ: (x, y) -> (-y, x)
|
||||
|
||||
sbpl_recovery::overlayLocalGrid(local, odom_to_map, merged);
|
||||
|
||||
// odom (0.25, 0.05) -> map (1.0 - 0.05, 0.5 + 0.25) = (0.95, 0.75).
|
||||
unsigned int mx = 0;
|
||||
unsigned int my = 0;
|
||||
ASSERT_TRUE(merged.worldToMap(0.95, 0.75, mx, my));
|
||||
EXPECT_EQ(merged.getCost(mx, my), LETHAL_OBSTACLE);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
#ifdef SBPL_RECOVERY_TEST_CONFIG_DIR
|
||||
// Trỏ cây config vào bản test TRƯỚC khi bất kỳ NodeHandle nào được dựng.
|
||||
setenv("PNKX_NAV_CORE_CONFIG_DIR", SBPL_RECOVERY_TEST_CONFIG_DIR, 1);
|
||||
#endif
|
||||
#ifdef SBPL_RECOVERY_TEST_LIBRARY_DIR
|
||||
// PluginLoaderHelper chỉ ghép ".so" trong nhánh PNKX_NAV_CORE_LIBRARY_PATH — không đặt biến này
|
||||
// thì `library_path: libsbpl_recovery` không resolve được khi LD_LIBRARY_PATH thiếu devel/lib.
|
||||
// Cùng khuôn với bộ test của recovery_core.
|
||||
setenv("PNKX_NAV_CORE_LIBRARY_PATH", SBPL_RECOVERY_TEST_LIBRARY_DIR, 0);
|
||||
#endif
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
94
test/plugins/test_line_planner.cpp
Normal file
94
test/plugins/test_line_planner.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* sbpl_recovery — global planner GIẢ cho unit test: nối thẳng start->goal.
|
||||
*
|
||||
* Không đụng costmap (test truyền placeholder không dereference được). Kịch bản lỗi điều khiển
|
||||
* bằng param `fail_first_attempts`: N lời gọi makePlan đầu tiên trả false — mô phỏng "ứng viên
|
||||
* điểm nối gần bị chắn, ứng viên xa hơn mới thông".
|
||||
*
|
||||
* Export hai alias từ cùng một factory: `TestLinePlanner` (luôn thành công) và `TestFlakyPlanner`
|
||||
* (đọc param theo tên riêng của nó) — mỗi symbol đọc param từ namespace mang tên chính nó.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/dll/alias.hpp>
|
||||
|
||||
#include <robot/node_handle.h>
|
||||
#include <robot_nav_core/base_global_planner.h>
|
||||
|
||||
namespace sbpl_recovery_test
|
||||
{
|
||||
|
||||
class TestLinePlanner final : public robot_nav_core::BaseGlobalPlanner
|
||||
{
|
||||
public:
|
||||
static robot_nav_core::BaseGlobalPlanner::Ptr create()
|
||||
{
|
||||
return std::make_shared<TestLinePlanner>();
|
||||
}
|
||||
|
||||
bool initialize(std::string name, robot_costmap_2d::Costmap2DROBOT* /*costmap*/) override
|
||||
{
|
||||
// Costmap là placeholder trong test — TUYỆT ĐỐI không dereference.
|
||||
robot::NodeHandle root;
|
||||
robot::NodeHandle nh(root, name);
|
||||
nh.param("fail_first_attempts", fail_first_attempts_, 0);
|
||||
nh.param("points", points_, 5);
|
||||
nh.param("lateral_offset", lateral_offset_, 0.0);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool makePlan(const robot_geometry_msgs::PoseStamped& start,
|
||||
const robot_geometry_msgs::PoseStamped& goal,
|
||||
std::vector<robot_geometry_msgs::PoseStamped>& plan) override
|
||||
{
|
||||
plan.clear();
|
||||
if (calls_made_++ < fail_first_attempts_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// `lateral_offset` đẩy các điểm GIỮA sang bên theo pháp tuyến của đoạn start->goal, giữ nguyên
|
||||
// hai đầu. Đây là hình dạng của một đường vòng thật, và là thứ guard `max_deviation` phải đo.
|
||||
const double dx = goal.pose.position.x - start.pose.position.x;
|
||||
const double dy = goal.pose.position.y - start.pose.position.y;
|
||||
const double length = std::hypot(dx, dy);
|
||||
const double nx = length > 1e-9 ? -dy / length : 0.0;
|
||||
const double ny = length > 1e-9 ? dx / length : 0.0;
|
||||
|
||||
for (int i = 0; i < points_; ++i)
|
||||
{
|
||||
const double ratio = points_ > 1 ? static_cast<double>(i) / (points_ - 1) : 1.0;
|
||||
// Hình chuông: 0 ở hai đầu, lớn nhất ở giữa.
|
||||
const double bulge = lateral_offset_ * std::sin(ratio * M_PI);
|
||||
|
||||
robot_geometry_msgs::PoseStamped pose = start;
|
||||
pose.pose.position.x = start.pose.position.x + ratio * dx + bulge * nx;
|
||||
pose.pose.position.y = start.pose.position.y + ratio * dy + bulge * ny;
|
||||
pose.pose.orientation = goal.pose.orientation;
|
||||
plan.push_back(pose);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
int fail_first_attempts_ = 0;
|
||||
int points_ = 5;
|
||||
double lateral_offset_ = 0.0; ///< [m] độ phình ngang lớn nhất so với đoạn start->goal.
|
||||
int calls_made_ = 0;
|
||||
};
|
||||
|
||||
} // namespace sbpl_recovery_test
|
||||
|
||||
BOOST_DLL_ALIAS(sbpl_recovery_test::TestLinePlanner::create, TestLinePlanner)
|
||||
BOOST_DLL_ALIAS(sbpl_recovery_test::TestLinePlanner::create, TestFlakyPlanner)
|
||||
BOOST_DLL_ALIAS(sbpl_recovery_test::TestLinePlanner::create, TestDeadPlanner)
|
||||
BOOST_DLL_ALIAS(sbpl_recovery_test::TestLinePlanner::create, TestOffsetPlanner)
|
||||
Reference in New Issue
Block a user