Find a dock plate in a laser scan and say where the robot must stop
The detection chain had a gap between line segments and a navigation goal. This is that step: segments plus the geometry of one charger, out comes the pose base_link has to reach, in the scan's own frame. Pure geometry, no TF and no action, so it is testable the same way the line extractor is - build the world, cast the rays, and the answer is known before the code runs. The tests exercise the real chain end to end, from synthetic world through robot_line_extraction into the matcher. The output is the goal pose, not the charger's pose. dock_planner deliberately carries no standoff key: it derives which edge of the robot meets the dock from which side of the goal the robot stands on, and that only works because the goal already is where base_link must end up. The standoff is also a property of the charger - how deep its contacts sit - so it belongs to the profile. apex_x/apex_y keep the pre-offset vertex, because a wrong offset and a wrong detection look identical from the final pose alone. Matching a V: The vertex is the intersection of the two fitted lines, not the midpoint of their endpoints. A line is a least-squares fit over tens of rays; an endpoint is one ray and much noisier. Both segments must END near that intersection. Any two non-parallel walls cross somewhere, and without this gate a corridor corner reads as a dock. The vertex angle is measured between the two wing vectors from the apex, not between the two normals. The two agree at exactly 90 degrees and diverge everywhere else, so confusing them is a silent error at every other opening. Wing length is what rejects a pallet corner: a pallet has a perfect right angle, it just has metre-long sides. Yaw is the bisector pointing back at the sensor, and its sign is chosen by comparing against the sensor position rather than by the order of the two wings. That order flips with which side the robot approaches from, and being 180 degrees out here means reversing into the plate instead of driving in. Confidence is the worst of the three criteria, not their mean. A mean lets a shape with a badly wrong angle through on the strength of the other two. The search gate is not optional in a warehouse: any two perpendicular pallets are a V. candidate_count reports how many survived every gate, so an ambiguous scene is visible to the layer above rather than silently resolved. A failed detection reports which gate it failed. "Nothing there" and "something there but the wrong length" need opposite responses, and one failure line cannot carry that. Declaring a pattern that is not implemented is refused at configure time. Accepting it would produce a detector that never finds anything, which looks exactly like a charger out of range.
This commit is contained in:
179
CMakeLists.txt
Normal file
179
CMakeLists.txt
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.0.2)
|
||||||
|
project(dock_pattern_detector VERSION 0.1.0 LANGUAGES CXX)
|
||||||
|
|
||||||
|
# ========================================================
|
||||||
|
# Build mode detection
|
||||||
|
# ========================================================
|
||||||
|
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
|
||||||
|
set(BUILDING_WITH_CATKIN TRUE)
|
||||||
|
message(STATUS "Building dock_pattern_detector with Catkin")
|
||||||
|
else()
|
||||||
|
set(BUILDING_WITH_CATKIN FALSE)
|
||||||
|
message(STATUS "Building dock_pattern_detector with Standalone CMake")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
# ========================================================
|
||||||
|
# C++ Standard
|
||||||
|
# ========================================================
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
|
||||||
|
# ========================================================
|
||||||
|
# Dependencies
|
||||||
|
# ========================================================
|
||||||
|
# Eigen là phụ thuộc DUY NHẤT, và chỉ dùng ở bước gộp hai đoạn. Gói này cố ý không kéo robot_cpp,
|
||||||
|
# costmap hay message nào — đó là thứ khiến nó test được bằng scan tổng hợp, không cần runtime.
|
||||||
|
find_package(Eigen3 REQUIRED)
|
||||||
|
|
||||||
|
|
||||||
|
# ========================================================
|
||||||
|
# Catkin configuration
|
||||||
|
# ========================================================
|
||||||
|
if(BUILDING_WITH_CATKIN)
|
||||||
|
|
||||||
|
find_package(catkin REQUIRED COMPONENTS
|
||||||
|
robot_line_extraction
|
||||||
|
)
|
||||||
|
|
||||||
|
catkin_package(
|
||||||
|
INCLUDE_DIRS
|
||||||
|
include
|
||||||
|
|
||||||
|
LIBRARIES
|
||||||
|
dock_pattern_detector
|
||||||
|
|
||||||
|
CATKIN_DEPENDS
|
||||||
|
robot_line_extraction
|
||||||
|
|
||||||
|
DEPENDS
|
||||||
|
EIGEN3
|
||||||
|
)
|
||||||
|
|
||||||
|
include_directories(SYSTEM ${catkin_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
# ========================================================
|
||||||
|
# Library
|
||||||
|
# ========================================================
|
||||||
|
add_library(dock_pattern_detector SHARED
|
||||||
|
src/dock_profile.cpp
|
||||||
|
src/v_pattern_matcher.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_options(dock_pattern_detector PRIVATE -Wall -Wextra)
|
||||||
|
|
||||||
|
target_include_directories(dock_pattern_detector
|
||||||
|
PUBLIC
|
||||||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||||
|
$<INSTALL_INTERFACE:include>
|
||||||
|
)
|
||||||
|
|
||||||
|
# Header của Eigen vào dạng SYSTEM: gói này build với -Wall -Wextra, mà Eigen sinh warning riêng
|
||||||
|
# của nó — để lẫn vào thì warning của chính gói này vô hình.
|
||||||
|
target_include_directories(dock_pattern_detector SYSTEM
|
||||||
|
PUBLIC
|
||||||
|
${EIGEN3_INCLUDE_DIRS}
|
||||||
|
)
|
||||||
|
|
||||||
|
if(BUILDING_WITH_CATKIN)
|
||||||
|
target_link_libraries(dock_pattern_detector PUBLIC ${catkin_LIBRARIES})
|
||||||
|
else()
|
||||||
|
target_include_directories(dock_pattern_detector SYSTEM
|
||||||
|
PUBLIC
|
||||||
|
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../robot_line_extraction/include>"
|
||||||
|
)
|
||||||
|
target_link_libraries(dock_pattern_detector PUBLIC robot_line_extraction)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT BUILDING_WITH_CATKIN)
|
||||||
|
set_target_properties(dock_pattern_detector PROPERTIES
|
||||||
|
POSITION_INDEPENDENT_CODE ON
|
||||||
|
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||||
|
BUILD_RPATH "${CMAKE_BINARY_DIR}"
|
||||||
|
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
# ========================================================
|
||||||
|
# Install
|
||||||
|
# ========================================================
|
||||||
|
if(BUILDING_WITH_CATKIN)
|
||||||
|
|
||||||
|
install(TARGETS dock_pattern_detector
|
||||||
|
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
|
||||||
|
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
|
||||||
|
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
|
||||||
|
)
|
||||||
|
|
||||||
|
install(DIRECTORY include/${PROJECT_NAME}/
|
||||||
|
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
|
||||||
|
FILES_MATCHING PATTERN "*.h"
|
||||||
|
)
|
||||||
|
|
||||||
|
else()
|
||||||
|
|
||||||
|
include(GNUInstallDirs)
|
||||||
|
|
||||||
|
install(TARGETS dock_pattern_detector
|
||||||
|
EXPORT ${PROJECT_NAME}-targets
|
||||||
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||||
|
)
|
||||||
|
|
||||||
|
install(EXPORT ${PROJECT_NAME}-targets
|
||||||
|
FILE ${PROJECT_NAME}-targets.cmake
|
||||||
|
NAMESPACE ${PROJECT_NAME}::
|
||||||
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
|
||||||
|
)
|
||||||
|
|
||||||
|
install(DIRECTORY include/${PROJECT_NAME}/
|
||||||
|
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
|
||||||
|
FILES_MATCHING PATTERN "*.h"
|
||||||
|
)
|
||||||
|
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
# ========================================================
|
||||||
|
# Tests
|
||||||
|
# ========================================================
|
||||||
|
if(CATKIN_ENABLE_TESTING AND BUILDING_WITH_CATKIN)
|
||||||
|
|
||||||
|
if(NOT COMMAND catkin_add_gtest)
|
||||||
|
message(FATAL_ERROR "catkin_add_gtest NOT FOUND")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
catkin_add_gtest(v_pattern_matcher_test test/v_pattern_matcher_test.cpp)
|
||||||
|
|
||||||
|
if(TARGET v_pattern_matcher_test)
|
||||||
|
# catkin_add_gtest đặt target EXCLUDE_FROM_ALL; mở ra để binary có mặt sau `catkin_make`
|
||||||
|
# thường, đúng quy trình verify của repo.
|
||||||
|
set_target_properties(v_pattern_matcher_test PROPERTIES EXCLUDE_FROM_ALL FALSE)
|
||||||
|
|
||||||
|
target_compile_options(v_pattern_matcher_test PRIVATE -Wall -Wextra)
|
||||||
|
|
||||||
|
target_include_directories(v_pattern_matcher_test
|
||||||
|
PRIVATE
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(v_pattern_matcher_test SYSTEM
|
||||||
|
PRIVATE
|
||||||
|
${EIGEN3_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(v_pattern_matcher_test
|
||||||
|
dock_pattern_detector
|
||||||
|
pthread
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
endif()
|
||||||
105
README.md
Normal file
105
README.md
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
# dock_pattern_detector
|
||||||
|
|
||||||
|
Tìm **biên dạng của trạm dock** trong một scan LiDAR và trả về **pose robot phải đạt**.
|
||||||
|
|
||||||
|
## Ba tầng, gói này là tầng giữa
|
||||||
|
|
||||||
|
```
|
||||||
|
robot_sensor_ports scan THÔ (ranges + intensities)
|
||||||
|
v
|
||||||
|
robot_line_extraction ranges -> đoạn thẳng
|
||||||
|
v
|
||||||
|
dock_pattern_detector đoạn thẳng + profile -> pose đích trong FRAME CỦA SCAN <-- gói này
|
||||||
|
v
|
||||||
|
(adapter) TF sang global -> ghi `dock_target`
|
||||||
|
```
|
||||||
|
|
||||||
|
Lõi này **không biết** TF, action, hay kho cảm biến là gì. Nên test được bằng scan dựng sẵn: dựng
|
||||||
|
hình trước, chiếu tia sau, đáp án biết trước và sai lệch đo được bằng số.
|
||||||
|
|
||||||
|
## Đầu ra là pose ĐÍCH, không phải pose cái sạc
|
||||||
|
|
||||||
|
`Detection::x/y/yaw` = **đỉnh hình ⊕ offset của profile**. Đó là chỗ `base_link` phải dừng.
|
||||||
|
|
||||||
|
Offset nằm ở đây chứ không ở planner vì `dock_planner` **cố ý không có khoá standoff nào** — nó suy
|
||||||
|
mép robot áp dock từ *phía robot đang đứng so với goal*, và suy được là nhờ goal ĐÃ là pose robot
|
||||||
|
phải đạt. Khoảng lùi cũng là thuộc tính của *cái sạc* (độ sâu chấu cắm), nên nó thuộc về profile.
|
||||||
|
|
||||||
|
`apex_x/apex_y` giữ lại đỉnh **trước** offset, để chẩn đoán: lệch offset và lệch dò trông giống hệt
|
||||||
|
nhau nếu chỉ nhìn pose cuối.
|
||||||
|
|
||||||
|
## Chữ V — luật khớp
|
||||||
|
|
||||||
|
Duyệt mọi cặp đoạn. Với mỗi cặp:
|
||||||
|
|
||||||
|
1. **Đỉnh = giao điểm hai ĐƯỜNG đã khớp**, không phải trung điểm hai đầu mút. Đường là kết quả bình
|
||||||
|
phương tối thiểu trên hàng chục tia; đầu mút là một tia đơn lẻ và nhiễu hơn hẳn.
|
||||||
|
2. **Hai đầu đoạn phải nằm sát đỉnh** (`vertex_gap`). Đây là cổng phân biệt chữ V thật với hai bức
|
||||||
|
tường **cắt nhau ở xa**: hai đường không song song nào cũng có giao điểm, nhưng chỉ chữ V mới có
|
||||||
|
hai đoạn *kết thúc* tại đó.
|
||||||
|
3. **Góc trong tại đỉnh** — đo giữa hai **vector cánh** xuất phát từ đỉnh, không phải giữa hai pháp
|
||||||
|
tuyến. Hai cách trùng nhau ở đúng 90 độ và lệch nhau ở mọi góc khác, nên nhầm là sai âm thầm.
|
||||||
|
4. **Chiều dài mỗi cánh** so với `wing_length`. Đây là cổng chặn **góc pallet**: pallet có đúng 90
|
||||||
|
độ, chỉ khác chiều dài cạnh.
|
||||||
|
5. **Cổng tìm kiếm** — `search_radius` quanh pose kỳ vọng, `search_fov` quanh trục x.
|
||||||
|
|
||||||
|
### Hướng: chọn dấu theo cảm biến, không theo thứ tự cánh
|
||||||
|
|
||||||
|
`yaw` là phân giác **hướng ra phía cảm biến**. Dấu chọn bằng cách so với vector từ đỉnh về gốc cảm
|
||||||
|
biến. Chọn theo thứ tự hai cánh thì thứ tự đổi theo phía robot đứng — và lệch 180 độ ở đây nghĩa là
|
||||||
|
**robot lùi vào tấm sạc thay vì tiến vào**.
|
||||||
|
|
||||||
|
### Điểm tin cậy lấy trục TỆ NHẤT
|
||||||
|
|
||||||
|
`confidence` = min(khớp góc, khớp cánh 1, khớp cánh 2, số tia đỡ). Không phải trung bình: trung bình
|
||||||
|
cho một hình sai hẳn góc nhưng đúng chiều dài lọt qua bằng điểm của hai tiêu chí kia.
|
||||||
|
|
||||||
|
## Vì sao cổng prior là bắt buộc ngoài hiện trường
|
||||||
|
|
||||||
|
Trong kho, **hai pallet vuông góc bất kỳ cũng là một chữ V**. `search_radius`/`search_fov` là cổng
|
||||||
|
chính chống dương tính giả. `has_prior: false` chấp nhận được trong sim; trên robot thì không.
|
||||||
|
|
||||||
|
`candidate_count` đếm số ứng viên vượt hết mọi cổng. `> 1` là tín hiệu cho tầng trên: trong tầm có
|
||||||
|
nhiều hình giống nhau, và prior mới là thứ quyết định lấy cái nào.
|
||||||
|
|
||||||
|
## Khi không dò ra, `reason` nói rõ chuyện gì
|
||||||
|
|
||||||
|
Không phải một dòng "detection failed". Sáu lý do, xếp theo mức cụ thể:
|
||||||
|
|
||||||
|
| `reason` chứa | nghĩa là | xử trí |
|
||||||
|
|---|---|---|
|
||||||
|
| `need 2 line segments` | chỉ thấy một mặt | một cánh bị che, hoặc đứng lệch |
|
||||||
|
| `do not end at the crossing` | hai tường cắt nhau ở xa | không phải trạm; đây là cổng chống nhầm |
|
||||||
|
| `wrong angle` | có đỉnh, sai góc mở | sai profile, hoặc vật thể khác |
|
||||||
|
| `wrong wing length` | đúng góc, sai chiều dài | **rất có thể là góc pallet** |
|
||||||
|
| `outside the search gate` | hình đúng, sai chỗ | trạm của người khác, hoặc prior sai |
|
||||||
|
| `too few supporting rays` | ở quá xa | tiến lại gần, hoặc hạ `min_wing_points` |
|
||||||
|
|
||||||
|
## Bốn dạng, hiện có một
|
||||||
|
|
||||||
|
| `pattern` | trạng thái | cần thêm gì |
|
||||||
|
|---|---|---|
|
||||||
|
| `v` | **chạy được** | — |
|
||||||
|
| `recess` | chưa | chỉ cần đoạn thẳng, lõi đã có |
|
||||||
|
| `cylinder` | chưa | lõi khớp đường tròn |
|
||||||
|
| `arc` | chưa | lõi khớp cung |
|
||||||
|
|
||||||
|
Khai một `pattern` chưa hiện thực thì `configure()` **từ chối lúc nạp** kèm lý do, thay vì nhận
|
||||||
|
config rồi không bao giờ dò ra gì — hai thứ đó nhìn giống hệt nhau từ bên ngoài.
|
||||||
|
|
||||||
|
## Mọi số trong profile phải là số ĐO
|
||||||
|
|
||||||
|
`vertex_angle`, `wing_length`, `offset_x` — đoán một con số ở đây là dò trúng một hình không tồn
|
||||||
|
tại, hoặc trượt cái hình có thật. Giá trị mặc định trong code chỉ để test chạy, **không phải** giá
|
||||||
|
trị cho trạm thật.
|
||||||
|
|
||||||
|
## Build và test
|
||||||
|
|
||||||
|
```bash
|
||||||
|
catkin_make --pkg dock_pattern_detector
|
||||||
|
./devel/lib/dock_pattern_detector/v_pattern_matcher_test # 16 test
|
||||||
|
```
|
||||||
|
|
||||||
|
Test chạy đúng chuỗi mà handler sẽ chạy: dựng thế giới → chiếu tia → `robot_line_extraction` →
|
||||||
|
matcher. Nhóm **từ chối** quan trọng hơn nhóm dò đúng, và mỗi ca từ chối khẳng định luôn *lý do* —
|
||||||
|
một test chỉ kiểm `!valid` sẽ pass cả khi extractor không ra đoạn nào, tức matcher chưa hề bị thử.
|
||||||
174
include/dock_pattern_detector/dock_profile.h
Normal file
174
include/dock_pattern_detector/dock_profile.h
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* dock_pattern_detector — hình học của MỘT loại trạm dock, và kết quả một lượt khớp.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#ifndef DOCK_PATTERN_DETECTOR_DOCK_PROFILE_H_
|
||||||
|
#define DOCK_PATTERN_DETECTOR_DOCK_PROFILE_H_
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace dock_pattern_detector
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @enum PatternType
|
||||||
|
* @brief Biên dạng của mục tiêu dock.
|
||||||
|
*
|
||||||
|
* Mỗi giá trị là một matcher riêng. Thêm dạng mới = thêm một giá trị ở đây + một matcher, không
|
||||||
|
* đụng handler và không đụng lõi khớp đường thẳng.
|
||||||
|
*/
|
||||||
|
enum class PatternType
|
||||||
|
{
|
||||||
|
kV, ///< Hai cánh gặp nhau tại một đỉnh. Tấm dẫn hướng chữ V.
|
||||||
|
kRecess, ///< Mặt phẳng có rãnh/gờ vuông góc: ba đoạn, hai góc ~90 độ.
|
||||||
|
kCylinder, ///< Trụ tròn. CẦN lõi khớp đường tròn — chưa hiện thực.
|
||||||
|
kArc ///< Cung tròn hở. CẦN lõi khớp cung — chưa hiện thực.
|
||||||
|
};
|
||||||
|
|
||||||
|
const char* toString(PatternType pattern);
|
||||||
|
|
||||||
|
/// @brief Đọc tên dạng từ YAML. @return false nếu tên không có thật.
|
||||||
|
bool patternFromString(const std::string& name, PatternType& out, std::string& error);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct DockProfile
|
||||||
|
* @brief Hình học và offset của MỘT cái sạc.
|
||||||
|
*
|
||||||
|
* Một profile cho một loại trạm. Order chọn profile lúc chạy qua `actionParameters`, nên hai trạm
|
||||||
|
* khác hình dùng chung một handler.
|
||||||
|
*
|
||||||
|
* @warning Mọi số ở đây phải là số **ĐO** trên trạm thật. Đoán một `vertex_angle` là dò trúng một
|
||||||
|
* hình không tồn tại, hoặc trượt cái hình có thật.
|
||||||
|
*/
|
||||||
|
struct DockProfile
|
||||||
|
{
|
||||||
|
PatternType pattern = PatternType::kV;
|
||||||
|
|
||||||
|
// --- Hình chữ V --------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [rad] Góc TRONG tại đỉnh, giữa hai cánh. Tấm mở 90 độ thì đặt 1.5708.
|
||||||
|
*
|
||||||
|
* Đo giữa hai vector cánh xuất phát từ đỉnh, không phải giữa hai pháp tuyến — hai cách đo trùng
|
||||||
|
* nhau ở đúng 90 độ và lệch nhau ở mọi góc khác, nên nhầm là sai âm thầm.
|
||||||
|
*/
|
||||||
|
double vertex_angle = M_PI / 2.0;
|
||||||
|
|
||||||
|
/// [m] Chiều dài MỘT cánh, từ đỉnh ra đầu mút.
|
||||||
|
double wing_length = 0.20;
|
||||||
|
|
||||||
|
/// [rad] Sai lệch góc đỉnh còn chấp nhận.
|
||||||
|
double angle_tolerance = 0.17;
|
||||||
|
|
||||||
|
/// [m] Sai lệch chiều dài cánh còn chấp nhận.
|
||||||
|
double length_tolerance = 0.05;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [m] Hai đầu cánh phải nằm trong ngần này quanh giao điểm hai đường.
|
||||||
|
*
|
||||||
|
* Cổng này phân biệt một chữ V thật với hai bức tường **cắt nhau ở xa**: hai đường bất kỳ không
|
||||||
|
* song song đều có giao điểm, nhưng chỉ chữ V mới có hai đầu đoạn nằm ngay tại đó.
|
||||||
|
*/
|
||||||
|
double vertex_gap = 0.05;
|
||||||
|
|
||||||
|
// --- Cổng tìm kiếm -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Có pose kỳ vọng của trạm hay không (trong frame của scan).
|
||||||
|
*
|
||||||
|
* `false` = tìm khắp scan. Chấp nhận được trong sim; ngoài kho thì hai pallet vuông góc bất kỳ
|
||||||
|
* cũng là một chữ V, nên đây là cổng chính chống dương tính giả.
|
||||||
|
*/
|
||||||
|
bool has_prior = false;
|
||||||
|
double prior_x = 0.0; ///< [m] trong frame của scan
|
||||||
|
double prior_y = 0.0; ///< [m]
|
||||||
|
|
||||||
|
/// [m] Ứng viên cách pose kỳ vọng xa hơn ngần này thì loại. Chỉ dùng khi `has_prior`.
|
||||||
|
double search_radius = 1.5;
|
||||||
|
|
||||||
|
/// [rad] Nửa góc quạt quanh trục x của cảm biến. <= 0 = không giới hạn quạt.
|
||||||
|
double search_fov = 0.0;
|
||||||
|
|
||||||
|
// --- Offset đầu ra -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [m] Lùi từ ĐỈNH hình ra pose robot phải đạt, dọc pháp tuyến hướng RA phía robot.
|
||||||
|
*
|
||||||
|
* Offset nằm ở đây chứ không ở planner: `dock_planner` cố ý không có khoá standoff nào và suy mép
|
||||||
|
* robot áp dock từ phía robot đứng so với goal — suy được là nhờ goal ĐÃ là pose robot phải đạt.
|
||||||
|
* Và khoảng lùi là thuộc tính của *cái sạc* (độ sâu chấu cắm), nên nó thuộc về profile.
|
||||||
|
*/
|
||||||
|
double offset_x = 0.0;
|
||||||
|
|
||||||
|
/// [m] Bù tấm lắp lệch NGANG so với đầu cắm. Trong hệ của chính hình, y sang trái.
|
||||||
|
double offset_y = 0.0;
|
||||||
|
|
||||||
|
/// [rad] Bù tấm lắp XOAY so với đầu cắm.
|
||||||
|
double offset_yaw = 0.0;
|
||||||
|
|
||||||
|
// --- Cổng chất lượng ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Số tia đỡ tối thiểu cho MỘT cánh. Dưới ngưỡng này phép khớp không còn nghĩa thống kê.
|
||||||
|
std::size_t min_wing_points = 5;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [-] Độ tin tối thiểu để nhận ứng viên, trong [0, 1].
|
||||||
|
*
|
||||||
|
* Điểm tin tổng hợp mức khớp góc, chiều dài và số tia đỡ. Nâng lên là khắt khe hơn với hình méo.
|
||||||
|
*/
|
||||||
|
double min_confidence = 0.5;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Kiểm miền giá trị.
|
||||||
|
* @param[out] error Khoá nào sai và vì sao.
|
||||||
|
*
|
||||||
|
* Kiểm lúc nạp config chứ không phải lúc dò: một profile không thể khớp được sẽ biểu hiện thành
|
||||||
|
* "dò mãi không ra", nhìn giống hệt trạm không có trong tầm.
|
||||||
|
*/
|
||||||
|
bool validate(std::string& error) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct Detection
|
||||||
|
* @brief Kết quả một lượt khớp, trong **frame của scan**.
|
||||||
|
*
|
||||||
|
* Quy sang global frame là việc của tầng adapter — lõi này không biết TF là gì.
|
||||||
|
*/
|
||||||
|
struct Detection
|
||||||
|
{
|
||||||
|
bool valid = false;
|
||||||
|
|
||||||
|
/// Pose ĐÍCH: đỉnh hình đã cộng offset của profile. Đây là thứ đi ra `dock_target`.
|
||||||
|
double x = 0.0; // [m]
|
||||||
|
double y = 0.0; // [m]
|
||||||
|
double yaw = 0.0; // [rad]
|
||||||
|
|
||||||
|
/// Đỉnh hình TRƯỚC offset, giữ lại để chẩn đoán — lệch offset và lệch dò trông giống nhau.
|
||||||
|
double apex_x = 0.0; // [m]
|
||||||
|
double apex_y = 0.0; // [m]
|
||||||
|
|
||||||
|
/// [-] Trong [0, 1]. 1 = khớp hoàn hảo cả góc, chiều dài lẫn số tia đỡ.
|
||||||
|
double confidence = 0.0;
|
||||||
|
|
||||||
|
/// [rad] Góc đỉnh đo được. So với `vertex_angle` của profile để biết hình méo cỡ nào.
|
||||||
|
double measured_vertex_angle = 0.0;
|
||||||
|
|
||||||
|
/// [m] Chiều dài hai cánh đo được.
|
||||||
|
double measured_wing_length[2] = { 0.0, 0.0 };
|
||||||
|
|
||||||
|
/// Số ứng viên vượt hết mọi cổng. > 1 nghĩa là trong tầm có nhiều hình giống nhau.
|
||||||
|
std::size_t candidate_count = 0;
|
||||||
|
|
||||||
|
/// Vì sao không nhận, khi `valid == false`. Một câu người đọc hiểu được, không phải mã lỗi.
|
||||||
|
std::string reason;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace dock_pattern_detector
|
||||||
|
|
||||||
|
#endif // DOCK_PATTERN_DETECTOR_DOCK_PROFILE_H_
|
||||||
80
include/dock_pattern_detector/v_pattern_matcher.h
Normal file
80
include/dock_pattern_detector/v_pattern_matcher.h
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* dock_pattern_detector — tìm tấm dẫn hướng chữ V trong một tập đoạn thẳng.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#ifndef DOCK_PATTERN_DETECTOR_V_PATTERN_MATCHER_H_
|
||||||
|
#define DOCK_PATTERN_DETECTOR_V_PATTERN_MATCHER_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <robot_line_extraction/line.h>
|
||||||
|
|
||||||
|
#include <dock_pattern_detector/dock_profile.h>
|
||||||
|
|
||||||
|
namespace dock_pattern_detector
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class VPatternMatcher
|
||||||
|
* @brief Ghép hai đoạn thẳng thành một chữ V và trả về pose đích.
|
||||||
|
*
|
||||||
|
* Hình học thuần. Đầu vào là danh sách đoạn của `robot_line_extraction`, đầu ra là một pose trong
|
||||||
|
* **frame của scan**. Không TF, không action, không kho cảm biến — nên test được bằng scan dựng
|
||||||
|
* sẵn, biết trước đáp án.
|
||||||
|
*
|
||||||
|
* ## Luật khớp
|
||||||
|
*
|
||||||
|
* Duyệt mọi cặp đoạn. Với mỗi cặp:
|
||||||
|
*
|
||||||
|
* 1. **Đỉnh = giao điểm hai ĐƯỜNG đã khớp**, không phải trung điểm hai đầu mút. Đường là kết quả
|
||||||
|
* bình phương tối thiểu trên hàng chục tia; đầu mút là một tia đơn lẻ và nhiễu hơn hẳn.
|
||||||
|
* 2. **Hai đầu đoạn phải nằm sát đỉnh** (`vertex_gap`). Đây là cổng phân biệt chữ V thật với hai
|
||||||
|
* bức tường cắt nhau ở xa: hai đường không song song nào cũng có giao điểm, nhưng chỉ chữ V mới
|
||||||
|
* có hai đầu đoạn nằm ngay tại đó.
|
||||||
|
* 3. **Góc trong tại đỉnh** đo giữa hai vector cánh xuất phát từ đỉnh, so với `vertex_angle`.
|
||||||
|
* 4. **Chiều dài mỗi cánh** so với `wing_length`.
|
||||||
|
* 5. **Cổng prior** — ứng viên ra ngoài `search_radius` quanh pose kỳ vọng, hoặc ngoài quạt
|
||||||
|
* `search_fov`, thì loại. Không có cổng này thì trong kho, hai pallet vuông góc bất kỳ cũng là
|
||||||
|
* một chữ V.
|
||||||
|
*
|
||||||
|
* ## Hướng
|
||||||
|
*
|
||||||
|
* `yaw` là **phân giác hướng RA phía cảm biến**. Chọn dấu bằng cách so với vector từ đỉnh về gốc
|
||||||
|
* cảm biến, không bằng thứ tự hai cánh — thứ tự đổi theo phía robot đứng, và lệch 180 độ ở đây là
|
||||||
|
* robot lùi vào tường.
|
||||||
|
*
|
||||||
|
* @note Không thread-safe. Một matcher cho một consumer.
|
||||||
|
*/
|
||||||
|
class VPatternMatcher
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
VPatternMatcher() = default;
|
||||||
|
|
||||||
|
/// @brief Nhận profile. Sai miền thì giữ bộ cũ và trả false.
|
||||||
|
bool configure(const DockProfile& profile, std::string& error);
|
||||||
|
|
||||||
|
const DockProfile& profile() const
|
||||||
|
{
|
||||||
|
return profile_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Tìm chữ V trong @p lines.
|
||||||
|
*
|
||||||
|
* @return Kết quả luôn hợp lệ để đọc. `valid == false` kèm `reason` nói rõ vì sao — "chưa thấy"
|
||||||
|
* và "thấy nhưng méo" là hai chuyện khác nhau, và người vận hành cần biết là chuyện nào.
|
||||||
|
*/
|
||||||
|
Detection match(const std::vector<robot_line_extraction::Line>& lines) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
DockProfile profile_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace dock_pattern_detector
|
||||||
|
|
||||||
|
#endif // DOCK_PATTERN_DETECTOR_V_PATTERN_MATCHER_H_
|
||||||
27
package.xml
Normal file
27
package.xml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<package>
|
||||||
|
<name>dock_pattern_detector</name>
|
||||||
|
<version>0.1.0</version>
|
||||||
|
<description>
|
||||||
|
Tìm biên dạng của trạm dock trong một scan LiDAR và trả về pose robot phải đạt.
|
||||||
|
|
||||||
|
Lõi thuần hình học: đầu vào là các đoạn thẳng do `robot_line_extraction` trích ra, đầu ra là một
|
||||||
|
pose trong FRAME CỦA SCAN kèm điểm tin cậy. Không TF, không action, không kho cảm biến — nên
|
||||||
|
test được bằng scan dựng sẵn, biết trước đáp án.
|
||||||
|
|
||||||
|
Mỗi loại trạm là một `profile`: hình học (`vertex_angle`, `wing_length`...) cộng offset từ đỉnh
|
||||||
|
hình ra pose robot phải đạt. Offset nằm ở đây chứ không ở planner vì `dock_planner` cố ý không
|
||||||
|
có khoá standoff nào — nó suy mép robot áp dock từ phía robot đứng so với goal, suy được là nhờ
|
||||||
|
goal ĐÃ là pose robot phải đạt.
|
||||||
|
|
||||||
|
Hiện có dạng chữ V. Khung để sẵn cho rãnh/gờ vuông góc, trụ tròn và cung tròn; hai dạng sau cần
|
||||||
|
thêm lõi khớp đường tròn.
|
||||||
|
</description>
|
||||||
|
<author>T800 Robotics</author>
|
||||||
|
<maintainer email="tdd@gmail.com">T800 Robotics</maintainer>
|
||||||
|
<license>BSD</license>
|
||||||
|
|
||||||
|
<buildtool_depend version_gte="0.5.68">catkin</buildtool_depend>
|
||||||
|
|
||||||
|
<build_depend>robot_line_extraction</build_depend>
|
||||||
|
<run_depend>robot_line_extraction</run_depend>
|
||||||
|
</package>
|
||||||
148
src/dock_profile.cpp
Normal file
148
src/dock_profile.cpp
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* dock_pattern_detector — kiểm miền giá trị của một profile dock.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#include <dock_pattern_detector/dock_profile.h>
|
||||||
|
|
||||||
|
namespace dock_pattern_detector
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
bool positive(double value)
|
||||||
|
{
|
||||||
|
return std::isfinite(value) && value > 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
const char* toString(PatternType pattern)
|
||||||
|
{
|
||||||
|
switch (pattern)
|
||||||
|
{
|
||||||
|
case PatternType::kV:
|
||||||
|
return "v";
|
||||||
|
case PatternType::kRecess:
|
||||||
|
return "recess";
|
||||||
|
case PatternType::kCylinder:
|
||||||
|
return "cylinder";
|
||||||
|
case PatternType::kArc:
|
||||||
|
return "arc";
|
||||||
|
}
|
||||||
|
return "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool patternFromString(const std::string& name, PatternType& out, std::string& error)
|
||||||
|
{
|
||||||
|
if (name == "v")
|
||||||
|
{
|
||||||
|
out = PatternType::kV;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (name == "recess")
|
||||||
|
{
|
||||||
|
out = PatternType::kRecess;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (name == "cylinder")
|
||||||
|
{
|
||||||
|
out = PatternType::kCylinder;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (name == "arc")
|
||||||
|
{
|
||||||
|
out = PatternType::kArc;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
error = "unknown pattern '" + name + "'; known patterns are: v, recess, cylinder, arc";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DockProfile::validate(std::string& error) const
|
||||||
|
{
|
||||||
|
if (!std::isfinite(vertex_angle) || vertex_angle <= 0.0 || vertex_angle >= M_PI)
|
||||||
|
{
|
||||||
|
// 0 nghĩa là hai cánh trùng nhau, pi nghĩa là chúng thành một đường thẳng. Cả hai đều không
|
||||||
|
// phải chữ V, và cả hai đều làm phép giao hai đường suy biến.
|
||||||
|
error = "vertex_angle must be in (0, pi) rad";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!positive(wing_length))
|
||||||
|
{
|
||||||
|
error = "wing_length must be finite and > 0";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!positive(angle_tolerance) || angle_tolerance >= M_PI / 2.0)
|
||||||
|
{
|
||||||
|
// Nới tới 90 độ thì mọi cặp đoạn đều "khớp góc" — cổng góc mất tác dụng hoàn toàn.
|
||||||
|
error = "angle_tolerance must be in (0, pi/2) rad";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!positive(length_tolerance) || length_tolerance >= wing_length)
|
||||||
|
{
|
||||||
|
// Dung sai >= chính chiều dài cánh nghĩa là chấp nhận cả cánh dài 0 — cổng chiều dài vô nghĩa.
|
||||||
|
error = "length_tolerance must be finite, > 0 and < wing_length";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!positive(vertex_gap))
|
||||||
|
{
|
||||||
|
error = "vertex_gap must be finite and > 0";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (vertex_gap >= wing_length)
|
||||||
|
{
|
||||||
|
// Khe đỉnh rộng bằng cả cánh thì hai bức tường cắt nhau ở bất kỳ đâu cũng lọt cổng — mất đúng
|
||||||
|
// cái cổng phân biệt chữ V thật với giao điểm ngẫu nhiên.
|
||||||
|
error = "vertex_gap must be < wing_length, otherwise any two crossing walls pass as a V";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (has_prior)
|
||||||
|
{
|
||||||
|
if (!std::isfinite(prior_x) || !std::isfinite(prior_y))
|
||||||
|
{
|
||||||
|
error = "prior_x/prior_y must be finite when has_prior is set";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!positive(search_radius))
|
||||||
|
{
|
||||||
|
error = "search_radius must be finite and > 0 when has_prior is set";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!std::isfinite(search_fov) || search_fov >= M_PI)
|
||||||
|
{
|
||||||
|
error = "search_fov must be finite and < pi rad (<= 0 disables the sector gate)";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!std::isfinite(offset_x) || !std::isfinite(offset_y) || !std::isfinite(offset_yaw))
|
||||||
|
{
|
||||||
|
error = "offset_x/offset_y/offset_yaw must be finite";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (min_wing_points < 2)
|
||||||
|
{
|
||||||
|
// Hai điểm là tối thiểu tuyệt đối để có một đoạn; dưới đó không có gì để khớp.
|
||||||
|
error = "min_wing_points must be >= 2";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!std::isfinite(min_confidence) || min_confidence < 0.0 || min_confidence > 1.0)
|
||||||
|
{
|
||||||
|
error = "min_confidence must be in [0, 1]";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (pattern != PatternType::kV)
|
||||||
|
{
|
||||||
|
// Nói thẳng thay vì nhận config rồi không bao giờ dò ra gì.
|
||||||
|
error = std::string("pattern '") + toString(pattern) +
|
||||||
|
"' is declared but not implemented yet; only 'v' is available";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dock_pattern_detector
|
||||||
311
src/v_pattern_matcher.cpp
Normal file
311
src/v_pattern_matcher.cpp
Normal file
@@ -0,0 +1,311 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* dock_pattern_detector — khớp chữ V.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#include <dock_pattern_detector/v_pattern_matcher.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace dock_pattern_detector
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
using robot_line_extraction::Line;
|
||||||
|
|
||||||
|
struct Point
|
||||||
|
{
|
||||||
|
double x = 0.0;
|
||||||
|
double y = 0.0;
|
||||||
|
};
|
||||||
|
|
||||||
|
double dist(const Point& a, const Point& b)
|
||||||
|
{
|
||||||
|
return std::hypot(a.x - b.x, a.y - b.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point startOf(const Line& line)
|
||||||
|
{
|
||||||
|
return { line.getStart()[0], line.getStart()[1] };
|
||||||
|
}
|
||||||
|
|
||||||
|
Point endOf(const Line& line)
|
||||||
|
{
|
||||||
|
return { line.getEnd()[0], line.getEnd()[1] };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Giao điểm hai đường ở dạng cực.
|
||||||
|
*
|
||||||
|
* Đường i: `x*cos(a_i) + y*sin(a_i) = r_i`. Định thức là `sin(a_2 - a_1)`, bằng 0 đúng khi hai
|
||||||
|
* đường song song — khi ấy không có đỉnh nào để nói tới.
|
||||||
|
*/
|
||||||
|
bool intersect(const Line& l1, const Line& l2, Point& out)
|
||||||
|
{
|
||||||
|
const double a1 = l1.getAngle();
|
||||||
|
const double a2 = l2.getAngle();
|
||||||
|
const double det = std::cos(a1) * std::sin(a2) - std::sin(a1) * std::cos(a2);
|
||||||
|
|
||||||
|
if (std::fabs(det) < 1e-9)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const double r1 = l1.getRadius();
|
||||||
|
const double r2 = l2.getRadius();
|
||||||
|
out.x = (r1 * std::sin(a2) - r2 * std::sin(a1)) / det;
|
||||||
|
out.y = (r2 * std::cos(a1) - r1 * std::cos(a2)) / det;
|
||||||
|
return std::isfinite(out.x) && std::isfinite(out.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Đầu mút của @p line xa @p apex nhất — đó là đầu MÚT cánh; đầu kia là chân cánh tại đỉnh.
|
||||||
|
Point farEndpoint(const Line& line, const Point& apex)
|
||||||
|
{
|
||||||
|
const Point s = startOf(line);
|
||||||
|
const Point e = endOf(line);
|
||||||
|
return dist(s, apex) >= dist(e, apex) ? s : e;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Đầu mút của @p line gần @p apex nhất.
|
||||||
|
Point nearEndpoint(const Line& line, const Point& apex)
|
||||||
|
{
|
||||||
|
const Point s = startOf(line);
|
||||||
|
const Point e = endOf(line);
|
||||||
|
return dist(s, apex) < dist(e, apex) ? s : e;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Điểm khớp trong [0, 1]: 1 khi lệch bằng 0, 0 khi lệch chạm dung sai.
|
||||||
|
double score(double deviation, double tolerance)
|
||||||
|
{
|
||||||
|
if (tolerance <= 0.0)
|
||||||
|
{
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
return std::max(0.0, 1.0 - std::fabs(deviation) / tolerance);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
bool VPatternMatcher::configure(const DockProfile& profile, std::string& error)
|
||||||
|
{
|
||||||
|
if (!profile.validate(error))
|
||||||
|
{
|
||||||
|
return false; // Giữ bộ cũ; một profile áp nửa vời còn tệ hơn không áp.
|
||||||
|
}
|
||||||
|
profile_ = profile;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Detection VPatternMatcher::match(const std::vector<Line>& lines) const
|
||||||
|
{
|
||||||
|
Detection best;
|
||||||
|
|
||||||
|
if (lines.size() < 2)
|
||||||
|
{
|
||||||
|
best.reason = "need 2 line segments, scan yielded " + std::to_string(lines.size());
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vì sao đếm riêng từng lý do loại: "thấy nhiều cặp nhưng cặp nào cũng sai góc" và "không cặp nào
|
||||||
|
// có đầu mút chụm" đòi hai cách xử trí khác hẳn nhau — chỉnh dung sai, hay chỉnh chỗ đứng.
|
||||||
|
std::size_t rejected_vertex_gap = 0;
|
||||||
|
std::size_t rejected_angle = 0;
|
||||||
|
std::size_t rejected_length = 0;
|
||||||
|
std::size_t rejected_support = 0;
|
||||||
|
std::size_t rejected_prior = 0;
|
||||||
|
std::size_t rejected_confidence = 0;
|
||||||
|
std::size_t accepted = 0;
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i + 1 < lines.size(); ++i)
|
||||||
|
{
|
||||||
|
for (std::size_t j = i + 1; j < lines.size(); ++j)
|
||||||
|
{
|
||||||
|
const Line& l1 = lines[i];
|
||||||
|
const Line& l2 = lines[j];
|
||||||
|
|
||||||
|
if (l1.numPoints() < profile_.min_wing_points || l2.numPoints() < profile_.min_wing_points)
|
||||||
|
{
|
||||||
|
++rejected_support;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Point apex;
|
||||||
|
if (!intersect(l1, l2, apex))
|
||||||
|
{
|
||||||
|
continue; // Song song. Không phải "gần đúng chữ V", chỉ là không phải chữ V.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cổng quyết định: hai ĐẦU ĐOẠN phải nằm ngay tại giao điểm. Hai bức tường không song song
|
||||||
|
// nào cũng cắt nhau ở đâu đó; chỉ chữ V thật mới có hai đoạn kết thúc tại chỗ cắt.
|
||||||
|
const Point near1 = nearEndpoint(l1, apex);
|
||||||
|
const Point near2 = nearEndpoint(l2, apex);
|
||||||
|
if (dist(near1, apex) > profile_.vertex_gap || dist(near2, apex) > profile_.vertex_gap)
|
||||||
|
{
|
||||||
|
++rejected_vertex_gap;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Góc TRONG tại đỉnh, đo giữa hai vector cánh. Không đo giữa hai pháp tuyến: hai cách trùng
|
||||||
|
// nhau ở đúng 90 độ và lệch nhau ở mọi góc khác, nên nhầm là sai âm thầm.
|
||||||
|
const Point tip1 = farEndpoint(l1, apex);
|
||||||
|
const Point tip2 = farEndpoint(l2, apex);
|
||||||
|
const double v1x = tip1.x - apex.x;
|
||||||
|
const double v1y = tip1.y - apex.y;
|
||||||
|
const double v2x = tip2.x - apex.x;
|
||||||
|
const double v2y = tip2.y - apex.y;
|
||||||
|
|
||||||
|
const double len1 = std::hypot(v1x, v1y);
|
||||||
|
const double len2 = std::hypot(v2x, v2y);
|
||||||
|
if (len1 < 1e-9 || len2 < 1e-9)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const double cos_vertex = (v1x * v2x + v1y * v2y) / (len1 * len2);
|
||||||
|
const double vertex = std::acos(std::max(-1.0, std::min(1.0, cos_vertex)));
|
||||||
|
|
||||||
|
const double angle_dev = vertex - profile_.vertex_angle;
|
||||||
|
if (std::fabs(angle_dev) > profile_.angle_tolerance)
|
||||||
|
{
|
||||||
|
++rejected_angle;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const double len_dev1 = len1 - profile_.wing_length;
|
||||||
|
const double len_dev2 = len2 - profile_.wing_length;
|
||||||
|
if (std::fabs(len_dev1) > profile_.length_tolerance ||
|
||||||
|
std::fabs(len_dev2) > profile_.length_tolerance)
|
||||||
|
{
|
||||||
|
++rejected_length;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Phân giác, rồi lật cho hướng RA phía cảm biến. Chọn dấu bằng vị trí cảm biến chứ không bằng
|
||||||
|
// thứ tự hai cánh: thứ tự đổi theo phía robot đứng, và lệch 180 độ ở đây là robot lùi vào
|
||||||
|
// tấm sạc thay vì tiến vào.
|
||||||
|
double bx = v1x / len1 + v2x / len2;
|
||||||
|
double by = v1y / len1 + v2y / len2;
|
||||||
|
const double bn = std::hypot(bx, by);
|
||||||
|
if (bn < 1e-9)
|
||||||
|
{
|
||||||
|
continue; // Hai cánh ngược chiều nhau: một đường thẳng, không phải chữ V.
|
||||||
|
}
|
||||||
|
bx /= bn;
|
||||||
|
by /= bn;
|
||||||
|
|
||||||
|
// Gốc cảm biến là (0, 0) trong frame của scan, nên vector từ đỉnh về phía cảm biến là -apex.
|
||||||
|
if (bx * (-apex.x) + by * (-apex.y) < 0.0)
|
||||||
|
{
|
||||||
|
bx = -bx;
|
||||||
|
by = -by;
|
||||||
|
}
|
||||||
|
const double yaw = std::atan2(by, bx);
|
||||||
|
|
||||||
|
if (profile_.has_prior)
|
||||||
|
{
|
||||||
|
const double d = std::hypot(apex.x - profile_.prior_x, apex.y - profile_.prior_y);
|
||||||
|
if (d > profile_.search_radius)
|
||||||
|
{
|
||||||
|
++rejected_prior;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (profile_.search_fov > 0.0 && std::fabs(std::atan2(apex.y, apex.x)) > profile_.search_fov)
|
||||||
|
{
|
||||||
|
++rejected_prior;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Điểm tin: trục tệ nhất trong ba tiêu chí, không phải trung bình. Trung bình cho một hình
|
||||||
|
// sai hẳn góc nhưng đúng chiều dài lọt qua bằng điểm của hai tiêu chí kia.
|
||||||
|
const double support = std::min(1.0, static_cast<double>(std::min(l1.numPoints(),
|
||||||
|
l2.numPoints())) /
|
||||||
|
static_cast<double>(profile_.min_wing_points * 2));
|
||||||
|
const double confidence =
|
||||||
|
std::min({ score(angle_dev, profile_.angle_tolerance),
|
||||||
|
score(len_dev1, profile_.length_tolerance),
|
||||||
|
score(len_dev2, profile_.length_tolerance), support });
|
||||||
|
|
||||||
|
if (confidence < profile_.min_confidence)
|
||||||
|
{
|
||||||
|
++rejected_confidence;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
++accepted;
|
||||||
|
if (confidence <= best.confidence && best.valid)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
best.valid = true;
|
||||||
|
best.confidence = confidence;
|
||||||
|
best.apex_x = apex.x;
|
||||||
|
best.apex_y = apex.y;
|
||||||
|
best.measured_vertex_angle = vertex;
|
||||||
|
best.measured_wing_length[0] = len1;
|
||||||
|
best.measured_wing_length[1] = len2;
|
||||||
|
|
||||||
|
// Pose đích = đỉnh dịch theo offset của profile, trong hệ của CHÍNH hình: x dọc pháp tuyến
|
||||||
|
// hướng ra phía robot, y sang trái.
|
||||||
|
const double cos_y = std::cos(yaw);
|
||||||
|
const double sin_y = std::sin(yaw);
|
||||||
|
best.x = apex.x + cos_y * profile_.offset_x - sin_y * profile_.offset_y;
|
||||||
|
best.y = apex.y + sin_y * profile_.offset_x + cos_y * profile_.offset_y;
|
||||||
|
best.yaw = std::atan2(std::sin(yaw + profile_.offset_yaw),
|
||||||
|
std::cos(yaw + profile_.offset_yaw));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
best.candidate_count = accepted;
|
||||||
|
|
||||||
|
if (!best.valid)
|
||||||
|
{
|
||||||
|
// Lý do cụ thể nhất trước: cái đếm nào lớn nhất nói đúng chỗ đang hỏng. "Không thấy gì" và
|
||||||
|
// "thấy nhưng méo" đòi hai cách xử trí ngược nhau, mà một dòng "detection failed" thì không.
|
||||||
|
if (rejected_prior > 0)
|
||||||
|
{
|
||||||
|
best.reason = std::to_string(rejected_prior) +
|
||||||
|
" candidate(s) matched the shape but fell outside the search gate";
|
||||||
|
}
|
||||||
|
else if (rejected_confidence > 0)
|
||||||
|
{
|
||||||
|
best.reason = std::to_string(rejected_confidence) +
|
||||||
|
" candidate(s) matched but scored below min_confidence";
|
||||||
|
}
|
||||||
|
else if (rejected_length > 0)
|
||||||
|
{
|
||||||
|
best.reason = std::to_string(rejected_length) + " pair(s) had the right vertex angle but the "
|
||||||
|
"wrong wing length";
|
||||||
|
}
|
||||||
|
else if (rejected_angle > 0)
|
||||||
|
{
|
||||||
|
best.reason =
|
||||||
|
std::to_string(rejected_angle) + " pair(s) met at a vertex but at the wrong angle";
|
||||||
|
}
|
||||||
|
else if (rejected_vertex_gap > 0)
|
||||||
|
{
|
||||||
|
best.reason = std::to_string(rejected_vertex_gap) +
|
||||||
|
" pair(s) of lines cross but do not end at the crossing — not a V";
|
||||||
|
}
|
||||||
|
else if (rejected_support > 0)
|
||||||
|
{
|
||||||
|
best.reason = std::to_string(rejected_support) +
|
||||||
|
" pair(s) had too few supporting rays; move closer or lower min_wing_points";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
best.reason = "no pair of segments forms a vertex";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dock_pattern_detector
|
||||||
458
test/v_pattern_matcher_test.cpp
Normal file
458
test/v_pattern_matcher_test.cpp
Normal file
@@ -0,0 +1,458 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* Kiểm khớp chữ V bằng scan TỔNG HỢP — dựng hình trước, sinh tia sau, nên biết trước đáp án.
|
||||||
|
*
|
||||||
|
* Bốn nhóm:
|
||||||
|
* 1. dò đúng — đỉnh, hướng, offset, hình nghiêng;
|
||||||
|
* 2. TỪ CHỐI — đây mới là phần quan trọng: pallet đánh lừa, tường cắt nhau ở xa, hình méo;
|
||||||
|
* 3. cổng tìm kiếm — prior và quạt;
|
||||||
|
* 4. nhiều ứng viên, và config hỏng.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <limits>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <robot_line_extraction/line_extractor.h>
|
||||||
|
|
||||||
|
#include <dock_pattern_detector/v_pattern_matcher.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
using dock_pattern_detector::Detection;
|
||||||
|
using dock_pattern_detector::DockProfile;
|
||||||
|
using dock_pattern_detector::PatternType;
|
||||||
|
using dock_pattern_detector::VPatternMatcher;
|
||||||
|
using robot_line_extraction::Line;
|
||||||
|
using robot_line_extraction::LineExtractionParams;
|
||||||
|
using robot_line_extraction::LineExtractor;
|
||||||
|
using robot_line_extraction::ScanGeometry;
|
||||||
|
|
||||||
|
constexpr double kInf = std::numeric_limits<double>::infinity();
|
||||||
|
|
||||||
|
struct Segment
|
||||||
|
{
|
||||||
|
double ax, ay, bx, by; // [m]
|
||||||
|
};
|
||||||
|
|
||||||
|
ScanGeometry geometry(std::size_t rays = 600, double angle_min = -0.8, double angle_max = 0.8)
|
||||||
|
{
|
||||||
|
ScanGeometry g;
|
||||||
|
g.angle_min = angle_min;
|
||||||
|
g.angle_increment = (angle_max - angle_min) / static_cast<double>(rays - 1);
|
||||||
|
g.size = rays;
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Chiếu tia lên một tập đoạn thẳng, lấy điểm trúng gần nhất. Không trúng gì thì kInf.
|
||||||
|
std::vector<double> rayCast(const ScanGeometry& g, const std::vector<Segment>& world)
|
||||||
|
{
|
||||||
|
std::vector<double> ranges(g.size, kInf);
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < g.size; ++i)
|
||||||
|
{
|
||||||
|
const double bearing = g.angle_min + static_cast<double>(i) * g.angle_increment;
|
||||||
|
const double dx = std::cos(bearing);
|
||||||
|
const double dy = std::sin(bearing);
|
||||||
|
|
||||||
|
for (const Segment& s : world)
|
||||||
|
{
|
||||||
|
const double ex = s.bx - s.ax;
|
||||||
|
const double ey = s.by - s.ay;
|
||||||
|
const double det = dx * (-ey) - dy * (-ex);
|
||||||
|
if (std::fabs(det) < 1e-12)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const double t = (s.ax * (-ey) - s.ay * (-ex)) / det;
|
||||||
|
const double u = (dx * s.ay - dy * s.ax) / det;
|
||||||
|
if (t > 0.0 && u >= 0.0 && u <= 1.0 && t < ranges[i])
|
||||||
|
{
|
||||||
|
ranges[i] = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ranges;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Hai cánh của một tấm chữ V.
|
||||||
|
*
|
||||||
|
* @param apex_x [m] đỉnh trên trục x
|
||||||
|
* @param half_angle [rad] độ nghiêng mỗi cánh so với trục y
|
||||||
|
* @param wing [m] chiều dài một cánh
|
||||||
|
* @param yaw [rad] xoay cả tấm quanh đỉnh
|
||||||
|
*
|
||||||
|
* Góc TRONG tại đỉnh = pi - 2*half_angle. `half = pi/4` cho tấm mở 90 độ.
|
||||||
|
*/
|
||||||
|
std::vector<Segment> vPlate(double apex_x, double half_angle, double wing, double yaw = 0.0)
|
||||||
|
{
|
||||||
|
const double c = std::cos(yaw);
|
||||||
|
const double s = std::sin(yaw);
|
||||||
|
const auto rot = [&](double x, double y, double& ox, double& oy) {
|
||||||
|
ox = apex_x + c * (x - apex_x) - s * y;
|
||||||
|
oy = s * (x - apex_x) + c * y;
|
||||||
|
};
|
||||||
|
|
||||||
|
const double tip_x = apex_x + wing * std::sin(half_angle);
|
||||||
|
const double tip_y = wing * std::cos(half_angle);
|
||||||
|
|
||||||
|
Segment w1;
|
||||||
|
Segment w2;
|
||||||
|
rot(apex_x, 0.0, w1.ax, w1.ay);
|
||||||
|
rot(tip_x, tip_y, w1.bx, w1.by);
|
||||||
|
rot(apex_x, 0.0, w2.ax, w2.ay);
|
||||||
|
rot(tip_x, -tip_y, w2.bx, w2.by);
|
||||||
|
return { w1, w2 };
|
||||||
|
}
|
||||||
|
|
||||||
|
DockProfile vProfile()
|
||||||
|
{
|
||||||
|
DockProfile p;
|
||||||
|
p.pattern = PatternType::kV;
|
||||||
|
p.vertex_angle = M_PI / 2.0; // [rad] tấm mở 90 độ
|
||||||
|
p.wing_length = 0.30; // [m]
|
||||||
|
p.angle_tolerance = 0.20; // [rad]
|
||||||
|
p.length_tolerance = 0.06; // [m]
|
||||||
|
p.vertex_gap = 0.05; // [m]
|
||||||
|
p.min_wing_points = 5;
|
||||||
|
p.min_confidence = 0.3;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
LineExtractionParams extractorParams()
|
||||||
|
{
|
||||||
|
LineExtractionParams p;
|
||||||
|
p.min_split_dist = 0.02; // [m]
|
||||||
|
p.max_line_gap = 0.08; // [m]
|
||||||
|
p.min_line_length = 0.05; // [m]
|
||||||
|
p.min_line_points = 5;
|
||||||
|
p.outlier_dist = 0.10; // [m]
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Dựng thế giới, chiếu tia, trích đoạn, khớp — đúng chuỗi mà handler sẽ chạy.
|
||||||
|
class Rig
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit Rig(const DockProfile& profile = vProfile())
|
||||||
|
{
|
||||||
|
std::string error;
|
||||||
|
ok_ = extractor_.configure(extractorParams(), error) && matcher_.configure(profile, error);
|
||||||
|
error_ = error;
|
||||||
|
}
|
||||||
|
|
||||||
|
Detection run(const std::vector<Segment>& world, const ScanGeometry& g = geometry())
|
||||||
|
{
|
||||||
|
std::string error;
|
||||||
|
if (!extractor_.extract(g, rayCast(g, world), lines_, error))
|
||||||
|
{
|
||||||
|
Detection d;
|
||||||
|
d.reason = error;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
return matcher_.match(lines_);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ok_ = false;
|
||||||
|
std::string error_;
|
||||||
|
LineExtractor extractor_;
|
||||||
|
VPatternMatcher matcher_;
|
||||||
|
std::vector<Line> lines_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ================================================================================================
|
||||||
|
// 1. Dò đúng
|
||||||
|
// ================================================================================================
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, FindsTheApexOfAPlateInFront)
|
||||||
|
{
|
||||||
|
Rig rig;
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
const Detection d = rig.run(vPlate(1.20, M_PI / 4.0, 0.30));
|
||||||
|
|
||||||
|
ASSERT_TRUE(d.valid) << d.reason;
|
||||||
|
EXPECT_NEAR(d.apex_x, 1.20, 0.02);
|
||||||
|
EXPECT_NEAR(d.apex_y, 0.00, 0.02);
|
||||||
|
EXPECT_NEAR(d.measured_vertex_angle, M_PI / 2.0, 0.10);
|
||||||
|
EXPECT_GT(d.confidence, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, TheYawPointsBackAtTheSensorNotIntoThePlate)
|
||||||
|
{
|
||||||
|
Rig rig;
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
const Detection d = rig.run(vPlate(1.20, M_PI / 4.0, 0.30));
|
||||||
|
ASSERT_TRUE(d.valid) << d.reason;
|
||||||
|
|
||||||
|
// Tấm ở phía +x, cảm biến ở gốc, nên phân giác phải chỉ về -x tức yaw ~ pi. Lệch 180 độ ở đây là
|
||||||
|
// robot lùi vào tấm sạc thay vì tiến vào — chính vì thế dấu được chọn theo vị trí cảm biến chứ
|
||||||
|
// không theo thứ tự hai cánh.
|
||||||
|
EXPECT_NEAR(std::fabs(d.yaw), M_PI, 0.10);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, TheOffsetMovesTheGoalOutTowardTheRobot)
|
||||||
|
{
|
||||||
|
DockProfile p = vProfile();
|
||||||
|
p.offset_x = 0.60; // [m] lùi ra trước mặt sạc
|
||||||
|
|
||||||
|
Rig rig(p);
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
const Detection d = rig.run(vPlate(1.20, M_PI / 4.0, 0.30));
|
||||||
|
ASSERT_TRUE(d.valid) << d.reason;
|
||||||
|
|
||||||
|
// Đỉnh ở x=1.20; offset dọc pháp tuyến hướng RA phía robot nên goal phải ở x ~ 0.60, GẦN hơn.
|
||||||
|
EXPECT_NEAR(d.apex_x, 1.20, 0.02);
|
||||||
|
EXPECT_NEAR(d.x, 0.60, 0.03);
|
||||||
|
EXPECT_NEAR(d.y, 0.00, 0.03);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, ATiltedPlateIsStillFoundAndItsYawFollows)
|
||||||
|
{
|
||||||
|
Rig rig;
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
const Detection d = rig.run(vPlate(1.20, M_PI / 4.0, 0.30, 0.25));
|
||||||
|
|
||||||
|
ASSERT_TRUE(d.valid) << d.reason;
|
||||||
|
EXPECT_NEAR(d.measured_vertex_angle, M_PI / 2.0, 0.12);
|
||||||
|
// Tấm xoay 0.25 rad thì phân giác hướng ra cũng xoay đúng ngần ấy.
|
||||||
|
const double expected = std::atan2(std::sin(M_PI + 0.25), std::cos(M_PI + 0.25));
|
||||||
|
EXPECT_NEAR(std::atan2(std::sin(d.yaw - expected), std::cos(d.yaw - expected)), 0.0, 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, AWiderPlateIsFoundWhenTheProfileSaysSo)
|
||||||
|
{
|
||||||
|
DockProfile p = vProfile();
|
||||||
|
p.vertex_angle = 2.0944; // [rad] 120 độ
|
||||||
|
p.wing_length = 0.25; // [m]
|
||||||
|
|
||||||
|
Rig rig(p);
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
// Góc trong = pi - 2*half; muốn 120 độ thì half = 30 độ.
|
||||||
|
const Detection d = rig.run(vPlate(1.10, M_PI / 6.0, 0.25));
|
||||||
|
|
||||||
|
ASSERT_TRUE(d.valid) << d.reason;
|
||||||
|
EXPECT_NEAR(d.measured_vertex_angle, 2.0944, 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================================================================================
|
||||||
|
// 2. Từ chối — phần quan trọng nhất
|
||||||
|
// ================================================================================================
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, APalletCornerIsNotADock)
|
||||||
|
{
|
||||||
|
Rig rig;
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
// Hai mặt vuông góc của một pallet: ĐÚNG góc 90 độ, nhưng cạnh dài 0.8 m chứ không phải 0.30 m.
|
||||||
|
// Đây là dương tính giả điển hình trong kho, và chỉ cổng chiều dài chặn được nó.
|
||||||
|
const Detection d = rig.run(vPlate(1.20, M_PI / 4.0, 0.80));
|
||||||
|
|
||||||
|
ASSERT_EQ(rig.lines_.size(), 2U) << "cần đúng 2 đoạn, nếu không thì cổng chiều dài chưa bị thử";
|
||||||
|
EXPECT_FALSE(d.valid);
|
||||||
|
EXPECT_NE(d.reason.find("wing length"), std::string::npos) << d.reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, TwoWallsThatCrossFarFromTheirEndsAreNotAV)
|
||||||
|
{
|
||||||
|
Rig rig;
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
// Hai bức tường vuông góc nhưng KHÔNG chạm nhau — giao điểm của hai ĐƯỜNG nằm ngoài cả hai đoạn.
|
||||||
|
// Chỉ dựa vào góc thì ca này lọt; cổng `vertex_gap` là thứ chặn nó.
|
||||||
|
const std::vector<Segment> world = {
|
||||||
|
{ 1.00, 0.35, 1.30, 0.65 },
|
||||||
|
{ 1.00, -0.35, 1.30, -0.65 },
|
||||||
|
};
|
||||||
|
|
||||||
|
const Detection d = rig.run(world);
|
||||||
|
ASSERT_EQ(rig.lines_.size(), 2U) << "cần đúng 2 đoạn, nếu không thì matcher chưa hề bị thử";
|
||||||
|
EXPECT_FALSE(d.valid);
|
||||||
|
EXPECT_NE(d.reason.find("do not end at the crossing"), std::string::npos) << d.reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, AFlatWallHasNoVertexAtAll)
|
||||||
|
{
|
||||||
|
Rig rig;
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
const Detection d = rig.run({ { 1.50, -1.00, 1.50, 1.00 } });
|
||||||
|
|
||||||
|
EXPECT_FALSE(d.valid);
|
||||||
|
EXPECT_FALSE(d.reason.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, APlateWithTheWrongOpeningIsRejected)
|
||||||
|
{
|
||||||
|
Rig rig; // profile chờ 90 độ
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
// Tấm mở 140 độ: đúng chiều dài cánh, đúng chỗ, chỉ sai góc.
|
||||||
|
const Detection d = rig.run(vPlate(1.20, M_PI / 9.0, 0.30));
|
||||||
|
|
||||||
|
EXPECT_FALSE(d.valid);
|
||||||
|
EXPECT_NE(d.reason.find("angle"), std::string::npos) << d.reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, HalfAPlateIsNotEnough)
|
||||||
|
{
|
||||||
|
Rig rig;
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
// Một cánh bị vật khác che hoàn toàn. Còn một đoạn thì không có đỉnh nào để đo.
|
||||||
|
std::vector<Segment> world = vPlate(1.20, M_PI / 4.0, 0.30);
|
||||||
|
world.pop_back();
|
||||||
|
|
||||||
|
const Detection d = rig.run(world);
|
||||||
|
EXPECT_FALSE(d.valid);
|
||||||
|
EXPECT_NE(d.reason.find("2 line segments"), std::string::npos) << d.reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, AnEmptySceneSaysSoInsteadOfGuessing)
|
||||||
|
{
|
||||||
|
Rig rig;
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
const Detection d = rig.run({});
|
||||||
|
EXPECT_FALSE(d.valid);
|
||||||
|
EXPECT_EQ(d.candidate_count, 0U);
|
||||||
|
EXPECT_FALSE(d.reason.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================================================================================
|
||||||
|
// 3. Cổng tìm kiếm
|
||||||
|
// ================================================================================================
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, ThePriorGateRejectsATrueVInTheWrongPlace)
|
||||||
|
{
|
||||||
|
DockProfile p = vProfile();
|
||||||
|
p.has_prior = true;
|
||||||
|
p.prior_x = 1.20; // [m] trạm kỳ vọng ở đây
|
||||||
|
p.prior_y = 0.00; // [m]
|
||||||
|
p.search_radius = 0.30; // [m]
|
||||||
|
|
||||||
|
Rig rig(p);
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
// Một tấm chữ V hoàn hảo, nhưng ở chỗ khác — trong kho thì đó là trạm của người khác, hoặc một
|
||||||
|
// vật thể trùng hình. Không có cổng này thì robot dock vào nhầm chỗ, rất tự tin.
|
||||||
|
const Detection d = rig.run(vPlate(2.20, M_PI / 4.0, 0.30));
|
||||||
|
|
||||||
|
EXPECT_FALSE(d.valid);
|
||||||
|
EXPECT_NE(d.reason.find("search gate"), std::string::npos) << d.reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, ThePriorGateAcceptsTheOneItExpects)
|
||||||
|
{
|
||||||
|
DockProfile p = vProfile();
|
||||||
|
p.has_prior = true;
|
||||||
|
p.prior_x = 1.20;
|
||||||
|
p.prior_y = 0.00;
|
||||||
|
p.search_radius = 0.30;
|
||||||
|
|
||||||
|
Rig rig(p);
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
const Detection d = rig.run(vPlate(1.20, M_PI / 4.0, 0.30));
|
||||||
|
EXPECT_TRUE(d.valid) << d.reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, TheSectorGateRejectsWhatIsNotInFront)
|
||||||
|
{
|
||||||
|
DockProfile p = vProfile();
|
||||||
|
p.search_fov = 0.15; // [rad] quạt hẹp quanh trục x
|
||||||
|
|
||||||
|
Rig rig(p);
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
// Tấm lệch sang bên: bearing của đỉnh ~0.46 rad, ngoài quạt.
|
||||||
|
const std::vector<Segment> world = vPlate(1.20, M_PI / 4.0, 0.30);
|
||||||
|
std::vector<Segment> shifted;
|
||||||
|
for (Segment s : world)
|
||||||
|
{
|
||||||
|
s.ay += 0.60;
|
||||||
|
s.by += 0.60;
|
||||||
|
shifted.push_back(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Detection d = rig.run(shifted);
|
||||||
|
EXPECT_FALSE(d.valid);
|
||||||
|
EXPECT_NE(d.reason.find("search gate"), std::string::npos) << d.reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================================================================================
|
||||||
|
// 4. Nhiều ứng viên và config hỏng
|
||||||
|
// ================================================================================================
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, TwoPlatesInViewAreBothCountedAndTheBetterOneWins)
|
||||||
|
{
|
||||||
|
Rig rig;
|
||||||
|
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||||
|
|
||||||
|
std::vector<Segment> world = vPlate(1.20, M_PI / 4.0, 0.30);
|
||||||
|
for (Segment s : vPlate(1.60, M_PI / 4.0, 0.30))
|
||||||
|
{
|
||||||
|
s.ay += 0.90;
|
||||||
|
s.by += 0.90;
|
||||||
|
world.push_back(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Detection d = rig.run(world);
|
||||||
|
ASSERT_TRUE(d.valid) << d.reason;
|
||||||
|
// Đếm được nhiều hơn một là tín hiệu cho tầng trên: trong tầm có nhiều hình giống nhau, và cổng
|
||||||
|
// prior mới là thứ quyết định lấy cái nào.
|
||||||
|
EXPECT_GE(d.candidate_count, 2U);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(VPatternMatcher, RefusesAProfileThatCanNeverMatch)
|
||||||
|
{
|
||||||
|
VPatternMatcher matcher;
|
||||||
|
std::string error;
|
||||||
|
|
||||||
|
DockProfile flat = vProfile();
|
||||||
|
flat.vertex_angle = M_PI; // hai cánh thành một đường thẳng
|
||||||
|
EXPECT_FALSE(matcher.configure(flat, error));
|
||||||
|
EXPECT_NE(error.find("vertex_angle"), std::string::npos);
|
||||||
|
|
||||||
|
DockProfile loose_gap = vProfile();
|
||||||
|
loose_gap.vertex_gap = loose_gap.wing_length;
|
||||||
|
// Khe đỉnh rộng bằng cả cánh thì hai bức tường cắt nhau ở đâu cũng lọt — mất đúng cái cổng phân
|
||||||
|
// biệt chữ V thật với giao điểm ngẫu nhiên.
|
||||||
|
EXPECT_FALSE(matcher.configure(loose_gap, error));
|
||||||
|
EXPECT_NE(error.find("vertex_gap"), std::string::npos);
|
||||||
|
|
||||||
|
DockProfile loose_len = vProfile();
|
||||||
|
loose_len.length_tolerance = loose_len.wing_length;
|
||||||
|
EXPECT_FALSE(matcher.configure(loose_len, error));
|
||||||
|
|
||||||
|
DockProfile not_ready = vProfile();
|
||||||
|
not_ready.pattern = PatternType::kCylinder;
|
||||||
|
// Nói thẳng thay vì nhận config rồi không bao giờ dò ra gì.
|
||||||
|
EXPECT_FALSE(matcher.configure(not_ready, error));
|
||||||
|
EXPECT_NE(error.find("not implemented"), std::string::npos);
|
||||||
|
|
||||||
|
// Profile hỏng bị từ chối thì bộ cũ phải còn nguyên.
|
||||||
|
DockProfile good = vProfile();
|
||||||
|
ASSERT_TRUE(matcher.configure(good, error)) << error;
|
||||||
|
EXPECT_DOUBLE_EQ(matcher.profile().wing_length, good.wing_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
testing::InitGoogleTest(&argc, argv);
|
||||||
|
return RUN_ALL_TESTS();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user