Compare commits
7 Commits
f2097380e4
...
2a8e4167e8
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a8e4167e8 | |||
| efd090a506 | |||
| 67cc877de8 | |||
| a9785b99ee | |||
| c0a04b29ad | |||
| 62692838d6 | |||
| 261cc20ad4 |
@@ -233,6 +233,8 @@ add_library(move_base2_core SHARED
|
|||||||
src/state_machine.cpp
|
src/state_machine.cpp
|
||||||
src/velocity_arbiter.cpp
|
src/velocity_arbiter.cpp
|
||||||
src/plan_inspector.cpp
|
src/plan_inspector.cpp
|
||||||
|
src/grid_analysis.cpp
|
||||||
|
src/env_monitor.cpp
|
||||||
src/control_loop.cpp
|
src/control_loop.cpp
|
||||||
src/config/move_base2_config.cpp
|
src/config/move_base2_config.cpp
|
||||||
src/runners/recovery_runner.cpp
|
src/runners/recovery_runner.cpp
|
||||||
@@ -243,6 +245,7 @@ add_library(move_base2_core SHARED
|
|||||||
src/io/sensor_gateway.cpp
|
src/io/sensor_gateway.cpp
|
||||||
src/io/sensor_store_types.cpp
|
src/io/sensor_store_types.cpp
|
||||||
src/io/costmap_exporter.cpp
|
src/io/costmap_exporter.cpp
|
||||||
|
src/io/env_logger.cpp
|
||||||
src/bridges/mission_adapter_bridge.cpp
|
src/bridges/mission_adapter_bridge.cpp
|
||||||
src/bridges/mission_layer.cpp
|
src/bridges/mission_layer.cpp
|
||||||
src/navigation_runtime.cpp
|
src/navigation_runtime.cpp
|
||||||
@@ -450,6 +453,8 @@ if(BUILD_MOVE_BASE2_TESTS AND BUILDING_WITH_CATKIN)
|
|||||||
state_machine_test
|
state_machine_test
|
||||||
velocity_arbiter_test
|
velocity_arbiter_test
|
||||||
plan_inspector_test
|
plan_inspector_test
|
||||||
|
grid_analysis_test
|
||||||
|
env_monitor_test
|
||||||
walking_skeleton_test
|
walking_skeleton_test
|
||||||
config_validation_test
|
config_validation_test
|
||||||
action_runner_test
|
action_runner_test
|
||||||
|
|||||||
185
config/runtime/env_monitor_params.yaml
Normal file
185
config/runtime/env_monitor_params.yaml
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
# =================================================================================================
|
||||||
|
# env_monitor_params.yaml — EnvMonitor: giám sát môi trường quanh robot để gọi recovery (thay PathMonitor)
|
||||||
|
# Ngày lập: 2026-09-05 (Test/ENV_MONITOR_PLAN.md). Giai đoạn hiện tại: P4 BƯỚC 5 (6/9 10:35) — CẢ BA VERDICT BẬT THẬT
|
||||||
|
# (unreachable bật lại cùng target_snap); PathMonitor cũ ĐÃ XOÁ (P5, 6/9), EnvMonitor là bộ giám sát duy nhất.
|
||||||
|
# stall cổng dịch chuyển thực (disp_*) là định nghĩa "bế tắc thật"; geodesic là điều kiện thứ hai + để ghi.
|
||||||
|
# contact action recover + apply_command true: bao tốc độ, phanh chỉ khi vượt bao, cờ collision, đứng ra sau detour
|
||||||
|
# vô ích. Run 23:32: khe 1,25 m đi qua 0,5 m/s, 0 phanh, 0 ABORT (plan §4.12).
|
||||||
|
# unreachable action recover: chưa nổ lần nào qua 5 lượt sim.
|
||||||
|
# stall action recover (bật 23:45): shadow 4 lượt — 20:20 4/6 đúng sớm 1–5,5 s + 1 oan/330 s; 23:32 2/2 đúng sớm
|
||||||
|
# 4,8–6,8 s trên đất sạch, 0 oan. Cờ no_progress -> route oscillation (trigger GỐC: cạn route = ABORT chặng).
|
||||||
|
# Đổi `action` / `contact/apply_command` là đổi hành vi thật, không cần build lại.
|
||||||
|
#
|
||||||
|
# Ai đọc: `move_base2` qua `robot::NodeHandle` (namespace `env_monitor`, hàm readEnvMonitor trong
|
||||||
|
# src/config/move_base2_config.cpp). NodeHandle merge MỌI .yaml trong thư mục này, nên khoá top-level
|
||||||
|
# `env_monitor:` phải xuất hiện ở đúng MỘT file.
|
||||||
|
#
|
||||||
|
# Triết lý (chốt 5/9): local planner được TỰ DO NÉ; recovery chỉ chạy khi BẾ TẮC THẬT. Không có phép đo
|
||||||
|
# "plan phía trước bị chắn" ở đây — đó là thứ làm bản cũ nổ oan và phải tắt. Ba verdict:
|
||||||
|
# stall lệnh đi mà cost_to_go (đường vòng tới điểm thoát + phần plan còn lại) không giảm -> no_progress
|
||||||
|
# unreachable không còn đường trong cửa sổ tới điểm thoát, vật đứng yên, xấu hơn lúc nhận plan -> path_blocked
|
||||||
|
# contact thân sắp chạm trên QUỸ ĐẠO THẬT của local planner: phanh; vật kề mũi đứng yên 1 s -> collision
|
||||||
|
# Cờ đi vào đúng bốn cổng của state machine như cũ; route bên recovery_behaviors_params.yaml không đổi.
|
||||||
|
#
|
||||||
|
# Mọi ngưỡng là GIÁ TRỊ KHỞI ĐIỂM rút từ hai bag sim 5/9 (plan §4.4). Chốt bằng shadow/replay, không mò tay.
|
||||||
|
# =================================================================================================
|
||||||
|
env_monitor:
|
||||||
|
# Công tắc chung. false = không lấy mẫu, không in, không ghi — hành vi y như không có EnvMonitor.
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
# Profile được giám sát. docking / go_straight / rotate KHÔNG: đường cong vào dock là một thể thống nhất.
|
||||||
|
profiles: [position]
|
||||||
|
|
||||||
|
# [s] Nhịp phần NẶNG (EDT + Dijkstra + hành lang, ~0.6 ms trên máy dev). Phần tiếp xúc chạy mỗi cycle.
|
||||||
|
# ► Chỉnh: ≈ chu kỳ cập nhật costmap local (10 Hz -> 0.1..0.2). TĂNG nếu CPU căng, phản ứng chậm đi đúng chừng đó.
|
||||||
|
check_period: 0.2
|
||||||
|
|
||||||
|
# [s] Sau khi nhận plan mới (planner hay detour) không kết luận gì: mẫu cũ thuộc plan cũ.
|
||||||
|
grace_after_new_plan: 1.0
|
||||||
|
|
||||||
|
# --- Thân robot và ngữ nghĩa vật cản --------------------------------------------------------------
|
||||||
|
body:
|
||||||
|
# [m] mũi / đuôi / nửa bề rộng từ base_link. CẢ BA = 0 = lấy footprint KHÔNG đệm của costmap mỗi lượt
|
||||||
|
# (cùng hình với SBPL và PRIEST). Khai tay thì khai đủ cả ba > 0.
|
||||||
|
front_offset: 0.0
|
||||||
|
rear_offset: 0.0
|
||||||
|
side_offset: 0.0
|
||||||
|
# [m] Đệm CHỈ cho dò tiếp xúc (phanh). Liên thông / hành lang dùng đúng biên SBPL (không đệm) — monitor
|
||||||
|
# khắt khe hơn planner vừa lập path là tự cãi nhau với chính path đó (vòng lặp 4/9).
|
||||||
|
padding: 0.0 # 5/9 23:20: về 0 — thân dò = footprint thật. Với 0.05 + ô 5 cm, khe 1.25 m (thân 0.96) vẫn bị coi
|
||||||
|
# là "sắp chạm" khi PRIEST đi lệch 11 cm một bên -> phanh -> detour từ trong khe -> ABORT (m1/m8 run
|
||||||
|
# 22:58). PRIEST tự giữ khoảng cách theo model của nó; monitor chỉ chặn footprint thật vào ô lethal.
|
||||||
|
# Robot thật: sai số localization 3–5 cm là cạ — đo trên robot rồi mới quyết 0.02–0.05.
|
||||||
|
# cost >= đây là vật cản THẬT. ĐỪNG hạ xuống 253: vành inflation của cây sim (0.3 m) nhỏ hơn inscribed
|
||||||
|
# (0.48 m) nên đã bị cắt cụt; mọi biên ở đây tính lại từ khoảng cách tới ô lethal + bán kính thân.
|
||||||
|
lethal_threshold: 254
|
||||||
|
|
||||||
|
# --- Hình học dọc plan (chỉ để ghi, giải thích, bao tốc độ; KHÔNG tự nổ) ---------------------------
|
||||||
|
geometry:
|
||||||
|
horizon: 3.0 # [m] quãng plan quét hành lang. <= nửa cửa sổ local.
|
||||||
|
corridor_margin: 0.0 # [m] mỗi bên, cộng vào bề rộng thân khi xét khe. 0 = đúng biên SBPL.
|
||||||
|
corridor_cap: 1.5 # [m] trần quãng trống đo về mỗi bên.
|
||||||
|
target_backoff: 0.5 # [m] điểm thoát T nằm trong vật cản thì lùi dọc plan tối đa chừng này.
|
||||||
|
target_snap: 0.5 # [m] lùi dọc plan vẫn không free thì dịch T ngang sang ô free gần nhất trong đây. Run 10:15: mọi "unreachable"
|
||||||
|
# đều là T nằm trong vành 0.48 m vì SBPL vẽ plan ôm mép vật cản giữa hành lang rộng 1.55 m — snap vào giữa
|
||||||
|
# là hết; cụm trụ chắn ngang hành lang (corridor 0.10) thì snap cũng kẹt sau cụm -> unreachable thật, nổ sớm
|
||||||
|
# 6–9 s từ đất sạch. 0 = không snap (bản cũ).
|
||||||
|
robot_snap: 0.35 # [m] ô robot không đặt được (đang ôm sát vật) thì lấy ô free gần nhất trong đây.
|
||||||
|
track_back: 1.0 # [m] bám index plan qua các tick: chỉ tìm lùi tối đa chừng này dọc plan so với tick trước.
|
||||||
|
exit_ahead: 2.0 # [m] điểm thoát T của cost_to_go = pose plan ở quãng này dọc plan kể từ index đang bám (kẹp trong cửa
|
||||||
|
# sổ). 0 = T là điểm plan rời cửa sổ (bản cũ). Run 23:37: plan detour vòng chữ U, robot đi theo về tây,
|
||||||
|
# phần plan quay về đông rời cửa sổ sớm hơn -> T LÙI, arc_rest tăng bù geo giảm, tiến độ thật đo = 0,
|
||||||
|
# stall oan lúc robot đang chạy 0.5 m/s. T ở quãng cố định chỉ tiến theo index; geodesic 2 m ít bị lật
|
||||||
|
# liên thông hơn 6–9 m. GIẢM (1.5) nếu còn thấy arc_rest tăng khi robot đi đúng plan.
|
||||||
|
track_ahead: 3.0 # [m] ... và tiến tối đa chừng này. Detour uốn quanh trụ có đoạn sau đi sát robot: tìm toàn plan
|
||||||
|
# nhảy sang đoạn đó, điểm thoát T đổi, cost_to_go nhảy +8 m/1 s (run 22:35) -> stall giả.
|
||||||
|
# Plan mới thì tìm lại toàn plan một lần. TĂNG track_ahead nếu robot cắt cua xa plan.
|
||||||
|
|
||||||
|
# --- stall: "planner đòi đi mà không tới gần lối ra" -> cờ no_progress -> route oscillation ----------
|
||||||
|
# oscillation là trigger GỐC: cạn route = ABORT chặng, nổ oan là MẤT CHẶNG -> bật "recover" SAU CÙNG.
|
||||||
|
stall:
|
||||||
|
enabled: true
|
||||||
|
action: recover # "recover" = giơ cờ no_progress (bật 5/9 23:45); "log" = chỉ ghi/in (shadow).
|
||||||
|
window_s: 4.0 # [s] cửa sổ đo eff_geo = (cost_to_go giảm) / (lệnh tích luỹ)
|
||||||
|
window_m: 1.5 # [m] hoặc theo lệnh tích luỹ — cái tới trước. Bag 5/9: né đúng eff 0.7–1.0, kẹt ≈ 0 hoặc âm.
|
||||||
|
jump_reject: 1.0 # [m] hai mẫu nặng liên tiếp (0.2 s) mà cost_to_go đổi quá (đây + quãng robot dịch) = mặt nạ free đổi liên
|
||||||
|
# thông (ô nhấp nháy ở mép khe), không phải tiến/lùi -> xoá cửa sổ đo, bắt đầu lại, bỏ kết luận kẹt cũ.
|
||||||
|
# Run 23:37: geo nhảy +3.2 m / 0.2 s -> eff −2.7 -> detour oan. 0 = tắt. Robot 0.5 m/s đi được 0.1 m/tick
|
||||||
|
# nên 1.0 là rộng gấp 10 — HẠ (0.5) nếu còn thấy eff âm sâu bất thường mà không có sự kiện.
|
||||||
|
min_commanded: 0.30 # [m] lệnh trong cửa sổ ít hơn = đang chờ/xoay, KHÔNG đo. ĐỪNG đặt 0.
|
||||||
|
eff_on: 0.20 # [-] eff dưới đây là kẹt.
|
||||||
|
eff_off: 0.50 # [-] trên đây mới xoá (hysteresis). Phải > eff_on.
|
||||||
|
confirm: 1.0 # [s] kẹt liên tục chừng này mới nổ.
|
||||||
|
goal_zone: 0.6 # [m] gần goal hơn đây không đo (rot2goal, dừng chính xác). >= xy_goal_tolerance + 0.3.
|
||||||
|
slip_on: 0.15 # [m/s] |v_cmd| - |v_đo| liên tục = kẹt vật lý (vật thấp, bumper). 0 = tắt.
|
||||||
|
slip_time: 1.0 # [s]
|
||||||
|
min_commanded_since_goal: 1.0 # [m] cổng đầu chặng — cắt hai lần nổ sớm ở bag 5/9 (rel 13.0, 145.6).
|
||||||
|
# --- Cổng DỊCH CHUYỂN THỰC (6/9 00:20) — định nghĩa vận hành của "bế tắc thật" ---------------------------------
|
||||||
|
# Cờ chỉ được giơ khi trong disp_window_s gần nhất robot dịch chuyển THỰC (khoảng cách đầu-cuối) < disp_min dù đã lệnh
|
||||||
|
# >= disp_min_commanded. Né tự do luôn dịch chuyển -> không nổ (run 00:05 cờ 95 s: robot chạy 0.5 m/s mà nổ vì lệch plan).
|
||||||
|
# Quần trong hộp 0.9 x 0.5 m (run 00:05: 97 s, dịch 0.13–0.62 m mỗi 20 s) -> nổ sau ~window. Cửa sổ bắt đầu lại ở mỗi
|
||||||
|
# plan mới, THAY ngân sách replan_budget cho stall (ngân sách đo theo quãng đường, đi vòng 6.6 m mới đốt hết -> câm 97 s).
|
||||||
|
disp_window_s: 12.0 # [s] cửa sổ đo dịch chuyển. GIẢM (8) nếu muốn nổ sớm hơn, TĂNG (20) nếu còn nổ khi né chậm trong đám đông.
|
||||||
|
disp_min: 0.6 # [m] dịch chuyển thực dưới đây = không đi được đâu. 0 = TẮT cổng, dùng lại ngân sách replan như cũ.
|
||||||
|
disp_min_commanded: 1.5 # [m] lệnh tích luỹ trong cửa sổ phải >= đây mới xét (đứng chờ không phải kẹt).
|
||||||
|
idle_window_s: 25.0 # [s] cổng ĐỨNG YÊN: dịch chuyển thực < disp_min suốt chừng này giây thì là kẹt, KHÔNG cần lệnh đủ và không
|
||||||
|
# cần geodesic đo được. Run 10:59: PRIEST bò 0.03–0.15 m/s tại chỗ 30 s (blocked_ahead cost 253, res 1.6
|
||||||
|
# chưa tới ngưỡng bất khả thi) -> lệnh 12 s chỉ 0.1–0.4 m -> cổng trên câm, kUnreachable mới gỡ ở 75 s.
|
||||||
|
# Thử trên 7 lượt: bắt đúng 2 ca quần tại chỗ (~40 s và ~125 s thay vì 75 s / 206 s), 0 lần ở 3 lượt
|
||||||
|
# sạch. Đứng chờ người > 25 s cũng nổ -> detour, chấp nhận. TĂNG (40) nếu thấy nổ khi robot chờ hợp lý.
|
||||||
|
# --- Cổng GIAM VÙNG (6/9 15:45): "đi loanh quanh trong local minimum" -----------------------------------------------
|
||||||
|
# Trong confine_window_s, mọi vị trí nằm trong vòng bán kính < confine_radius quanh tâm của chúng, VÀ lệnh tích luỹ
|
||||||
|
# >= confine_cmd_factor x max(2 x bán kính đo được, disp_min). Cổng dịch chuyển đo đầu–cuối nên mù với vòng bán kính
|
||||||
|
# > 0.3 m; cổng đứng yên chỉ bắt gần như đứng. Replay 8 lượt 5–6/9: bắt ca 00:05 ở 101 s (thay vì 206), 10:57 ở 27 s
|
||||||
|
# (thay vì 75), 23:37 ở 33 s; 0 lần ở 5 lượt sạch. Hệ số lệnh chặn ca bò thẳng chậm sau người: 2 m/20 s là bán kính
|
||||||
|
# 1 m, lệnh 2 m < 1.5 x 2 m -> không giam. Chỉ hoạt động khi disp_min > 0.
|
||||||
|
confine_window_s: 20.0 # [s] GIẢM (15) để nổ sớm hơn; TĂNG (30) nếu thấy nổ khi robot né vòng rộng hợp lý.
|
||||||
|
confine_radius: 1.2 # [m] vùng lớn hơn đây không gọi là giam. TĂNG (1.5) nếu local minimum của bãi rộng hơn.
|
||||||
|
confine_cmd_factor: 1.5 # [-] lệnh phải gấp chừng này lần đường kính vùng. GIẢM (1.2) nếu thấy quần chậm mà không nổ.
|
||||||
|
retry_backoff: 3.0 # [s] sau một lượt recovery HỎNG không tích bằng chứng (thay cho behavior wait).
|
||||||
|
|
||||||
|
# --- unreachable: không còn đường trong cửa sổ tới điểm thoát -> cờ path_blocked -> route path_blocked --
|
||||||
|
# Cạn route chỉ KHOÁ trigger (contract 05e), an toàn để bật trước stall. Bag 5/9: 0/3278 khung — hiếm.
|
||||||
|
unreachable:
|
||||||
|
enabled: true
|
||||||
|
action: recover # 6/9 10:35: BẬT LẠI cùng geometry/target_snap. Run 10:15 (ở log) nó báo trước 2/2 lần PRIEST kẹt sát vật
|
||||||
|
# (sớm 6,1 s và 8,9 s, edt 0.87/0.78) và 1 lần thừa — lần thừa là plan ôm tường, snap xử. Lịch sử: 00:05 nổ 4
|
||||||
|
# lần khi T nằm trong vành (chưa snap) -> về log 00:20.
|
||||||
|
static_on: 1.5 # [s] mất liên thông liên tục chừng này mới nổ (người đi ngang < 1 s; run 10:15 ca thừa dài 6.6 s, ca đúng 2.5/4.4 s
|
||||||
|
# -> thời gian không tách được, snap mới tách).
|
||||||
|
clean_start_margin: 0.20 # [m] thân còn cách vật ít nhất chừng này mới gọi replan (SBPL cần chỗ bắt đầu).
|
||||||
|
retry_backoff: 2.0 # [s]
|
||||||
|
|
||||||
|
# --- contact: thân sắp chạm trên quỹ đạo thật -> phanh; kề mũi đứng yên -> cờ collision -----------
|
||||||
|
contact:
|
||||||
|
enabled: true
|
||||||
|
action: recover # cờ collision (route collision). P4 bước 1: bật thật.
|
||||||
|
apply_command: true # true = phanh + bao tốc độ thật (P4). false = SHADOW: chỉ ghi "sẽ phanh/kẹp bao nhiêu", KHÔNG đụng lệnh.
|
||||||
|
# Chủ thể phanh duy nhất trong move_base2 (guard cũ đã xoá); cổng dừng trong PRIEST vẫn phải để tắt.
|
||||||
|
# Phanh mà cờ collision đã giơ nhưng route khoá -> controller bị báo "không có lệnh" -> controller_patience
|
||||||
|
# -> controlling_failed (cùng luật guard cũ), robot không đứng chết im lặng.
|
||||||
|
use_local_plan: auto # auto = dùng quỹ đạo local planner khi qua kiểm hợp lệ (pose đầu cách robot <= local_plan_start_tol),
|
||||||
|
# không thì cung (v, omega); true = ép dùng; false = luôn cung (như guard cũ).
|
||||||
|
local_plan_start_tol: 0.15 # [m]
|
||||||
|
lookahead: 1.5 # [m] quãng quỹ đạo cần dò.
|
||||||
|
confirm_cycles: 2 # [cycle] chạm trong quãng phanh liên tục chừng này thì phanh (0.1 s @ 20 Hz).
|
||||||
|
trigger_static_time: 1.0 # [s] tiếp xúc trong clean_start_margin còn đó chừng này (bất kể tốc độ) mới giơ cờ.
|
||||||
|
clean_start_margin: 0.20 # [m] dừng với thân còn cách vật ít nhất chừng này.
|
||||||
|
brake_decel: 0.35 # [m/s^2] decel dùng cho quãng phanh v^2/2a và bao tốc độ sqrt(2a(d - margin)). Đặt = decel thật của base.
|
||||||
|
release_hysteresis: 0.10 # [m] đã kề mũi (d <= clean_start_margin) thì còn kề tới khi d > margin + đây. Run 5/9 22:35: PRIEST đổi
|
||||||
|
# quỹ đạo mỗi cycle, d nhảy 0.08 <-> 0.25 quanh margin -> phanh bật/nhả 4 lần/s, đồng hồ giơ cờ reset
|
||||||
|
# liên tục nên không bao giờ nổ. TĂNG nếu còn thấy "receded" rồi "braking" trong < 0.5 s.
|
||||||
|
release_confirm_cycles: 4 # [cycle] nhả phanh (quỹ đạo sạch hoặc lùi xa) chỉ sau chừng này cycle liên tiếp không "sắp chạm"
|
||||||
|
# (0.2 s @ 20 Hz). Người đi ngang thì chậm 0.2 s là chấp nhận; 1 = nhả ngay như bản 22:35.
|
||||||
|
envelope_margin: 0.10 # [m] bao tốc độ giữ dư chừng này so với ngưỡng phanh: v_cap = sqrt(2a(d - margin - dư)), bò min_speed
|
||||||
|
# cho tới d <= margin. 0 = bao đặt đúng biên phanh -> kẹp xong là chớm "sắp chạm", phanh/nhả liên tục
|
||||||
|
# khi tiếp cận (run 22:58: 9/28 lần phanh < 0.3 s). TĂNG nếu còn rung; robot dừng xa vật hơn đúng chừng đó.
|
||||||
|
standdown_min_travel: 0.10 # [m] detour do chính cờ collision vừa xong mà robot dịch chưa tới đây rồi lại "sắp chạm" -> kContact
|
||||||
|
# ĐỨNG RA: không phanh, không cờ, không bao — nhường local planner (lưới an toàn duy nhất). Detour từ
|
||||||
|
# trong khe là vô nghĩa: xoay là góc thân chạm (run 22:58: 3 detour trong 4 s rồi ABORT ở m1 và m8;
|
||||||
|
# mission 6 thoát được đúng lúc guard cạn route và nhường). 0 = tắt, hành vi như 22:58.
|
||||||
|
standdown_travel: 0.30 # [m] đứng ra cho tới khi robot đi được chừng này (hoặc quỹ đạo sạch release_confirm_cycles cycle) rồi
|
||||||
|
# vũ trang lại. TĂNG nếu thấy "re-armed" rồi "braking" lại ngay trong cùng khe.
|
||||||
|
min_speed: 0.05 # [m/s] bao tốc độ không kẹp thấp hơn đây; thấp hơn là dừng hẳn.
|
||||||
|
min_check_speed: 0.10 # [m/s] |v_cmd|, |v_đo| VÀ |v controller muốn| (trước bao/phanh) đều dưới đây mà chưa phanh = đang
|
||||||
|
# xoay tại chỗ / bò: KHÔNG phanh, KHÔNG cờ — góc thân quét vào ô kề bên khi xoay là việc của local
|
||||||
|
# planner (cùng vai collision_min_speed của guard cũ). Tính cả "lệnh controller muốn" vì bao tốc
|
||||||
|
# độ kẹp lệnh về 0 trong margin: nếu chỉ nhìn lệnh đã kẹp thì robot dừng câm không cờ. Shadow 5/9:
|
||||||
|
# 3 lần "braking to zero" ngay đầu chặng (v 0.00–0.07) là nổ oan kiểu này; mọi tiếp xúc thật đều ở
|
||||||
|
# v >= 0.25. HẠ xuống 0 = dò cả khi xoay -> robot đứng cạnh vật bị ghim rồi gọi detour từ đó (churn).
|
||||||
|
envelope: true # bao tốc độ theo tiếp xúc (chỉ có tác dụng khi apply_command: true).
|
||||||
|
|
||||||
|
# --- Ngân sách replan theo quãng đường (chống "đi một đoạn lại nổ" trong đám đông) -------------------
|
||||||
|
replan_budget:
|
||||||
|
min_travel: 1.5 # [m] sau detour thành công, stall/unreachable im tới khi robot đi chừng này trên plan mới.
|
||||||
|
escalate_factor: 2.0 # [-] replan lặp trong escalate_window_m thì nhân ngân sách: 1.5 -> 3 -> 6 m.
|
||||||
|
escalate_window_m: 10.0 # [m] đi chừng này không replan thì về min_travel.
|
||||||
|
escalate_max: 6.0 # [m] trần leo thang; 0 = không trần. Run 22:35: 4 detour PRIEST trong 10 m -> 1.5 -> 3 -> 6 -> 12 m,
|
||||||
|
# kStall câm gần hết chặng. Phải >= min_travel.
|
||||||
|
|
||||||
|
# --- Ghi JSONL cho replay / ML ------------------------------------------------------------------------
|
||||||
|
logger:
|
||||||
|
enabled: true # true = ghi mỗi tick nặng + mỗi sườn sự kiện vào log_dir/envmon_<epoch>.jsonl (bật 5/9 22:50 để
|
||||||
|
# đọc profile phanh/bao tốc độ; tắt khi lên robot nếu không cần replay).
|
||||||
|
log_dir: /tmp/env_monitor
|
||||||
|
flush_period: 1.0 # [s]
|
||||||
@@ -10,7 +10,8 @@
|
|||||||
# Đây là bản đang có hiệu lực. Bản trong `Test/mission_adapters/test/config/` chỉ phục vụ test và
|
# Đây là bản đang có hiệu lực. Bản trong `Test/mission_adapters/test/config/` chỉ phục vụ test và
|
||||||
# chỉ được đọc khi chạy kèm PNKX_NAV_CORE_CONFIG_DIR trỏ vào đúng thư mục đó.
|
# chỉ được đọc khi chạy kèm PNKX_NAV_CORE_CONFIG_DIR trỏ vào đúng thư mục đó.
|
||||||
#
|
#
|
||||||
# Bật/tắt cả tầng này bằng `mission_layer_enabled` trong `move_base_common_params.yaml`.
|
# Tầng này LUÔN chạy (2026-09-06 bỏ khoá bật/tắt); namespace đọc nó là `mission_namespace` trong
|
||||||
|
# `move_base_common_params.yaml`.
|
||||||
# =================================================================================================
|
# =================================================================================================
|
||||||
|
|
||||||
mission_adapters:
|
mission_adapters:
|
||||||
@@ -24,7 +25,7 @@ mission_adapters:
|
|||||||
# instant_src : một action lẻ ngoài order (VDA5050 `instantActions`).
|
# instant_src : một action lẻ ngoài order (VDA5050 `instantActions`).
|
||||||
#
|
#
|
||||||
# ► Chỉnh: bỏ bớt một nguồn = tắt đường vào tương ứng. Bỏ HẾT thì runtime tự quay về đường trực
|
# ► Chỉnh: bỏ bớt một nguồn = tắt đường vào tương ứng. Bỏ HẾT thì runtime tự quay về đường trực
|
||||||
# tiếp (như `mission_layer_enabled: false`) kèm log cảnh báo — thiếu plugin không được phép biến
|
# tiếp (một goal cho cả order) kèm log cảnh báo — thiếu plugin không được phép biến
|
||||||
# thành robot đứng im không rõ lý do.
|
# thành robot đứng im không rõ lý do.
|
||||||
#
|
#
|
||||||
# ⚠ Bỏ `instant_src` KHÔNG quay về hành vi cũ: `executeInstantAction` sẽ trả false và mọi
|
# ⚠ Bỏ `instant_src` KHÔNG quay về hành vi cũ: `executeInstantAction` sẽ trả false và mọi
|
||||||
|
|||||||
@@ -56,12 +56,6 @@ rotate:
|
|||||||
# và hình học khác production. Đừng tinh chỉnh tham số tuyến chỉ dựa trên sim goal-trần.
|
# và hình học khác production. Đừng tinh chỉnh tham số tuyến chỉ dựa trên sim goal-trần.
|
||||||
backup_global_planner: SBPLLatticePlanner
|
backup_global_planner: SBPLLatticePlanner
|
||||||
|
|
||||||
# Compound docking quy `goal_frame` thành pose tuyệt đối; HybridLocalPlanner không đọc maker_name.
|
|
||||||
# `true` chỉ dành cho PNKXDockingLocalPlanner legacy, vốn phải chọn marker trước initialize().
|
|
||||||
#
|
|
||||||
# ► Chỉnh: giữ false cho move_base2. Đặt true chỉ khi quay lại dùng local planner docking legacy.
|
|
||||||
docking_requires_marker: false
|
|
||||||
|
|
||||||
# Bảng `library_path` và tham số riêng của CustomPlanner, DockPlanner, TwoPointsPlanner cùng các
|
# Bảng `library_path` và tham số riêng của CustomPlanner, DockPlanner, TwoPointsPlanner cùng các
|
||||||
# local planner nằm trong các YAML runtime đồng hành (symlink từ pnkx_nav_core/config/). Không lặp
|
# local planner nằm trong các YAML runtime đồng hành (symlink từ pnkx_nav_core/config/). Không lặp
|
||||||
# lại chúng ở đây để tránh hai nguồn cấu hình cho cùng một plugin.
|
# lại chúng ở đây để tránh hai nguồn cấu hình cho cùng một plugin.
|
||||||
@@ -204,8 +198,8 @@ acc_lim_x: 1.0
|
|||||||
acc_lim_theta: 1.20
|
acc_lim_theta: 1.20
|
||||||
|
|
||||||
# =================================================================================================
|
# =================================================================================================
|
||||||
# Giám sát tuyến (path_monitor) — ĐÃ TÁCH sang `path_monitor_params.yaml` (3/9/2026).
|
# Giám sát môi trường — xem `env_monitor_params.yaml` (EnvMonitor thay PathMonitor, 6/9/2026).
|
||||||
# Đừng khai lại `path_monitor:` ở đây: NodeHandle merge mọi file, hai bản sẽ đè nhau theo thứ tự
|
# Đừng khai lại `env_monitor:` ở đây: NodeHandle merge mọi file, hai bản sẽ đè nhau theo thứ tự
|
||||||
# thư mục (không xác định) và người chỉnh sẽ thấy "sửa mãi không ăn".
|
# thư mục (không xác định) và người chỉnh sẽ thấy "sửa mãi không ăn".
|
||||||
# =================================================================================================
|
# =================================================================================================
|
||||||
|
|
||||||
@@ -230,25 +224,13 @@ runtime_stats_period: 0.0
|
|||||||
# debug; trên hiện trường để true.
|
# debug; trên hiện trường để true.
|
||||||
recovery_behavior_enabled: true
|
recovery_behavior_enabled: true
|
||||||
|
|
||||||
# =================================================================================================
|
# Mission layer LUÔN chạy (2026-09-06, bỏ khoá `mission_layer_enabled`): VDA5050 Order đi qua
|
||||||
# Mission layer
|
# `mission_adapters` — order được cắt thành từng chặng tại mỗi node có action, chỉ phần `released`
|
||||||
# =================================================================================================
|
# được chạy, `orderUpdateId` nối tiếp thay vì chạy lại từ đầu, và có `mission_timeout` làm lưới cuối.
|
||||||
|
# Nguồn mission và tham số của layer khai ở `mission_adapters_params.yaml`; namespace đọc chúng là
|
||||||
# true (mặc định của move_base2): VDA5050 Order đi qua `mission_adapters` — order được cắt thành
|
# `mission_namespace` ở trên.
|
||||||
# từng chặng tại mỗi node có action, chỉ phần `released` được chạy, `orderUpdateId` nối tiếp thay vì
|
# Không nạp được nguồn nào thì runtime TỰ quay về đường trực tiếp kèm log cảnh báo — thiếu plugin
|
||||||
# chạy lại từ đầu, và có `mission_timeout` làm lưới cuối. Nguồn mission và tham số của layer khai ở
|
# không được phép biến thành robot đứng im không rõ lý do.
|
||||||
# `mission_adapters_params.yaml`.
|
|
||||||
#
|
|
||||||
# false: order đi thẳng xuống navigation như MỘT goal duy nhất — hành vi của move_base gen-1, và
|
|
||||||
# cũng là hành vi đã chạy được trên sim trước 2026-07-31. Đây là đường lùi khi mission layer gây vấn
|
|
||||||
# đề trên hiện trường: đổi một khoá, không phải build lại.
|
|
||||||
#
|
|
||||||
# Bật mà không nạp được nguồn nào thì runtime TỰ quay về đường trực tiếp kèm log cảnh báo — thiếu
|
|
||||||
# plugin không được phép biến thành robot đứng im không rõ lý do.
|
|
||||||
#
|
|
||||||
# ► Chỉnh: tắt thì MẤT compound action (charge/PickUp), mất cắt chặng theo node, mất `mission_timeout`.
|
|
||||||
# Chỉ tắt để cô lập sự cố, và nhớ bật lại.
|
|
||||||
mission_layer_enabled: true
|
|
||||||
|
|
||||||
# Bảng symbol -> thư viện cho Boost.DLL. Đây KHÔNG phải tham số tinh chỉnh — thiếu khoá này là
|
# Bảng symbol -> thư viện cho Boost.DLL. Đây KHÔNG phải tham số tinh chỉnh — thiếu khoá này là
|
||||||
# nguyên nhân phổ biến nhất của lỗi "plugin build xong nhưng runtime báo không tìm thấy".
|
# nguyên nhân phổ biến nhất của lỗi "plugin build xong nhưng runtime báo không tìm thấy".
|
||||||
|
|||||||
@@ -1,256 +0,0 @@
|
|||||||
# =================================================================================================
|
|
||||||
# path_monitor_params.yaml — Giám sát tuyến của move_base2 (tách khỏi move_base_common_params.yaml
|
|
||||||
# ngày 3/9/2026; sắp xếp lại theo phép đo ngày 4/9/2026)
|
|
||||||
#
|
|
||||||
# Ai đọc: `move_base2` qua `robot::NodeHandle` (namespace `path_monitor`, hàm readPathMonitor trong
|
|
||||||
# src/config/move_base2_config.cpp). NodeHandle merge MỌI .yaml trong thư mục này vào một cây gốc, nên
|
|
||||||
# tên file không quan trọng — chỉ khoá top-level `path_monitor:` là quan trọng, và nó PHẢI xuất hiện ở
|
|
||||||
# đúng MỘT file. Khai lại ở file khác là hai bản merge chồng nhau theo thứ tự thư mục (không xác định).
|
|
||||||
#
|
|
||||||
# Ai KHÔNG đọc: PRIEST, SBPL, costmap. Các phép đo ở đây chỉ dùng dữ liệu move_base2 đang có (pose,
|
|
||||||
# plan đang bám, lệnh sau trọng tài, local costmap qua PathCheckPort) và chỉ ra QUYẾT ĐỊNH dừng / gọi
|
|
||||||
# recovery; chúng không sửa lệnh của local planner.
|
|
||||||
#
|
|
||||||
# Bốn phép đo, mỗi cái trả lời một câu khác nhau, không chồng nhau:
|
|
||||||
# 1 path_blocked plan phía trước chạm vành 253 trong `horizon` -> route path_blocked
|
|
||||||
# 2 off_path robot lệch plan đang bám quá `max_cross_track` -> route off_path
|
|
||||||
# 3 collision thân robot sắp chạm ô LETHAL trên CUNG LỆNH (v,omega) -> lệnh 0 + path_blocked
|
|
||||||
# 4 progress planner ĐÒI ĐI mà robot KHÔNG TIẾN (local minimum) -> route oscillation
|
|
||||||
# (5 liên thông cửa sổ local — xem Test/LOCAL_MINIMUM_PREDICT_PLAN.md, đang pending)
|
|
||||||
#
|
|
||||||
# Mỗi phép đo có BA nút độc lập và phải đủ CẢ BA mới sống: cờ riêng `*_enabled` (đứng đầu khối của
|
|
||||||
# nó), ngưỡng > 0, route tương ứng bên recovery_behaviors_params.yaml khác rỗng. Công tắc chung
|
|
||||||
# `enabled` tắt tất cả.
|
|
||||||
#
|
|
||||||
# Cấu trúc file: khối CHUNG (áp cho mọi phép đo), rồi mỗi phép đo một khối, khoá của phép đo nào nằm
|
|
||||||
# trọn trong khối đó, cờ bật/tắt là dòng đầu tiên của khối.
|
|
||||||
# =================================================================================================
|
|
||||||
path_monitor:
|
|
||||||
# ===============================================================================================
|
|
||||||
# CHUNG
|
|
||||||
# ===============================================================================================
|
|
||||||
|
|
||||||
# Công tắc CHUNG; tắt cái này là tắt cả bốn phép đo.
|
|
||||||
# ► Chỉnh: false = đưa hành vi về đúng như trước 2026-08-04d (chỉ còn controller_patience lo).
|
|
||||||
# Dùng làm đường lùi nhanh khi nghi cơ chế giám sát gây vấn đề ngoài hiện trường.
|
|
||||||
enabled: true
|
|
||||||
|
|
||||||
# Profile được giám sát. docking/go_straight/rotate KHÔNG được detour: đường cong vào dock là một
|
|
||||||
# thể thống nhất, cắt ghép nó là phá chính hình học đó.
|
|
||||||
# ► Chỉnh: gần như luôn để `[position]`. Thêm profile khác chỉ khi profile đó dùng tuyến tự do.
|
|
||||||
profiles: [position]
|
|
||||||
|
|
||||||
# [s] Nhịp quét costmap của phép đo 1, 2, 4. KHÔNG quét mỗi control cycle — quá phí. (Phép đo 3
|
|
||||||
# chạy MỖI cycle, không theo nhịp này — xem khối của nó.)
|
|
||||||
# ► Chỉnh: đặt ~= chu kỳ cập nhật costmap local (5 Hz ⇒ 0.2 s). Nhỏ hơn thế là quét lại cùng một
|
|
||||||
# dữ liệu. TĂNG nếu CPU căng; đổi lại phản ứng chậm đi đúng chừng đó.
|
|
||||||
check_period: 0.2
|
|
||||||
|
|
||||||
# [s] Điều kiện của phép đo 1, 2 phải đúng LIÊN TỤC chừng này mới khai hoả (chống nhiễu một nhịp).
|
|
||||||
# ► Chỉnh: TĂNG (0.8–1.5) nếu người đi ngang qua làm robot dừng oan. GIẢM nếu robot phản ứng quá
|
|
||||||
# chậm với vật cản thật. Phải >= vài lần `check_period`, nếu không debounce vô nghĩa.
|
|
||||||
confirm_time: 0.5
|
|
||||||
|
|
||||||
# [s] Bỏ qua phép đo 2 và 4 ngay sau khi nhận plan mới — lúc preempt/đổi chặng pose robot có thể
|
|
||||||
# lệch nhất thời so với plan vừa tới, và mẫu tiến độ cũ thuộc plan cũ.
|
|
||||||
# ► Chỉnh: TĂNG nếu `off_path` / `no progress` khai hoả ngay sau mỗi lần đổi chặng hoặc detour.
|
|
||||||
grace_after_new_plan: 1.0
|
|
||||||
|
|
||||||
# ===============================================================================================
|
|
||||||
# PHÉP ĐO 1 — tuyến phía trước bị chắn (plan ∩ vành 253 trong `horizon`) -> route path_blocked
|
|
||||||
# ===============================================================================================
|
|
||||||
|
|
||||||
# ► Chỉnh: hạ cờ để tắt mà VẪN GIỮ ngưỡng — bật lại chỉ sửa một dòng. Sim 4/9: plan tắc ở 2.09 m
|
|
||||||
# lúc robot còn sạch mà không ai phản ứng vì cờ này tắt; recovery giờ lập lại cả đường (không
|
|
||||||
# detour vòng tròn nữa) nên lý do tắt cũ hết hiệu lực — cân nhắc bật.
|
|
||||||
path_blocked_enabled: false
|
|
||||||
|
|
||||||
# [m] Quãng tuyến phía trước cần quét.
|
|
||||||
# ► Chỉnh: phải <= nửa cửa sổ costmap local (8x8 m ⇒ ~4 m), ngoài đó không có dữ liệu.
|
|
||||||
# - TĂNG (tới ~3.5) để phát hiện sớm hơn, có nhiều chỗ hơn để lập đường vòng;
|
|
||||||
# - GIẢM (1.5–2.0) nếu robot dừng quá sớm trước vật cản mà local planner tự lách được.
|
|
||||||
horizon: 3.0
|
|
||||||
|
|
||||||
# [m] Vật cản gần hơn mức này thì KHÔNG khai hoả: lập lại đường từ sát nách hầu như không ra
|
|
||||||
# nghiệm (start trong vành 253), và ca đó đã có phép đo 3 + controller_patience lo.
|
|
||||||
# ► Chỉnh: đặt ~= bán kính ngoại tiếp footprint + dư địa (thân 1.166x0.96 ⇒ ngoại tiếp 0.75 m).
|
|
||||||
# TĂNG nếu thấy nhiều lượt `Solution not found` với vật cản rất gần.
|
|
||||||
min_detour_distance: 0.5
|
|
||||||
|
|
||||||
# ===============================================================================================
|
|
||||||
# PHÉP ĐO 2 — robot lệch khỏi plan đang bám quá `max_cross_track` -> route off_path
|
|
||||||
# ===============================================================================================
|
|
||||||
|
|
||||||
# Tắt tường minh: max_cross_track chưa đo được nên chưa có ngưỡng thật. Bật thì đặt ngưỡng dưới.
|
|
||||||
off_path_enabled: false
|
|
||||||
|
|
||||||
# [m] Lệch khỏi plan ĐANG CHẠY quá mức này thì khai hoả. Đo với plan đang chạy chứ không phải
|
|
||||||
# tuyến gốc — sau một lượt lập lại đường hợp lệ, plan đang chạy CHÍNH LÀ đường mới nên không báo
|
|
||||||
# oan. (Ràng buộc "lệch bao nhiêu so với tuyến fleet" là việc của `max_deviation` bên detour.)
|
|
||||||
# ► Chỉnh: cách đặt đúng: chạy sim/robot qua các khúc cua gắt nhất của tuyến, ghi cross-track
|
|
||||||
# đỉnh, rồi đặt ~1.5–2 lần giá trị đó. Đặt số mò là robot dừng oan giữa đường cong. PRIEST né
|
|
||||||
# người lệch tới 2–3 m (sim 3–4/9) nên 1.0 hiện là số giữ chỗ, không phải số đo.
|
|
||||||
max_cross_track: 1.0
|
|
||||||
|
|
||||||
# ===============================================================================================
|
|
||||||
# PHÉP ĐO 3 — thân robot sắp chạm vật cản THẬT trên cung lệnh (2026-09-03) -> lệnh 0 + path_blocked
|
|
||||||
# ===============================================================================================
|
|
||||||
#
|
|
||||||
# Đối tượng đo KHÁC HẲN hai phép đo trên: không phải global plan, mà là CUNG mà lệnh (v, omega) của
|
|
||||||
# local planner sắp vẽ ra từ pose hiện tại. Local planner lệch plan cả mét khi né, lúc đó quét
|
|
||||||
# plan bảo "sạch" trong khi thân đang lao hướng khác — mission 5 (29/8) húc vào khe 0.76 m như thế.
|
|
||||||
#
|
|
||||||
# Chạy MỖI control cycle, không theo check_period/confirm_time: 0.2 s + 0.5 s xác nhận ở 0.5 m/s
|
|
||||||
# là 0.35 m đi thêm — bằng cả quãng dừng. Hai bước khi chạm trong collision_stop_distance:
|
|
||||||
# (1) ngay cycle đó lệnh ra = 0 và controller bị coi là "không có lệnh" (controller_patience chạy);
|
|
||||||
# (2) sau collision_confirm_cycles cycle liên tiếp, giơ cờ path_blocked -> route lập lại đường.
|
|
||||||
#
|
|
||||||
# ► Khi bật cái này, TẮT các cổng dừng trong priest (collision_check_enabled, start_violation_
|
|
||||||
# stop_count) — hai chủ thể cùng ra lệnh dừng là deadlock 2/9 quay lại.
|
|
||||||
collision_enabled: true
|
|
||||||
|
|
||||||
# --- 3a. Dò cái gì -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
# [m] Quãng cung cần dò — tầm phanh, không hơn. Dò xa hơn là soi thứ solver còn chưa được thấy
|
|
||||||
# và báo fake ở đuôi (lý do gate toàn-quỹ-đạo cũ trong priest bị tắt).
|
|
||||||
collision_lookahead: 0.8
|
|
||||||
|
|
||||||
# Trần THƯA của số mẫu trên cung: bước = lookahead / samples nhưng không bao giờ lớn hơn một ô lưới
|
|
||||||
# (tường mỏng 1 ô lọt giữa hai mẫu là đâm xuyên). Lưới 0.05 m ⇒ thực tế 16 mẫu, mẫu đầu ở 0.05 m.
|
|
||||||
collision_max_samples: 6
|
|
||||||
|
|
||||||
# cost >= đây là chạm. 254 = chỉ ô vật cản THẬT (LETHAL). ĐỪNG hạ xuống 253: vành inflation là biên
|
|
||||||
# dành cho TÂM robot, đem áp lên mũi/hông là đếm biên hai lần — báo "chạm" khi tường còn cách nửa
|
|
||||||
# mét và robot kẹt vì một cái bóng (2/9: "body inside 24 obstacles" với tường cách 0.48+ m).
|
|
||||||
collision_cost_threshold: 254
|
|
||||||
|
|
||||||
# --- 3b. Thân robot ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
# [m] Hình chữ nhật thân tính từ base_link: mũi / đuôi / nửa bề rộng.
|
|
||||||
# CẢ BA = 0 (mặc định) = lấy từ footprint KHÔNG đệm của costmap mỗi cycle — cùng hình với SBPL và
|
|
||||||
# PRIEST, đổi footprint lúc chạy (setRobotFootprint) là guard đổi theo, không phải chép số vào hai
|
|
||||||
# file. Boot log in "collision body from the costmap footprint: front=.. rear=.. side=..".
|
|
||||||
# ► Chỉnh: chỉ khai tay khi cố ý muốn guard dùng thân KHÁC costmap (ví dụ chở hàng nhô ra) — khi
|
|
||||||
# đó phải khai đủ CẢ BA > 0, khai lửng là config sai (runtime từ chối).
|
|
||||||
collision_front_offset: 0.0
|
|
||||||
collision_rear_offset: 0.0
|
|
||||||
collision_side_offset: 0.0
|
|
||||||
|
|
||||||
# [m] Biên an toàn TƯỜNG MINH cộng vào mọi cạnh — đây là thứ thay cho inflation ở phép đo này.
|
|
||||||
# ► Chỉnh: ~ sai số định vị + sai số footprint. 0.05 = dừng khi thân còn cách vật 5 cm. LƯU Ý:
|
|
||||||
# SBPL lập đường với footprint KHÔNG đệm, nên khe hẹp hơn thân+2·đệm mà rộng hơn thân thật là
|
|
||||||
# chỗ planner bảo đi được còn guard bảo không — muốn qua khe đó thì hạ về 0.0.
|
|
||||||
collision_footprint_padding: 0.05
|
|
||||||
|
|
||||||
# Loại trừ baseline (2026-09-04): ô LETHAL đang nằm DƯỚI thân đệm ngay tại chỗ đứng KHÔNG được
|
|
||||||
# tính là chạm ở các mẫu sau. Guard đổi câu hỏi từ "thân có đè LETHAL không" thành "lệnh này có
|
|
||||||
# đưa THÊM ô LETHAL vào thân không": trượt song song / rời xa người đang kề hông thì đi được,
|
|
||||||
# lấn sâu thêm (rẽ vào họ) thì vẫn chặn. Sim 4/9: người kề hông phải ở 0.46 m ngang (thân 0.48 +
|
|
||||||
# đệm 0.05) làm mẫu đầu tiên báo "0.05 m" mãi dù PRIEST lệnh đi thẳng; SBPL (không đệm) bảo đi
|
|
||||||
# được; robot đứng chết. Baseline tính lại MỖI cycle: người bước vào robot đang đứng yên cũng
|
|
||||||
# thành baseline — chấp nhận, vì robot không phải bên khép khoảng cách.
|
|
||||||
# ► Chỉnh: false = hành vi cũ (mọi ô dưới thân đệm đều là "sắp chạm").
|
|
||||||
# Debug (khoá `debug/state` trong move_base_common_params.yaml, mặc định bật):
|
|
||||||
# - "body overlaps N LETHAL cell(s) ... letting it move; cells in base frame: (x, y) ..." — đang
|
|
||||||
# cho đi vì cung không thêm ô, kèm toạ độ ô dưới thân (frame base_link, x tới, y trái).
|
|
||||||
# - "hit at sample K: <right|nose|left|tail> edge touches cell (x, y) ...; baseline N cell(s): ..."
|
|
||||||
# — mỗi lần guard nổ: cạnh nào chạm, ô chạm ở đâu, ô nào đã bị bỏ qua. Ô chạm ở góc trái-sau
|
|
||||||
# khi đang rẽ phải = thân quét tới; ô chạm ở mũi khi đi thẳng mà cycle trước còn sạch = người
|
|
||||||
# bước tới.
|
|
||||||
collision_exclude_baseline: true
|
|
||||||
|
|
||||||
# --- 3c. Phản ứng khi chạm ---------------------------------------------------------------------
|
|
||||||
|
|
||||||
# [m] Chạm gần hơn đây (VÀ "sắp" theo stop_time/stop_angle ở dưới): lệnh 0 ngay + đếm xác nhận.
|
|
||||||
# Xa hơn: chỉ kẹp tốc độ (xem brake_decel).
|
|
||||||
# ► Chỉnh: >= quãng phanh ở tốc độ hành trình với decel thật của base (0.5 m/s, 0.35 m/s^2 ⇒
|
|
||||||
# 0.36 m). GIẢM để đi sát vật cản hơn — chỉ khi đã đo decel thật trên robot.
|
|
||||||
collision_stop_distance: 0.2
|
|
||||||
|
|
||||||
# [cycle] chạm liên tiếp chừng này mới giơ cờ replan. Lệnh 0 vẫn phát từ cycle đầu — cái này chỉ
|
|
||||||
# chống một nhịp costmap nhấp nháy kéo cả recovery ra. 2 cycle @20 Hz = 0.1 s.
|
|
||||||
collision_confirm_cycles: 2
|
|
||||||
|
|
||||||
# [m/s^2] Chạm XA hơn stop_distance thì kẹp v <= sqrt(2 a (d - stop)) trước khi phát, để không
|
|
||||||
# lao vào ở tốc độ tối đa rồi phanh gấp ở stop_distance. 0 = không kẹp. Đặt = decel thoải mái của
|
|
||||||
# base. Vật cản ở HÔNG không có pha kẹp này (guard chỉ thấy nó khi mép hông phủ lên) — đó là việc
|
|
||||||
# của baseline ở trên.
|
|
||||||
collision_brake_decel: 0.35
|
|
||||||
|
|
||||||
# [m/s] |v| dưới đây không dò: cung suy biến, xoay tại chỗ có phép kiểm riêng bên planner.
|
|
||||||
collision_min_speed: 0.02
|
|
||||||
|
|
||||||
# "Sắp chạm" theo THỜI GIAN và GÓC (2026-09-04, run 16:26). Trong stop_distance, mét dọc cung
|
|
||||||
# chưa nói lên gì với cung xoay gần tại chỗ: cùng 0.15 m cung, đi thẳng 0.36 m/s là chạm sau
|
|
||||||
# 0.4 s; xoay bò v 0.04 ω −0.5 (R 0.08 m) là quay 107° và còn 3.8 s — PRIEST đã thôi xoay sau
|
|
||||||
# 30–60° để thẳng hàng với path. Guard mô hình lệnh là cung không đổi nên phải đo bằng thời gian
|
|
||||||
# và góc: dừng khi còn <= stop_time HOẶC còn <= stop_angle; chưa tới thì KHÔNG dừng, không replan,
|
|
||||||
# chỉ kẹp v <= (walk − padding)/stop_time. Đi thẳng (góc ≈ 0) luôn "sắp" -> y như trước.
|
|
||||||
# Hôm đó: 11 lần nổ trong 15 s tại góc trụ, xoay 0.9 rad mất 12 s thay vì 2 s -> với luật này 1 lần.
|
|
||||||
# ► Chỉnh stop_time: TĂNG (1.5) nếu base phanh chậm; GIẢM (0.6) chỉ khi đã đo decel thật.
|
|
||||||
# ► Chỉnh stop_angle: là biên còn lại khi xoay sát vật; 0.35 rad ≈ 0.27 m quãng góc thân. GIẢM
|
|
||||||
# (0.2) nếu robot hay bị dừng oan giữa cú xoay; TĂNG nếu đã thấy góc thân chạm.
|
|
||||||
collision_stop_time: 1.0
|
|
||||||
collision_stop_angle: 0.35
|
|
||||||
|
|
||||||
# [rad] Cung không dò quá góc quay này. R = 0.1 m thì 0.8 m lookahead là quay 8 rad (hơn một
|
|
||||||
# vòng) — soi thứ không xảy ra và làm "would hit"/"clear again" lật mỗi vài cycle. Đi thẳng không
|
|
||||||
# bị cắt. Phải >= collision_stop_angle.
|
|
||||||
collision_lookahead_angle: 1.0
|
|
||||||
|
|
||||||
# ===============================================================================================
|
|
||||||
# PHÉP ĐO 4 — tiến độ so với lệnh (2026-09-04) -> route oscillation
|
|
||||||
# ===============================================================================================
|
|
||||||
#
|
|
||||||
# "Planner ĐÒI ĐI mà robot KHÔNG TIẾN" — định nghĩa vận hành của local minimum, không cần biết vì
|
|
||||||
# sao kẹt. Mỗi cycle cộng |v| ĐÃ PHÁT sau trọng tài; mỗi nhịp check_period lấy mẫu (tiến dọc plan,
|
|
||||||
# khoảng cách tới goal). So mẫu bây giờ với mẫu cách đây `progress_window`:
|
|
||||||
# cmd = tổng lệnh trong cửa sổ [m]
|
|
||||||
# prog = max(tiến dọc plan, giảm khoảng cách tới goal) [m]
|
|
||||||
# eff = prog / cmd
|
|
||||||
# Kẹt khi cmd >= min_commanded VÀ eff < min_efficiency, liên tục đủ progress_confirm_time.
|
|
||||||
#
|
|
||||||
# Số đo trên sim 3/9: chạy bình thường eff 0.65-0.85, kể cả đoạn né lệch plan 1.9 m; hai ca kẹt
|
|
||||||
# thật eff -0.14..0.27; nổ 12–54 s TRƯỚC khi hệ cũ vào RECOVERING, lúc robot còn cách vành 253
|
|
||||||
# tới 0.84-1.42 m — đứng trên đất sạch, SBPL có chỗ bắt đầu.
|
|
||||||
#
|
|
||||||
# KHÔNG nổ khi: planner hold chờ người (twist 0 -> cmd = 0), phép đo 3 phanh (lệnh sau trọng tài
|
|
||||||
# = 0), xoay tại chỗ đầu chặng (v = 0), sát goal (goal_zone), vừa nhận plan (grace_after_new_plan).
|
|
||||||
# KHÔNG bắt được: robot lao THẲNG về goal vào vật cản (khoảng cách goal vẫn giảm) — đó là việc của
|
|
||||||
# phép đo 1 và 3. Lỗ đã biết: robot LẾT chậm (< min_commanded / window) không được đo — cố ý, để
|
|
||||||
# không nhầm với chờ.
|
|
||||||
progress_enabled: true
|
|
||||||
|
|
||||||
# "log" = chỉ in cảnh báo, KHÔNG leo thang. Giai đoạn quan sát: chạy vài ca, đọc dòng
|
|
||||||
# "PathMonitor: no progress" xem có nổ oan không rồi mới chuyển.
|
|
||||||
# "recover" = leo thang kOscillation -> route `oscillation` bên recovery_behaviors_params.yaml.
|
|
||||||
# kOscillation là trigger GỐC: cạn route = ABORT chặng, báo fleet. Nổ oan là MẤT CHẶNG.
|
|
||||||
# ► Ngưỡng bên dưới rút từ ĐÚNG HAI ca kẹt trong một log sim — chưa có plan vòng gắt, chưa có
|
|
||||||
# robot thật (bánh trượt, odom nhiễu).
|
|
||||||
progress_action: recover
|
|
||||||
|
|
||||||
# [s] Cửa sổ đo. Mission 2 trong log: khoảng cách tới goal đứng đúng 3 s trước khi planner hold.
|
|
||||||
# ► Chỉnh: TĂNG (5.0) nếu nổ oan ở chỗ robot lết chậm hợp lệ (cửa hẹp có người); GIẢM = nhạy hơn
|
|
||||||
# nhưng dễ nổ vì một nhịp odom.
|
|
||||||
progress_window: 3.0
|
|
||||||
|
|
||||||
# [m] Lệnh tích luỹ trong cửa sổ ít hơn đây = robot đang chờ / xoay, KHÔNG đo. 0.30 m / 3 s =
|
|
||||||
# 0.1 m/s trung bình. ► TĂNG (0.5) để bỏ qua lệnh rất nhỏ; ĐỪNG đặt 0 (chờ hợp lệ thành "kẹt").
|
|
||||||
progress_min_commanded: 0.30
|
|
||||||
|
|
||||||
# [-] prog/cmd DƯỚI đây là kẹt. Log: kẹt <= 0.27, bình thường >= 0.65 — biên rộng hơn gấp đôi.
|
|
||||||
# ► GIẢM (0.20) nếu nổ oan trên plan vòng gắt (tiến dọc plan thật nhưng khoảng cách goal tăng).
|
|
||||||
progress_min_efficiency: 0.30
|
|
||||||
|
|
||||||
# [-] prog/cmd TRÊN đây mới xoá chuỗi kẹt. Phải > min_efficiency: vùng 0.30-0.60 là vùng chết
|
|
||||||
# (giữ trạng thái) — bằng nhau là cờ nhấp nháy quanh một ngưỡng mỗi lượt quét.
|
|
||||||
# Chỉ tiến bộ THẬT mới xoá cờ; cmd tụt dưới min_commanded (chậm lại, hold) thì GIỮ NGUYÊN — sim 4/9
|
|
||||||
# cho thấy robot kẹt hay lượn quanh đúng ngưỡng tốc độ và làm cờ bật/tắt 5 lần cho một đợt.
|
|
||||||
progress_clear_efficiency: 0.60
|
|
||||||
|
|
||||||
# [s] Kẹt liên tục chừng này mới khai hoả (= 5 lượt check_period 0.2). Lọc một nhịp dist nhiễu.
|
|
||||||
progress_confirm_time: 1.0
|
|
||||||
|
|
||||||
# [m] Gần goal hơn đây không đo: rot2goal / dừng chính xác có lệnh nhỏ mà không tiến — hợp lệ.
|
|
||||||
# ► Đặt >= xy_goal_tolerance của profile + 0.3.
|
|
||||||
progress_goal_zone: 0.6
|
|
||||||
@@ -54,7 +54,7 @@ PriestLocalPlanner:
|
|||||||
# sự dài thì khoảng hụt kéo dài qua ngưỡng này và horizon vẫn bung.
|
# sự dài thì khoảng hụt kéo dài qua ngưỡng này và horizon vẫn bung.
|
||||||
# 0 = bung ngay (hành vi cũ)
|
# 0 = bung ngay (hành vi cũ)
|
||||||
# --- Solver tự khai bất khả thi (2026-09-04) ---
|
# --- Solver tự khai bất khả thi (2026-09-04) ---
|
||||||
# Mọi cổng va chạm của planner này đã chuyển sang path_monitor/collision của move_base2 (LETHAL +
|
# Mọi cổng va chạm của planner này đã chuyển sang env_monitor/contact (guard cũ path_monitor/collision đã xoá 6/9/2026) của move_base2 (LETHAL +
|
||||||
# chu vi footprint đệm); planner không còn tự dò costmap để dừng. Thứ duy nhất nó còn giữ là tín
|
# chu vi footprint đệm); planner không còn tự dò costmap để dừng. Thứ duy nhất nó còn giữ là tín
|
||||||
# hiệu không ai khác có: best_res_norm — chuẩn vi phạm ràng buộc của CẢ batch. Thang đo thực tế:
|
# hiệu không ai khác có: best_res_norm — chuẩn vi phạm ràng buộc của CẢ batch. Thang đo thực tế:
|
||||||
# bám đường sạch 0–0.3, né bình thường 1–2, mọi ca ngõ cụt đã đọc 4–7. Đo 4/9: res treo 4.5–7.0
|
# bám đường sạch 0–0.3, né bình thường 1–2, mọi ca ngõ cụt đã đọc 4–7. Đo 4/9: res treo 4.5–7.0
|
||||||
@@ -64,7 +64,7 @@ PriestLocalPlanner:
|
|||||||
# CỔNG sv > 0 (2026-09-04): res tính trên CẢ horizon, nên hàng người cách 1.7 m — solver còn 5 s
|
# CỔNG sv > 0 (2026-09-04): res tính trên CẢ horizon, nên hàng người cách 1.7 m — solver còn 5 s
|
||||||
# để xử — đọc y hệt ngõ cụt (đo 15:21: khai 10 lần, cả 10 lần sv=0, gần nhất 1.3 m). Chỉ khai khi
|
# để xử — đọc y hệt ngõ cụt (đo 15:21: khai 10 lần, cả 10 lần sv=0, gần nhất 1.3 m). Chỉ khai khi
|
||||||
# start_violations > 0, tức thân đã lấn vành ellipse của ít nhất một vật cản THẬT (với ngưỡng 254).
|
# start_violations > 0, tức thân đã lấn vành ellipse của ít nhất một vật cản THẬT (với ngưỡng 254).
|
||||||
infeasible_residual: 3.5 # [-] res >= mức này coi là bất khả thi. Hạ (2.0) = khai sớm hơn,
|
infeasible_residual: 3.0 # [-] res >= mức này coi là bất khả thi. Hạ (2.0) = khai sớm hơn,
|
||||||
# dễ khai oan ở bãi dày mà vẫn lách được; nâng (4–5) = chỉ khai khi
|
# dễ khai oan ở bãi dày mà vẫn lách được; nâng (4–5) = chỉ khai khi
|
||||||
# đã bế tắc rõ. 0 = TẮT.
|
# đã bế tắc rõ. 0 = TẮT.
|
||||||
infeasible_streak_cycles: 20 # [chu kỳ] treo liên tục ngần này mới khai (20 @20 Hz = 1 s) —
|
infeasible_streak_cycles: 20 # [chu kỳ] treo liên tục ngần này mới khai (20 @20 Hz = 1 s) —
|
||||||
@@ -309,7 +309,7 @@ PriestLocalPlanner:
|
|||||||
|
|
||||||
# ================= Hình học thân cho các probe còn lại =================
|
# ================= Hình học thân cho các probe còn lại =================
|
||||||
# Cổng va chạm của planner này (dò quỹ đạo / dò cung lệnh / chốt thân-trong-vành) đã CHUYỂN sang
|
# Cổng va chạm của planner này (dò quỹ đạo / dò cung lệnh / chốt thân-trong-vành) đã CHUYỂN sang
|
||||||
# path_monitor/collision của move_base2 (2026-09-03): LETHAL + chu vi footprint đệm, planner-agnostic.
|
# env_monitor/contact (guard cũ path_monitor/collision đã xoá 6/9/2026) của move_base2 (2026-09-03): LETHAL + chu vi footprint đệm, planner-agnostic.
|
||||||
# Planner không còn tự dừng vì va chạm. Bốn khoá dưới đây chỉ còn là HÌNH HỌC THÂN cho ba phép dò
|
# Planner không còn tự dừng vì va chạm. Bốn khoá dưới đây chỉ còn là HÌNH HỌC THÂN cho ba phép dò
|
||||||
# mềm còn lại: head_slowdown, quét pivot trước khi xoay tại chỗ, đĩa kiểm goal bị đè. Giữ tên khoá
|
# mềm còn lại: head_slowdown, quét pivot trước khi xoay tại chỗ, đĩa kiểm goal bị đè. Giữ tên khoá
|
||||||
# để không đổi schema.
|
# để không đổi schema.
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# =================================================================================================
|
# =================================================================================================
|
||||||
# recovery_behaviors_params.yaml — bộ recovery behavior (recovery_core, gen-2 tick-based) và tham số
|
# recovery_behaviors_params.yaml — bộ recovery behavior (recovery_core, gen-2 tick-based) và tham số
|
||||||
# vận hành. Sắp xếp lại theo bốn phép đo của PathMonitor ngày 4/9/2026.
|
# vận hành. Sắp xếp lại theo bốn phép đo của PathMonitor ngày 4/9/2026; từ 6/9/2026 các cờ do EnvMonitor cấp (env_monitor_params.yaml).
|
||||||
#
|
#
|
||||||
# Quy ước comment: dòng mô tả = LÀ GÌ / vì sao; `► Chỉnh:` = tăng giảm theo triệu chứng nào.
|
# Quy ước comment: dòng mô tả = LÀ GÌ / vì sao; `► Chỉnh:` = tăng giảm theo triệu chứng nào.
|
||||||
#
|
#
|
||||||
@@ -50,8 +50,8 @@ recovery:
|
|||||||
# 2. ROUTES — trigger nào chạy chuỗi behavior nào
|
# 2. ROUTES — trigger nào chạy chuỗi behavior nào
|
||||||
# ===============================================================================================
|
# ===============================================================================================
|
||||||
#
|
#
|
||||||
# Sáu trigger, chia hai nhóm theo HẬU QUẢ KHI CẠN ROUTE. Bốn phép đo của PathMonitor
|
# Sáu trigger, chia hai nhóm theo HẬU QUẢ KHI CẠN ROUTE. Ba verdict của EnvMonitor
|
||||||
# (`path_monitor_params.yaml`) đổ vào bốn trigger, mỗi phép đo có ngân sách và khoá RIÊNG:
|
# (`env_monitor_params.yaml`: kUnreachable -> path_blocked, kContact -> collision, kStall -> oscillation; off_path không còn nguồn) đổ vào bốn trigger, mỗi phép đo có ngân sách và khoá RIÊNG:
|
||||||
#
|
#
|
||||||
# Phép đo Trigger Cạn route thì...
|
# Phép đo Trigger Cạn route thì...
|
||||||
# ------------------ ------------------ -------------------------------------------------
|
# ------------------ ------------------ -------------------------------------------------
|
||||||
@@ -82,7 +82,7 @@ recovery:
|
|||||||
# hết ra lệnh quá `controller_patience`), `planning_failed`, hoặc `oscillation`.
|
# hết ra lệnh quá `controller_patience`), `planning_failed`, hoặc `oscillation`.
|
||||||
#
|
#
|
||||||
# ► Chỉnh route (áp cho cả sáu):
|
# ► Chỉnh route (áp cho cả sáu):
|
||||||
# - route RỖNG = TẮT HẲN trigger đó. Bốn route của PathMonitor được phép rỗng; hai route
|
# - route RỖNG = TẮT HẲN trigger đó. Bốn route giám sát (path_blocked/off_path/collision/oscillation) được phép rỗng; hai route
|
||||||
# `planning_failed`/`controlling_failed` bắt buộc có ít nhất một behavior (validate chặn).
|
# `planning_failed`/`controlling_failed` bắt buộc có ít nhất một behavior (validate chặn).
|
||||||
# - route được phép LẶP behavior: mỗi lần xuất hiện là một lượt thử (`[detour, wait, detour]`).
|
# - route được phép LẶP behavior: mỗi lần xuất hiện là một lượt thử (`[detour, wait, detour]`).
|
||||||
# - route dài hơn = kiên nhẫn hơn nhưng chậm báo lỗi lên fleet hơn.
|
# - route dài hơn = kiên nhẫn hơn nhưng chậm báo lỗi lên fleet hơn.
|
||||||
@@ -114,11 +114,12 @@ recovery:
|
|||||||
# trước lượt lập đường sau; đổi lại chậm báo lỗi lên fleet.
|
# trước lượt lập đường sau; đổi lại chậm báo lỗi lên fleet.
|
||||||
oscillation: [detour_path]
|
oscillation: [detour_path]
|
||||||
|
|
||||||
# --- Nhóm B: DỰ BÁO của PathMonitor — cạn route là KHOÁ, robot đi tiếp ------------------------
|
# --- Nhóm B: DỰ BÁO của EnvMonitor (kUnreachable, kContact) — cạn route là KHOÁ, robot đi tiếp -----
|
||||||
|
|
||||||
# PHÉP ĐO 1: plan phía trước chạm vành 253 trong `horizon` (3 m). Cảnh báo SỚM nhất, robot còn
|
# PHÉP ĐO 1: plan phía trước chạm vành 253 trong `horizon` (3 m). Cảnh báo SỚM nhất, robot còn
|
||||||
# cách vật cản cả mét — lập lại đường là đủ, không cần lùi.
|
# cách vật cản cả mét — lập lại đường là đủ, không cần lùi.
|
||||||
# ► Chỉnh: phép đo này đang TẮT ở `path_monitor_params.yaml` (`path_blocked_enabled: false`).
|
# ► Chỉnh: cờ này do kUnreachable của `env_monitor_params.yaml` cấp (`unreachable/action: recover`); phép đo 1
|
||||||
|
# "plan ∩ 253" của PathMonitor cũ đã xoá 6/9/2026.
|
||||||
# Route vẫn khai sẵn để bật lại chỉ bằng một dòng bên đó.
|
# Route vẫn khai sẵn để bật lại chỉ bằng một dòng bên đó.
|
||||||
path_blocked: [detour_path]
|
path_blocked: [detour_path]
|
||||||
|
|
||||||
|
|||||||
@@ -145,13 +145,12 @@ struct MoveBase2Config
|
|||||||
/// Lỗi schema của `docking_marker_profiles`, giữ lại để @ref validate chặn boot an toàn.
|
/// Lỗi schema của `docking_marker_profiles`, giữ lại để @ref validate chặn boot an toàn.
|
||||||
std::string docking_marker_profiles_error;
|
std::string docking_marker_profiles_error;
|
||||||
|
|
||||||
/// Giữ marker cho docking planner legacy; false cho profile docking dựa hoàn toàn vào goal_frame.
|
// --- EnvMonitor (Test/ENV_MONITOR_PLAN.md) — namespace con `env_monitor` ------------------------
|
||||||
bool docking_requires_marker = true;
|
EnvMonitorConfig env_monitor; ///< lõi quyết định (stall / unreachable / contact / ngân sách).
|
||||||
|
EnvSamplingConfig env_sampling; ///< lấy mẫu ở cổng (nhịp, thân, hình học, quỹ đạo).
|
||||||
// --- Giám sát tuyến ---------------------------------------------------------------------------
|
EnvLoggerConfig env_logger; ///< ghi JSONL cho replay/ML. Tắt mặc định.
|
||||||
|
/// Nhóm log `debug/env`: một dòng tóm tắt feature mỗi giây khi đang giám sát. Mặc định bật.
|
||||||
/// Đọc từ namespace con `path_monitor`. Tắt mặc định — bật bằng config, không bật ngầm.
|
bool debug_env = true;
|
||||||
ControlLoopConfig::PathMonitorConfig path_monitor;
|
|
||||||
|
|
||||||
// --- Namespace cho các thành phần nạp plugin ---------------------------------------------------
|
// --- Namespace cho các thành phần nạp plugin ---------------------------------------------------
|
||||||
|
|
||||||
@@ -164,22 +163,6 @@ struct MoveBase2Config
|
|||||||
/// Namespace chứa cấu hình mission layer.
|
/// Namespace chứa cấu hình mission layer.
|
||||||
std::string mission_namespace = "mission_adapters";
|
std::string mission_namespace = "mission_adapters";
|
||||||
|
|
||||||
/**
|
|
||||||
* Dựng mission layer (`mission_adapters`) hay không.
|
|
||||||
*
|
|
||||||
* `true` (mặc định): VDA5050 Order đi qua mission layer — order được cắt thành từng chặng tại
|
|
||||||
* mỗi node có action, chỉ phần `released` được chạy, `orderUpdateId` nối tiếp thay vì chạy lại,
|
|
||||||
* và có `mission_timeout` làm lưới cuối.
|
|
||||||
*
|
|
||||||
* `false`: order đi thẳng xuống navigation như MỘT goal duy nhất (hành vi của move_base gen-1).
|
|
||||||
* Đây là đường lùi khi mission layer gây vấn đề trên hiện trường — đổi một khoá YAML, không phải
|
|
||||||
* build lại.
|
|
||||||
*
|
|
||||||
* @note Bật mà không nạp được nguồn nào thì runtime tự quay về đường trực tiếp **kèm log cảnh
|
|
||||||
* báo**: thiếu plugin không được phép biến thành robot đứng im không rõ lý do.
|
|
||||||
*/
|
|
||||||
bool mission_layer_enabled = true;
|
|
||||||
|
|
||||||
// --- Frame ------------------------------------------------------------------------------------
|
// --- Frame ------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/// Frame mà goal được quy về trước khi lập plan.
|
/// Frame mà goal được quy về trước khi lập plan.
|
||||||
|
|||||||
@@ -12,7 +12,9 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <limits>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -22,11 +24,13 @@
|
|||||||
#include <move_base2/core/navigation_request.h>
|
#include <move_base2/core/navigation_request.h>
|
||||||
#include <move_base2/core/state_machine.h>
|
#include <move_base2/core/state_machine.h>
|
||||||
#include <move_base2/core/velocity_arbiter.h>
|
#include <move_base2/core/velocity_arbiter.h>
|
||||||
|
#include <move_base2/core/env_monitor.h>
|
||||||
|
#include <move_base2/io/env_logger.h>
|
||||||
#include <move_base2/ports/action_port.h>
|
#include <move_base2/ports/action_port.h>
|
||||||
#include <move_base2/ports/clock_port.h>
|
#include <move_base2/ports/clock_port.h>
|
||||||
#include <move_base2/ports/controller_port.h>
|
#include <move_base2/ports/controller_port.h>
|
||||||
#include <move_base2/ports/mission_port.h>
|
#include <move_base2/ports/mission_port.h>
|
||||||
#include <move_base2/ports/path_check_port.h>
|
#include <move_base2/ports/environment_port.h>
|
||||||
#include <move_base2/ports/planner_port.h>
|
#include <move_base2/ports/planner_port.h>
|
||||||
#include <move_base2/ports/pose_port.h>
|
#include <move_base2/ports/pose_port.h>
|
||||||
#include <move_base2/ports/costmap_status_port.h>
|
#include <move_base2/ports/costmap_status_port.h>
|
||||||
@@ -60,12 +64,12 @@ struct ControlLoopDeps
|
|||||||
CostmapStatusPort* costmap_status = nullptr;
|
CostmapStatusPort* costmap_status = nullptr;
|
||||||
ActionPort* action = nullptr; ///< Có thể null (D8) — null thì yêu cầu có action bị từ chối.
|
ActionPort* action = nullptr; ///< Có thể null (D8) — null thì yêu cầu có action bị từ chối.
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Nguồn trả lời "tuyến này còn đi được không". **Có thể null** — null thì `PathMonitor` và luật
|
* Nguồn dữ liệu của EnvMonitor (Test/ENV_MONITOR_PLAN.md). **Có thể null** — null thì EnvMonitor tắt
|
||||||
* lọc plan bẩn đều tắt, hành vi y hệt trước 2026-08-04d. Mọi bộ test dùng cổng giả rơi vào nhánh
|
* hoàn toàn, mọi bộ test dùng cổng giả rơi vào nhánh này.
|
||||||
* này.
|
|
||||||
*/
|
*/
|
||||||
PathCheckPort* path_check = nullptr;
|
EnvironmentPort* environment = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -145,133 +149,12 @@ struct ControlLoopConfig
|
|||||||
/// Override cho profile docking. Không có entry hoặc marker rỗng -> dùng @ref docking.
|
/// Override cho profile docking. Không có entry hoặc marker rỗng -> dùng @ref docking.
|
||||||
DockingMarkerProfiles docking_marker_profiles;
|
DockingMarkerProfiles docking_marker_profiles;
|
||||||
|
|
||||||
/**
|
|
||||||
* Giữ contract `dockTo` cũ: `PNKXDockingLocalPlanner` cần marker để đọc `maker_name` lúc init.
|
|
||||||
* Đặt false khi profile docking nhận goal tuyệt đối/goal_frame (vd `HybridLocalPlanner`) và không
|
|
||||||
* đọc marker; vẫn validate marker khi caller cung cấp nó.
|
|
||||||
*/
|
|
||||||
bool docking_requires_marker = true;
|
|
||||||
|
|
||||||
/**
|
// --- EnvMonitor (Test/ENV_MONITOR_PLAN.md) — bộ giám sát môi trường duy nhất từ 6/9/2026 ----------
|
||||||
* @struct PathMonitorConfig
|
EnvMonitorConfig env_monitor;
|
||||||
* @brief Giám sát tuyến trong lúc bám plan (contract 2026-08-04d).
|
EnvSamplingConfig env_sampling;
|
||||||
*
|
EnvLoggerConfig env_logger;
|
||||||
* Hai phép đo độc lập, cùng một kết cục: robot **dừng** rồi chạy recovery của trigger tương ứng.
|
bool debug_env = true; ///< nhóm log `debug/env`: một dòng tóm tắt feature mỗi giây khi đang giám sát.
|
||||||
* - tuyến phía trước bị chắn → `path_blocked`
|
|
||||||
* - robot đã lệch khỏi plan → `off_path`
|
|
||||||
*
|
|
||||||
* Mỗi phép đo tắt riêng được, và route rỗng cũng là tắt — có hai nút để đảm bảo đường lùi.
|
|
||||||
*/
|
|
||||||
struct PathMonitorConfig
|
|
||||||
{
|
|
||||||
bool enabled = false; ///< Công tắc CHUNG. Tắt mặc định: bật bằng config, không bật ngầm.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Công tắc RIÊNG cho từng phép đo, nằm dưới @ref enabled (tắt cái chung là tắt cả hai).
|
|
||||||
*
|
|
||||||
* Trước 2026-08-06 chỉ tắt được bằng cách đặt `horizon`/`max_cross_track` về 0 — một giá trị
|
|
||||||
* ma, đọc config không thấy ý định, và lẫn với "quên khai". Hai cờ này nói thẳng. Ngưỡng 0 vẫn
|
|
||||||
* còn tác dụng tắt để config cũ không đổi hành vi.
|
|
||||||
*/
|
|
||||||
bool path_blocked_enabled = true;
|
|
||||||
bool off_path_enabled = true;
|
|
||||||
|
|
||||||
double check_period = 0.2; ///< [s] nhịp quét. KHÔNG quét mỗi control cycle — quá phí.
|
|
||||||
double confirm_time = 0.5; ///< [s] điều kiện phải đúng liên tục chừng này mới khai hoả.
|
|
||||||
|
|
||||||
/// [m] Quãng tuyến phía trước cần quét. Phải <= nửa cửa sổ costmap local, ngoài đó không có dữ
|
|
||||||
/// liệu. 0 = tắt phép đo "tuyến bị chắn" (cách cũ; ưu tiên @ref path_blocked_enabled).
|
|
||||||
double horizon = 3.0;
|
|
||||||
|
|
||||||
/// [m] Vật cản gần hơn mức này thì KHÔNG khai hoả: detour từ sát nách vật cản hầu như không ra
|
|
||||||
/// nghiệm, và tình huống đó đã có `controller_patience` lo.
|
|
||||||
double min_detour_distance = 0.5;
|
|
||||||
|
|
||||||
/// [m] Lệch khỏi plan quá mức này thì khai hoả. 0 = tắt phép đo "lệch tuyến" (cách cũ; ưu tiên
|
|
||||||
/// @ref off_path_enabled). Để 0 nghĩa là bật cờ thôi vẫn chưa đủ — phải cho một ngưỡng thật.
|
|
||||||
double max_cross_track = 0.0;
|
|
||||||
|
|
||||||
/// [s] Bỏ qua phép đo lệch tuyến ngay sau khi nhận plan mới — lúc preempt/đổi chặng pose robot
|
|
||||||
/// có thể lệch nhất thời so với plan vừa tới.
|
|
||||||
double grace_after_new_plan = 1.0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* -- Phép đo 3: thân robot sắp chạm vật cản THẬT (2026-09-03) ---------------------------------
|
|
||||||
*
|
|
||||||
* Độc lập với hai phép đo trên — không dùng chung nhịp, ngưỡng hay công tắc nào của chúng.
|
|
||||||
* Đối tượng đo là cung mà lệnh `(v, omega)` của controller sắp vẽ ra từ pose hiện tại, dò
|
|
||||||
* bằng footprint đã đệm, ngưỡng LETHAL. Chạy MỖI control cycle (không theo `check_period`):
|
|
||||||
* 0.2 s + 0.5 s xác nhận ở 0.5 m/s là 0.35 m — bằng cả quãng dừng.
|
|
||||||
*
|
|
||||||
* Hai bước khi chạm trong `collision_stop_distance`: (1) ngay cycle đó lệnh ra bằng 0 — trọng
|
|
||||||
* tài phát 0 tức thì như costmap stale; (2) sau `collision_confirm_cycles` cycle liên tiếp,
|
|
||||||
* giơ cờ `collision` để state machine chạy route `collision` (tách khỏi phép đo 1 ngày 4/9: nổ
|
|
||||||
* ở 0.2 m thì lùi trước khi lập lại đường có nghĩa, ở 3 m thì không).
|
|
||||||
*/
|
|
||||||
bool collision_enabled = false;
|
|
||||||
double collision_lookahead = 0.8; ///< [m] quãng cung cần dò — tầm phanh.
|
|
||||||
int collision_max_samples = 6; ///< số mẫu trên cung.
|
|
||||||
double collision_stop_distance = 0.35; ///< [m] chạm gần hơn đây: lệnh 0 ngay + đếm xác nhận.
|
|
||||||
int collision_confirm_cycles = 2; ///< [cycle] chạm liên tiếp chừng này mới giơ cờ.
|
|
||||||
int collision_cost_threshold = 254; ///< cost >= đây là chạm. 254 = chỉ LETHAL, không inflation.
|
|
||||||
/**
|
|
||||||
* [m] Hình chữ nhật thân tính từ base_link: mũi / đuôi / nửa bề rộng. CẢ BA bằng 0 (mặc định)
|
|
||||||
* = lấy từ footprint không đệm của costmap qua PathCheckPort::robotBodyExtents() mỗi cycle — cùng
|
|
||||||
* hình với SBPL và PRIEST, đổi footprint lúc chạy là guard đổi theo. Khai tay thì phải khai đủ
|
|
||||||
* cả ba > 0; khai lửng là config sai. Không có cả hai nguồn thì guard báo lỗi và KHÔNG kết luận
|
|
||||||
* (không dừng) ở cycle đó.
|
|
||||||
*/
|
|
||||||
double collision_front_offset = 0.0;
|
|
||||||
double collision_rear_offset = 0.0;
|
|
||||||
double collision_side_offset = 0.0;
|
|
||||||
double collision_footprint_padding = 0.1;///< [m] biên an toàn tường minh, thay cho inflation.
|
|
||||||
/// Loại trừ ô LETHAL đang nằm dưới thân đệm tại pose hiện tại (xem MotionCheckParams).
|
|
||||||
bool collision_exclude_baseline = true;
|
|
||||||
double collision_min_speed = 0.02; ///< [m/s] |v| dưới đây không dò (xoay tại chỗ).
|
|
||||||
/**
|
|
||||||
* Tiêu chí "sắp chạm" theo THỜI GIAN và GÓC (2026-09-04, run 16:26). Trong stop_distance,
|
|
||||||
* "walk mét dọc cung" không nói lên gì với cung xoay gần tại chỗ: 0.15 m ở R = 0.08 m là quay
|
|
||||||
* 107° và còn 3.8 s, còn PRIEST sẽ thôi xoay sau 30–60° để thẳng hàng. Chỉ dừng khi
|
|
||||||
* `walk/|v| <= collision_stop_time` HOẶC `|omega/v|·walk <= collision_stop_angle`; ngoài đó chỉ
|
|
||||||
* kẹp v <= (walk − padding)/stop_time. Đi thẳng (turn ≈ 0) luôn "sắp" -> y như trước.
|
|
||||||
*/
|
|
||||||
double collision_stop_time = 1.0; ///< [s]
|
|
||||||
double collision_stop_angle = 0.35; ///< [rad] ~20°
|
|
||||||
double collision_lookahead_angle = 1.0; ///< [rad] cung không dò quá góc này (xem MotionCheckParams).
|
|
||||||
/// [m/s^2] Chạm XA hơn stop_distance thì kẹp `v <= sqrt(2 a (d - stop))` trước khi phát;
|
|
||||||
/// 0 = không kẹp, chỉ dừng khi đã vào stop_distance.
|
|
||||||
double collision_brake_decel = 0.0;
|
|
||||||
|
|
||||||
// --- Phép đo 4: tiến độ so với lệnh (2026-09-04) ------------------------------------------
|
|
||||||
//
|
|
||||||
// "Planner ĐÒI ĐI mà robot KHÔNG TIẾN" — định nghĩa vận hành của local minimum. Không cần biết
|
|
||||||
// vì sao kẹt: trong cửa sổ `progress_window`, cộng tổng |v| ĐÃ PHÁT (sau trọng tài) và so với
|
|
||||||
// quãng tiến được (max của tiến dọc plan và giảm khoảng cách tới goal). Lệnh nhiều mà tiến ít
|
|
||||||
// là kẹt. Đo trên sim 3/9: bình thường eff 0.65-0.85 kể cả khi né lệch plan 1.9 m; hai ca kẹt
|
|
||||||
// thật eff -0.14..0.27, nổ 12 s trước khi hệ cũ vào RECOVERING.
|
|
||||||
//
|
|
||||||
// KHÔNG nổ khi: PRIEST hold chờ người (twist 0 -> cmd = 0), Phép đo 3 phanh (lệnh sau trọng
|
|
||||||
// tài = 0), rot2path (v = 0), sát goal (`progress_goal_zone`), vừa nhận plan (grace).
|
|
||||||
bool progress_enabled = false;
|
|
||||||
/// "log" = chỉ in cảnh báo (giai đoạn quan sát); "recover" = leo thang kOscillation.
|
|
||||||
/// Mặc định log: ngưỡng rút từ hai ca kẹt trong một log sim, và kOscillation là trigger GỐC —
|
|
||||||
/// cạn route là ABORT chặng, nổ oan là mất chặng chứ không chỉ mất vài giây.
|
|
||||||
std::string progress_action = "log";
|
|
||||||
double progress_window = 3.0; ///< [s] cửa sổ đo.
|
|
||||||
double progress_min_commanded = 0.30; ///< [m] lệnh tích luỹ ít hơn = đang chờ, không đo.
|
|
||||||
double progress_min_efficiency = 0.30; ///< [-] prog/cmd dưới đây là kẹt.
|
|
||||||
double progress_clear_efficiency = 0.60; ///< [-] prog/cmd trên đây mới xoá chuỗi kẹt (hysteresis).
|
|
||||||
double progress_confirm_time = 1.0; ///< [s] kẹt liên tục chừng này mới khai hoả.
|
|
||||||
double progress_goal_zone = 0.6; ///< [m] gần goal hơn đây không đo (rot2goal, dừng chính xác).
|
|
||||||
|
|
||||||
/// Profile được giám sát. Docking/go_straight/rotate KHÔNG bao giờ nên có mặt: đường cong vào
|
|
||||||
/// dock là một thể thống nhất, cắt ghép nó là phá chính hình học đó.
|
|
||||||
std::vector<MotionProfile> profiles{ MotionProfile::kPosition };
|
|
||||||
|
|
||||||
bool monitors(MotionProfile profile) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
PathMonitorConfig path_monitor;
|
|
||||||
|
|
||||||
bool validate(std::string& error) const;
|
bool validate(std::string& error) const;
|
||||||
std::string describe() const;
|
std::string describe() const;
|
||||||
@@ -461,42 +344,11 @@ private:
|
|||||||
void runController(robot_geometry_msgs::Twist& candidate);
|
void runController(robot_geometry_msgs::Twist& candidate);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Chạy PathMonitor và cập nhật hai cờ khai hoả.
|
* @brief EnvMonitor: lấy mẫu môi trường (nặng theo `check_period`, quỹ đạo mỗi cycle), chạy lõi, giữ
|
||||||
*
|
* verdict cho state machine và trọng tài của CYCLE NÀY. Gọi trước state machine.
|
||||||
* Gọi ở đầu cycle, TRƯỚC state machine — hai cờ là dữ liệu vào của nó.
|
|
||||||
*
|
|
||||||
* @param now Thời điểm cycle này.
|
|
||||||
* @param robot_pose Pose robot; chỉ dùng khi @p pose_available.
|
|
||||||
* @param pose_available Mất pose thì không đo được gì cả.
|
|
||||||
* @param costmap_stale Costmap quá hạn thì mọi kết luận về vật cản đều là quá khứ.
|
|
||||||
*/
|
*/
|
||||||
void updatePathMonitor(const robot::Time& now, const robot_geometry_msgs::PoseStamped& robot_pose,
|
void updateEnvMonitor(const robot::Time& now, double dt, const robot_geometry_msgs::PoseStamped& robot_pose,
|
||||||
bool pose_available, bool costmap_stale);
|
bool pose_available, bool costmap_stale);
|
||||||
|
|
||||||
/// @brief Đặt lại toàn bộ trạng thái debounce của monitor (đổi chặng, rời kControlling, tắt).
|
|
||||||
void resetPathMonitor();
|
|
||||||
|
|
||||||
/// @brief Phép đo 4: "planner đòi đi mà không tiến". Gọi từ updatePathMonitor theo nhịp quét.
|
|
||||||
void updateProgressMonitor(const robot::Time& now, std::size_t from_index,
|
|
||||||
const robot_geometry_msgs::PoseStamped& robot_pose);
|
|
||||||
void resetProgressMonitor();
|
|
||||||
/// @brief Dựng lại cache độ dài cộng dồn của latest_plan_ và xoá ring (mẫu cũ thuộc plan cũ).
|
|
||||||
void rebuildPlanArcLength();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Phép đo 3 của PathMonitor: dò cung lệnh @p candidate, ghi đè lệnh nếu sắp chạm.
|
|
||||||
*
|
|
||||||
* Gọi NGAY SAU controller, TRƯỚC trọng tài, mỗi cycle. Đặt @ref collision_brake_ (lệnh ra = 0
|
|
||||||
* cycle này) và, sau đủ cycle xác nhận, @ref collision_blocked_ (cờ vào state machine cycle
|
|
||||||
* sau, gộp với `path_blocked`).
|
|
||||||
*/
|
|
||||||
void applyCollisionGuard(const robot_geometry_msgs::PoseStamped& robot_pose, bool pose_available,
|
|
||||||
robot_geometry_msgs::Twist& candidate);
|
|
||||||
|
|
||||||
/// @brief Plan này có bị chắn không. false = không kết luận được (thiếu cổng/TF/costmap).
|
|
||||||
bool planIsBlocked(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
|
||||||
std::size_t from_index) const;
|
|
||||||
|
|
||||||
/// @brief Quaternion có chuẩn hoá được không — goal hỏng phải bị từ chối ngay tại cửa.
|
/// @brief Quaternion có chuẩn hoá được không — goal hỏng phải bị từ chối ngay tại cửa.
|
||||||
static bool isQuaternionValid(const robot_geometry_msgs::PoseStamped& pose);
|
static bool isQuaternionValid(const robot_geometry_msgs::PoseStamped& pose);
|
||||||
|
|
||||||
@@ -520,6 +372,10 @@ private:
|
|||||||
PlannerFeedback planner_feedback_ = PlannerFeedback::kIdle;
|
PlannerFeedback planner_feedback_ = PlannerFeedback::kIdle;
|
||||||
ControllerFeedback controller_feedback_ = ControllerFeedback::kIdle;
|
ControllerFeedback controller_feedback_ = ControllerFeedback::kIdle;
|
||||||
RecoveryFeedback recovery_feedback_ = RecoveryFeedback::kIdle;
|
RecoveryFeedback recovery_feedback_ = RecoveryFeedback::kIdle;
|
||||||
|
/// Trigger của behavior đang chạy. `StateMachineOutput::recovery_trigger` chỉ được điền ở cycle khởi động
|
||||||
|
/// (và khi trừ ngân sách), ở cycle tick nó là giá trị mặc định — đọc nó lúc kết thúc recovery là sai
|
||||||
|
/// (stand-down của kContact không bao giờ được vũ trang, run 6/9 16:30: 4 detour collision cùng chỗ → ABORT).
|
||||||
|
RecoveryTrigger running_recovery_trigger_ = RecoveryTrigger::kPlanningFailed;
|
||||||
ActionFeedback action_feedback_ = ActionFeedback::kIdle;
|
ActionFeedback action_feedback_ = ActionFeedback::kIdle;
|
||||||
|
|
||||||
/// Lý do action handler đưa ra cho lần tick vừa rồi; chỉ giữ khi tick đó HỎNG, xem
|
/// Lý do action handler đưa ra cho lần tick vừa rồi; chỉ giữ khi tick đó HỎNG, xem
|
||||||
@@ -550,60 +406,27 @@ private:
|
|||||||
/// (@ref ControlLoopConfig::planner_frequency). Đặt lại khi nhận yêu cầu mới.
|
/// (@ref ControlLoopConfig::planner_frequency). Đặt lại khi nhận yêu cầu mới.
|
||||||
robot::Time last_plan_started_;
|
robot::Time last_plan_started_;
|
||||||
|
|
||||||
// --- PathMonitor -----------------------------------------------------------------------------
|
|
||||||
//
|
|
||||||
// Hai cờ này là KẾT QUẢ ĐÃ DEBOUNCE, không phải phép đo thô: chúng chỉ bật sau khi điều kiện đúng
|
|
||||||
// liên tục đủ `confirm_time`. Người/xe đi ngang qua đúng một lượt quét không được làm robot dừng.
|
|
||||||
bool path_blocked_ = false;
|
|
||||||
bool off_path_ = false;
|
|
||||||
/// [m] Chỗ chắn gần nhất đo được. `mutable` vì nó chỉ phục vụ log — `planIsBlocked` vẫn là phép
|
|
||||||
/// hỏi thuần, không đổi quyết định nào của loop.
|
|
||||||
mutable double last_block_distance_ = 0.0;
|
|
||||||
|
|
||||||
robot::Time last_path_check_; ///< Mốc nhịp quét.
|
|
||||||
bool has_last_path_check_ = false;
|
|
||||||
|
|
||||||
robot::Time blocked_since_; ///< Lần đầu thấy tuyến bẩn trong chuỗi hiện tại.
|
|
||||||
bool blocked_seen_ = false;
|
|
||||||
robot::Time off_path_since_; ///< Lần đầu thấy lệch quá ngưỡng trong chuỗi hiện tại.
|
|
||||||
bool off_path_seen_ = false;
|
|
||||||
|
|
||||||
// Phép đo 3 — trạng thái riêng, không dính nhịp `last_path_check_`.
|
|
||||||
/// Thân đang dùng khi lấy từ costmap, chỉ để log khi thay đổi (footprint đổi lúc chạy).
|
|
||||||
bool collision_body_logged_ = false;
|
|
||||||
double collision_body_front_ = 0.0; ///< [m]
|
|
||||||
double collision_body_rear_ = 0.0; ///< [m]
|
|
||||||
double collision_body_side_ = 0.0; ///< [m]
|
|
||||||
bool collision_brake_ = false; ///< Cycle này lệnh ra phải bằng 0 (chạm trong stop_distance).
|
|
||||||
bool collision_blocked_ = false; ///< Đã xác nhận đủ cycle — giơ cờ collision (trigger riêng).
|
|
||||||
int collision_streak_ = 0; ///< Số cycle liên tiếp chạm trong stop_distance.
|
|
||||||
double collision_hit_distance_ = 0.0; ///< [m] chỉ để log.
|
|
||||||
|
|
||||||
// Phép đo 4 — tiến độ so với lệnh. Ring mẫu theo nhịp `check_period`; 32 mẫu x 0.2 s = 6.4 s
|
|
||||||
// lịch sử, đủ cho `progress_window` 3 s và còn dư.
|
|
||||||
struct ProgressSample
|
|
||||||
{
|
|
||||||
robot::Time t;
|
|
||||||
double s_plan = 0.0; ///< [m] độ dài plan tính tới pose gần robot nhất.
|
|
||||||
double d_goal = 0.0; ///< [m] khoảng cách chim bay tới goal.
|
|
||||||
double cmd = 0.0; ///< [m] tổng |v| đã phát tính tới lúc lấy mẫu.
|
|
||||||
};
|
|
||||||
static constexpr std::size_t kProgressRing = 32;
|
|
||||||
std::array<ProgressSample, kProgressRing> progress_ring_{};
|
|
||||||
std::size_t progress_head_ = 0; ///< Vị trí ghi kế tiếp.
|
|
||||||
std::size_t progress_count_ = 0; ///< Số mẫu hợp lệ (<= kProgressRing).
|
|
||||||
double progress_cmd_accum_ = 0.0; ///< [m] tổng |v| đã phát, cộng mỗi cycle SAU trọng tài.
|
|
||||||
robot::Time progress_stall_since_;
|
|
||||||
bool progress_stall_seen_ = false;
|
|
||||||
bool no_progress_ = false; ///< Kết quả đã debounce; chỉ vào state machine ở mode "recover".
|
|
||||||
std::vector<double> plan_arclen_; ///< [m] độ dài cộng dồn của latest_plan_, dựng khi plan đổi.
|
|
||||||
std::uint64_t plan_revision_ = 0; ///< Tăng mỗi lần latest_plan_ đổi.
|
|
||||||
std::uint64_t plan_arclen_revision_ = ~0ull; ///< Revision mà plan_arclen_ và ring đang khớp.
|
|
||||||
|
|
||||||
/// Mốc nhận plan mới — dùng cho `grace_after_new_plan`.
|
/// Mốc nhận plan mới — dùng cho `grace_after_new_plan`.
|
||||||
robot::Time last_plan_applied_;
|
robot::Time last_plan_applied_;
|
||||||
bool has_last_plan_applied_ = false;
|
bool has_last_plan_applied_ = false;
|
||||||
|
|
||||||
|
// --- EnvMonitor -------------------------------------------------------------------------------
|
||||||
|
EnvMonitor env_monitor_;
|
||||||
|
EnvVerdict env_verdict_; ///< verdict của cycle này (vào state machine + trọng tài).
|
||||||
|
EnvGeometry env_geometry_; ///< mẫu nặng gần nhất (giữ giữa hai tick 5 Hz để log/ghi).
|
||||||
|
robot::Time last_env_heavy_;
|
||||||
|
bool has_last_env_heavy_ = false;
|
||||||
|
robot::Time last_env_debug_;
|
||||||
|
bool has_last_env_debug_ = false;
|
||||||
|
bool env_brake_ = false; ///< cycle này EnvMonitor đòi lệnh 0 (chỉ khi apply_command).
|
||||||
|
bool env_used_local_plan_ = false;
|
||||||
|
double env_contact_dist_ = std::numeric_limits<double>::quiet_NaN(); ///< [m] tiếp xúc gần nhất (inf = trống) cho log.
|
||||||
|
double env_controller_v_ = 0.0; ///< [m/s] lệnh controller muốn ở cycle trước, trước bao/phanh/trọng tài.
|
||||||
|
std::size_t env_plan_index_ = 0; ///< index plan đang bám cho EnvMonitor (trackedNearestIndex).
|
||||||
|
bool env_plan_index_valid_ = false; ///< false = plan mới, tick nặng kế tìm lại toàn plan.
|
||||||
|
robot_nav_2d_msgs::Path2D env_local_plan_; ///< bộ đệm quỹ đạo local planner, tránh cấp phát mỗi cycle.
|
||||||
|
std::unique_ptr<EnvLogger> env_logger_;
|
||||||
|
|
||||||
robot_geometry_msgs::PoseStamped oscillation_origin_;
|
robot_geometry_msgs::PoseStamped oscillation_origin_;
|
||||||
bool has_oscillation_origin_ = false;
|
bool has_oscillation_origin_ = false;
|
||||||
|
|
||||||
|
|||||||
377
include/move_base2/core/env_monitor.h
Normal file
377
include/move_base2/core/env_monitor.h
Normal file
@@ -0,0 +1,377 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* move_base2 — lõi quyết định của EnvMonitor (Test/ENV_MONITOR_PLAN.md §2.3, P2).
|
||||||
|
*
|
||||||
|
* Thuần: không costmap, không TF, không log, không cấp phát sau `configure`. Vào là một @ref EnvSample
|
||||||
|
* mỗi control cycle (feature đã tính ở cổng bằng `grid_analysis`), ra là một @ref EnvVerdict: các cờ
|
||||||
|
* cho state machine đúng như PathMonitor cũ (`no_progress`, `path_blocked`, `collision`), lệnh phanh và
|
||||||
|
* bao tốc độ. Nhờ thuần nên kiểm được bằng bảng và replay được trên feature ghi lại.
|
||||||
|
*
|
||||||
|
* Triết lý (chốt 5/9/2026): local planner tự do né, recovery chỉ khi BẾ TẮC THẬT. Không có verdict
|
||||||
|
* "plan bị chắn". Ba verdict:
|
||||||
|
* - kStall — lệnh ≠ 0 mà `cost_to_go` (địa trắc tới điểm thoát + phần plan còn lại) không giảm.
|
||||||
|
* - kUnreachable — không còn đường trong cửa sổ tới điểm thoát, tĩnh, và xấu hơn lúc nhận plan.
|
||||||
|
* - kContact — thân sắp chạm trên quỹ đạo thật: phanh ngay, cờ chỉ khi tiếp xúc đứng yên.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#ifndef MOVE_BASE2_CORE_ENV_MONITOR_H_
|
||||||
|
#define MOVE_BASE2_CORE_ENV_MONITOR_H_
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <limits>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <robot/time.h>
|
||||||
|
|
||||||
|
namespace move_base2
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct EnvMonitorConfig
|
||||||
|
* @brief Tham số lõi. Đơn vị trong tên hoặc comment. Mọi ngưỡng số là GIÁ TRỊ KHỞI ĐIỂM rút từ hai bag
|
||||||
|
* sim 5/9/2026 (ENV_MONITOR_PLAN.md §4.4); chốt lại bằng shadow/replay, không bằng tay.
|
||||||
|
*/
|
||||||
|
struct EnvMonitorConfig
|
||||||
|
{
|
||||||
|
bool enabled = true;
|
||||||
|
|
||||||
|
/// [s] Sau khi nhận plan mới (planner hoặc detour) không kết luận gì: mẫu cũ thuộc plan cũ.
|
||||||
|
double grace_after_new_plan = 1.0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* kStall — "planner đòi đi mà cost_to_go không giảm". Đi cờ `no_progress` -> trigger oscillation
|
||||||
|
* (trigger GỐC: cạn route là ABORT chặng), nên `action` mặc định "log" cho tới khi shadow đủ dài.
|
||||||
|
*/
|
||||||
|
struct Stall
|
||||||
|
{
|
||||||
|
bool enabled = true;
|
||||||
|
std::string action = "log"; ///< "log" = chỉ báo trong verdict; "recover" = giơ cờ.
|
||||||
|
double window_s = 4.0; ///< [s] cửa sổ đo eff.
|
||||||
|
/// [m] Hai mẫu nặng liên tiếp mà cost_to_go đổi quá (đây + quãng robot dịch) = mặt nạ free đổi liên thông (ô
|
||||||
|
/// nhấp nháy ở mép khe), không phải tiến/lùi: xoá cửa sổ đo, bắt đầu lại. Run 5/9 23:37: geo nhảy +3,2 m trong
|
||||||
|
/// 0,2 s, eff −2,7, detour oan. 0 = tắt.
|
||||||
|
double jump_reject = 1.0;
|
||||||
|
/// Cổng DỊCH CHUYỂN THỰC (6/9): "bế tắc thật" = robot không đi được đâu, không phải "không tiến theo plan".
|
||||||
|
/// Chỉ giơ cờ khi trong disp_window_s gần nhất robot dịch chuyển thực (khoảng cách đầu-cuối) < disp_min dù đã
|
||||||
|
/// lệnh >= disp_min_commanded. Né tự do luôn dịch chuyển -> không nổ; quần trong hộp 0,9 m (run 5/9 00:05,
|
||||||
|
/// 97 s) -> nổ sau ~window. Cửa sổ vị trí bắt đầu lại ở mỗi plan mới, thay cho ngân sách replan R3/R5 (đo
|
||||||
|
/// theo quãng đường nên đi vòng đốt ngân sách rất chậm, câm cờ đúng 97 s). disp_min = 0 -> tắt cổng, dùng lại
|
||||||
|
/// ngân sách replan như cũ.
|
||||||
|
double disp_window_s = 12.0;
|
||||||
|
double disp_min = 0.6; ///< [m]
|
||||||
|
double disp_min_commanded = 1.5; ///< [m]
|
||||||
|
/// [s] Cổng ĐỨNG YÊN: robot dịch chuyển thực < disp_min trong chừng này giây (không cần lệnh đủ) -> kẹt, kể cả
|
||||||
|
/// khi geodesic không đo được. Run 6/9 10:59: PRIEST bò 0,03–0,15 m/s tại chỗ 30 s (blocked_ahead cost 253, res
|
||||||
|
/// 1,6 chưa tới ngưỡng khai bất khả thi), lệnh tích luỹ 12 s chỉ 0,1–0,4 m nên cổng dịch chuyển câm; kUnreachable
|
||||||
|
/// mới gỡ ở 75 s. Thử trên 7 lượt: bắt đúng 2 ca quần tại chỗ, 0 lần ở 3 lượt sạch. 0 = tắt.
|
||||||
|
double idle_window_s = 25.0;
|
||||||
|
/// Cổng GIAM VÙNG: trong confine_window_s mọi vị trí nằm trong vòng bán kính < confine_radius quanh tâm của chúng,
|
||||||
|
/// và lệnh tích luỹ >= confine_cmd_factor x max(2 x bán kính đo được, disp_min) — đi nhiều mà không ra khỏi vùng
|
||||||
|
/// nhỏ = loanh quanh trong local minimum. Cổng dịch chuyển đo đầu–cuối nên mù với vòng bán kính > 0,3 m; cổng
|
||||||
|
/// đứng yên chỉ bắt gần như đứng. Replay 8 lượt 5–6/9: bắt 00:05 (101 s thay vì 206), 10:57 (27 s thay vì 75),
|
||||||
|
/// 23:37; 0 lần ở 5 lượt sạch. Hệ số lệnh chặn ca bò thẳng chậm sau người (2 m/20 s: bán kính 1 m, lệnh 2 m
|
||||||
|
/// < 1,5 x 2 m). confine_radius = 0 -> tắt. Chỉ hoạt động khi disp_min > 0.
|
||||||
|
double confine_window_s = 20.0;
|
||||||
|
double confine_radius = 1.2; ///< [m]
|
||||||
|
double confine_cmd_factor = 1.5; ///< [-]
|
||||||
|
double window_m = 1.5; ///< [m] hoặc theo lệnh tích luỹ — cái tới trước.
|
||||||
|
double min_commanded = 0.30; ///< [m] lệnh trong cửa sổ ít hơn = đang chờ, không đo.
|
||||||
|
double eff_on = 0.20; ///< [-] eff dưới đây là kẹt.
|
||||||
|
double eff_off = 0.50; ///< [-] trên đây mới xoá (hysteresis).
|
||||||
|
double confirm = 1.0; ///< [s] kẹt liên tục chừng này mới nổ.
|
||||||
|
double goal_zone = 0.6; ///< [m] gần goal hơn đây không đo.
|
||||||
|
double slip_on = 0.15; ///< [m/s] |v_cmd| - |v_meas| liên tục = kẹt vật lý. 0 = tắt.
|
||||||
|
double slip_time = 1.0; ///< [s]
|
||||||
|
double min_commanded_since_goal = 1.0; ///< [m] cổng đầu chặng (cắt nổ sớm 13,0 / 145,6 bag 1).
|
||||||
|
double retry_backoff = 3.0; ///< [s] sau một lượt recovery HỎNG không tích bằng chứng.
|
||||||
|
} stall;
|
||||||
|
|
||||||
|
/// kUnreachable — cờ `path_blocked` -> route path_blocked (cạn route chỉ KHOÁ, an toàn để bật).
|
||||||
|
struct Unreachable
|
||||||
|
{
|
||||||
|
bool enabled = true;
|
||||||
|
std::string action = "recover";
|
||||||
|
double static_on = 1.0; ///< [s] mất liên thông liên tục chừng này mới nổ.
|
||||||
|
double clean_start_margin = 0.20; ///< [m] edt_body_min tối thiểu để còn gọi replan được.
|
||||||
|
double retry_backoff = 2.0; ///< [s]
|
||||||
|
} unreachable;
|
||||||
|
|
||||||
|
/// kContact — phanh theo quỹ đạo thật; cờ `collision` -> route collision.
|
||||||
|
struct Contact
|
||||||
|
{
|
||||||
|
bool enabled = true;
|
||||||
|
std::string action = "recover";
|
||||||
|
int confirm_cycles = 2; ///< [cycle] chạm liên tục chừng này thì phanh.
|
||||||
|
double trigger_static_time = 1.0; ///< [s] phanh mà tiếp xúc còn đó chừng này mới giơ cờ.
|
||||||
|
double clean_start_margin = 0.20; ///< [m] dừng với thân còn cách vật ít nhất chừng này.
|
||||||
|
double brake_decel = 0.35; ///< [m/s^2] decel dùng cho quãng phanh và bao tốc độ.
|
||||||
|
double min_speed = 0.05; ///< [m/s] bao tốc độ không kẹp thấp hơn đây (thấp hơn = dừng).
|
||||||
|
/// [m/s] |v_cmd| và |v_đo| đều dưới đây mà monitor CHƯA phanh = robot đang xoay tại chỗ hoặc bò: đó là
|
||||||
|
/// chuyển động của controller, không phải bài toán quãng phanh -> không phanh, không cờ (cùng vai
|
||||||
|
/// `path_monitor/collision_min_speed` cũ). Shadow 5/9: ba lần "braking to zero" ngay đầu chặng khi
|
||||||
|
/// robot xoay ra khỏi vật kề mũi (v 0.00–0.07 m/s) đều là nổ oan kiểu này.
|
||||||
|
double min_check_speed = 0.10;
|
||||||
|
/// [m] Hysteresis cho "kề mũi": đã kề (d <= margin) thì còn kề tới khi d > margin + đây. Local planner
|
||||||
|
/// đổi quỹ đạo mỗi cycle nên d nhảy 0,08 <-> 0,25 m quanh margin (run 5/9 22:35): không có nó thì phanh
|
||||||
|
/// bật/nhả 4 lần/s và đồng hồ giơ cờ bị reset liên tục.
|
||||||
|
double release_hysteresis = 0.10;
|
||||||
|
/// [cycle] Nhả phanh (quỹ đạo sạch hoặc lùi xa) chỉ sau chừng này cycle liên tiếp không còn "sắp chạm".
|
||||||
|
int release_confirm_cycles = 4;
|
||||||
|
/// [m] Dư của bao tốc độ so với ngưỡng phanh: v_cap = sqrt(2a(d - margin - đây)), tối thiểu min_speed cho
|
||||||
|
/// tới d <= margin (= 0). 0 = bao đặt đúng biên phanh -> cứ kẹp xong là chớm "sắp chạm" (rung tiếp cận run
|
||||||
|
/// 5/9 22:58: 9/28 lần phanh < 0,3 s).
|
||||||
|
double envelope_margin = 0.10;
|
||||||
|
/// [m] Detour do CHÍNH cờ collision vừa xong mà robot dịch chưa tới đây và lại "sắp chạm" -> kContact ĐỨNG RA
|
||||||
|
/// (không phanh, không cờ, không bao) tới khi đi được standdown_travel hoặc quỹ đạo sạch: detour từ trong khe
|
||||||
|
/// là vô nghĩa, xoay là chạm (m1/m8 run 5/9 22:58 churn 3 detour rồi ABORT); mission 6 cùng lượt thoát được
|
||||||
|
/// đúng lúc guard "nhường controller". 0 = tắt.
|
||||||
|
double standdown_min_travel = 0.10;
|
||||||
|
double standdown_travel = 0.30; ///< [m] đứng ra chừng này quãng đường rồi vũ trang lại.
|
||||||
|
bool envelope = true; ///< bật bao tốc độ theo tiếp xúc trên quỹ đạo.
|
||||||
|
/// false = SHADOW: `brake`/`v_cap` chỉ được báo trong verdict, ControlLoop không đụng lệnh.
|
||||||
|
/// true = phanh và bao tốc độ tác động thật lên lệnh trước trọng tài. Lõi không dùng cờ này.
|
||||||
|
bool apply_command = false;
|
||||||
|
} contact;
|
||||||
|
|
||||||
|
/// Ngân sách replan theo quãng đường (R3 + R5) cho kStall và kUnreachable.
|
||||||
|
struct ReplanBudget
|
||||||
|
{
|
||||||
|
double min_travel = 1.5; ///< [m] sau detour thành công, im tới khi đi chừng này.
|
||||||
|
double escalate_factor = 2.0; ///< [-] replan lặp trong escalate_window_m thì nhân ngân sách.
|
||||||
|
double escalate_window_m = 10.0; ///< [m] đi chừng này không replan thì về min_travel.
|
||||||
|
/// [m] Trần của leo thang; 0 = không trần. Run 5/9 22:35: 4 detour PRIEST trong 10 m -> 1,5 -> 3 -> 6 -> 12 m,
|
||||||
|
/// kStall câm gần hết chặng.
|
||||||
|
double escalate_max = 6.0;
|
||||||
|
} replan;
|
||||||
|
|
||||||
|
bool validate(std::string& error) const;
|
||||||
|
std::string describe() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct EnvSample
|
||||||
|
* @brief Dữ liệu vào của một cycle. Phần "nặng" chỉ có nghĩa khi @ref geometry_valid (nhịp 5 Hz).
|
||||||
|
* NaN / +inf = không có; lõi không suy diễn từ giá trị thiếu.
|
||||||
|
*/
|
||||||
|
struct EnvSample
|
||||||
|
{
|
||||||
|
robot::Time now;
|
||||||
|
double dt = 0.0; ///< [s] từ cycle trước; 0 ở cycle đầu.
|
||||||
|
bool costmap_current = true; ///< costmap stale -> mọi kết luận hình học là quá khứ.
|
||||||
|
bool has_plan = false;
|
||||||
|
double robot_x = 0.0; ///< [m] pose robot, frame bất kỳ nhưng cố định (đo quãng đường).
|
||||||
|
double robot_y = 0.0;
|
||||||
|
double v_cmd = 0.0; ///< [m/s] lệnh SAU trọng tài của cycle trước.
|
||||||
|
double w_cmd = 0.0; ///< [rad/s]
|
||||||
|
double v_meas = std::numeric_limits<double>::quiet_NaN(); ///< [m/s] đo được; NaN = không có.
|
||||||
|
/// [m/s] lệnh controller MUỐN ở cycle trước, TRƯỚC bao tốc độ / phanh / trọng tài; NaN = không biết.
|
||||||
|
/// Cần cho cổng xoay của kContact: bao kẹp lệnh về 0 trong margin làm v_cmd = 0 dù controller vẫn đòi
|
||||||
|
/// đi — không có số này, cổng xoay coi đó là "đứng yên" và robot dừng câm không cờ.
|
||||||
|
double v_controller = std::numeric_limits<double>::quiet_NaN();
|
||||||
|
bool controller_valid = true; ///< controller cycle trước có ra lệnh không.
|
||||||
|
double d_goal = std::numeric_limits<double>::infinity(); ///< [m] Euclid tới goal.
|
||||||
|
|
||||||
|
bool geometry_valid = false; ///< các trường dưới đây vừa được tính ở tick này.
|
||||||
|
bool cost_conclusive = false; ///< costToGo kết luận được.
|
||||||
|
bool reachable = false; ///< có đường địa trắc tới T.
|
||||||
|
double cost_to_go = std::numeric_limits<double>::infinity(); ///< [m]
|
||||||
|
double edt_body_min = std::numeric_limits<double>::quiet_NaN(); ///< [m]
|
||||||
|
|
||||||
|
bool contact_conclusive = false; ///< sweep quỹ đạo kết luận được (mỗi cycle).
|
||||||
|
double traj_contact_dist = std::numeric_limits<double>::infinity(); ///< [m] dọc quỹ đạo.
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Kết quả một lượt recovery mà lõi cần biết (không include recovery_core: chỉ ba ý nghĩa).
|
||||||
|
enum class EnvRecoveryOutcome
|
||||||
|
{
|
||||||
|
kSucceededWithPath, ///< detour_path nộp path mới -> ngân sách theo quãng đường.
|
||||||
|
kSucceeded, ///< behavior khác thành công (không đổi plan).
|
||||||
|
kFailed ///< hỏng -> retry_backoff.
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct EnvVerdict
|
||||||
|
* @brief Kết quả một cycle. Cờ đầu là cái đưa vào `StateMachineInput` (đã qua action + cổng +
|
||||||
|
* ngân sách); phần chẩn đoán là số thô để log / ghi JSONL.
|
||||||
|
*/
|
||||||
|
struct EnvVerdict
|
||||||
|
{
|
||||||
|
bool no_progress = false; ///< kStall hiệu lực.
|
||||||
|
bool path_blocked = false; ///< kUnreachable hiệu lực.
|
||||||
|
bool collision = false; ///< kContact hiệu lực (cờ, không phải phanh).
|
||||||
|
bool brake = false; ///< Cycle này lệnh ra phải bằng 0.
|
||||||
|
double v_cap = std::numeric_limits<double>::infinity(); ///< [m/s] trần |v| cycle này; +inf = không kẹp.
|
||||||
|
|
||||||
|
bool stall_detected = false; ///< điều kiện kẹt đã đủ confirm, chưa xét action/cổng.
|
||||||
|
bool unreachable_detected = false;
|
||||||
|
bool contact_imminent = false; ///< chạm trong quãng phanh ở cycle này.
|
||||||
|
bool contact_standdown = false; ///< kContact đang đứng ra sau detour từ chính chỗ này (nhường controller).
|
||||||
|
bool cost_jump = false; ///< tick này cost_to_go nhảy quá jump_reject: cửa sổ stall vừa bị xoá.
|
||||||
|
bool stall_muted = false; ///< detected nhưng bị cổng/ngân sách/backoff/grace giữ lại.
|
||||||
|
double net_disp = std::numeric_limits<double>::quiet_NaN(); ///< [m] dịch chuyển thực trong disp_window_s (NaN = chưa đủ mẫu).
|
||||||
|
bool wandering = false; ///< cổng dịch chuyển đã mở: lệnh đủ mà không đi được đâu.
|
||||||
|
bool idle = false; ///< cổng đứng yên đã mở: không dịch chuyển suốt idle_window_s.
|
||||||
|
bool confined = false; ///< cổng giam vùng đã mở: đi nhiều mà ở trong vòng nhỏ suốt confine_window_s.
|
||||||
|
double region_radius = std::numeric_limits<double>::quiet_NaN(); ///< [m] bán kính vùng đã ở trong confine_window_s.
|
||||||
|
bool unreachable_muted = false;
|
||||||
|
double eff_geo = std::numeric_limits<double>::quiet_NaN(); ///< [-] eff của cửa sổ gần nhất.
|
||||||
|
double cmd_window = 0.0; ///< [m] lệnh tích luỹ trong cửa sổ đó.
|
||||||
|
double brake_dist = 0.0; ///< [m]
|
||||||
|
double replan_budget_left = 0.0; ///< [m] còn phải đi bao xa mới hết mute sau replan (0 = không mute).
|
||||||
|
const char* event = ""; ///< Sườn sự kiện của cycle này ("" = không có). Dùng để log một lần.
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class EnvMonitor
|
||||||
|
* @brief Lõi thuần. Không thread-safe; chỉ control thread gọi.
|
||||||
|
*/
|
||||||
|
class EnvMonitor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
EnvMonitor() = default;
|
||||||
|
|
||||||
|
bool configure(const EnvMonitorConfig& config, std::string& error);
|
||||||
|
bool configured() const
|
||||||
|
{
|
||||||
|
return configured_;
|
||||||
|
}
|
||||||
|
const EnvMonitorConfig& config() const
|
||||||
|
{
|
||||||
|
return config_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Về trạng thái ban đầu (rời kControlling, huỷ chặng). Giữ config.
|
||||||
|
void reset();
|
||||||
|
|
||||||
|
/// @brief Chặng mới được nhận: đặt lại mọi ngân sách, mốc lệnh đầu chặng.
|
||||||
|
void onGoalAccepted(const robot::Time& now);
|
||||||
|
|
||||||
|
/// @brief Plan mới được áp (planner hay detour): xoá cửa sổ, bắt đầu grace, chờ baseline liên thông.
|
||||||
|
void onPlanApplied(const robot::Time& now);
|
||||||
|
|
||||||
|
/// @brief Một lượt recovery vừa kết thúc (trước khi plan mới, nếu có, được áp).
|
||||||
|
/// @param from_contact recovery này do cờ collision của chính monitor gọi (trigger kCollision).
|
||||||
|
void onRecoveryFinished(const robot::Time& now, EnvRecoveryOutcome outcome, bool from_contact = false);
|
||||||
|
|
||||||
|
/// @brief Một cycle. Gọi SAU khi các feature của cycle đã có, TRƯỚC state machine.
|
||||||
|
EnvVerdict update(const EnvSample& sample);
|
||||||
|
|
||||||
|
// --- soi trạng thái (test, log) ---
|
||||||
|
double commandedTotal() const
|
||||||
|
{
|
||||||
|
return cmd_accum_;
|
||||||
|
}
|
||||||
|
double travelledTotal() const
|
||||||
|
{
|
||||||
|
return travel_total_;
|
||||||
|
}
|
||||||
|
double replanBudget() const
|
||||||
|
{
|
||||||
|
return budget_travel_;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct HeavySample
|
||||||
|
{
|
||||||
|
robot::Time t;
|
||||||
|
double cost = 0.0; ///< [m]
|
||||||
|
double cmd = 0.0; ///< [m] cmd_accum_ lúc lấy mẫu.
|
||||||
|
};
|
||||||
|
/// 64 mẫu x 0,2 s = 12,8 s lịch sử, dư cho window_s 4 s kể cả khi nhịp nặng chậm lại.
|
||||||
|
static constexpr std::size_t kRing = 64;
|
||||||
|
struct PosSample
|
||||||
|
{
|
||||||
|
robot::Time t;
|
||||||
|
double x = 0.0;
|
||||||
|
double y = 0.0;
|
||||||
|
double cmd = 0.0; ///< [m] cmd_accum_ lúc lấy mẫu.
|
||||||
|
};
|
||||||
|
/// 128 mẫu x 0,2 s = 25,6 s, dư cho disp_window_s 12 s.
|
||||||
|
static constexpr std::size_t kPosRing = 192; ///< 192 x 0,2 s = 38 s, đủ idle_window_s 25 s.
|
||||||
|
bool oldestPos(const robot::Time& now, double window_s, PosSample& out) const;
|
||||||
|
|
||||||
|
void clearWindow();
|
||||||
|
void clearPosWindow();
|
||||||
|
bool oldestInWindow(const robot::Time& now, HeavySample& out) const;
|
||||||
|
void updateStall(const EnvSample& s, EnvVerdict& v);
|
||||||
|
void updateUnreachable(const EnvSample& s, EnvVerdict& v);
|
||||||
|
void updateContact(const EnvSample& s, EnvVerdict& v);
|
||||||
|
|
||||||
|
EnvMonitorConfig config_;
|
||||||
|
bool configured_ = false;
|
||||||
|
|
||||||
|
// Tích luỹ
|
||||||
|
double cmd_accum_ = 0.0; ///< [m] Σ|v_cmd|·dt
|
||||||
|
double travel_total_ = 0.0; ///< [m] odometer từ pose
|
||||||
|
bool has_last_pose_ = false;
|
||||||
|
double last_x_ = 0.0;
|
||||||
|
double last_y_ = 0.0;
|
||||||
|
|
||||||
|
// Mốc chặng / plan
|
||||||
|
bool has_goal_ = false;
|
||||||
|
double cmd_at_goal_ = 0.0;
|
||||||
|
bool has_plan_time_ = false;
|
||||||
|
robot::Time plan_time_;
|
||||||
|
|
||||||
|
// Cửa sổ mẫu nặng
|
||||||
|
std::array<HeavySample, kRing> ring_{};
|
||||||
|
std::size_t ring_head_ = 0;
|
||||||
|
std::size_t ring_count_ = 0;
|
||||||
|
std::array<PosSample, kPosRing> pos_ring_{};
|
||||||
|
std::size_t pos_head_ = 0;
|
||||||
|
std::size_t pos_count_ = 0;
|
||||||
|
bool wandering_ = false;
|
||||||
|
bool idle_ = false; ///< cổng đứng yên đang mở (xem Stall::idle_window_s).
|
||||||
|
bool confined_ = false; ///< cổng giam vùng đang mở (xem Stall::confine_radius).
|
||||||
|
double region_radius_ = std::numeric_limits<double>::quiet_NaN();
|
||||||
|
double net_disp_ = std::numeric_limits<double>::quiet_NaN();
|
||||||
|
|
||||||
|
// kStall
|
||||||
|
bool stall_seen_ = false;
|
||||||
|
robot::Time stall_since_;
|
||||||
|
bool stall_fired_ = false;
|
||||||
|
bool slip_seen_ = false;
|
||||||
|
robot::Time slip_since_;
|
||||||
|
bool has_stall_backoff_ = false;
|
||||||
|
robot::Time stall_backoff_until_;
|
||||||
|
|
||||||
|
// kUnreachable
|
||||||
|
bool baseline_pending_ = true; ///< chưa có mẫu kết luận nào từ plan hiện tại.
|
||||||
|
bool baseline_unreachable_ = false; ///< plan đã không liên thông ngay từ lúc nhận (R2: không cãi SBPL).
|
||||||
|
bool unreach_seen_ = false;
|
||||||
|
robot::Time unreach_since_;
|
||||||
|
bool unreach_fired_ = false;
|
||||||
|
bool has_unreach_backoff_ = false;
|
||||||
|
robot::Time unreach_backoff_until_;
|
||||||
|
|
||||||
|
// kContact
|
||||||
|
int contact_streak_ = 0;
|
||||||
|
int release_streak_ = 0; ///< cycle liên tiếp không "sắp chạm" trong khi đang phanh.
|
||||||
|
bool has_prev_heavy_ = false; ///< mẫu nặng trước cho bộ lọc nhảy cost_to_go.
|
||||||
|
double prev_heavy_cost_ = 0.0;
|
||||||
|
double prev_heavy_travel_ = 0.0;
|
||||||
|
bool braking_ = false;
|
||||||
|
bool contact_close_ = false; ///< tiếp xúc trong clean_start_margin (bất kể tốc độ).
|
||||||
|
robot::Time close_since_;
|
||||||
|
bool contact_fired_ = false;
|
||||||
|
bool detour_from_contact_ = false; ///< detour gần nhất là do cờ collision; mốc quãng đường ở dưới.
|
||||||
|
double detour_mark_travel_ = 0.0; ///< [m]
|
||||||
|
bool standdown_ = false; ///< đang đứng ra (xem Contact::standdown_min_travel).
|
||||||
|
double standdown_mark_travel_ = 0.0; ///< [m]
|
||||||
|
int standdown_clean_streak_ = 0; ///< cycle quỹ đạo sạch liên tiếp trong khi đứng ra.
|
||||||
|
|
||||||
|
// Ngân sách replan (R3/R5)
|
||||||
|
bool has_replan_mark_ = false;
|
||||||
|
double replan_mark_travel_ = 0.0; ///< odometer tại lần detour thành công gần nhất.
|
||||||
|
double budget_travel_ = 0.0; ///< [m] ngân sách hiện hành (= min_travel, nhân factor khi lặp).
|
||||||
|
bool has_prev_replan_ = false;
|
||||||
|
double prev_replan_travel_ = 0.0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace move_base2
|
||||||
|
|
||||||
|
#endif // MOVE_BASE2_CORE_ENV_MONITOR_H_
|
||||||
284
include/move_base2/core/grid_analysis.h
Normal file
284
include/move_base2/core/grid_analysis.h
Normal file
@@ -0,0 +1,284 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* move_base2 — phân tích hình học trên local costmap cho EnvMonitor (Test/ENV_MONITOR_PLAN.md, P1).
|
||||||
|
*
|
||||||
|
* Hàm thuần trên `robot_costmap_2d::Costmap2D`, không giữ trạng thái ngoài @ref GridScratch (bộ đệm
|
||||||
|
* tái dùng để không cấp phát trong control loop). Không log, không TF, không quyết định gì — người
|
||||||
|
* dùng là `EnvMonitor` và bộ phân tích offline `Test/env_monitor_tools/envmon_offline.py`, hai bên
|
||||||
|
* phải ra cùng số trên cùng lưới.
|
||||||
|
*
|
||||||
|
* Ngữ nghĩa cost, cố ý và khác `plan_inspector::checkPlan`:
|
||||||
|
* - Vật cản là ô có cost >= `lethal_threshold` (mặc định 254 = LETHAL). Vành inflation 253 KHÔNG
|
||||||
|
* được dùng ở đây: `inflation_radius` của cây sim (0.3 m) nhỏ hơn inscribed (0.48 m) nên vành đó
|
||||||
|
* bị cắt cụt; mọi biên an toàn được tính lại từ EDT tới ô lethal cộng bán kính thân.
|
||||||
|
* - `NO_INFORMATION` KHÔNG phải vật cản, cùng quy ước với SBPL (`unknown_is_free`).
|
||||||
|
* - Ra ngoài lưới = không có dữ liệu, không phải bị chắn.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#ifndef MOVE_BASE2_CORE_GRID_ANALYSIS_H_
|
||||||
|
#define MOVE_BASE2_CORE_GRID_ANALYSIS_H_
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <limits>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <robot_geometry_msgs/PoseStamped.h>
|
||||||
|
|
||||||
|
#include <move_base2/core/plan_inspector.h>
|
||||||
|
|
||||||
|
namespace robot_costmap_2d { class Costmap2D; }
|
||||||
|
|
||||||
|
namespace move_base2
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct GridScratch
|
||||||
|
* @brief Bộ đệm của một lượt phân tích: EDT, mặt nạ tự do, trường địa trắc, hàng đợi.
|
||||||
|
*
|
||||||
|
* Gọi @ref analyzeGrid mỗi tick (5 Hz) rồi đưa cùng một scratch cho mọi hàm bên dưới. Kích thước
|
||||||
|
* chỉ đổi khi costmap đổi kích thước (`ensure`), sau đó không cấp phát heap nữa.
|
||||||
|
*/
|
||||||
|
struct GridScratch
|
||||||
|
{
|
||||||
|
unsigned int width = 0;
|
||||||
|
unsigned int height = 0;
|
||||||
|
double resolution = 0.0; ///< [m/ô]
|
||||||
|
double origin_x = 0.0; ///< [m] toạ độ góc dưới-trái của ô (0,0), frame của lưới
|
||||||
|
double origin_y = 0.0; ///< [m]
|
||||||
|
double free_radius = 0.0; ///< [m] bán kính đã dùng cho @ref free
|
||||||
|
|
||||||
|
std::vector<float> edt; ///< [m] khoảng cách tâm ô -> ô lethal gần nhất; 0 tại ô lethal
|
||||||
|
std::vector<std::uint8_t> free; ///< 1 = tâm robot đặt được (edt >= free_radius)
|
||||||
|
std::vector<float> geo; ///< [m] trường địa trắc của lượt @ref computeGeodesic gần nhất
|
||||||
|
std::vector<std::uint32_t> baseline; ///< index ô lethal đang nằm dưới thân đệm (sorted), cho sweep
|
||||||
|
|
||||||
|
// Bộ đệm nội bộ của EDT (Felzenszwalb), Dijkstra và dò chu vi — không đọc từ ngoài.
|
||||||
|
std::vector<float> f_;
|
||||||
|
std::vector<float> d_;
|
||||||
|
std::vector<int> v_;
|
||||||
|
std::vector<float> z_;
|
||||||
|
std::vector<float> tmp_;
|
||||||
|
std::vector<std::uint64_t> heap_;
|
||||||
|
std::vector<double> perim_; ///< (bx, by, edge) x N — chu vi footprint đệm trong frame thân.
|
||||||
|
|
||||||
|
/// @brief Đổi kích thước theo lưới; giữ nguyên nếu đã khớp. Trả false nếu lưới rỗng.
|
||||||
|
bool ensure(const robot_costmap_2d::Costmap2D& grid);
|
||||||
|
|
||||||
|
bool inBounds(int mx, int my) const
|
||||||
|
{
|
||||||
|
return mx >= 0 && my >= 0 && static_cast<unsigned int>(mx) < width &&
|
||||||
|
static_cast<unsigned int>(my) < height;
|
||||||
|
}
|
||||||
|
std::size_t index(unsigned int mx, unsigned int my) const
|
||||||
|
{
|
||||||
|
return static_cast<std::size_t>(my) * width + mx;
|
||||||
|
}
|
||||||
|
/// @brief Ô chứa điểm (x, y) frame lưới. Trả false nếu ra ngoài.
|
||||||
|
bool toCell(double x, double y, unsigned int& mx, unsigned int& my) const;
|
||||||
|
/// @brief Tâm ô (mx, my) [m].
|
||||||
|
void cellCenter(unsigned int mx, unsigned int my, double& x, double& y) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Bước 1 của mỗi tick: EDT tới ô >= @p lethal_threshold và mặt nạ tự do `edt >= free_radius`.
|
||||||
|
*
|
||||||
|
* EDT chính xác (Euclid), hai lượt 1-D theo Felzenszwalb–Huttenlocher, O(số ô). Lưới 160x160 ~ 0.3 ms.
|
||||||
|
* Lưới không có ô lethal nào -> edt = +inf mọi nơi, mọi ô đều free.
|
||||||
|
*
|
||||||
|
* @param free_radius [m] bán kính mà tâm robot phải cách vật cản để coi ô là đặt được — inscribed
|
||||||
|
* radius KHÔNG đệm (cùng biên với SBPL, luật R2 của plan: monitor không được khắt khe hơn
|
||||||
|
* planner vừa lập path).
|
||||||
|
* @return false nếu lưới rỗng.
|
||||||
|
*/
|
||||||
|
bool analyzeGrid(const robot_costmap_2d::Costmap2D& grid, unsigned char lethal_threshold,
|
||||||
|
double free_radius, GridScratch& scratch);
|
||||||
|
|
||||||
|
/// @brief [m] EDT tại điểm (x, y) frame lưới; NaN nếu ra ngoài lưới hoặc chưa @ref analyzeGrid.
|
||||||
|
double distanceAt(const GridScratch& scratch, double x, double y);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Trường địa trắc từ ô (tx, ty) trên mặt nạ tự do: Dijkstra 8 láng giềng, cấm cắt góc.
|
||||||
|
*
|
||||||
|
* Cắt góc bị cấm: bước chéo chỉ hợp lệ khi CẢ HAI ô trực giao trung gian cũng free — không thì
|
||||||
|
* đường "chui" qua hai ô chéo mà thân không lọt. Trọng số `res` và `res*sqrt(2)`. Kết quả ở
|
||||||
|
* `scratch.geo`, +inf = không tới được.
|
||||||
|
*
|
||||||
|
* @param[out] visited số ô đã lấy ra khỏi hàng đợi — để log chi phí.
|
||||||
|
* @param stop_index dừng sớm ngay khi ô này được lấy ra (giá trị của nó đã đúng); các ô còn lại giữ
|
||||||
|
* +inf nên KHÔNG đọc trường ở chỗ khác sau khi dừng sớm. Mặc định = chạy trọn thành phần liên
|
||||||
|
* thông (cần khi muốn biết một ô có tới được không).
|
||||||
|
* @return false nếu (tx, ty) ngoài lưới hoặc không free (không tính gì).
|
||||||
|
*/
|
||||||
|
bool computeGeodesic(unsigned int tx, unsigned int ty, GridScratch& scratch,
|
||||||
|
std::size_t* visited = nullptr,
|
||||||
|
std::size_t stop_index = static_cast<std::size_t>(-1));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Ô free gần (mx, my) nhất trong bán kính @p max_dist [m]; chính nó nếu đã free.
|
||||||
|
* @return false nếu không có ô free nào trong bán kính.
|
||||||
|
*/
|
||||||
|
bool nearestFreeCell(const GridScratch& scratch, unsigned int mx, unsigned int my, double max_dist,
|
||||||
|
unsigned int& fx, unsigned int& fy);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief [m] Khoảng trống của thân: min EDT trên chu vi hình chữ nhật thân (KHÔNG đệm) tại pose.
|
||||||
|
*
|
||||||
|
* Đây là `edt_body_min` của plan — điều kiện "còn đất sạch để SBPL bắt đầu". Thân: mũi @p front,
|
||||||
|
* đuôi @p rear (số dương), nửa rộng @p side, tính từ base_link, frame lưới. Điểm chu vi nằm ngoài
|
||||||
|
* lưới bị bỏ qua; NaN nếu không điểm nào trong lưới.
|
||||||
|
*/
|
||||||
|
double bodyClearance(const GridScratch& scratch, double x, double y, double yaw, double front,
|
||||||
|
double rear, double side);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct CostToGoParams
|
||||||
|
* @brief Tham số của @ref costToGo. Đơn vị mét.
|
||||||
|
*/
|
||||||
|
struct CostToGoParams
|
||||||
|
{
|
||||||
|
double target_backoff = 0.5; ///< [m] T bị bẩn thì lùi dọc plan tối đa chừng này để tìm T free.
|
||||||
|
double robot_snap = 0.35; ///< [m] ô robot không free (đang ôm sát vật) thì lấy ô free gần nhất trong bán kính này.
|
||||||
|
/// [m] T = pose plan ở quãng này dọc plan kể từ from_index (vẫn kẹp trong cửa sổ). 0 = T là điểm plan rời cửa sổ
|
||||||
|
/// (bản cũ). Lý do (run 5/9 23:37): plan vòng chữ U trong tầm cửa sổ — robot đi theo plan về tây, phần plan quay
|
||||||
|
/// về đông rời cửa sổ sớm hơn, T LÙI dọc plan, arc_rest tăng bù đúng phần geo giảm, tiến độ thật đo thành 0
|
||||||
|
/// -> stall oan. T ở quãng cố định chỉ tiến theo index, và geodesic 2 m ít bị lật liên thông hơn 6–9 m.
|
||||||
|
double exit_ahead = 0.0;
|
||||||
|
/// [m] T không free và lùi dọc plan (target_backoff) cũng không được thì snap T sang ô free gần nhất trong bán
|
||||||
|
/// kính này. 0 = không snap (bản cũ: kBlockedExit ngay). Lý do (run 6/9 10:15): mọi "unreachable" đều là
|
||||||
|
/// kBlockedExit — T nằm trong vành 0,48 m vì SBPL vẽ plan ôm mép vật cản trong hành lang rộng 1,55 m, không
|
||||||
|
/// phải mất liên thông. Snap tách được "plan ôm tường" (không nổ) khỏi "cụm trụ chắn ngang hành lang" (nổ).
|
||||||
|
double target_snap = 0.0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct CostToGoResult
|
||||||
|
* @brief Kết quả @ref costToGo — thước đo tiến độ địa trắc (v1 trong ENV_MONITOR_PLAN.md §4.4).
|
||||||
|
*/
|
||||||
|
struct CostToGoResult
|
||||||
|
{
|
||||||
|
enum class Target
|
||||||
|
{
|
||||||
|
kExit, ///< T = pose plan xa nhất còn trong cửa sổ (plan rời cửa sổ).
|
||||||
|
kGoal, ///< T = pose cuối plan (goal nằm trong cửa sổ).
|
||||||
|
kBackedOff, ///< T đã lùi dọc plan vì pose gốc không free.
|
||||||
|
kBlockedExit, ///< Không tìm được T free trong `target_backoff` — "đích bị bịt".
|
||||||
|
kAhead, ///< T = pose plan ở quãng `exit_ahead` dọc plan (chưa tới goal, chưa rời cửa sổ).
|
||||||
|
kSnapped ///< T đã snap sang ô free gần nhất trong `target_snap` (plan ôm tường / xuyên vật mỏng).
|
||||||
|
};
|
||||||
|
|
||||||
|
bool conclusive = false; ///< false = không kết luận được (plan rỗng, robot ngoài lưới, robot trong vật cản).
|
||||||
|
bool reachable = false; ///< Có đường địa trắc từ robot tới T không. Chỉ có nghĩa khi conclusive.
|
||||||
|
bool robot_in_ring = false; ///< Ô robot không free, đã snap sang ô free gần nhất.
|
||||||
|
Target target = Target::kExit;
|
||||||
|
std::size_t target_index = 0; ///< Index của T trên plan.
|
||||||
|
double geo_to_exit = std::numeric_limits<double>::infinity(); ///< [m] geo(robot -> T).
|
||||||
|
double arc_rest = 0.0; ///< [m] độ dài plan từ T tới cuối.
|
||||||
|
double cost_to_go = std::numeric_limits<double>::infinity(); ///< [m] geo_to_exit + arc_rest.
|
||||||
|
std::size_t cells_visited = 0; ///< Chi phí Dijkstra, để log.
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief `cost_to_go = geo(robot -> T) + arc(T -> goal)`, T = điểm plan rời cửa sổ.
|
||||||
|
*
|
||||||
|
* Vì sao tham chiếu là PLAN chứ không phải bản đồ tĩnh: plan đang bám đã chứa detour SBPL, còn bản
|
||||||
|
* đồ tĩnh không chứa vật cản chưa vẽ (bãi trụ trong sim, kệ tạm ngoài đời) — đo trên bag 5/9 cho
|
||||||
|
* thấy phương án bản đồ tĩnh nổ oan khi robot đi đúng detour. Vì sao geo chứ không phải Euclid:
|
||||||
|
* chui vào túi mở về phía goal thì Euclid vẫn giảm, geo thì tăng (mission 2 và mission 8 ngày 5/9).
|
||||||
|
*
|
||||||
|
* Yêu cầu @ref analyzeGrid đã chạy trên scratch ở tick này. @p plan_to_grid quy plan về frame lưới.
|
||||||
|
* @p robot_x/@p robot_y là pose robot trong frame lưới.
|
||||||
|
*/
|
||||||
|
CostToGoResult costToGo(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
||||||
|
std::size_t from_index, double robot_x, double robot_y,
|
||||||
|
const CostToGoParams& params, const PlanarTransform& plan_to_grid,
|
||||||
|
GridScratch& scratch);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct CorridorParams
|
||||||
|
* @brief Tham số quét hành lang dọc plan (@ref scanCorridor).
|
||||||
|
*/
|
||||||
|
struct CorridorParams
|
||||||
|
{
|
||||||
|
double horizon = 3.0; ///< [m] quãng plan cần quét.
|
||||||
|
double body_width = 0.96; ///< [m] bề rộng thân.
|
||||||
|
double margin = 0.0; ///< [m] cộng mỗi bên vào bề rộng thân. 0 = đúng biên SBPL (luật R2).
|
||||||
|
double side_cap = 1.5; ///< [m] trần quãng trống đo về mỗi bên.
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct CorridorResult
|
||||||
|
* @brief Kết quả @ref scanCorridor. Hai đại lượng độc lập, cùng đo dọc plan từ `from_index`.
|
||||||
|
*/
|
||||||
|
struct CorridorResult
|
||||||
|
{
|
||||||
|
std::size_t scanned = 0; ///< Số pose plan đã quét trong lưới. 0 = không kết luận gì.
|
||||||
|
/// [m] Quãng dọc plan tới pose đầu tiên có EDT(tâm) < free_radius; = horizon nếu không có.
|
||||||
|
double plan_clear_ahead = 0.0;
|
||||||
|
/// [m] Bề rộng trống nhỏ nhất vuông góc với plan (trái + phải, mỗi bên trần side_cap).
|
||||||
|
double min_width = std::numeric_limits<double>::infinity();
|
||||||
|
/// [m] Quãng dọc plan tới mẫu đầu tiên có bề rộng < body_width + 2*margin; +inf nếu không có.
|
||||||
|
double block_dist = std::numeric_limits<double>::infinity();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Quét hành lang: plan còn sạch bao xa, và chỗ hẹp đầu tiên thân không lọt cách bao xa.
|
||||||
|
*
|
||||||
|
* Bề rộng đo VUÔNG GÓC với plan tại từng pose: đi theo pháp tuyến hai phía cho tới ô lethal hoặc ra
|
||||||
|
* lưới, mỗi phía trần `side_cap`. KHÔNG dùng 2*EDT(tâm): plan sượt tường một bên mà bên kia trống
|
||||||
|
* vẫn là hành lang rộng (mission 1 ngày 5/9).
|
||||||
|
*/
|
||||||
|
CorridorResult scanCorridor(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
||||||
|
std::size_t from_index, const CorridorParams& params,
|
||||||
|
const PlanarTransform& plan_to_grid, const GridScratch& scratch);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct SweepParams
|
||||||
|
* @brief Hình học và ngưỡng của @ref sweepTrajectory.
|
||||||
|
*/
|
||||||
|
struct SweepParams
|
||||||
|
{
|
||||||
|
double front = 0.583; ///< [m] mũi tính từ base_link.
|
||||||
|
double rear = 0.583; ///< [m] đuôi.
|
||||||
|
double side = 0.48; ///< [m] nửa bề rộng.
|
||||||
|
double padding = 0.05; ///< [m] biên an toàn tường minh cộng mọi cạnh (chỉ cho phanh).
|
||||||
|
double lookahead = 1.5; ///< [m] quãng quỹ đạo cần dò.
|
||||||
|
unsigned char lethal_threshold = 254;
|
||||||
|
bool exclude_baseline = true; ///< Bỏ ô lethal đang nằm dưới thân đệm tại pose hiện tại.
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct SweepResult
|
||||||
|
* @brief Kết quả @ref sweepTrajectory.
|
||||||
|
*/
|
||||||
|
struct SweepResult
|
||||||
|
{
|
||||||
|
bool conclusive = false; ///< false = quỹ đạo rỗng hoặc không bắt đầu gần robot.
|
||||||
|
bool blocked = false;
|
||||||
|
double distance = std::numeric_limits<double>::infinity(); ///< [m] quãng dọc quỹ đạo tới mẫu chạm.
|
||||||
|
std::size_t sample = 0; ///< Index pose quỹ đạo chạm.
|
||||||
|
int hit_edge = -1; ///< 0 phải, 1 mũi, 2 trái, 3 đuôi.
|
||||||
|
int baseline_cells = 0;
|
||||||
|
std::size_t scanned = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Kéo footprint đệm dọc QUỸ ĐẠO THẬT của local planner, dò chu vi ở từng pose.
|
||||||
|
*
|
||||||
|
* Thay cho cung (v, omega) không đổi của `checkMotion` khi planner có quỹ đạo (`getLocalPlan()`).
|
||||||
|
* Quỹ đạo trong frame lưới, pose đầu phải cách robot <= @p start_tol (không thì trả không kết luận
|
||||||
|
* — bên gọi rơi về cung). Bước giữa hai pose lớn hơn một ô thì chèn thêm mẫu để tường mỏng không lọt.
|
||||||
|
* Baseline: ô lethal đang nằm dưới thân đệm tại (robot_x, robot_y, robot_yaw) không được tính là
|
||||||
|
* chạm — cùng luật với checkMotion (2026-09-04).
|
||||||
|
*/
|
||||||
|
SweepResult sweepTrajectory(const std::vector<robot_geometry_msgs::PoseStamped>& trajectory,
|
||||||
|
double robot_x, double robot_y, double robot_yaw, double start_tol,
|
||||||
|
const SweepParams& params, const robot_costmap_2d::Costmap2D& grid,
|
||||||
|
GridScratch& scratch);
|
||||||
|
|
||||||
|
} // namespace move_base2
|
||||||
|
|
||||||
|
#endif // MOVE_BASE2_CORE_GRID_ANALYSIS_H_
|
||||||
@@ -2,10 +2,12 @@
|
|||||||
*
|
*
|
||||||
* Software License Agreement (BSD License)
|
* Software License Agreement (BSD License)
|
||||||
*
|
*
|
||||||
* move_base2 — đo đạc trên global plan: tuyến phía trước có sạch không, robot cách plan bao xa.
|
* move_base2 — hình học trên global plan: quy đổi frame, index gần robot (toàn plan / bám cửa sổ),
|
||||||
|
* khoảng cách lệch tuyến.
|
||||||
*
|
*
|
||||||
* Hàm thuần, không giữ trạng thái, không quyết định gì. Người dùng duy nhất là `PathMonitor` của
|
* Hàm thuần, không giữ trạng thái, không quyết định gì. Người dùng: `ControlLoop` (index bám cho
|
||||||
* `ControlLoop` (và luật lọc plan trong `collectPlannerResult`).
|
* EnvMonitor) và `CostmapEnvironment` (quy đổi plan về frame lưới). Các hàm quét tuyến/cung của
|
||||||
|
* PathMonitor cũ (`checkPlan`, `checkMotion`) đã xoá 6/9/2026 — thay bằng `core/grid_analysis`.
|
||||||
*
|
*
|
||||||
* Author: DuongTD
|
* Author: DuongTD
|
||||||
*********************************************************************/
|
*********************************************************************/
|
||||||
@@ -25,18 +27,6 @@ namespace tf3 { class BufferCore; }
|
|||||||
namespace move_base2
|
namespace move_base2
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
|
||||||
* @struct PlanBlockResult
|
|
||||||
* @brief Kết quả quét tuyến phía trước.
|
|
||||||
*/
|
|
||||||
struct PlanBlockResult
|
|
||||||
{
|
|
||||||
bool blocked = false; ///< Có ô nào trên tuyến không đặt được robot vào không.
|
|
||||||
std::size_t index = 0; ///< Index pose bẩn đầu tiên. Chỉ có nghĩa khi @ref blocked.
|
|
||||||
double distance = 0.0; ///< [m] Quãng đường dọc plan từ pose bắt đầu tới đó.
|
|
||||||
std::size_t scanned = 0; ///< Số pose đã quét — 0 nghĩa là không kết luận được gì.
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @struct PlanarTransform
|
* @struct PlanarTransform
|
||||||
* @brief Phép biến đổi 2D phẳng: `p_out = R(yaw) * p_in + t`.
|
* @brief Phép biến đổi 2D phẳng: `p_out = R(yaw) * p_in + t`.
|
||||||
@@ -74,6 +64,19 @@ bool planarTransform(const tf3::BufferCore& tf, const std::string& from, const s
|
|||||||
std::size_t nearestIndex(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
std::size_t nearestIndex(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
||||||
const robot_geometry_msgs::PoseStamped& pose);
|
const robot_geometry_msgs::PoseStamped& pose);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Như @ref nearestIndex nhưng chỉ tìm quanh @p hint: lùi tối đa @p back_m và tiến tối đa
|
||||||
|
* @p ahead_m dọc plan (theo chiều dài cung).
|
||||||
|
*
|
||||||
|
* Detour SBPL uốn quanh trụ có đoạn sau đi sát chỗ robot đang đứng: `nearestIndex` toàn plan nhảy
|
||||||
|
* sang đoạn đó, điểm thoát T của EnvMonitor đổi và `cost_to_go` nhảy +8 m trong 1 s (run 5/9 22:35,
|
||||||
|
* eff_geo −4,96 giả). Bên gọi giữ index qua các cycle và reset về `nearestIndex` khi có plan mới.
|
||||||
|
* @p hint vượt cỡ plan thì được kẹp; plan < 2 pose → 0.
|
||||||
|
*/
|
||||||
|
std::size_t trackedNearestIndex(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
||||||
|
const robot_geometry_msgs::PoseStamped& pose, std::size_t hint,
|
||||||
|
double back_m, double ahead_m);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief [m] Khoảng cách vuông góc từ @p pose tới polyline của @p plan.
|
* @brief [m] Khoảng cách vuông góc từ @p pose tới polyline của @p plan.
|
||||||
*
|
*
|
||||||
@@ -83,109 +86,6 @@ std::size_t nearestIndex(const std::vector<robot_geometry_msgs::PoseStamped>& pl
|
|||||||
double crossTrackError(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
double crossTrackError(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
||||||
const robot_geometry_msgs::PoseStamped& pose);
|
const robot_geometry_msgs::PoseStamped& pose);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Quét tuyến phía trước xem có ô nào không đặt được robot vào không.
|
|
||||||
*
|
|
||||||
* @param plan Tuyến cần quét, trong frame của plan.
|
|
||||||
* @param from_index Bắt đầu quét từ đây (thường là @ref nearestIndex của pose robot).
|
|
||||||
* @param horizon [m] Quãng đường dọc plan tối đa cần quét.
|
|
||||||
* @param grid Lưới cost. Bên gọi phải giữ mutex của nó.
|
|
||||||
* @param plan_to_grid Phép quy đổi frame plan → frame lưới.
|
|
||||||
*
|
|
||||||
* Ngữ nghĩa cost, cả ba đều cố ý:
|
|
||||||
* - `>= INSCRIBED_INFLATED_OBSTACLE` = **bị chắn**: đặt tâm robot ở đây thì footprint đã đệm chạm
|
|
||||||
* vật cản.
|
|
||||||
* - `NO_INFORMATION` = **KHÔNG** bị chắn. Vùng chưa quét mà tính là chắn thì robot dừng suốt ở mọi
|
|
||||||
* tuyến đi vào chỗ chưa thấy.
|
|
||||||
* - Pose ra ngoài lưới = **dừng quét**, không phải bị chắn. Cửa sổ costmap lăn theo robot nên phần
|
|
||||||
* xa luôn nằm ngoài; coi đó là bị chắn thì mọi plan đều bẩn.
|
|
||||||
*/
|
|
||||||
PlanBlockResult checkPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
|
||||||
std::size_t from_index, double horizon,
|
|
||||||
const robot_costmap_2d::Costmap2D& grid,
|
|
||||||
const PlanarTransform& plan_to_grid);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @struct MotionCheckParams
|
|
||||||
* @brief Hình học và ngưỡng cho phép đo "thân robot có sắp chạm không" (@ref checkMotion).
|
|
||||||
*
|
|
||||||
* Khác hẳn @ref checkPlan: đối tượng đo là CHUYỂN ĐỘNG SẮP XẢY RA (cung cong không đổi vẽ bởi lệnh
|
|
||||||
* `(v, omega)` từ pose hiện tại), không phải global plan — local planner lệch plan cả mét khi né,
|
|
||||||
* quét plan lúc đó bảo "sạch" trong khi thân đang đi hướng khác (đo 29/8, mission 5: cte 0.97 m,
|
|
||||||
* robot húc vào khe 0.76 m). Ngưỡng mặc định là LETHAL: chỉ ô vật cản THẬT, không đếm vành
|
|
||||||
* inflation — inflation là biên cho tâm robot, đem áp lên mũi/hông là đếm biên hai lần và báo
|
|
||||||
* chạm giả khi tường còn cách nửa mét. Biên an toàn thay bằng @ref padding tường minh.
|
|
||||||
*/
|
|
||||||
struct MotionCheckParams
|
|
||||||
{
|
|
||||||
double lookahead = 0.8; ///< [m] quãng cung cần dò — tầm phanh, không hơn.
|
|
||||||
/// Trần THƯA của số mẫu: bước = lookahead / max_samples nhưng không bao giờ lớn hơn một ô lưới
|
|
||||||
/// (tường mỏng 1 ô lọt giữa hai mẫu là đâm xuyên). Ở lưới 0.05 m, 0.8 m dò 16 mẫu.
|
|
||||||
int max_samples = 6;
|
|
||||||
double front_offset = 0.0; ///< [m] mũi robot tính từ base_link, dọc heading.
|
|
||||||
double rear_offset = 0.0; ///< [m] đuôi robot tính từ base_link, ngược heading.
|
|
||||||
double side_offset = 0.0; ///< [m] nửa bề rộng thân (chu vi dò cả bốn cạnh).
|
|
||||||
double padding = 0.0; ///< [m] nới thêm mọi offset — biên an toàn tường minh.
|
|
||||||
unsigned char cost_threshold = 254; ///< cost >= đây là chạm. 254 = chỉ LETHAL.
|
|
||||||
double min_speed = 0.02; ///< [m/s] |v| dưới đây cung suy biến, không dò (xoay tại chỗ).
|
|
||||||
/**
|
|
||||||
* [rad] Không dò cung quá góc quay này: |omega/v|·walk > lookahead_angle thì dừng vẽ. Với cung
|
|
||||||
* R = 0.1 m, 0.8 m lookahead là quay 8 rad — hơn một vòng tròn — soi thứ không bao giờ xảy ra vì
|
|
||||||
* lệnh đã đổi từ lâu, và là nguồn của "would hit"/"clear again" lật mỗi vài cycle khi omega/v đổi
|
|
||||||
* chút ở đuôi cung. Mặc định pi = không cắt (nguyên thuỷ giữ hành vi cũ; policy đặt ở config).
|
|
||||||
*/
|
|
||||||
double lookahead_angle = 3.14159265358979323846;
|
|
||||||
/**
|
|
||||||
* Loại trừ baseline (2026-09-04): ô >= cost_threshold đang nằm DƯỚI thân đệm tại pose hiện tại
|
|
||||||
* không được tính là chạm ở các mẫu sau. Câu hỏi đổi từ "thân có đè LETHAL không" thành "lệnh
|
|
||||||
* này có đưa THÊM ô LETHAL vào thân không": trượt song song / rời xa vật đang kề hông thì đi
|
|
||||||
* được, lấn sâu thêm thì vẫn chặn. Sim 4/9: người kề hông ở 0.46 m ngang (thân 0.48 + đệm 0.05)
|
|
||||||
* làm mẫu đầu tiên báo 0.05 m mãi dù lệnh là đi thẳng. false = hành vi cũ.
|
|
||||||
*/
|
|
||||||
bool exclude_baseline = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @struct MotionBlockResult
|
|
||||||
* @brief Kết quả dò cung lệnh.
|
|
||||||
*/
|
|
||||||
struct MotionBlockResult
|
|
||||||
{
|
|
||||||
bool blocked = false; ///< Có điểm dò nào chạm ngưỡng không.
|
|
||||||
int sample = -1; ///< Chỉ số mẫu chạm đầu tiên (1-based theo bước). Chỉ có nghĩa khi blocked.
|
|
||||||
double distance = 0.0; ///< [m] quãng dọc cung từ robot tới mẫu chạm.
|
|
||||||
int scanned = 0; ///< Số mẫu đã dò — 0 nghĩa là không dò được gì (v quá nhỏ).
|
|
||||||
int baseline_cells = 0; ///< Số ô >= ngưỡng đang nằm dưới thân đệm và đã được loại trừ. Chỉ để log.
|
|
||||||
|
|
||||||
/// Chi tiết để debug hiện trường (không ảnh hưởng quyết định). Toạ độ trong frame robot HIỆN TẠI
|
|
||||||
/// (x tới, y trái), không phải frame lưới, để người đọc log biết ngay "ô người ở trái-sau".
|
|
||||||
static constexpr int kMaxListedBaseline = 8;
|
|
||||||
int hit_edge = -1; ///< Cạnh chạm: 0 phải, 1 mũi, 2 trái, 3 đuôi. -1 = không chạm.
|
|
||||||
double hit_x = 0.0; ///< [m] ô chạm, frame robot hiện tại.
|
|
||||||
double hit_y = 0.0; ///< [m]
|
|
||||||
int baseline_listed = 0; ///< Số ô baseline được liệt kê (<= kMaxListedBaseline).
|
|
||||||
std::array<std::array<double, 2>, kMaxListedBaseline> baseline_xy{}; ///< [m] frame robot hiện tại.
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Dò cung cong không đổi mà lệnh `(v, omega)` vẽ ra từ pose `(x, y, yaw)` — tất cả trong
|
|
||||||
* frame của @p grid.
|
|
||||||
*
|
|
||||||
* @param baseline_scratch Bộ đệm cho tập ô baseline (xem @ref MotionCheckParams::exclude_baseline),
|
|
||||||
* để caller gọi 20 Hz không cấp phát heap mỗi cycle; nullptr = dùng vector cục bộ.
|
|
||||||
*
|
|
||||||
* Mỗi mẫu dò TOÀN BỘ CHU VI hình chữ nhật footprint đã cộng @ref MotionCheckParams::padding,
|
|
||||||
* lấy mẫu dày hơn một ô lưới. Năm điểm rời (bản đầu) để hở nửa bề rộng thân giữa các đường quét —
|
|
||||||
* trụ mảnh đứng ở góc trước lọt qua mà không probe nào thấy (đo 4/9). Bỏ 0.05 m đầu: đất robot đang đứng không phải bằng chứng về tương
|
|
||||||
* lai, và phán nó là "chạm" biến robot đứng cạnh vật cản thành đứng vĩnh viễn. `NO_INFORMATION`
|
|
||||||
* và điểm ra ngoài lưới = KHÔNG chạm (cùng lý do với @ref checkPlan). v < 0 (lùi) dò theo chiều
|
|
||||||
* lùi với cùng hình học.
|
|
||||||
*/
|
|
||||||
MotionBlockResult checkMotion(double x, double y, double yaw, double v, double omega,
|
|
||||||
const MotionCheckParams& params,
|
|
||||||
const robot_costmap_2d::Costmap2D& grid,
|
|
||||||
std::vector<unsigned int>* baseline_scratch = nullptr);
|
|
||||||
|
|
||||||
} // namespace move_base2
|
} // namespace move_base2
|
||||||
|
|
||||||
#endif // MOVE_BASE2_CORE_PLAN_INSPECTOR_H_
|
#endif // MOVE_BASE2_CORE_PLAN_INSPECTOR_H_
|
||||||
|
|||||||
@@ -197,7 +197,8 @@ struct StateMachineInput
|
|||||||
bool robot_stopped = true;
|
bool robot_stopped = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tuyến phía trước bị chắn (PathMonitor của control loop đã xác nhận đủ `confirm_time`).
|
* Tuyến phía trước bị chắn (EnvMonitor kUnreachable: không còn đường trong cửa sổ tới điểm thoát, đã
|
||||||
|
* tĩnh đủ `static_on`).
|
||||||
*
|
*
|
||||||
* Khác mọi cờ khác trong struct này: nó là **dự báo**, không phải báo hỏng. Controller vẫn ra
|
* Khác mọi cờ khác trong struct này: nó là **dự báo**, không phải báo hỏng. Controller vẫn ra
|
||||||
* lệnh bình thường, plan vẫn hợp lệ — chỉ là đi tiếp thì sẽ tắc. Bên gọi chịu trách nhiệm mọi
|
* lệnh bình thường, plan vẫn hợp lệ — chỉ là đi tiếp thì sẽ tắc. Bên gọi chịu trách nhiệm mọi
|
||||||
@@ -205,15 +206,16 @@ struct StateMachineInput
|
|||||||
*/
|
*/
|
||||||
bool path_blocked = false;
|
bool path_blocked = false;
|
||||||
|
|
||||||
/// Robot đã lệch khỏi plan đang chạy quá ngưỡng. Cùng quy ước với @ref path_blocked.
|
/// Robot đã lệch khỏi plan đang chạy quá ngưỡng. Cùng quy ước với @ref path_blocked. Từ 6/9/2026 không
|
||||||
|
/// còn nguồn cấp (phép đo 2 của PathMonitor cũ đã xoá); giữ cổng cho hợp đồng route/khoá và test.
|
||||||
bool off_path = false;
|
bool off_path = false;
|
||||||
|
|
||||||
/// Phép đo 3 của PathMonitor: thân sắp chạm vật cản thật trên cung lệnh, đã đủ cycle xác nhận.
|
/// EnvMonitor kContact: thân sắp chạm vật cản thật trên quỹ đạo local planner, kề mũi tĩnh đủ lâu.
|
||||||
/// Cùng quy ước với @ref path_blocked (dự báo, route riêng `collision`, cạn route thì khoá).
|
/// Cùng quy ước với @ref path_blocked (dự báo, route riêng `collision`, cạn route thì khoá).
|
||||||
/// Thắng cả path_blocked lẫn off_path khi cùng đúng: gần nhất thì xử lý trước.
|
/// Thắng cả path_blocked lẫn off_path khi cùng đúng: gần nhất thì xử lý trước.
|
||||||
bool collision = false;
|
bool collision = false;
|
||||||
|
|
||||||
/// Phép đo 4 của PathMonitor: planner đòi đi mà robot không tiến (local minimum đã bắt đầu).
|
/// EnvMonitor kStall: planner đòi đi mà robot không đi được đâu (cổng dịch chuyển thực mở).
|
||||||
/// Đi đường kOscillation — trigger GỐC — nên cạn route là ABORT hợp lệ, không rơi vào bẫy trigger
|
/// Đi đường kOscillation — trigger GỐC — nên cạn route là ABORT hợp lệ, không rơi vào bẫy trigger
|
||||||
/// giám sát bị khoá rồi đứng im. Bên gọi chịu debounce/route/mode; state machine chỉ tin cờ.
|
/// giám sát bị khoá rồi đứng im. Bên gọi chịu debounce/route/mode; state machine chỉ tin cờ.
|
||||||
bool no_progress = false;
|
bool no_progress = false;
|
||||||
|
|||||||
77
include/move_base2/io/env_logger.h
Normal file
77
include/move_base2/io/env_logger.h
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* move_base2 — ghi feature + verdict của EnvMonitor ra JSONL (Test/ENV_MONITOR_PLAN.md §2.6).
|
||||||
|
*
|
||||||
|
* Mục đích: replay offline để chỉnh ngưỡng không cần sim, và là dữ liệu huấn luyện ML sau này. Mỗi
|
||||||
|
* dòng là một tick nặng (5 Hz) hoặc một sườn sự kiện. Tắt mặc định; khi tắt không cấp phát, không mở
|
||||||
|
* file.
|
||||||
|
*
|
||||||
|
* Ghi trên control thread: dòng ~400 byte, gom vào buffer và flush mỗi `flush_period` — một lần
|
||||||
|
* `write()` vài kB mỗi giây. Nếu đo thấy ảnh hưởng chu kỳ thì chuyển sang thread riêng.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#ifndef MOVE_BASE2_IO_ENV_LOGGER_H_
|
||||||
|
#define MOVE_BASE2_IO_ENV_LOGGER_H_
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <robot/time.h>
|
||||||
|
|
||||||
|
#include <move_base2/core/env_monitor.h>
|
||||||
|
#include <move_base2/ports/environment_port.h>
|
||||||
|
|
||||||
|
namespace move_base2
|
||||||
|
{
|
||||||
|
|
||||||
|
struct EnvLoggerConfig
|
||||||
|
{
|
||||||
|
bool enabled = false;
|
||||||
|
std::string log_dir = "/tmp/env_monitor"; ///< tạo nếu chưa có; file `envmon_<epoch>.jsonl`.
|
||||||
|
double flush_period = 1.0; ///< [s]
|
||||||
|
|
||||||
|
bool validate(std::string& error) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
class EnvLogger
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
EnvLogger() = default;
|
||||||
|
~EnvLogger();
|
||||||
|
EnvLogger(const EnvLogger&) = delete;
|
||||||
|
EnvLogger& operator=(const EnvLogger&) = delete;
|
||||||
|
|
||||||
|
/// @return false kèm lý do nếu không mở được file. Không mở gì khi `config.enabled == false`.
|
||||||
|
bool open(const EnvLoggerConfig& config, std::string& error);
|
||||||
|
bool isOpen() const
|
||||||
|
{
|
||||||
|
return out_.is_open();
|
||||||
|
}
|
||||||
|
const std::string& path() const
|
||||||
|
{
|
||||||
|
return path_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Một dòng JSON. @p geometry có thể null (tick nhẹ). @p state là tên state machine.
|
||||||
|
void record(const robot::Time& now, const EnvSample& sample, const EnvVerdict& verdict,
|
||||||
|
const EnvGeometry* geometry, const char* state, std::uint64_t mission_id,
|
||||||
|
bool used_local_plan);
|
||||||
|
|
||||||
|
void flush();
|
||||||
|
|
||||||
|
private:
|
||||||
|
EnvLoggerConfig config_;
|
||||||
|
std::ofstream out_;
|
||||||
|
std::string path_;
|
||||||
|
std::string buffer_;
|
||||||
|
robot::Time last_flush_;
|
||||||
|
bool has_last_flush_ = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace move_base2
|
||||||
|
|
||||||
|
#endif // MOVE_BASE2_IO_ENV_LOGGER_H_
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
#include <move_base2/control_loop.h>
|
#include <move_base2/control_loop.h>
|
||||||
#include <move_base2/io/costmap_exporter.h>
|
#include <move_base2/io/costmap_exporter.h>
|
||||||
#include <move_base2/ports/clock_port.h>
|
#include <move_base2/ports/clock_port.h>
|
||||||
#include <move_base2/ports/path_check_port.h>
|
#include <move_base2/ports/environment_port.h>
|
||||||
#include <move_base2/ports/pose_port.h>
|
#include <move_base2/ports/pose_port.h>
|
||||||
#include <move_base2/runners/action_runner.h>
|
#include <move_base2/runners/action_runner.h>
|
||||||
#include <move_base2/runners/controller_runner.h>
|
#include <move_base2/runners/controller_runner.h>
|
||||||
@@ -88,48 +88,38 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class CostmapPathCheck
|
* @class CostmapEnvironment
|
||||||
* @brief Hiện thực @ref PathCheckPort trên costmap ĐIỀU KHIỂN (local).
|
* @brief Hiện thực @ref EnvironmentPort trên costmap ĐIỀU KHIỂN (local) — nguồn dữ liệu của EnvMonitor.
|
||||||
*
|
*
|
||||||
* Local chứ không global, hai lý do và cả hai đều quan trọng:
|
* Đọc snapshot (`snapshot_for_readers`) qua `getCostmapForRead()` sau `refresh()`; không có snapshot thì
|
||||||
* - **Độ tươi**: local chạy ở `update_frequency: 6.0` Hz, global chỉ `1.0` Hz. Vật cản vừa xuất
|
* giữ mutex trong lúc phân tích (~1 ms). Toán nằm ở `core/grid_analysis`; ở đây chỉ TF + lưới + thân.
|
||||||
* hiện có mặt ở local trước.
|
|
||||||
* - **Đồng thuận với detour**: `DetourPathRecovery` lập đường vòng trên chính local costmap.
|
|
||||||
* Monitor báo "bẩn" trên một lưới mà planner detour lại thấy "sạch" là vòng lặp vô nghĩa.
|
|
||||||
*
|
|
||||||
* Plan ở frame global (`map`) còn lưới ở `odom`, nên phải quy đổi — tra TF **một lần** cho cả lượt
|
|
||||||
* quét (xem @ref PlanarTransform), không phải mỗi pose.
|
|
||||||
*
|
|
||||||
* @note Con trỏ costmap **non-owning và có thể bị thay** — lấy lại mỗi lần dùng, không cache
|
|
||||||
* `Costmap2D*` bên trong.
|
|
||||||
*/
|
*/
|
||||||
class CostmapPathCheck final : public PathCheckPort
|
class CostmapEnvironment final : public EnvironmentPort
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void setCostmap(robot_costmap_2d::Costmap2DROBOT* costmap)
|
void setCostmap(robot_costmap_2d::Costmap2DROBOT* costmap)
|
||||||
{
|
{
|
||||||
costmap_ = costmap;
|
costmap_ = costmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setTf(const std::shared_ptr<tf3::BufferCore>& tf)
|
void setTf(const std::shared_ptr<tf3::BufferCore>& tf)
|
||||||
{
|
{
|
||||||
tf_ = tf;
|
tf_ = tf;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool checkPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
void refresh() override;
|
||||||
std::size_t from_index, double horizon, PlanBlockResult& out) const override;
|
bool sampleGeometry(const EnvGeometryRequest& request, EnvGeometry& out) override;
|
||||||
|
bool sweepTrajectory(const EnvTrajectoryRequest& request, SweepResult& out,
|
||||||
bool checkMotion(const robot_geometry_msgs::PoseStamped& robot_pose, double v, double omega,
|
bool& used_local_plan) override;
|
||||||
const MotionCheckParams& params, MotionBlockResult& out) const override;
|
|
||||||
|
|
||||||
bool robotBodyExtents(double& front, double& rear, double& side) const override;
|
bool robotBodyExtents(double& front, double& rear, double& side) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/// Lưới để đọc + có phải lưới sống (cần khoá) không. nullptr = chưa có costmap.
|
||||||
|
robot_costmap_2d::Costmap2D* gridForRead(bool& live) const;
|
||||||
|
|
||||||
robot_costmap_2d::Costmap2DROBOT* costmap_ = nullptr;
|
robot_costmap_2d::Costmap2DROBOT* costmap_ = nullptr;
|
||||||
std::shared_ptr<tf3::BufferCore> tf_;
|
std::shared_ptr<tf3::BufferCore> tf_;
|
||||||
/// Bộ đệm baseline của checkMotion — tránh cấp phát heap mỗi cycle. Cổng này chỉ được gọi từ
|
GridScratch scratch_; ///< chỉ control thread dùng
|
||||||
/// control thread (một consumer), nên `mutable` không tạo race.
|
std::vector<robot_geometry_msgs::PoseStamped> traj_scratch_; ///< quỹ đạo đã quy về frame lưới
|
||||||
mutable std::vector<unsigned int> baseline_scratch_ = std::vector<unsigned int>(64);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -355,6 +345,9 @@ private:
|
|||||||
/// Guard "không đi mù": hỏi costmap điều khiển xem observation buffer còn hạn không.
|
/// Guard "không đi mù": hỏi costmap điều khiển xem observation buffer còn hạn không.
|
||||||
CostmapStatusAdapter costmap_status_;
|
CostmapStatusAdapter costmap_status_;
|
||||||
|
|
||||||
|
/// Nguồn dữ liệu của EnvMonitor: costmap local, cùng lưới với detour.
|
||||||
|
CostmapEnvironment environment_;
|
||||||
|
|
||||||
std::unique_ptr<robot_costmap_2d::Costmap2DROBOT> global_costmap_;
|
std::unique_ptr<robot_costmap_2d::Costmap2DROBOT> global_costmap_;
|
||||||
std::unique_ptr<robot_costmap_2d::Costmap2DROBOT> local_costmap_;
|
std::unique_ptr<robot_costmap_2d::Costmap2DROBOT> local_costmap_;
|
||||||
|
|
||||||
@@ -380,7 +373,6 @@ private:
|
|||||||
*/
|
*/
|
||||||
CostmapPosePort global_pose_; ///< frame `map` — planner, chống quẩn, pose báo về host.
|
CostmapPosePort global_pose_; ///< frame `map` — planner, chống quẩn, pose báo về host.
|
||||||
CostmapPosePort local_pose_; ///< frame `odom` — controller và recovery.
|
CostmapPosePort local_pose_; ///< frame `odom` — controller và recovery.
|
||||||
CostmapPathCheck path_check_; ///< quét tuyến trên costmap local (PathMonitor + lọc plan).
|
|
||||||
CostmapExporter global_exporter_;
|
CostmapExporter global_exporter_;
|
||||||
CostmapExporter local_exporter_;
|
CostmapExporter local_exporter_;
|
||||||
|
|
||||||
|
|||||||
@@ -97,6 +97,17 @@ public:
|
|||||||
*/
|
*/
|
||||||
virtual void setMeasuredVelocity(const robot_geometry_msgs::Twist& velocity) = 0;
|
virtual void setMeasuredVelocity(const robot_geometry_msgs::Twist& velocity) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Vận tốc đo được gần nhất đã nhận qua @ref setMeasuredVelocity (EnvMonitor dùng để tính quãng
|
||||||
|
* phanh và phát hiện trượt). Mặc định "chưa có" để cổng giả/hiện thực cũ không đổi.
|
||||||
|
* @return false nếu chưa từng nhận giá trị nào.
|
||||||
|
*/
|
||||||
|
virtual bool measuredVelocity(robot_geometry_msgs::Twist& out) const
|
||||||
|
{
|
||||||
|
(void)out;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Đặt trần vận tốc thẳng. **Dấu chọn chiều**: dương = tiến, âm = lùi.
|
* @brief Đặt trần vận tốc thẳng. **Dấu chọn chiều**: dương = tiến, âm = lùi.
|
||||||
*
|
*
|
||||||
|
|||||||
147
include/move_base2/ports/environment_port.h
Normal file
147
include/move_base2/ports/environment_port.h
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* move_base2 — cổng môi trường cho EnvMonitor: "quanh robot lúc này trông thế nào".
|
||||||
|
*
|
||||||
|
* Kế thừa vai trò của `PathCheckPort` (Test/ENV_MONITOR_PLAN.md §2.5): lõi hỏi, hạ tầng trả lời.
|
||||||
|
* Cổng lo costmap + TF; toán nằm ở `core/grid_analysis`; quyết định nằm ở `core/env_monitor`. Nhờ vậy
|
||||||
|
* `ControlLoop` kiểm được bằng cổng giả (mặc định = "không kết luận được").
|
||||||
|
*
|
||||||
|
* @note Chỉ control thread được gọi. Hiện thực thật đọc snapshot costmap (`snapshot_for_readers`) hoặc
|
||||||
|
* giữ mutex khi không có snapshot.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#ifndef MOVE_BASE2_PORTS_ENVIRONMENT_PORT_H_
|
||||||
|
#define MOVE_BASE2_PORTS_ENVIRONMENT_PORT_H_
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <limits>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <robot_geometry_msgs/PoseStamped.h>
|
||||||
|
#include <robot_nav_2d_msgs/Path2D.h>
|
||||||
|
|
||||||
|
#include <move_base2/core/grid_analysis.h>
|
||||||
|
#include <move_base2/core/navigation_request.h>
|
||||||
|
|
||||||
|
namespace move_base2
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct EnvSamplingConfig
|
||||||
|
* @brief Tham số LẤY MẪU của EnvMonitor (phần chạy ở cổng / ControlLoop, không phải lõi quyết định).
|
||||||
|
*/
|
||||||
|
struct EnvSamplingConfig
|
||||||
|
{
|
||||||
|
double check_period = 0.2; ///< [s] nhịp phần nặng (EDT + Dijkstra + hành lang).
|
||||||
|
int lethal_threshold = 254; ///< cost >= đây là vật cản thật. KHÔNG hạ xuống 253.
|
||||||
|
/// [m] thân robot tính từ base_link; cả ba = 0 -> lấy footprint không đệm của costmap mỗi lượt.
|
||||||
|
double body_front = 0.0;
|
||||||
|
double body_rear = 0.0;
|
||||||
|
double body_side = 0.0;
|
||||||
|
double padding = 0.05; ///< [m] đệm chỉ cho dò tiếp xúc; liên thông dùng biên SBPL (0).
|
||||||
|
double horizon = 3.0; ///< [m] quãng plan quét hành lang.
|
||||||
|
double corridor_margin = 0.0; ///< [m] mỗi bên, cộng vào bề rộng thân khi xét khe. 0 = biên SBPL.
|
||||||
|
double corridor_cap = 1.5; ///< [m] trần quãng trống mỗi bên.
|
||||||
|
double target_backoff = 0.5; ///< [m] T bẩn thì lùi dọc plan tối đa chừng này.
|
||||||
|
double robot_snap = 0.35; ///< [m] ô robot không free thì snap sang ô free gần nhất trong đây.
|
||||||
|
/// [m] Cửa sổ bám index plan qua các tick: lùi tối đa track_back, tiến tối đa track_ahead dọc plan kể từ
|
||||||
|
/// index tick trước (xem trackedNearestIndex). Plan mới -> tìm lại toàn plan một lần.
|
||||||
|
double track_back = 1.0;
|
||||||
|
double track_ahead = 3.0;
|
||||||
|
/// [m] Điểm thoát T của cost_to_go ở quãng này dọc plan kể từ index đang bám (kẹp trong cửa sổ); 0 = T là điểm
|
||||||
|
/// plan rời cửa sổ (bản cũ, lùi được khi cửa sổ trôi -> stall oan trên plan chữ U, run 5/9 23:37).
|
||||||
|
double exit_ahead = 2.0;
|
||||||
|
/// [m] T không free (plan ôm tường) thì snap sang ô free gần nhất trong đây trước khi kết luận "không tới được".
|
||||||
|
double target_snap = 0.5;
|
||||||
|
std::string use_local_plan = "auto"; ///< auto | true | false — quỹ đạo local planner cho dò tiếp xúc.
|
||||||
|
double local_plan_start_tol = 0.15; ///< [m] pose đầu quỹ đạo phải cách robot dưới đây mới tin.
|
||||||
|
double contact_lookahead = 1.5; ///< [m] quãng quỹ đạo cần dò.
|
||||||
|
std::vector<MotionProfile> profiles{ MotionProfile::kPosition };
|
||||||
|
|
||||||
|
bool monitors(MotionProfile profile) const;
|
||||||
|
bool validate(std::string& error) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @brief Yêu cầu lấy mẫu hình học nặng (5 Hz).
|
||||||
|
struct EnvGeometryRequest
|
||||||
|
{
|
||||||
|
const std::vector<robot_geometry_msgs::PoseStamped>* plan = nullptr; ///< frame map, KHÔNG null.
|
||||||
|
std::size_t from_index = 0;
|
||||||
|
robot_geometry_msgs::PoseStamped robot_pose; ///< frame của cổng pose.
|
||||||
|
const EnvSamplingConfig* sampling = nullptr;
|
||||||
|
double front = 0.0; ///< [m] thân đã chốt (config hoặc footprint).
|
||||||
|
double rear = 0.0;
|
||||||
|
double side = 0.0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @brief Kết quả lấy mẫu hình học.
|
||||||
|
struct EnvGeometry
|
||||||
|
{
|
||||||
|
bool valid = false;
|
||||||
|
double free_radius = 0.0; ///< [m] bán kính tâm đã dùng (inscribed).
|
||||||
|
CostToGoResult cost;
|
||||||
|
double edt_body_min = std::numeric_limits<double>::quiet_NaN();
|
||||||
|
CorridorResult corridor;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @brief Yêu cầu dò tiếp xúc dọc quỹ đạo (mỗi cycle).
|
||||||
|
struct EnvTrajectoryRequest
|
||||||
|
{
|
||||||
|
robot_geometry_msgs::PoseStamped robot_pose;
|
||||||
|
const robot_nav_2d_msgs::Path2D* local_plan = nullptr; ///< quỹ đạo local planner; null = không có.
|
||||||
|
double v = 0.0; ///< [m/s] lệnh gần nhất — cho cung (v, omega) khi không dùng được quỹ đạo.
|
||||||
|
double omega = 0.0; ///< [rad/s]
|
||||||
|
const EnvSamplingConfig* sampling = nullptr;
|
||||||
|
double front = 0.0;
|
||||||
|
double rear = 0.0;
|
||||||
|
double side = 0.0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class EnvironmentPort
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~EnvironmentPort() = default;
|
||||||
|
|
||||||
|
/// @brief Một lần đầu cycle: làm mới snapshot costmap (nếu bật) để cả lượt đọc cùng một lưới.
|
||||||
|
virtual void refresh()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @return false = KHÔNG kết luận được (thiếu costmap/TF). Bên gọi không được suy thành gì cả.
|
||||||
|
virtual bool sampleGeometry(const EnvGeometryRequest& request, EnvGeometry& out)
|
||||||
|
{
|
||||||
|
(void)request;
|
||||||
|
(void)out;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Kéo footprint đệm dọc quỹ đạo local planner; không dùng được thì dọc cung (v, omega).
|
||||||
|
* @param[out] used_local_plan quỹ đạo thật đã được dùng (false = đã rơi về cung).
|
||||||
|
*/
|
||||||
|
virtual bool sweepTrajectory(const EnvTrajectoryRequest& request, SweepResult& out,
|
||||||
|
bool& used_local_plan)
|
||||||
|
{
|
||||||
|
(void)request;
|
||||||
|
(void)out;
|
||||||
|
used_local_plan = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Thân robot từ footprint KHÔNG đệm của costmap. false = không có.
|
||||||
|
virtual bool robotBodyExtents(double& front, double& rear, double& side) const
|
||||||
|
{
|
||||||
|
(void)front;
|
||||||
|
(void)rear;
|
||||||
|
(void)side;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace move_base2
|
||||||
|
|
||||||
|
#endif // MOVE_BASE2_PORTS_ENVIRONMENT_PORT_H_
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
/*********************************************************************
|
|
||||||
*
|
|
||||||
* Software License Agreement (BSD License)
|
|
||||||
*
|
|
||||||
* move_base2 — cổng kiểm tra tuyến trên costmap.
|
|
||||||
*
|
|
||||||
* Author: DuongTD
|
|
||||||
*********************************************************************/
|
|
||||||
#ifndef MOVE_BASE2_PORTS_PATH_CHECK_PORT_H_
|
|
||||||
#define MOVE_BASE2_PORTS_PATH_CHECK_PORT_H_
|
|
||||||
|
|
||||||
#include <cstddef>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <robot_geometry_msgs/PoseStamped.h>
|
|
||||||
|
|
||||||
#include <move_base2/core/plan_inspector.h>
|
|
||||||
|
|
||||||
namespace move_base2
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @class PathCheckPort
|
|
||||||
* @brief "Tuyến này có đi được không" — lõi hỏi, hạ tầng trả lời.
|
|
||||||
*
|
|
||||||
* Tách thành cổng vì phép kiểm cần **costmap + TF**, hai thứ `ControlLoop` cố ý không giữ. Nhờ vậy
|
|
||||||
* lõi vẫn kiểm được bằng cổng giả, còn hiện thực thật (`CostmapPathCheck`) sống cạnh costmap trong
|
|
||||||
* `NavigationRuntime`.
|
|
||||||
*
|
|
||||||
* @note Chỉ control thread được gọi: hiện thực thật giữ mutex costmap trong lúc quét.
|
|
||||||
*/
|
|
||||||
class PathCheckPort
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual ~PathCheckPort() = default;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Quét @p plan từ @p from_index về phía trước tối đa @p horizon [m].
|
|
||||||
*
|
|
||||||
* @param[out] out Kết quả; chỉ được ghi khi hàm trả true.
|
|
||||||
* @return false = **không kết luận được** (thiếu TF, chưa có costmap). Bên gọi phải coi đó là
|
|
||||||
* "không biết" và KHÔNG khai hoả — không được suy thành "sạch" hay "bẩn".
|
|
||||||
*/
|
|
||||||
virtual bool checkPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
|
||||||
std::size_t from_index, double horizon, PlanBlockResult& out) const = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Dò cung mà lệnh `(v, omega)` sắp vẽ ra từ @p robot_pose — thân robot có chạm không.
|
|
||||||
*
|
|
||||||
* Mặc định = "không kết luận được": cổng giả và hiện thực cũ không cần biết phép đo này; chỉ
|
|
||||||
* `CostmapPathCheck` hiện thực thật. Cùng quy ước với @ref checkPlan: false = KHÔNG BIẾT, bên gọi
|
|
||||||
* không được khai hoả.
|
|
||||||
*/
|
|
||||||
virtual bool checkMotion(const robot_geometry_msgs::PoseStamped& robot_pose, double v,
|
|
||||||
double omega, const MotionCheckParams& params,
|
|
||||||
MotionBlockResult& out) const
|
|
||||||
{
|
|
||||||
(void)robot_pose;
|
|
||||||
(void)v;
|
|
||||||
(void)omega;
|
|
||||||
(void)params;
|
|
||||||
(void)out;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Kích thước thân robot từ footprint mà costmap đang dùng (KHÔNG đệm), trong frame base.
|
|
||||||
*
|
|
||||||
* @param[out] front [m] mũi tính từ base_link (max x), @p rear [m] đuôi (-min x),
|
|
||||||
* @p side [m] nửa bề rộng (max |y|). Chỉ ghi khi trả true.
|
|
||||||
* @return false nếu cổng không có footprint (test giả, costmap chưa dựng) hoặc footprint suy biến —
|
|
||||||
* caller phải dùng số khai tay hoặc báo không kết luận được.
|
|
||||||
*
|
|
||||||
* Collision guard dùng hình chữ nhật bao của footprint; footprint đa giác lồi khác chữ nhật thì bao
|
|
||||||
* hơi rộng hơn, an toàn về phía dừng sớm.
|
|
||||||
*/
|
|
||||||
virtual bool robotBodyExtents(double& front, double& rear, double& side) const
|
|
||||||
{
|
|
||||||
(void)front;
|
|
||||||
(void)rear;
|
|
||||||
(void)side;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace move_base2
|
|
||||||
|
|
||||||
#endif // MOVE_BASE2_PORTS_PATH_CHECK_PORT_H_
|
|
||||||
@@ -171,6 +171,7 @@ public:
|
|||||||
bool isGoalReached() override;
|
bool isGoalReached() override;
|
||||||
void getLocalPlan(robot_nav_2d_msgs::Path2D& plan) override;
|
void getLocalPlan(robot_nav_2d_msgs::Path2D& plan) override;
|
||||||
void setMeasuredVelocity(const robot_geometry_msgs::Twist& velocity) override;
|
void setMeasuredVelocity(const robot_geometry_msgs::Twist& velocity) override;
|
||||||
|
bool measuredVelocity(robot_geometry_msgs::Twist& out) const override;
|
||||||
bool setTwistLinear(const robot_geometry_msgs::Vector3& linear) override;
|
bool setTwistLinear(const robot_geometry_msgs::Vector3& linear) override;
|
||||||
bool setTwistAngular(const robot_geometry_msgs::Vector3& angular) override;
|
bool setTwistAngular(const robot_geometry_msgs::Vector3& angular) override;
|
||||||
std::string activeController() const override;
|
std::string activeController() const override;
|
||||||
@@ -181,6 +182,13 @@ public:
|
|||||||
return controllers_.size();
|
return controllers_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief ControlLoop đã làm mới snapshot costmap đầu cycle (qua EnvironmentPort::refresh) thì runner
|
||||||
|
/// không làm lại trước computeVelocityCommands/isGoalReached/setPlan — một lưới cho cả cycle (P0b).
|
||||||
|
void setSnapshotRefreshedByLoop(bool refreshed)
|
||||||
|
{
|
||||||
|
snapshot_refreshed_by_loop_ = refreshed;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// @brief Một plugin đã nạp: factory phải sống cùng instance — vứt factory là để `.so` unload.
|
/// @brief Một plugin đã nạp: factory phải sống cùng instance — vứt factory là để `.so` unload.
|
||||||
struct Loaded
|
struct Loaded
|
||||||
@@ -222,6 +230,8 @@ private:
|
|||||||
|
|
||||||
/// Khoá costmap mutex quanh lời gọi plugin. Chỉ unit test với costmap placeholder mới được tắt.
|
/// Khoá costmap mutex quanh lời gọi plugin. Chỉ unit test với costmap placeholder mới được tắt.
|
||||||
bool costmap_lock_enabled_ = true;
|
bool costmap_lock_enabled_ = true;
|
||||||
|
/// true = ControlLoop đã refreshSnapshot() một lần đầu cycle qua EnvironmentPort; runner không làm lại.
|
||||||
|
bool snapshot_refreshed_by_loop_ = false;
|
||||||
|
|
||||||
/// Telemetry non-owning, null = tắt đo. Chỉ đọc sau khi @ref attachStats.
|
/// Telemetry non-owning, null = tắt đo. Chỉ đọc sau khi @ref attachStats.
|
||||||
RuntimeStats* stats_ = nullptr;
|
RuntimeStats* stats_ = nullptr;
|
||||||
@@ -266,6 +276,7 @@ private:
|
|||||||
bool has_limit_angular_ = false;
|
bool has_limit_angular_ = false;
|
||||||
|
|
||||||
robot_geometry_msgs::Twist measured_velocity_;
|
robot_geometry_msgs::Twist measured_velocity_;
|
||||||
|
bool has_measured_velocity_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace move_base2
|
} // namespace move_base2
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ void readBinding(robot::NodeHandle& nh, const std::string& ns, ProfileBinding& b
|
|||||||
|
|
||||||
void readSensors(robot::NodeHandle& nh, SensorGatewayConfig& sensors);
|
void readSensors(robot::NodeHandle& nh, SensorGatewayConfig& sensors);
|
||||||
void readDebug(robot::NodeHandle& nh, MoveBase2Config& config);
|
void readDebug(robot::NodeHandle& nh, MoveBase2Config& config);
|
||||||
|
void readEnvMonitor(robot::NodeHandle& nh, MoveBase2Config& config);
|
||||||
|
|
||||||
void readSensorStore(robot::NodeHandle& nh, MoveBase2Config& config);
|
void readSensorStore(robot::NodeHandle& nh, MoveBase2Config& config);
|
||||||
|
|
||||||
@@ -139,94 +140,6 @@ void readDockingMarkerProfiles(robot::NodeHandle& nh, DockingMarkerProfiles& pro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Đọc khối `path_monitor`.
|
|
||||||
*
|
|
||||||
* Khối vắng mặt = giám sát tuyến TẮT, hành vi y hệt trước 2026-08-04d. Đây là đường lùi thứ nhất
|
|
||||||
* (thứ hai là route `path_blocked`/`off_path` rỗng) — hai nút độc lập, cố ý.
|
|
||||||
*/
|
|
||||||
void readPathMonitor(robot::NodeHandle& nh, ControlLoopConfig::PathMonitorConfig& monitor)
|
|
||||||
{
|
|
||||||
robot::NodeHandle monitor_nh(nh, "path_monitor");
|
|
||||||
|
|
||||||
readBool(monitor_nh, "enabled", monitor.enabled);
|
|
||||||
readBool(monitor_nh, "path_blocked_enabled", monitor.path_blocked_enabled);
|
|
||||||
readBool(monitor_nh, "off_path_enabled", monitor.off_path_enabled);
|
|
||||||
readDouble(monitor_nh, "check_period", monitor.check_period);
|
|
||||||
readDouble(monitor_nh, "confirm_time", monitor.confirm_time);
|
|
||||||
readDouble(monitor_nh, "horizon", monitor.horizon);
|
|
||||||
readDouble(monitor_nh, "min_detour_distance", monitor.min_detour_distance);
|
|
||||||
readDouble(monitor_nh, "max_cross_track", monitor.max_cross_track);
|
|
||||||
readDouble(monitor_nh, "grace_after_new_plan", monitor.grace_after_new_plan);
|
|
||||||
|
|
||||||
// Phép đo 3 (2026-09-03): thân sắp chạm vật cản THẬT trên cung lệnh. Khoá riêng, không dùng chung
|
|
||||||
// tham số nào với hai phép đo trên.
|
|
||||||
readBool(monitor_nh, "collision_enabled", monitor.collision_enabled);
|
|
||||||
readDouble(monitor_nh, "collision_lookahead", monitor.collision_lookahead);
|
|
||||||
monitor_nh.param("collision_max_samples", monitor.collision_max_samples,
|
|
||||||
monitor.collision_max_samples);
|
|
||||||
readDouble(monitor_nh, "collision_stop_distance", monitor.collision_stop_distance);
|
|
||||||
monitor_nh.param("collision_confirm_cycles", monitor.collision_confirm_cycles,
|
|
||||||
monitor.collision_confirm_cycles);
|
|
||||||
monitor_nh.param("collision_cost_threshold", monitor.collision_cost_threshold,
|
|
||||||
monitor.collision_cost_threshold);
|
|
||||||
readDouble(monitor_nh, "collision_front_offset", monitor.collision_front_offset);
|
|
||||||
readDouble(monitor_nh, "collision_rear_offset", monitor.collision_rear_offset);
|
|
||||||
readDouble(monitor_nh, "collision_side_offset", monitor.collision_side_offset);
|
|
||||||
readDouble(monitor_nh, "collision_footprint_padding", monitor.collision_footprint_padding);
|
|
||||||
readBool(monitor_nh, "collision_exclude_baseline", monitor.collision_exclude_baseline);
|
|
||||||
readDouble(monitor_nh, "collision_min_speed", monitor.collision_min_speed);
|
|
||||||
readDouble(monitor_nh, "collision_stop_time", monitor.collision_stop_time);
|
|
||||||
readDouble(monitor_nh, "collision_stop_angle", monitor.collision_stop_angle);
|
|
||||||
readDouble(monitor_nh, "collision_lookahead_angle", monitor.collision_lookahead_angle);
|
|
||||||
readDouble(monitor_nh, "collision_brake_decel", monitor.collision_brake_decel);
|
|
||||||
|
|
||||||
// Phép đo 4 (2026-09-04): planner đòi đi mà robot không tiến.
|
|
||||||
readBool(monitor_nh, "progress_enabled", monitor.progress_enabled);
|
|
||||||
readString(monitor_nh, "progress_action", monitor.progress_action);
|
|
||||||
readDouble(monitor_nh, "progress_window", monitor.progress_window);
|
|
||||||
readDouble(monitor_nh, "progress_min_commanded", monitor.progress_min_commanded);
|
|
||||||
readDouble(monitor_nh, "progress_min_efficiency", monitor.progress_min_efficiency);
|
|
||||||
readDouble(monitor_nh, "progress_clear_efficiency", monitor.progress_clear_efficiency);
|
|
||||||
readDouble(monitor_nh, "progress_confirm_time", monitor.progress_confirm_time);
|
|
||||||
readDouble(monitor_nh, "progress_goal_zone", monitor.progress_goal_zone);
|
|
||||||
|
|
||||||
std::vector<std::string> names;
|
|
||||||
monitor_nh.param("profiles", names, std::vector<std::string>{});
|
|
||||||
if (names.empty())
|
|
||||||
{
|
|
||||||
return; // Giữ mặc định: chỉ profile position.
|
|
||||||
}
|
|
||||||
|
|
||||||
monitor.profiles.clear();
|
|
||||||
for (const std::string& name : names)
|
|
||||||
{
|
|
||||||
if (name == "position")
|
|
||||||
{
|
|
||||||
monitor.profiles.push_back(MotionProfile::kPosition);
|
|
||||||
}
|
|
||||||
else if (name == "docking")
|
|
||||||
{
|
|
||||||
monitor.profiles.push_back(MotionProfile::kDocking);
|
|
||||||
}
|
|
||||||
else if (name == "go_straight")
|
|
||||||
{
|
|
||||||
monitor.profiles.push_back(MotionProfile::kGoStraight);
|
|
||||||
}
|
|
||||||
else if (name == "rotate")
|
|
||||||
{
|
|
||||||
monitor.profiles.push_back(MotionProfile::kRotate);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Không đoán: tên lạ mà im lặng bỏ qua thì người vận hành tưởng đã bật giám sát cho profile
|
|
||||||
// đó. Cảnh báo rồi bỏ — profile lạ không thể tự sinh ra hành vi nào.
|
|
||||||
robot::log_warning("[move_base2] path_monitor/profiles: unknown profile '%s', ignored.\n",
|
|
||||||
name.c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Các tham số chung của move_base2, dùng chung cho schema namespace và schema root-profile.
|
/// Các tham số chung của move_base2, dùng chung cho schema namespace và schema root-profile.
|
||||||
void readMoveBase2Fields(robot::NodeHandle& nh, MoveBase2Config& config)
|
void readMoveBase2Fields(robot::NodeHandle& nh, MoveBase2Config& config)
|
||||||
{
|
{
|
||||||
@@ -270,13 +183,11 @@ void readMoveBase2Fields(robot::NodeHandle& nh, MoveBase2Config& config)
|
|||||||
readSensors(nh, config.sensors);
|
readSensors(nh, config.sensors);
|
||||||
readSensorStore(nh, config);
|
readSensorStore(nh, config);
|
||||||
readDebug(nh, config);
|
readDebug(nh, config);
|
||||||
readPathMonitor(nh, config.path_monitor);
|
readEnvMonitor(nh, config);
|
||||||
readBool(nh, "docking_requires_marker", config.docking_requires_marker);
|
|
||||||
|
|
||||||
readString(nh, "recovery_namespace", config.recovery_namespace);
|
readString(nh, "recovery_namespace", config.recovery_namespace);
|
||||||
readString(nh, "action_namespace", config.action_namespace);
|
readString(nh, "action_namespace", config.action_namespace);
|
||||||
readString(nh, "mission_namespace", config.mission_namespace);
|
readString(nh, "mission_namespace", config.mission_namespace);
|
||||||
readBool(nh, "mission_layer_enabled", config.mission_layer_enabled);
|
|
||||||
readString(nh, "backup_global_planner", config.backup_global_planner_name);
|
readString(nh, "backup_global_planner", config.backup_global_planner_name);
|
||||||
readString(nh, "global_frame", config.global_frame);
|
readString(nh, "global_frame", config.global_frame);
|
||||||
readString(nh, "robot_base_frame", config.robot_base_frame);
|
readString(nh, "robot_base_frame", config.robot_base_frame);
|
||||||
@@ -320,6 +231,129 @@ void readDebug(robot::NodeHandle& nh, MoveBase2Config& config)
|
|||||||
readBool(debug_nh, "state", config.debug_state);
|
readBool(debug_nh, "state", config.debug_state);
|
||||||
readBool(debug_nh, "plan", config.debug_plan);
|
readBool(debug_nh, "plan", config.debug_plan);
|
||||||
readBool(debug_nh, "skip", config.debug_skip);
|
readBool(debug_nh, "skip", config.debug_skip);
|
||||||
|
readBool(debug_nh, "env", config.debug_env);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Đọc khối `env_monitor` (xem config/runtime/env_monitor_params.yaml).
|
||||||
|
void readEnvMonitor(robot::NodeHandle& nh, MoveBase2Config& config)
|
||||||
|
{
|
||||||
|
robot::NodeHandle env_nh(nh, "env_monitor");
|
||||||
|
EnvMonitorConfig& m = config.env_monitor;
|
||||||
|
EnvSamplingConfig& s = config.env_sampling;
|
||||||
|
|
||||||
|
readBool(env_nh, "enabled", m.enabled);
|
||||||
|
readDouble(env_nh, "grace_after_new_plan", m.grace_after_new_plan);
|
||||||
|
readDouble(env_nh, "check_period", s.check_period);
|
||||||
|
|
||||||
|
robot::NodeHandle body_nh(env_nh, "body");
|
||||||
|
readDouble(body_nh, "front_offset", s.body_front);
|
||||||
|
readDouble(body_nh, "rear_offset", s.body_rear);
|
||||||
|
readDouble(body_nh, "side_offset", s.body_side);
|
||||||
|
readDouble(body_nh, "padding", s.padding);
|
||||||
|
readInt(body_nh, "lethal_threshold", s.lethal_threshold);
|
||||||
|
|
||||||
|
robot::NodeHandle geo_nh(env_nh, "geometry");
|
||||||
|
readDouble(geo_nh, "horizon", s.horizon);
|
||||||
|
readDouble(geo_nh, "corridor_margin", s.corridor_margin);
|
||||||
|
readDouble(geo_nh, "corridor_cap", s.corridor_cap);
|
||||||
|
readDouble(geo_nh, "target_backoff", s.target_backoff);
|
||||||
|
readDouble(geo_nh, "robot_snap", s.robot_snap);
|
||||||
|
readDouble(geo_nh, "track_back", s.track_back);
|
||||||
|
readDouble(geo_nh, "track_ahead", s.track_ahead);
|
||||||
|
readDouble(geo_nh, "exit_ahead", s.exit_ahead);
|
||||||
|
readDouble(geo_nh, "target_snap", s.target_snap);
|
||||||
|
|
||||||
|
robot::NodeHandle stall_nh(env_nh, "stall");
|
||||||
|
readBool(stall_nh, "enabled", m.stall.enabled);
|
||||||
|
readString(stall_nh, "action", m.stall.action);
|
||||||
|
readDouble(stall_nh, "window_s", m.stall.window_s);
|
||||||
|
readDouble(stall_nh, "window_m", m.stall.window_m);
|
||||||
|
readDouble(stall_nh, "min_commanded", m.stall.min_commanded);
|
||||||
|
readDouble(stall_nh, "eff_on", m.stall.eff_on);
|
||||||
|
readDouble(stall_nh, "eff_off", m.stall.eff_off);
|
||||||
|
readDouble(stall_nh, "confirm", m.stall.confirm);
|
||||||
|
readDouble(stall_nh, "goal_zone", m.stall.goal_zone);
|
||||||
|
readDouble(stall_nh, "slip_on", m.stall.slip_on);
|
||||||
|
readDouble(stall_nh, "slip_time", m.stall.slip_time);
|
||||||
|
readDouble(stall_nh, "min_commanded_since_goal", m.stall.min_commanded_since_goal);
|
||||||
|
readDouble(stall_nh, "retry_backoff", m.stall.retry_backoff);
|
||||||
|
readDouble(stall_nh, "jump_reject", m.stall.jump_reject);
|
||||||
|
readDouble(stall_nh, "disp_window_s", m.stall.disp_window_s);
|
||||||
|
readDouble(stall_nh, "disp_min", m.stall.disp_min);
|
||||||
|
readDouble(stall_nh, "disp_min_commanded", m.stall.disp_min_commanded);
|
||||||
|
readDouble(stall_nh, "idle_window_s", m.stall.idle_window_s);
|
||||||
|
readDouble(stall_nh, "confine_window_s", m.stall.confine_window_s);
|
||||||
|
readDouble(stall_nh, "confine_radius", m.stall.confine_radius);
|
||||||
|
readDouble(stall_nh, "confine_cmd_factor", m.stall.confine_cmd_factor);
|
||||||
|
|
||||||
|
robot::NodeHandle unreach_nh(env_nh, "unreachable");
|
||||||
|
readBool(unreach_nh, "enabled", m.unreachable.enabled);
|
||||||
|
readString(unreach_nh, "action", m.unreachable.action);
|
||||||
|
readDouble(unreach_nh, "static_on", m.unreachable.static_on);
|
||||||
|
readDouble(unreach_nh, "clean_start_margin", m.unreachable.clean_start_margin);
|
||||||
|
readDouble(unreach_nh, "retry_backoff", m.unreachable.retry_backoff);
|
||||||
|
|
||||||
|
robot::NodeHandle contact_nh(env_nh, "contact");
|
||||||
|
readBool(contact_nh, "enabled", m.contact.enabled);
|
||||||
|
readString(contact_nh, "action", m.contact.action);
|
||||||
|
readBool(contact_nh, "apply_command", m.contact.apply_command);
|
||||||
|
readString(contact_nh, "use_local_plan", s.use_local_plan);
|
||||||
|
readDouble(contact_nh, "local_plan_start_tol", s.local_plan_start_tol);
|
||||||
|
readDouble(contact_nh, "lookahead", s.contact_lookahead);
|
||||||
|
readInt(contact_nh, "confirm_cycles", m.contact.confirm_cycles);
|
||||||
|
readDouble(contact_nh, "trigger_static_time", m.contact.trigger_static_time);
|
||||||
|
readDouble(contact_nh, "clean_start_margin", m.contact.clean_start_margin);
|
||||||
|
readDouble(contact_nh, "brake_decel", m.contact.brake_decel);
|
||||||
|
readDouble(contact_nh, "min_speed", m.contact.min_speed);
|
||||||
|
readDouble(contact_nh, "min_check_speed", m.contact.min_check_speed);
|
||||||
|
readDouble(contact_nh, "release_hysteresis", m.contact.release_hysteresis);
|
||||||
|
readInt(contact_nh, "release_confirm_cycles", m.contact.release_confirm_cycles);
|
||||||
|
readDouble(contact_nh, "envelope_margin", m.contact.envelope_margin);
|
||||||
|
readDouble(contact_nh, "standdown_min_travel", m.contact.standdown_min_travel);
|
||||||
|
readDouble(contact_nh, "standdown_travel", m.contact.standdown_travel);
|
||||||
|
readBool(contact_nh, "envelope", m.contact.envelope);
|
||||||
|
|
||||||
|
robot::NodeHandle budget_nh(env_nh, "replan_budget");
|
||||||
|
readDouble(budget_nh, "min_travel", m.replan.min_travel);
|
||||||
|
readDouble(budget_nh, "escalate_factor", m.replan.escalate_factor);
|
||||||
|
readDouble(budget_nh, "escalate_window_m", m.replan.escalate_window_m);
|
||||||
|
readDouble(budget_nh, "escalate_max", m.replan.escalate_max);
|
||||||
|
|
||||||
|
robot::NodeHandle logger_nh(env_nh, "logger");
|
||||||
|
readBool(logger_nh, "enabled", config.env_logger.enabled);
|
||||||
|
readString(logger_nh, "log_dir", config.env_logger.log_dir);
|
||||||
|
readDouble(logger_nh, "flush_period", config.env_logger.flush_period);
|
||||||
|
|
||||||
|
std::vector<std::string> names;
|
||||||
|
env_nh.param("profiles", names, std::vector<std::string>{});
|
||||||
|
if (!names.empty())
|
||||||
|
{
|
||||||
|
s.profiles.clear();
|
||||||
|
for (const std::string& name : names)
|
||||||
|
{
|
||||||
|
if (name == "position")
|
||||||
|
{
|
||||||
|
s.profiles.push_back(MotionProfile::kPosition);
|
||||||
|
}
|
||||||
|
else if (name == "docking")
|
||||||
|
{
|
||||||
|
s.profiles.push_back(MotionProfile::kDocking);
|
||||||
|
}
|
||||||
|
else if (name == "go_straight")
|
||||||
|
{
|
||||||
|
s.profiles.push_back(MotionProfile::kGoStraight);
|
||||||
|
}
|
||||||
|
else if (name == "rotate")
|
||||||
|
{
|
||||||
|
s.profiles.push_back(MotionProfile::kRotate);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
robot::log_warning("[move_base2] env_monitor/profiles: unknown profile '%s', ignored.\n",
|
||||||
|
name.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Đọc cấu hình đường vào cảm biến từ namespace con `sensors`.
|
/// Đọc cấu hình đường vào cảm biến từ namespace con `sensors`.
|
||||||
@@ -393,164 +427,6 @@ bool MoveBase2Config::validate(std::string& error) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path_monitor.enabled)
|
|
||||||
{
|
|
||||||
// Chỉ kiểm khi BẬT: tham số vô nghĩa trong một khối đang tắt không có lý do gì để chặn boot.
|
|
||||||
if (!std::isfinite(path_monitor.check_period) || path_monitor.check_period <= 0.0)
|
|
||||||
{
|
|
||||||
error = "path_monitor/check_period must be > 0 [s]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.confirm_time) || path_monitor.confirm_time < 0.0)
|
|
||||||
{
|
|
||||||
error = "path_monitor/confirm_time must be >= 0 [s]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.horizon) || path_monitor.horizon < 0.0)
|
|
||||||
{
|
|
||||||
error = "path_monitor/horizon must be >= 0 [m] (0 = the blocked-path check is off)";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.min_detour_distance) || path_monitor.min_detour_distance < 0.0)
|
|
||||||
{
|
|
||||||
error = "path_monitor/min_detour_distance must be >= 0 [m]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.max_cross_track) || path_monitor.max_cross_track < 0.0)
|
|
||||||
{
|
|
||||||
error = "path_monitor/max_cross_track must be >= 0 [m] (0 = the off-path check is off)";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.grace_after_new_plan) ||
|
|
||||||
path_monitor.grace_after_new_plan < 0.0)
|
|
||||||
{
|
|
||||||
error = "path_monitor/grace_after_new_plan must be >= 0 [s]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (path_monitor.collision_enabled)
|
|
||||||
{
|
|
||||||
const auto bad = [](double v) { return !std::isfinite(v) || v < 0.0; };
|
|
||||||
if (!std::isfinite(path_monitor.collision_lookahead) || path_monitor.collision_lookahead <= 0.0)
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_lookahead must be > 0 [m]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (path_monitor.collision_max_samples < 1)
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_max_samples must be >= 1";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (bad(path_monitor.collision_stop_distance) ||
|
|
||||||
path_monitor.collision_stop_distance > path_monitor.collision_lookahead)
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_stop_distance must be in [0, collision_lookahead] [m]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (path_monitor.collision_confirm_cycles < 1)
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_confirm_cycles must be >= 1";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (path_monitor.collision_cost_threshold < 1 || path_monitor.collision_cost_threshold > 254)
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_cost_threshold must be in [1, 254] (254 = LETHAL only)";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (bad(path_monitor.collision_front_offset) || bad(path_monitor.collision_rear_offset) ||
|
|
||||||
bad(path_monitor.collision_side_offset) || bad(path_monitor.collision_footprint_padding))
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_*_offset and collision_footprint_padding must be >= 0 [m]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
{
|
|
||||||
// Cả ba = 0: lấy thân từ footprint costmap lúc chạy. Khai tay thì phải đủ cả ba > 0 — mũi = 0
|
|
||||||
// là chỉ dò tâm (dừng khi tâm đã ở trong tường), hông = 0 là bỏ hai cạnh hông.
|
|
||||||
const int declared = (path_monitor.collision_front_offset > 0.0 ? 1 : 0) +
|
|
||||||
(path_monitor.collision_rear_offset > 0.0 ? 1 : 0) +
|
|
||||||
(path_monitor.collision_side_offset > 0.0 ? 1 : 0);
|
|
||||||
if (declared != 0 && declared != 3)
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_front_offset, collision_rear_offset and "
|
|
||||||
"collision_side_offset must either all be 0 (take the body from the costmap "
|
|
||||||
"footprint) or all be > 0 [m]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.collision_stop_time) || path_monitor.collision_stop_time <= 0.0)
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_stop_time must be > 0 [s]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.collision_stop_angle) || path_monitor.collision_stop_angle <= 0.0 ||
|
|
||||||
path_monitor.collision_stop_angle > M_PI)
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_stop_angle must be in (0, pi] [rad]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.collision_lookahead_angle) ||
|
|
||||||
path_monitor.collision_lookahead_angle < path_monitor.collision_stop_angle ||
|
|
||||||
path_monitor.collision_lookahead_angle > M_PI)
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_lookahead_angle must be in [collision_stop_angle, pi] [rad]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (bad(path_monitor.collision_min_speed) || bad(path_monitor.collision_brake_decel))
|
|
||||||
{
|
|
||||||
error = "path_monitor/collision_min_speed [m/s] and collision_brake_decel [m/s^2] must be >= 0";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (path_monitor.progress_enabled)
|
|
||||||
{
|
|
||||||
if (path_monitor.progress_action != "log" && path_monitor.progress_action != "recover")
|
|
||||||
{
|
|
||||||
error = "path_monitor/progress_action must be 'log' or 'recover'";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.progress_window) || path_monitor.progress_window <= 0.0)
|
|
||||||
{
|
|
||||||
error = "path_monitor/progress_window must be > 0 [s]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.progress_min_commanded) ||
|
|
||||||
path_monitor.progress_min_commanded <= 0.0)
|
|
||||||
{
|
|
||||||
// 0 là đo cả lúc đứng yên: cmd = 0, eff = 0/0 -> mọi lần chờ hợp lệ đều thành "kẹt".
|
|
||||||
error = "path_monitor/progress_min_commanded must be > 0 [m]";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.progress_min_efficiency) ||
|
|
||||||
!std::isfinite(path_monitor.progress_clear_efficiency) ||
|
|
||||||
path_monitor.progress_min_efficiency < 0.0 ||
|
|
||||||
path_monitor.progress_clear_efficiency <= path_monitor.progress_min_efficiency)
|
|
||||||
{
|
|
||||||
// clear == min là cờ nhấp nháy quanh một ngưỡng: bật, xoá, bật... mỗi lượt quét.
|
|
||||||
error = "path_monitor/progress_clear_efficiency must be > progress_min_efficiency >= 0";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!std::isfinite(path_monitor.progress_confirm_time) ||
|
|
||||||
path_monitor.progress_confirm_time < 0.0 ||
|
|
||||||
!std::isfinite(path_monitor.progress_goal_zone) || path_monitor.progress_goal_zone < 0.0)
|
|
||||||
{
|
|
||||||
error = "path_monitor/progress_confirm_time [s] and progress_goal_zone [m] must be >= 0";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!(path_monitor.horizon > 0.0) && !(path_monitor.max_cross_track > 0.0) &&
|
|
||||||
!path_monitor.collision_enabled && !path_monitor.progress_enabled)
|
|
||||||
{
|
|
||||||
// Bật khối mà tắt cả bốn phép đo là mâu thuẫn: người khai tưởng giám sát đang chạy.
|
|
||||||
error = "path_monitor is enabled but all checks are off; set horizon > 0 [m], "
|
|
||||||
"max_cross_track > 0 [m], collision_enabled: true and/or progress_enabled: true, "
|
|
||||||
"or set enabled: false";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (path_monitor.profiles.empty())
|
|
||||||
{
|
|
||||||
error = "path_monitor/profiles is empty; no leg would ever be monitored";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!std::isfinite(runtime_stats_period) || runtime_stats_period < 0.0)
|
if (!std::isfinite(runtime_stats_period) || runtime_stats_period < 0.0)
|
||||||
{
|
{
|
||||||
error = "runtime_stats_period must be >= 0 [s] (0 = telemetry off)";
|
error = "runtime_stats_period must be >= 0 [s] (0 = telemetry off)";
|
||||||
@@ -562,10 +438,10 @@ bool MoveBase2Config::validate(std::string& error) const
|
|||||||
error = "recovery_namespace is empty";
|
error = "recovery_namespace is empty";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (mission_layer_enabled && mission_namespace.empty())
|
if (mission_namespace.empty())
|
||||||
{
|
{
|
||||||
error = "mission_layer_enabled is true but mission_namespace is empty — there would be no "
|
error = "mission_namespace is empty — there would be no namespace to read `mission_sources` "
|
||||||
"namespace to read `mission_sources` from";
|
"from, and the mission layer is always built";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (global_frame.empty() || robot_base_frame.empty())
|
if (global_frame.empty() || robot_base_frame.empty())
|
||||||
@@ -594,6 +470,14 @@ bool MoveBase2Config::validate(std::string& error) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (env_monitor.enabled)
|
||||||
|
{
|
||||||
|
if (!env_monitor.validate(error) || !env_sampling.validate(error) || !env_logger.validate(error))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!velocity.validate(error))
|
if (!velocity.validate(error))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -645,16 +529,12 @@ std::string MoveBase2Config::describe() const
|
|||||||
out << " require_current_costmap: " << (require_current_costmap ? "true" : "false") << "\n";
|
out << " require_current_costmap: " << (require_current_costmap ? "true" : "false") << "\n";
|
||||||
out << " namespaces: recovery='" << recovery_namespace << "' actions='" << action_namespace
|
out << " namespaces: recovery='" << recovery_namespace << "' actions='" << action_namespace
|
||||||
<< "' mission='" << mission_namespace << "'\n";
|
<< "' mission='" << mission_namespace << "'\n";
|
||||||
out << " mission_layer_enabled: " << (mission_layer_enabled ? "true" : "false")
|
|
||||||
<< (mission_layer_enabled ? " (orders are split into legs)\n"
|
|
||||||
: " (orders go straight down as one goal)\n");
|
|
||||||
describeBinding(out, "position", position);
|
describeBinding(out, "position", position);
|
||||||
describeBinding(out, "docking", docking);
|
describeBinding(out, "docking", docking);
|
||||||
for (const auto& entry : docking_marker_profiles)
|
for (const auto& entry : docking_marker_profiles)
|
||||||
{
|
{
|
||||||
describeBinding(out, ("docking marker '" + entry.first + "'").c_str(), entry.second);
|
describeBinding(out, ("docking marker '" + entry.first + "'").c_str(), entry.second);
|
||||||
}
|
}
|
||||||
out << " docking_requires_marker: " << (docking_requires_marker ? "true" : "false") << '\n';
|
|
||||||
describeBinding(out, "go_straight", go_straight);
|
describeBinding(out, "go_straight", go_straight);
|
||||||
describeBinding(out, "rotate", rotate);
|
describeBinding(out, "rotate", rotate);
|
||||||
out << " backup_global_planner: '" << backup_global_planner_name << "'"
|
out << " backup_global_planner: '" << backup_global_planner_name << "'"
|
||||||
@@ -705,12 +585,8 @@ void MoveBase2Config::fromLegacyNodeHandle(robot::NodeHandle& nh)
|
|||||||
// khi observation buffer bị cấu hình sai, mặc định vẫn là bật.
|
// khi observation buffer bị cấu hình sai, mặc định vẫn là bật.
|
||||||
readBool(nh, "require_current_costmap", require_current_costmap);
|
readBool(nh, "require_current_costmap", require_current_costmap);
|
||||||
|
|
||||||
// Cũng không có trong schema gen-1: gen-1 không có mission layer nên không có khoá tương ứng để
|
// Không có trong schema gen-1: gen-1 không có mission layer nên không có khoá tương ứng để dịch.
|
||||||
// dịch. Đọc ở đây để tắt/bật được ngay trên cây config đang chạy mà không phải chuyển schema —
|
|
||||||
// đây là đường lùi khi mission layer gây vấn đề trên hiện trường.
|
|
||||||
readBool(nh, "mission_layer_enabled", mission_layer_enabled);
|
|
||||||
readString(nh, "mission_namespace", mission_namespace);
|
readString(nh, "mission_namespace", mission_namespace);
|
||||||
readBool(nh, "docking_requires_marker", docking_requires_marker);
|
|
||||||
|
|
||||||
std::string root_global_planner;
|
std::string root_global_planner;
|
||||||
readString(nh, "base_global_planner", root_global_planner);
|
readString(nh, "base_global_planner", root_global_planner);
|
||||||
@@ -841,8 +717,10 @@ ControlLoopConfig MoveBase2Config::toControlLoopConfig() const
|
|||||||
config.go_straight = go_straight;
|
config.go_straight = go_straight;
|
||||||
config.rotate = rotate;
|
config.rotate = rotate;
|
||||||
config.backup_global_planner_name = backup_global_planner_name;
|
config.backup_global_planner_name = backup_global_planner_name;
|
||||||
config.docking_requires_marker = docking_requires_marker;
|
config.env_monitor = env_monitor;
|
||||||
config.path_monitor = path_monitor;
|
config.env_sampling = env_sampling;
|
||||||
|
config.env_logger = env_logger;
|
||||||
|
config.debug_env = debug_env;
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
841
src/env_monitor.cpp
Normal file
841
src/env_monitor.cpp
Normal file
@@ -0,0 +1,841 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* move_base2 — cài đặt lõi EnvMonitor (xem header).
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#include <move_base2/core/env_monitor.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace move_base2
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
bool validAction(const std::string& action)
|
||||||
|
{
|
||||||
|
return action == "log" || action == "recover";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool finitePositive(double v)
|
||||||
|
{
|
||||||
|
return std::isfinite(v) && v > 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool finiteNonNegative(double v)
|
||||||
|
{
|
||||||
|
return std::isfinite(v) && v >= 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------ config
|
||||||
|
|
||||||
|
bool EnvMonitorConfig::validate(std::string& error) const
|
||||||
|
{
|
||||||
|
if (!finiteNonNegative(grace_after_new_plan))
|
||||||
|
{
|
||||||
|
error = "env_monitor/grace_after_new_plan must be >= 0 [s]";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!validAction(stall.action) || !validAction(unreachable.action) || !validAction(contact.action))
|
||||||
|
{
|
||||||
|
error = "env_monitor/*/action must be \"log\" or \"recover\"";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!finitePositive(stall.window_s) || !finitePositive(stall.window_m))
|
||||||
|
{
|
||||||
|
error = "env_monitor/stall/window_s and window_m must be > 0";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!finitePositive(stall.min_commanded))
|
||||||
|
{
|
||||||
|
error = "env_monitor/stall/min_commanded must be > 0 [m] (0 would turn waiting into stalling)";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!std::isfinite(stall.eff_on) || !std::isfinite(stall.eff_off) || stall.eff_off <= stall.eff_on)
|
||||||
|
{
|
||||||
|
error = "env_monitor/stall/eff_off must be > eff_on (hysteresis)";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!finiteNonNegative(stall.confirm) || !finiteNonNegative(stall.goal_zone) ||
|
||||||
|
!finiteNonNegative(stall.slip_on) || !finiteNonNegative(stall.slip_time) ||
|
||||||
|
!finiteNonNegative(stall.min_commanded_since_goal) || !finiteNonNegative(stall.retry_backoff) ||
|
||||||
|
!finiteNonNegative(stall.jump_reject) || !finitePositive(stall.disp_window_s) ||
|
||||||
|
!finiteNonNegative(stall.disp_min) || !finiteNonNegative(stall.disp_min_commanded) ||
|
||||||
|
!finiteNonNegative(stall.idle_window_s) || !finiteNonNegative(stall.confine_radius) ||
|
||||||
|
!finitePositive(stall.confine_window_s) || !std::isfinite(stall.confine_cmd_factor) ||
|
||||||
|
stall.confine_cmd_factor < 1.0)
|
||||||
|
{
|
||||||
|
error = "env_monitor/stall: confirm, goal_zone, slip_on, slip_time, min_commanded_since_goal, "
|
||||||
|
"retry_backoff must be >= 0";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!finiteNonNegative(unreachable.static_on) || !finitePositive(unreachable.clean_start_margin) ||
|
||||||
|
!finiteNonNegative(unreachable.retry_backoff))
|
||||||
|
{
|
||||||
|
error = "env_monitor/unreachable: static_on >= 0 [s], clean_start_margin > 0 [m], retry_backoff >= 0";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (contact.confirm_cycles < 1)
|
||||||
|
{
|
||||||
|
error = "env_monitor/contact/confirm_cycles must be >= 1";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!finiteNonNegative(contact.trigger_static_time) || !finitePositive(contact.clean_start_margin) ||
|
||||||
|
!finitePositive(contact.brake_decel) || !finiteNonNegative(contact.min_speed) ||
|
||||||
|
!finiteNonNegative(contact.min_check_speed) || !finiteNonNegative(contact.release_hysteresis) ||
|
||||||
|
contact.release_confirm_cycles < 1 || !finiteNonNegative(contact.envelope_margin) ||
|
||||||
|
!finiteNonNegative(contact.standdown_min_travel) || !finiteNonNegative(contact.standdown_travel))
|
||||||
|
{
|
||||||
|
error = "env_monitor/contact: trigger_static_time >= 0, clean_start_margin > 0, brake_decel > 0, "
|
||||||
|
"min_speed >= 0, min_check_speed >= 0, release_hysteresis >= 0, release_confirm_cycles >= 1, "
|
||||||
|
"envelope_margin >= 0, standdown_min_travel >= 0, standdown_travel >= 0";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!finiteNonNegative(replan.min_travel) || !std::isfinite(replan.escalate_factor) ||
|
||||||
|
replan.escalate_factor < 1.0 || !finiteNonNegative(replan.escalate_window_m) ||
|
||||||
|
!finiteNonNegative(replan.escalate_max) || (replan.escalate_max > 0.0 && replan.escalate_max < replan.min_travel))
|
||||||
|
{
|
||||||
|
error = "env_monitor/replan_budget: min_travel >= 0, escalate_factor >= 1, escalate_window_m >= 0, "
|
||||||
|
"escalate_max = 0 or >= min_travel";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string EnvMonitorConfig::describe() const
|
||||||
|
{
|
||||||
|
std::ostringstream out;
|
||||||
|
out << "env_monitor " << (enabled ? "ON" : "OFF") << " (grace " << grace_after_new_plan << " s):\n";
|
||||||
|
out << " stall : " << (stall.enabled ? "on" : "off") << " action=" << stall.action
|
||||||
|
<< " window=" << stall.window_s << " s/" << stall.window_m << " m min_cmd=" << stall.min_commanded
|
||||||
|
<< " m eff<" << stall.eff_on << " ket / >=" << stall.eff_off << " xoa confirm=" << stall.confirm
|
||||||
|
<< " s goal_zone=" << stall.goal_zone << " m slip>=" << stall.slip_on << " m/s " << stall.slip_time
|
||||||
|
<< " s cmd_since_goal>=" << stall.min_commanded_since_goal << " m backoff=" << stall.retry_backoff
|
||||||
|
<< " s jump_reject=" << stall.jump_reject << " m disp_gate=" << stall.disp_min << " m/"
|
||||||
|
<< stall.disp_window_s << " s (cmd>=" << stall.disp_min_commanded << " m) idle=" << stall.idle_window_s
|
||||||
|
<< " s confine=" << stall.confine_radius << " m/" << stall.confine_window_s << " s x" << stall.confine_cmd_factor
|
||||||
|
<< "\n";
|
||||||
|
out << " unreachable : " << (unreachable.enabled ? "on" : "off") << " action=" << unreachable.action
|
||||||
|
<< " static_on=" << unreachable.static_on << " s clean_margin=" << unreachable.clean_start_margin
|
||||||
|
<< " m backoff=" << unreachable.retry_backoff << " s\n";
|
||||||
|
out << " contact : " << (contact.enabled ? "on" : "off") << " action=" << contact.action
|
||||||
|
<< " confirm=" << contact.confirm_cycles << " cycles trigger_static=" << contact.trigger_static_time
|
||||||
|
<< " s clean_margin=" << contact.clean_start_margin << " m decel=" << contact.brake_decel
|
||||||
|
<< " m/s^2 envelope=" << (contact.envelope ? "on" : "off") << " min_speed=" << contact.min_speed
|
||||||
|
<< " m/s min_check_speed=" << contact.min_check_speed << " m/s release=" << contact.release_hysteresis
|
||||||
|
<< " m/" << contact.release_confirm_cycles << " cycles envelope_margin=" << contact.envelope_margin
|
||||||
|
<< " m standdown=" << contact.standdown_min_travel << "/" << contact.standdown_travel
|
||||||
|
<< " m apply_command=" << (contact.apply_command ? "YES" : "no (shadow)") << "\n";
|
||||||
|
out << " replan : min_travel=" << replan.min_travel << " m x" << replan.escalate_factor
|
||||||
|
<< " within " << replan.escalate_window_m << " m, max " << replan.escalate_max << " m";
|
||||||
|
return out.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------------------- lifecycle
|
||||||
|
|
||||||
|
bool EnvMonitor::configure(const EnvMonitorConfig& config, std::string& error)
|
||||||
|
{
|
||||||
|
if (!config.validate(error))
|
||||||
|
{
|
||||||
|
configured_ = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
config_ = config;
|
||||||
|
configured_ = true;
|
||||||
|
reset();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnvMonitor::reset()
|
||||||
|
{
|
||||||
|
cmd_accum_ = 0.0;
|
||||||
|
travel_total_ = 0.0;
|
||||||
|
has_last_pose_ = false;
|
||||||
|
has_goal_ = false;
|
||||||
|
cmd_at_goal_ = 0.0;
|
||||||
|
has_plan_time_ = false;
|
||||||
|
clearWindow();
|
||||||
|
stall_seen_ = false;
|
||||||
|
stall_fired_ = false;
|
||||||
|
slip_seen_ = false;
|
||||||
|
has_stall_backoff_ = false;
|
||||||
|
baseline_pending_ = true;
|
||||||
|
baseline_unreachable_ = false;
|
||||||
|
unreach_seen_ = false;
|
||||||
|
unreach_fired_ = false;
|
||||||
|
has_unreach_backoff_ = false;
|
||||||
|
contact_streak_ = 0;
|
||||||
|
release_streak_ = 0;
|
||||||
|
has_prev_heavy_ = false;
|
||||||
|
clearPosWindow();
|
||||||
|
braking_ = false;
|
||||||
|
contact_close_ = false;
|
||||||
|
contact_fired_ = false;
|
||||||
|
detour_from_contact_ = false;
|
||||||
|
detour_mark_travel_ = 0.0;
|
||||||
|
standdown_ = false;
|
||||||
|
standdown_mark_travel_ = 0.0;
|
||||||
|
standdown_clean_streak_ = 0;
|
||||||
|
has_replan_mark_ = false;
|
||||||
|
replan_mark_travel_ = 0.0;
|
||||||
|
budget_travel_ = config_.replan.min_travel;
|
||||||
|
has_prev_replan_ = false;
|
||||||
|
prev_replan_travel_ = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnvMonitor::onGoalAccepted(const robot::Time& now)
|
||||||
|
{
|
||||||
|
(void)now;
|
||||||
|
const double cmd = cmd_accum_;
|
||||||
|
const double travel = travel_total_;
|
||||||
|
const bool had_pose = has_last_pose_;
|
||||||
|
const double lx = last_x_;
|
||||||
|
const double ly = last_y_;
|
||||||
|
reset();
|
||||||
|
// Odometer và lệnh tích luỹ là đơn điệu, chỉ dùng qua hiệu số: giữ lại để mốc không nhảy.
|
||||||
|
cmd_accum_ = cmd;
|
||||||
|
travel_total_ = travel;
|
||||||
|
has_last_pose_ = had_pose;
|
||||||
|
last_x_ = lx;
|
||||||
|
last_y_ = ly;
|
||||||
|
has_goal_ = true;
|
||||||
|
cmd_at_goal_ = cmd_accum_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnvMonitor::onPlanApplied(const robot::Time& now)
|
||||||
|
{
|
||||||
|
has_plan_time_ = true;
|
||||||
|
plan_time_ = now;
|
||||||
|
clearWindow();
|
||||||
|
clearPosWindow(); // cửa sổ dịch chuyển bắt đầu lại: plan mới là một lượt thử mới
|
||||||
|
has_prev_heavy_ = false;
|
||||||
|
stall_seen_ = false;
|
||||||
|
stall_fired_ = false;
|
||||||
|
// Plan mới có baseline riêng: chỉ nổ "mất liên thông" vì thứ xảy ra SAU khi plan được lập (R2).
|
||||||
|
baseline_pending_ = true;
|
||||||
|
baseline_unreachable_ = false;
|
||||||
|
unreach_seen_ = false;
|
||||||
|
unreach_fired_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnvMonitor::onRecoveryFinished(const robot::Time& now, EnvRecoveryOutcome outcome, bool from_contact)
|
||||||
|
{
|
||||||
|
switch (outcome)
|
||||||
|
{
|
||||||
|
case EnvRecoveryOutcome::kSucceededWithPath:
|
||||||
|
{
|
||||||
|
// Detour do chính cờ collision: nếu robot không rời chỗ mà lại "sắp chạm" thì lần sau đứng ra.
|
||||||
|
detour_from_contact_ = from_contact;
|
||||||
|
detour_mark_travel_ = travel_total_;
|
||||||
|
// R3/R5: im tới khi đi đủ budget_travel_ kể từ đây; lặp trong escalate_window_m thì nhân lên.
|
||||||
|
if (has_prev_replan_ && travel_total_ - prev_replan_travel_ < config_.replan.escalate_window_m)
|
||||||
|
{
|
||||||
|
budget_travel_ *= config_.replan.escalate_factor;
|
||||||
|
if (config_.replan.escalate_max > 0.0)
|
||||||
|
{
|
||||||
|
budget_travel_ = std::min(budget_travel_, config_.replan.escalate_max);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
budget_travel_ = config_.replan.min_travel;
|
||||||
|
}
|
||||||
|
has_prev_replan_ = true;
|
||||||
|
prev_replan_travel_ = travel_total_;
|
||||||
|
has_replan_mark_ = true;
|
||||||
|
replan_mark_travel_ = travel_total_;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case EnvRecoveryOutcome::kSucceeded:
|
||||||
|
break;
|
||||||
|
case EnvRecoveryOutcome::kFailed:
|
||||||
|
has_stall_backoff_ = true;
|
||||||
|
stall_backoff_until_ = now + robot::Duration(config_.stall.retry_backoff);
|
||||||
|
has_unreach_backoff_ = true;
|
||||||
|
unreach_backoff_until_ = now + robot::Duration(config_.unreachable.retry_backoff);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Mọi kết cục đều kết thúc một pha dừng: chuỗi xác nhận cũ không còn giá trị.
|
||||||
|
stall_seen_ = false;
|
||||||
|
stall_fired_ = false;
|
||||||
|
unreach_seen_ = false;
|
||||||
|
unreach_fired_ = false;
|
||||||
|
contact_streak_ = 0;
|
||||||
|
release_streak_ = 0;
|
||||||
|
braking_ = false;
|
||||||
|
contact_close_ = false;
|
||||||
|
contact_fired_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------------------------- window
|
||||||
|
|
||||||
|
void EnvMonitor::clearWindow()
|
||||||
|
{
|
||||||
|
ring_head_ = 0;
|
||||||
|
ring_count_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnvMonitor::clearPosWindow()
|
||||||
|
{
|
||||||
|
pos_head_ = 0;
|
||||||
|
pos_count_ = 0;
|
||||||
|
wandering_ = false;
|
||||||
|
idle_ = false;
|
||||||
|
confined_ = false;
|
||||||
|
region_radius_ = std::numeric_limits<double>::quiet_NaN();
|
||||||
|
net_disp_ = std::numeric_limits<double>::quiet_NaN();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EnvMonitor::oldestPos(const robot::Time& now, double window_s, PosSample& out) const
|
||||||
|
{
|
||||||
|
// Mẫu MỚI NHẤT đã đủ tuổi window_s — cùng lý do với oldestInWindow.
|
||||||
|
for (std::size_t k = 1; k <= pos_count_; ++k)
|
||||||
|
{
|
||||||
|
const PosSample& p = pos_ring_[(pos_head_ + kPosRing - k) % kPosRing];
|
||||||
|
if ((now - p.t).toSec() >= window_s)
|
||||||
|
{
|
||||||
|
out = p;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EnvMonitor::oldestInWindow(const robot::Time& now, HeavySample& out) const
|
||||||
|
{
|
||||||
|
// Mẫu MỚI NHẤT đã đủ tuổi window_s hoặc đủ xa window_m lệnh — cửa sổ sát ngưỡng nhất, không phải
|
||||||
|
// mẫu cũ nhất trong ring (cửa sổ giãn ra làm eff mượt giả).
|
||||||
|
for (std::size_t k = 1; k <= ring_count_; ++k)
|
||||||
|
{
|
||||||
|
const HeavySample& s = ring_[(ring_head_ + kRing - k) % kRing];
|
||||||
|
const bool old_enough = (now - s.t).toSec() >= config_.stall.window_s;
|
||||||
|
const bool far_enough = cmd_accum_ - s.cmd >= config_.stall.window_m;
|
||||||
|
if (old_enough || far_enough)
|
||||||
|
{
|
||||||
|
out = s;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------------------------- update
|
||||||
|
|
||||||
|
EnvVerdict EnvMonitor::update(const EnvSample& s)
|
||||||
|
{
|
||||||
|
EnvVerdict v;
|
||||||
|
if (!configured_ || !config_.enabled)
|
||||||
|
{
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tích luỹ lệnh và quãng đường — mọi cửa sổ, cổng, ngân sách đều đo bằng hai con số này.
|
||||||
|
if (s.dt > 0.0 && std::isfinite(s.v_cmd))
|
||||||
|
{
|
||||||
|
cmd_accum_ += std::fabs(s.v_cmd) * s.dt;
|
||||||
|
}
|
||||||
|
if (std::isfinite(s.robot_x) && std::isfinite(s.robot_y))
|
||||||
|
{
|
||||||
|
if (has_last_pose_)
|
||||||
|
{
|
||||||
|
travel_total_ += std::hypot(s.robot_x - last_x_, s.robot_y - last_y_);
|
||||||
|
}
|
||||||
|
last_x_ = s.robot_x;
|
||||||
|
last_y_ = s.robot_y;
|
||||||
|
has_last_pose_ = true;
|
||||||
|
}
|
||||||
|
if (has_replan_mark_)
|
||||||
|
{
|
||||||
|
v.replan_budget_left = std::max(0.0, budget_travel_ - (travel_total_ - replan_mark_travel_));
|
||||||
|
}
|
||||||
|
|
||||||
|
updateContact(s, v);
|
||||||
|
|
||||||
|
if (!s.has_plan || !s.costmap_current)
|
||||||
|
{
|
||||||
|
// Không có gì để đo tiến độ; xoá chuỗi để không nối "đúng liên tục" qua quãng gián đoạn.
|
||||||
|
stall_seen_ = false;
|
||||||
|
stall_fired_ = false;
|
||||||
|
unreach_seen_ = false;
|
||||||
|
unreach_fired_ = false;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
updateStall(s, v);
|
||||||
|
updateUnreachable(s, v);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnvMonitor::updateStall(const EnvSample& s, EnvVerdict& v)
|
||||||
|
{
|
||||||
|
const EnvMonitorConfig::Stall& c = config_.stall;
|
||||||
|
if (!c.enabled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cửa sổ dịch chuyển thực: lấy mẫu vị trí mỗi tick nặng, không phụ thuộc cost/plan/mặt nạ.
|
||||||
|
if (s.geometry_valid && std::isfinite(s.robot_x) && std::isfinite(s.robot_y))
|
||||||
|
{
|
||||||
|
pos_ring_[pos_head_] = PosSample{ s.now, s.robot_x, s.robot_y, cmd_accum_ };
|
||||||
|
pos_head_ = (pos_head_ + 1) % kPosRing;
|
||||||
|
pos_count_ = std::min(pos_count_ + 1, kPosRing);
|
||||||
|
PosSample old;
|
||||||
|
if (oldestPos(s.now, c.disp_window_s, old))
|
||||||
|
{
|
||||||
|
net_disp_ = std::hypot(s.robot_x - old.x, s.robot_y - old.y);
|
||||||
|
wandering_ = (cmd_accum_ - old.cmd >= c.disp_min_commanded) && net_disp_ < c.disp_min;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
net_disp_ = std::numeric_limits<double>::quiet_NaN();
|
||||||
|
wandering_ = false;
|
||||||
|
}
|
||||||
|
// Cổng đứng yên: không cần lệnh đủ — PRIEST bò 0,05 m/s tại chỗ 30 s cũng là kẹt.
|
||||||
|
idle_ = false;
|
||||||
|
PosSample older;
|
||||||
|
if (c.idle_window_s > 0.0 && c.disp_min > 0.0 && oldestPos(s.now, c.idle_window_s, older))
|
||||||
|
{
|
||||||
|
idle_ = std::hypot(s.robot_x - older.x, s.robot_y - older.y) < c.disp_min;
|
||||||
|
}
|
||||||
|
// Cổng giam vùng: bán kính của tập vị trí trong confine_window_s quanh tâm của chúng, so với lệnh tích luỹ.
|
||||||
|
confined_ = false;
|
||||||
|
region_radius_ = std::numeric_limits<double>::quiet_NaN();
|
||||||
|
if (c.confine_radius > 0.0 && c.disp_min > 0.0)
|
||||||
|
{
|
||||||
|
double sx = 0.0;
|
||||||
|
double sy = 0.0;
|
||||||
|
std::size_t n = 0;
|
||||||
|
double oldest_cmd = cmd_accum_;
|
||||||
|
double oldest_age = 0.0;
|
||||||
|
for (std::size_t k = 1; k <= pos_count_; ++k)
|
||||||
|
{
|
||||||
|
const PosSample& p = pos_ring_[(pos_head_ + kPosRing - k) % kPosRing];
|
||||||
|
const double age = (s.now - p.t).toSec();
|
||||||
|
if (age > c.confine_window_s)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sx += p.x;
|
||||||
|
sy += p.y;
|
||||||
|
++n;
|
||||||
|
oldest_cmd = p.cmd;
|
||||||
|
oldest_age = age;
|
||||||
|
}
|
||||||
|
if (n >= 2 && oldest_age >= c.confine_window_s - 0.5)
|
||||||
|
{
|
||||||
|
const double cx = sx / static_cast<double>(n);
|
||||||
|
const double cy = sy / static_cast<double>(n);
|
||||||
|
double radius = 0.0;
|
||||||
|
for (std::size_t k = 1; k <= n; ++k)
|
||||||
|
{
|
||||||
|
const PosSample& p = pos_ring_[(pos_head_ + kPosRing - k) % kPosRing];
|
||||||
|
radius = std::max(radius, std::hypot(p.x - cx, p.y - cy));
|
||||||
|
}
|
||||||
|
region_radius_ = radius;
|
||||||
|
const double cmd = cmd_accum_ - oldest_cmd;
|
||||||
|
confined_ = radius < c.confine_radius &&
|
||||||
|
cmd >= c.confine_cmd_factor * std::max(2.0 * radius, c.disp_min);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
v.net_disp = net_disp_;
|
||||||
|
v.wandering = wandering_;
|
||||||
|
v.idle = idle_;
|
||||||
|
v.confined = confined_;
|
||||||
|
v.region_radius = region_radius_;
|
||||||
|
|
||||||
|
// Mẫu nặng mới: ghi vào ring nếu có cost hữu hạn. +inf (mất liên thông tạm) không ghi — không kết
|
||||||
|
// luận từ mẫu thiếu.
|
||||||
|
if (s.geometry_valid && s.cost_conclusive && s.reachable && std::isfinite(s.cost_to_go))
|
||||||
|
{
|
||||||
|
// Lật liên thông: cost đổi nhiều hơn robot có thể đã đi trong một tick -> mặt nạ free vừa đóng/mở một khe,
|
||||||
|
// không phải tiến độ. Bắt đầu đo lại; kết luận kẹt cũ (nếu có) cũng không còn cơ sở.
|
||||||
|
if (c.jump_reject > 0.0 && has_prev_heavy_ &&
|
||||||
|
std::fabs(s.cost_to_go - prev_heavy_cost_) > c.jump_reject + (travel_total_ - prev_heavy_travel_))
|
||||||
|
{
|
||||||
|
clearWindow();
|
||||||
|
stall_seen_ = false;
|
||||||
|
stall_fired_ = false;
|
||||||
|
v.cost_jump = true;
|
||||||
|
}
|
||||||
|
has_prev_heavy_ = true;
|
||||||
|
prev_heavy_cost_ = s.cost_to_go;
|
||||||
|
prev_heavy_travel_ = travel_total_;
|
||||||
|
ring_[ring_head_] = HeavySample{ s.now, s.cost_to_go, cmd_accum_ };
|
||||||
|
ring_head_ = (ring_head_ + 1) % kRing;
|
||||||
|
ring_count_ = std::min(ring_count_ + 1, kRing);
|
||||||
|
|
||||||
|
HeavySample old;
|
||||||
|
if (oldestInWindow(s.now, old))
|
||||||
|
{
|
||||||
|
const double cmd = cmd_accum_ - old.cmd;
|
||||||
|
v.cmd_window = cmd;
|
||||||
|
if (cmd >= c.min_commanded)
|
||||||
|
{
|
||||||
|
v.eff_geo = (old.cost - s.cost_to_go) / cmd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trượt: lệnh đi mà bánh không quay (vật thấp, bumper). Chỉ khi có v_meas.
|
||||||
|
bool slipping = false;
|
||||||
|
if (c.slip_on > 0.0 && std::isfinite(s.v_meas))
|
||||||
|
{
|
||||||
|
const bool slip_now = std::fabs(s.v_cmd) - std::fabs(s.v_meas) >= c.slip_on;
|
||||||
|
if (slip_now)
|
||||||
|
{
|
||||||
|
if (!slip_seen_)
|
||||||
|
{
|
||||||
|
slip_seen_ = true;
|
||||||
|
slip_since_ = s.now;
|
||||||
|
}
|
||||||
|
slipping = (s.now - slip_since_).toSec() >= c.slip_time;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
slip_seen_ = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool grace = has_plan_time_ && (s.now - plan_time_).toSec() < config_.grace_after_new_plan;
|
||||||
|
const bool near_goal = std::isfinite(s.d_goal) && s.d_goal < c.goal_zone;
|
||||||
|
if (grace || near_goal)
|
||||||
|
{
|
||||||
|
stall_seen_ = false;
|
||||||
|
stall_fired_ = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool eff_known = !std::isnan(v.eff_geo);
|
||||||
|
const bool stalled = (eff_known && v.eff_geo < c.eff_on) || slipping;
|
||||||
|
const bool cleared = eff_known && v.eff_geo >= c.eff_off && !slipping;
|
||||||
|
if (stalled)
|
||||||
|
{
|
||||||
|
if (!stall_seen_)
|
||||||
|
{
|
||||||
|
stall_seen_ = true;
|
||||||
|
stall_since_ = s.now;
|
||||||
|
}
|
||||||
|
if (!stall_fired_ && (s.now - stall_since_).toSec() >= c.confirm)
|
||||||
|
{
|
||||||
|
stall_fired_ = true;
|
||||||
|
v.event = slipping ? "stall: wheels slip against the command" : "stall: cost_to_go not decreasing";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (cleared)
|
||||||
|
{
|
||||||
|
if (stall_fired_)
|
||||||
|
{
|
||||||
|
v.event = "stall cleared: progress resumed";
|
||||||
|
}
|
||||||
|
stall_seen_ = false;
|
||||||
|
stall_fired_ = false;
|
||||||
|
}
|
||||||
|
// Còn lại (không đo được, hoặc eff trong vùng chết): giữ trạng thái — không kết luận ≠ hết kẹt.
|
||||||
|
// Đứng yên suốt idle_window_s, hoặc đi nhiều mà bị giam trong vùng nhỏ suốt confine_window_s, là kẹt kể cả
|
||||||
|
// khi geodesic không đo được (lệnh quá ít để có eff) hoặc đang lật dấu vì robot đi tới đi lui.
|
||||||
|
if ((idle_ || confined_) && !stall_fired_)
|
||||||
|
{
|
||||||
|
stall_fired_ = true;
|
||||||
|
stall_seen_ = true;
|
||||||
|
stall_since_ = s.now;
|
||||||
|
v.event = idle_ ? "stall: no net displacement for the idle window"
|
||||||
|
: "stall: confined to a small region while commanded";
|
||||||
|
}
|
||||||
|
|
||||||
|
v.stall_detected = stall_fired_;
|
||||||
|
if (!stall_fired_)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Cổng đầu chặng, ngân sách replan, backoff sau lượt hỏng.
|
||||||
|
bool muted = false;
|
||||||
|
if (has_goal_ && cmd_accum_ - cmd_at_goal_ < c.min_commanded_since_goal)
|
||||||
|
{
|
||||||
|
muted = true;
|
||||||
|
}
|
||||||
|
if (c.disp_min > 0.0)
|
||||||
|
{
|
||||||
|
// Cổng dịch chuyển thực thay ngân sách replan: cửa sổ vị trí đã bắt đầu lại ở plan mới, nên "sau detour
|
||||||
|
// vẫn quần tại chỗ disp_window_s" mới được nổ lại; né tự do (dịch chuyển >= disp_min) không bao giờ nổ.
|
||||||
|
if (!wandering_ && !idle_ && !confined_)
|
||||||
|
{
|
||||||
|
muted = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (has_replan_mark_ && travel_total_ - replan_mark_travel_ < budget_travel_)
|
||||||
|
{
|
||||||
|
muted = true;
|
||||||
|
}
|
||||||
|
if (has_stall_backoff_ && s.now < stall_backoff_until_)
|
||||||
|
{
|
||||||
|
muted = true;
|
||||||
|
}
|
||||||
|
v.stall_muted = muted;
|
||||||
|
v.no_progress = !muted && c.action == "recover";
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnvMonitor::updateUnreachable(const EnvSample& s, EnvVerdict& v)
|
||||||
|
{
|
||||||
|
const EnvMonitorConfig::Unreachable& c = config_.unreachable;
|
||||||
|
if (!c.enabled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Trạng thái chỉ đổi ở tick nặng có kết luận; verdict thì phải ra MỖI cycle từ trạng thái, nếu
|
||||||
|
// không cờ nhấp nháy giữa hai tick 5 Hz và state machine 20 Hz thấy cờ bật/tắt.
|
||||||
|
if (s.geometry_valid && s.cost_conclusive)
|
||||||
|
{
|
||||||
|
// Baseline của plan: mẫu kết luận đầu tiên sau khi nhận plan. Plan đã không liên thông ngay từ
|
||||||
|
// đầu thì đó là chuyện của SBPL/planner đã lập nó — monitor không cãi trên cùng snapshot (R2).
|
||||||
|
if (baseline_pending_)
|
||||||
|
{
|
||||||
|
baseline_pending_ = false;
|
||||||
|
baseline_unreachable_ = !s.reachable;
|
||||||
|
}
|
||||||
|
if (s.reachable)
|
||||||
|
{
|
||||||
|
if (unreach_fired_)
|
||||||
|
{
|
||||||
|
v.event = "unreachable cleared: the exit is connected again";
|
||||||
|
}
|
||||||
|
unreach_seen_ = false;
|
||||||
|
unreach_fired_ = false;
|
||||||
|
baseline_unreachable_ = false; // từ đây mất liên thông là chuyện mới
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const bool grace = has_plan_time_ && (s.now - plan_time_).toSec() < config_.grace_after_new_plan;
|
||||||
|
const bool clean = std::isfinite(s.edt_body_min) && s.edt_body_min >= c.clean_start_margin;
|
||||||
|
if (baseline_unreachable_ || grace || !clean)
|
||||||
|
{
|
||||||
|
// Không tích: chưa phải "xấu đi", hoặc robot đã lọt vành (SBPL không có chỗ bắt đầu — việc
|
||||||
|
// của kContact + controller_patience).
|
||||||
|
unreach_seen_ = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!unreach_seen_)
|
||||||
|
{
|
||||||
|
unreach_seen_ = true;
|
||||||
|
unreach_since_ = s.now;
|
||||||
|
}
|
||||||
|
if (!unreach_fired_ && (s.now - unreach_since_).toSec() >= c.static_on)
|
||||||
|
{
|
||||||
|
unreach_fired_ = true;
|
||||||
|
v.event = "unreachable: no path to the plan exit inside the window";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
v.unreachable_detected = unreach_fired_;
|
||||||
|
if (!unreach_fired_)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bool muted = false;
|
||||||
|
if (has_replan_mark_ && travel_total_ - replan_mark_travel_ < budget_travel_)
|
||||||
|
{
|
||||||
|
muted = true;
|
||||||
|
}
|
||||||
|
if (has_unreach_backoff_ && s.now < unreach_backoff_until_)
|
||||||
|
{
|
||||||
|
muted = true;
|
||||||
|
}
|
||||||
|
v.unreachable_muted = muted;
|
||||||
|
v.path_blocked = !muted && c.action == "recover";
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnvMonitor::updateContact(const EnvSample& s, EnvVerdict& v)
|
||||||
|
{
|
||||||
|
const EnvMonitorConfig::Contact& c = config_.contact;
|
||||||
|
if (!c.enabled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
double v_ref = std::fabs(s.v_cmd);
|
||||||
|
if (std::isfinite(s.v_meas))
|
||||||
|
{
|
||||||
|
v_ref = std::max(v_ref, std::fabs(s.v_meas));
|
||||||
|
}
|
||||||
|
v.brake_dist = v_ref * v_ref / (2.0 * c.brake_decel);
|
||||||
|
|
||||||
|
if (!s.contact_conclusive || !s.costmap_current)
|
||||||
|
{
|
||||||
|
// Không biết thì không dừng, cũng không xoá chuỗi (cùng hợp đồng với guard cũ).
|
||||||
|
v.brake = braking_; // đang phanh thì giữ phanh tới khi có bằng chứng ngược lại
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (standdown_)
|
||||||
|
{
|
||||||
|
// Đang đứng ra: không phanh, không cờ, không bao. Chỉ theo dõi điều kiện vũ trang lại.
|
||||||
|
v.contact_standdown = true;
|
||||||
|
if (std::isfinite(s.traj_contact_dist))
|
||||||
|
{
|
||||||
|
standdown_clean_streak_ = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
++standdown_clean_streak_;
|
||||||
|
}
|
||||||
|
const bool travelled = travel_total_ - standdown_mark_travel_ >= c.standdown_travel;
|
||||||
|
if (travelled || standdown_clean_streak_ >= c.release_confirm_cycles)
|
||||||
|
{
|
||||||
|
standdown_ = false;
|
||||||
|
standdown_clean_streak_ = 0;
|
||||||
|
v.contact_standdown = false;
|
||||||
|
v.event = travelled ? "contact: re-armed after moving on" : "contact: re-armed, the trajectory is clear";
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!std::isfinite(s.traj_contact_dist))
|
||||||
|
{
|
||||||
|
contact_streak_ = 0;
|
||||||
|
if (braking_ && ++release_streak_ < c.release_confirm_cycles)
|
||||||
|
{
|
||||||
|
// Một mẫu sạch chưa là sạch: local planner đổi quỹ đạo mỗi cycle, sạch 1 cycle rồi chạm lại là
|
||||||
|
// chuyện thường (run 5/9 22:35: 4 lần nhả/phanh trong 3 s). Giữ phanh và cờ tới khi sạch liên tiếp.
|
||||||
|
v.brake = true;
|
||||||
|
v.collision = contact_fired_ && c.action == "recover";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (braking_ || contact_fired_)
|
||||||
|
{
|
||||||
|
v.event = "contact cleared: the trajectory is free again";
|
||||||
|
}
|
||||||
|
braking_ = false;
|
||||||
|
release_streak_ = 0;
|
||||||
|
contact_close_ = false;
|
||||||
|
contact_fired_ = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bao tốc độ: |v| <= sqrt(2 a (d - margin)) để luôn còn dừng được trên đất sạch.
|
||||||
|
// Bao giữ thêm envelope_margin so với ngưỡng phanh: lệnh nằm trên bao thì quãng phanh còn dư đúng chừng
|
||||||
|
// đó, nhiễu quỹ đạo vài cm không đẩy sang "sắp chạm". Trong margin thì 0; giữa margin và margin+dư thì bò
|
||||||
|
// min_speed.
|
||||||
|
if (c.envelope)
|
||||||
|
{
|
||||||
|
if (s.traj_contact_dist <= c.clean_start_margin)
|
||||||
|
{
|
||||||
|
v.v_cap = 0.0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const double room = s.traj_contact_dist - c.clean_start_margin - c.envelope_margin;
|
||||||
|
v.v_cap = std::max(c.min_speed, room > 0.0 ? std::sqrt(2.0 * c.brake_decel * room) : 0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Xoay tại chỗ / bò mà monitor chưa phanh: quỹ đạo local planner lúc này là một cú xoay, góc thân
|
||||||
|
// quét vào ô kề bên là chuyện của controller (nó tự kiểm footprint), không phải bài toán quãng phanh.
|
||||||
|
// Phanh ở đây là chặn robot xoay ra khỏi vật đang kề mũi, rồi 1 s sau giơ cờ và gọi detour từ đúng
|
||||||
|
// chỗ đứng — vòng churn đầu chặng của guard cũ (mission 5/6 shadow 5/9). Đã phanh rồi thì vẫn xét
|
||||||
|
// tiếp để "kề mũi đứng yên" còn nổ được.
|
||||||
|
double v_intent = v_ref;
|
||||||
|
if (std::isfinite(s.v_controller))
|
||||||
|
{
|
||||||
|
v_intent = std::max(v_intent, std::fabs(s.v_controller));
|
||||||
|
}
|
||||||
|
if (v_intent < c.min_check_speed && !braking_)
|
||||||
|
{
|
||||||
|
contact_streak_ = 0;
|
||||||
|
release_streak_ = 0;
|
||||||
|
contact_close_ = false;
|
||||||
|
contact_fired_ = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hai câu hỏi khác nhau:
|
||||||
|
// - "sắp chạm" = còn dừng kịp không (theo quãng phanh) -> PHANH. Đứng lại rồi thì quãng phanh về 0
|
||||||
|
// và câu này tự hết đúng, robot được bò tiếp dưới bao tốc độ.
|
||||||
|
// - "đang kề vật" = tiếp xúc trong đúng clean_start_margin bất kể tốc độ -> nếu còn đó đủ
|
||||||
|
// trigger_static_time thì vật đứng yên trước mũi, không phải người đi qua -> giơ CỜ.
|
||||||
|
// Bao tốc độ đặt lệnh đúng lên biên v_cap = sqrt(2a(d - margin)), tức d - quãng phanh == margin: nếu
|
||||||
|
// "sắp chạm" là <= thì mỗi lần bao kẹp xong là phanh, nhả, kẹp, phanh (chattering 4 cycle). Chỉ phanh
|
||||||
|
// khi vượt bao quá 1 mm — lệnh nằm trên bao là đang giảm tốc đúng luật, không phải sắp chạm.
|
||||||
|
// Trong margin thì bao đã là 0: đó là một cú dừng của monitor dù quãng phanh bằng 0 — phải được gọi
|
||||||
|
// là phanh để có sự kiện, để giữ 0 tức thì qua trọng tài và để cổng xoay không nuốt nó.
|
||||||
|
constexpr double kEnvelopeTol = 1e-3; // [m]
|
||||||
|
// Đã kề thì còn kề tới margin + release_hysteresis: quỹ đạo nhảy 0,08 <-> 0,25 m quanh margin không
|
||||||
|
// được reset đồng hồ giơ cờ, cũng không được nhả phanh.
|
||||||
|
const double close_limit = c.clean_start_margin + (contact_close_ ? c.release_hysteresis : 0.0);
|
||||||
|
const bool close = s.traj_contact_dist <= close_limit;
|
||||||
|
const bool imminent = close || s.traj_contact_dist - v.brake_dist < c.clean_start_margin - kEnvelopeTol;
|
||||||
|
v.contact_imminent = imminent;
|
||||||
|
if (imminent)
|
||||||
|
{
|
||||||
|
release_streak_ = 0;
|
||||||
|
++contact_streak_;
|
||||||
|
if (!braking_ && contact_streak_ >= c.confirm_cycles)
|
||||||
|
{
|
||||||
|
if (c.standdown_min_travel > 0.0 && detour_from_contact_ &&
|
||||||
|
travel_total_ - detour_mark_travel_ < c.standdown_min_travel)
|
||||||
|
{
|
||||||
|
// Vừa detour từ đúng chỗ này vì chính cú chạm này, robot chưa rời chỗ: phanh + cờ lần nữa chỉ lặp
|
||||||
|
// lại churn (3 detour rồi ABORT). Nhường controller — nó là lưới an toàn duy nhất tới khi robot đi
|
||||||
|
// được standdown_travel hoặc quỹ đạo sạch.
|
||||||
|
standdown_ = true;
|
||||||
|
standdown_mark_travel_ = travel_total_;
|
||||||
|
standdown_clean_streak_ = 0;
|
||||||
|
detour_from_contact_ = false;
|
||||||
|
contact_streak_ = 0;
|
||||||
|
release_streak_ = 0;
|
||||||
|
contact_close_ = false;
|
||||||
|
contact_fired_ = false;
|
||||||
|
v.contact_standdown = true;
|
||||||
|
v.contact_imminent = false;
|
||||||
|
v.v_cap = std::numeric_limits<double>::infinity();
|
||||||
|
v.event = "contact: standing down — the detour from this spot did not move the robot, the controller "
|
||||||
|
"is the only safety net until it travels on";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
braking_ = true;
|
||||||
|
v.event = "contact: braking to zero";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
contact_streak_ = 0;
|
||||||
|
if (braking_ && ++release_streak_ >= c.release_confirm_cycles)
|
||||||
|
{
|
||||||
|
braking_ = false;
|
||||||
|
release_streak_ = 0;
|
||||||
|
v.event = "contact receded: releasing the brake";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
v.brake = braking_;
|
||||||
|
|
||||||
|
if (close)
|
||||||
|
{
|
||||||
|
if (!contact_close_)
|
||||||
|
{
|
||||||
|
contact_close_ = true;
|
||||||
|
close_since_ = s.now;
|
||||||
|
}
|
||||||
|
if (!contact_fired_ && (s.now - close_since_).toSec() >= c.trigger_static_time)
|
||||||
|
{
|
||||||
|
contact_fired_ = true;
|
||||||
|
v.event = "contact persists while stopped: asking for a replan";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (contact_fired_)
|
||||||
|
{
|
||||||
|
v.event = "contact cleared: the obstacle moved away";
|
||||||
|
}
|
||||||
|
contact_close_ = false;
|
||||||
|
contact_fired_ = false;
|
||||||
|
}
|
||||||
|
v.collision = contact_fired_ && c.action == "recover";
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace move_base2
|
||||||
826
src/grid_analysis.cpp
Normal file
826
src/grid_analysis.cpp
Normal file
@@ -0,0 +1,826 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* move_base2 — cài đặt grid_analysis (xem header).
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#include <move_base2/core/grid_analysis.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
#include <robot_costmap_2d/costmap_2d.h>
|
||||||
|
#include <robot_costmap_2d/cost_values.h>
|
||||||
|
|
||||||
|
namespace move_base2
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
constexpr float kInf = std::numeric_limits<float>::infinity();
|
||||||
|
/// Giá trị "vô cùng" hữu hạn cho EDT 1-D: bình phương khoảng cách theo ô tối đa là 160^2 = 25 600,
|
||||||
|
/// 1e6 vẫn biểu diễn chính xác trong float và không sinh NaN khi trừ nhau như +inf.
|
||||||
|
constexpr float kFarSq = 1.0e6f;
|
||||||
|
/// [m] Bỏ quãng đầu quỹ đạo khi kéo footprint — cùng lý do với plan_inspector::kSkipOwnFootprintDist.
|
||||||
|
constexpr double kSkipOwnFootprintDist = 0.05;
|
||||||
|
/// Lượng tử hoá khoảng cách trong khoá heap: 0.1 mm, đủ cho 20 m trong 32 bit.
|
||||||
|
constexpr double kHeapQuant = 10000.0;
|
||||||
|
|
||||||
|
double yawOf(const robot_geometry_msgs::PoseStamped& pose)
|
||||||
|
{
|
||||||
|
const auto& q = pose.pose.orientation;
|
||||||
|
return std::atan2(2.0 * (q.w * q.z + q.x * q.y), 1.0 - 2.0 * (q.y * q.y + q.z * q.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Biến đổi khoảng cách bình phương 1-D (Felzenszwalb & Huttenlocher 2004) — parabola lower envelope.
|
||||||
|
* @param f n giá trị vào (0 tại vật cản, kFarSq nơi khác; hoặc kết quả lượt trước).
|
||||||
|
* @param d n giá trị ra.
|
||||||
|
* @param v,z bộ đệm >= n và >= n+1.
|
||||||
|
*/
|
||||||
|
void distanceTransform1D(const float* f, int n, float* d, int* v, float* z)
|
||||||
|
{
|
||||||
|
int k = 0;
|
||||||
|
v[0] = 0;
|
||||||
|
z[0] = -kInf;
|
||||||
|
z[1] = kInf;
|
||||||
|
for (int q = 1; q < n; ++q)
|
||||||
|
{
|
||||||
|
float s = ((f[q] + static_cast<float>(q) * q) - (f[v[k]] + static_cast<float>(v[k]) * v[k])) /
|
||||||
|
(2.0f * q - 2.0f * v[k]);
|
||||||
|
while (s <= z[k])
|
||||||
|
{
|
||||||
|
--k;
|
||||||
|
s = ((f[q] + static_cast<float>(q) * q) - (f[v[k]] + static_cast<float>(v[k]) * v[k])) /
|
||||||
|
(2.0f * q - 2.0f * v[k]);
|
||||||
|
}
|
||||||
|
++k;
|
||||||
|
v[k] = q;
|
||||||
|
z[k] = s;
|
||||||
|
z[k + 1] = kInf;
|
||||||
|
}
|
||||||
|
k = 0;
|
||||||
|
for (int q = 0; q < n; ++q)
|
||||||
|
{
|
||||||
|
while (z[k + 1] < static_cast<float>(q))
|
||||||
|
{
|
||||||
|
++k;
|
||||||
|
}
|
||||||
|
const float dq = static_cast<float>(q - v[k]);
|
||||||
|
d[q] = dq * dq + f[v[k]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint64_t heapKey(double dist, std::uint32_t idx)
|
||||||
|
{
|
||||||
|
const std::uint64_t q = static_cast<std::uint64_t>(std::llround(dist * kHeapQuant));
|
||||||
|
return (q << 32) | idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Dựng chu vi hình chữ nhật thân (frame thân) với bước @p spacing; mỗi điểm mang id cạnh.
|
||||||
|
void buildPerimeter(double front, double rear, double side, double spacing, std::vector<double>& out)
|
||||||
|
{
|
||||||
|
out.clear();
|
||||||
|
const double x0[4] = { -rear, front, front, -rear };
|
||||||
|
const double y0[4] = { -side, -side, side, side };
|
||||||
|
const double x1[4] = { front, front, -rear, -rear };
|
||||||
|
const double y1[4] = { -side, side, side, -side };
|
||||||
|
for (int e = 0; e < 4; ++e)
|
||||||
|
{
|
||||||
|
const double len = std::hypot(x1[e] - x0[e], y1[e] - y0[e]);
|
||||||
|
const int n = std::max(1, static_cast<int>(std::ceil(len / spacing)));
|
||||||
|
for (int i = 0; i <= n; ++i)
|
||||||
|
{
|
||||||
|
const double t = static_cast<double>(i) / n;
|
||||||
|
out.push_back(x0[e] + (x1[e] - x0[e]) * t);
|
||||||
|
out.push_back(y0[e] + (y1[e] - y0[e]) * t);
|
||||||
|
out.push_back(static_cast<double>(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------ scratch
|
||||||
|
|
||||||
|
bool GridScratch::ensure(const robot_costmap_2d::Costmap2D& grid)
|
||||||
|
{
|
||||||
|
const unsigned int w = grid.getSizeInCellsX();
|
||||||
|
const unsigned int h = grid.getSizeInCellsY();
|
||||||
|
resolution = grid.getResolution();
|
||||||
|
origin_x = grid.getOriginX();
|
||||||
|
origin_y = grid.getOriginY();
|
||||||
|
if (w == 0 || h == 0 || !(resolution > 0.0))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (w != width || h != height)
|
||||||
|
{
|
||||||
|
width = w;
|
||||||
|
height = h;
|
||||||
|
const std::size_t n = static_cast<std::size_t>(w) * h;
|
||||||
|
edt.assign(n, kInf);
|
||||||
|
free.assign(n, 0);
|
||||||
|
geo.assign(n, kInf);
|
||||||
|
tmp_.assign(n, kFarSq);
|
||||||
|
const std::size_t m = std::max(w, h) + 2;
|
||||||
|
f_.assign(m, 0.0f);
|
||||||
|
d_.assign(m, 0.0f);
|
||||||
|
v_.assign(m, 0);
|
||||||
|
z_.assign(m + 1, 0.0f);
|
||||||
|
heap_.clear();
|
||||||
|
heap_.reserve(n * 2);
|
||||||
|
baseline.reserve(1024);
|
||||||
|
perim_.reserve(3 * 512);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GridScratch::toCell(double x, double y, unsigned int& mx, unsigned int& my) const
|
||||||
|
{
|
||||||
|
if (width == 0 || height == 0 || !(resolution > 0.0))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const double fx = std::floor((x - origin_x) / resolution);
|
||||||
|
const double fy = std::floor((y - origin_y) / resolution);
|
||||||
|
if (fx < 0.0 || fy < 0.0 || fx >= static_cast<double>(width) || fy >= static_cast<double>(height))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
mx = static_cast<unsigned int>(fx);
|
||||||
|
my = static_cast<unsigned int>(fy);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GridScratch::cellCenter(unsigned int mx, unsigned int my, double& x, double& y) const
|
||||||
|
{
|
||||||
|
x = origin_x + (mx + 0.5) * resolution;
|
||||||
|
y = origin_y + (my + 0.5) * resolution;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------------------------- EDT
|
||||||
|
|
||||||
|
bool analyzeGrid(const robot_costmap_2d::Costmap2D& grid, unsigned char lethal_threshold,
|
||||||
|
double free_radius, GridScratch& s)
|
||||||
|
{
|
||||||
|
if (!s.ensure(grid))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const unsigned char* cost = grid.getCharMap();
|
||||||
|
const std::size_t n = static_cast<std::size_t>(s.width) * s.height;
|
||||||
|
bool any_lethal = false;
|
||||||
|
for (std::size_t i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
// NO_INFORMATION (255) không phải vật cản — cùng quy ước với checkPlan và SBPL.
|
||||||
|
const bool lethal = cost[i] >= lethal_threshold && cost[i] != robot_costmap_2d::NO_INFORMATION;
|
||||||
|
s.tmp_[i] = lethal ? 0.0f : kFarSq;
|
||||||
|
any_lethal |= lethal;
|
||||||
|
}
|
||||||
|
s.free_radius = free_radius;
|
||||||
|
if (!any_lethal)
|
||||||
|
{
|
||||||
|
std::fill(s.edt.begin(), s.edt.end(), kInf);
|
||||||
|
std::fill(s.free.begin(), s.free.end(), static_cast<std::uint8_t>(1));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Lượt 1: theo hàng (trục x).
|
||||||
|
const int w = static_cast<int>(s.width);
|
||||||
|
const int h = static_cast<int>(s.height);
|
||||||
|
for (int y = 0; y < h; ++y)
|
||||||
|
{
|
||||||
|
float* row = &s.tmp_[static_cast<std::size_t>(y) * s.width];
|
||||||
|
distanceTransform1D(row, w, s.d_.data(), s.v_.data(), s.z_.data());
|
||||||
|
std::copy(s.d_.begin(), s.d_.begin() + w, row);
|
||||||
|
}
|
||||||
|
// Lượt 2: theo cột (trục y).
|
||||||
|
for (int x = 0; x < w; ++x)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < h; ++y)
|
||||||
|
{
|
||||||
|
s.f_[y] = s.tmp_[static_cast<std::size_t>(y) * s.width + x];
|
||||||
|
}
|
||||||
|
distanceTransform1D(s.f_.data(), h, s.d_.data(), s.v_.data(), s.z_.data());
|
||||||
|
for (int y = 0; y < h; ++y)
|
||||||
|
{
|
||||||
|
const std::size_t i = static_cast<std::size_t>(y) * s.width + x;
|
||||||
|
const float dist = std::sqrt(s.d_[y]) * static_cast<float>(s.resolution);
|
||||||
|
s.edt[i] = dist;
|
||||||
|
s.free[i] = dist >= static_cast<float>(free_radius) ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
double distanceAt(const GridScratch& s, double x, double y)
|
||||||
|
{
|
||||||
|
unsigned int mx = 0;
|
||||||
|
unsigned int my = 0;
|
||||||
|
if (!s.toCell(x, y, mx, my) || s.edt.empty())
|
||||||
|
{
|
||||||
|
return std::numeric_limits<double>::quiet_NaN();
|
||||||
|
}
|
||||||
|
return s.edt[s.index(mx, my)];
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------- geodesic
|
||||||
|
|
||||||
|
bool computeGeodesic(unsigned int tx, unsigned int ty, GridScratch& s, std::size_t* visited,
|
||||||
|
std::size_t stop_index)
|
||||||
|
{
|
||||||
|
if (visited != nullptr)
|
||||||
|
{
|
||||||
|
*visited = 0;
|
||||||
|
}
|
||||||
|
if (s.width == 0 || tx >= s.width || ty >= s.height || s.free.empty())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::fill(s.geo.begin(), s.geo.end(), kInf);
|
||||||
|
const std::size_t t = s.index(tx, ty);
|
||||||
|
if (!s.free[t])
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const float res = static_cast<float>(s.resolution);
|
||||||
|
const float diag = res * 1.41421356f;
|
||||||
|
const int w = static_cast<int>(s.width);
|
||||||
|
const int h = static_cast<int>(s.height);
|
||||||
|
|
||||||
|
s.heap_.clear();
|
||||||
|
s.geo[t] = 0.0f;
|
||||||
|
s.heap_.push_back(heapKey(0.0, static_cast<std::uint32_t>(t)));
|
||||||
|
std::greater<std::uint64_t> cmp;
|
||||||
|
|
||||||
|
// 8 láng giềng: (dx, dy, chéo?)
|
||||||
|
static const int kDx[8] = { 1, -1, 0, 0, 1, 1, -1, -1 };
|
||||||
|
static const int kDy[8] = { 0, 0, 1, -1, 1, -1, 1, -1 };
|
||||||
|
|
||||||
|
std::size_t popped = 0;
|
||||||
|
while (!s.heap_.empty())
|
||||||
|
{
|
||||||
|
std::pop_heap(s.heap_.begin(), s.heap_.end(), cmp);
|
||||||
|
const std::uint64_t key = s.heap_.back();
|
||||||
|
s.heap_.pop_back();
|
||||||
|
const std::uint32_t idx = static_cast<std::uint32_t>(key & 0xffffffffu);
|
||||||
|
const double qd = static_cast<double>(key >> 32) / kHeapQuant;
|
||||||
|
if (qd > s.geo[idx] + 0.5 / kHeapQuant)
|
||||||
|
{
|
||||||
|
continue; // mục cũ, đã có đường ngắn hơn
|
||||||
|
}
|
||||||
|
++popped;
|
||||||
|
if (idx == stop_index)
|
||||||
|
{
|
||||||
|
break; // ô cần hỏi đã có khoảng cách đúng (Dijkstra: pop = chốt); phần còn lại không cần
|
||||||
|
}
|
||||||
|
const int x = static_cast<int>(idx % s.width);
|
||||||
|
const int y = static_cast<int>(idx / s.width);
|
||||||
|
const float base = s.geo[idx];
|
||||||
|
for (int k = 0; k < 8; ++k)
|
||||||
|
{
|
||||||
|
const int nx = x + kDx[k];
|
||||||
|
const int ny = y + kDy[k];
|
||||||
|
if (nx < 0 || ny < 0 || nx >= w || ny >= h)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const std::size_t ni = s.index(static_cast<unsigned int>(nx), static_cast<unsigned int>(ny));
|
||||||
|
if (!s.free[ni])
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
float step = res;
|
||||||
|
if (k >= 4)
|
||||||
|
{
|
||||||
|
// Cấm cắt góc: hai ô trực giao trung gian phải free, không thì đường chui qua khe chéo.
|
||||||
|
const std::size_t a = s.index(static_cast<unsigned int>(nx), static_cast<unsigned int>(y));
|
||||||
|
const std::size_t b = s.index(static_cast<unsigned int>(x), static_cast<unsigned int>(ny));
|
||||||
|
if (!s.free[a] || !s.free[b])
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
step = diag;
|
||||||
|
}
|
||||||
|
const float nd = base + step;
|
||||||
|
if (nd < s.geo[ni])
|
||||||
|
{
|
||||||
|
s.geo[ni] = nd;
|
||||||
|
s.heap_.push_back(heapKey(nd, static_cast<std::uint32_t>(ni)));
|
||||||
|
std::push_heap(s.heap_.begin(), s.heap_.end(), cmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (visited != nullptr)
|
||||||
|
{
|
||||||
|
*visited = popped;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool nearestFreeCell(const GridScratch& s, unsigned int mx, unsigned int my, double max_dist,
|
||||||
|
unsigned int& fx, unsigned int& fy)
|
||||||
|
{
|
||||||
|
if (s.free.empty() || mx >= s.width || my >= s.height)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (s.free[s.index(mx, my)])
|
||||||
|
{
|
||||||
|
fx = mx;
|
||||||
|
fy = my;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const int r = static_cast<int>(std::ceil(max_dist / s.resolution));
|
||||||
|
long best = -1;
|
||||||
|
for (int dy = -r; dy <= r; ++dy)
|
||||||
|
{
|
||||||
|
for (int dx = -r; dx <= r; ++dx)
|
||||||
|
{
|
||||||
|
const int x = static_cast<int>(mx) + dx;
|
||||||
|
const int y = static_cast<int>(my) + dy;
|
||||||
|
if (!s.inBounds(x, y))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const long d2 = static_cast<long>(dx) * dx + static_cast<long>(dy) * dy;
|
||||||
|
if (s.free[s.index(static_cast<unsigned int>(x), static_cast<unsigned int>(y))] &&
|
||||||
|
(best < 0 || d2 < best))
|
||||||
|
{
|
||||||
|
best = d2;
|
||||||
|
fx = static_cast<unsigned int>(x);
|
||||||
|
fy = static_cast<unsigned int>(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return best >= 0 && std::sqrt(static_cast<double>(best)) * s.resolution <= max_dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------ body clearance
|
||||||
|
|
||||||
|
double bodyClearance(const GridScratch& s, double x, double y, double yaw, double front, double rear,
|
||||||
|
double side)
|
||||||
|
{
|
||||||
|
if (s.edt.empty())
|
||||||
|
{
|
||||||
|
return std::numeric_limits<double>::quiet_NaN();
|
||||||
|
}
|
||||||
|
const double spacing = 0.5 * s.resolution;
|
||||||
|
const double c = std::cos(yaw);
|
||||||
|
const double sn = std::sin(yaw);
|
||||||
|
double best = std::numeric_limits<double>::infinity();
|
||||||
|
bool any = false;
|
||||||
|
const double x0[4] = { -rear, front, front, -rear };
|
||||||
|
const double y0[4] = { -side, -side, side, side };
|
||||||
|
const double x1[4] = { front, front, -rear, -rear };
|
||||||
|
const double y1[4] = { -side, side, side, -side };
|
||||||
|
for (int e = 0; e < 4; ++e)
|
||||||
|
{
|
||||||
|
const double len = std::hypot(x1[e] - x0[e], y1[e] - y0[e]);
|
||||||
|
const int n = std::max(1, static_cast<int>(std::ceil(len / spacing)));
|
||||||
|
for (int i = 0; i <= n; ++i)
|
||||||
|
{
|
||||||
|
const double t = static_cast<double>(i) / n;
|
||||||
|
const double bx = x0[e] + (x1[e] - x0[e]) * t;
|
||||||
|
const double by = y0[e] + (y1[e] - y0[e]) * t;
|
||||||
|
const double wx = x + bx * c - by * sn;
|
||||||
|
const double wy = y + bx * sn + by * c;
|
||||||
|
const double d = distanceAt(s, wx, wy);
|
||||||
|
if (!std::isnan(d))
|
||||||
|
{
|
||||||
|
any = true;
|
||||||
|
best = std::min(best, d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return any ? best : std::numeric_limits<double>::quiet_NaN();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------- cost to go
|
||||||
|
|
||||||
|
CostToGoResult costToGo(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
||||||
|
std::size_t from_index, double robot_x, double robot_y,
|
||||||
|
const CostToGoParams& params, const PlanarTransform& plan_to_grid,
|
||||||
|
GridScratch& s)
|
||||||
|
{
|
||||||
|
CostToGoResult r;
|
||||||
|
if (plan.size() < 2 || from_index >= plan.size() || s.free.empty())
|
||||||
|
{
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Độ dài cộng dồn từ from_index tới cuối, và pose plan cuối cùng còn trong lưới.
|
||||||
|
// arc_at[i] chỉ cần cho các pose từ from_index; dùng hai con trỏ chạy để không cấp phát.
|
||||||
|
double total = 0.0;
|
||||||
|
{
|
||||||
|
double px = 0.0;
|
||||||
|
double py = 0.0;
|
||||||
|
plan_to_grid.apply(plan[from_index].pose.position.x, plan[from_index].pose.position.y, px, py);
|
||||||
|
for (std::size_t i = from_index + 1; i < plan.size(); ++i)
|
||||||
|
{
|
||||||
|
double qx = 0.0;
|
||||||
|
double qy = 0.0;
|
||||||
|
plan_to_grid.apply(plan[i].pose.position.x, plan[i].pose.position.y, qx, qy);
|
||||||
|
total += std::hypot(qx - px, qy - py);
|
||||||
|
px = qx;
|
||||||
|
py = qy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t ti = from_index;
|
||||||
|
double arc_ti = 0.0; // độ dài từ from_index tới ti
|
||||||
|
bool found = false;
|
||||||
|
bool ahead = false;
|
||||||
|
{
|
||||||
|
double px = 0.0;
|
||||||
|
double py = 0.0;
|
||||||
|
double walk = 0.0;
|
||||||
|
for (std::size_t i = from_index; i < plan.size(); ++i)
|
||||||
|
{
|
||||||
|
double qx = 0.0;
|
||||||
|
double qy = 0.0;
|
||||||
|
plan_to_grid.apply(plan[i].pose.position.x, plan[i].pose.position.y, qx, qy);
|
||||||
|
if (i > from_index)
|
||||||
|
{
|
||||||
|
walk += std::hypot(qx - px, qy - py);
|
||||||
|
}
|
||||||
|
px = qx;
|
||||||
|
py = qy;
|
||||||
|
unsigned int mx = 0;
|
||||||
|
unsigned int my = 0;
|
||||||
|
if (!s.toCell(qx, qy, mx, my))
|
||||||
|
{
|
||||||
|
break; // cửa sổ lăn theo robot: ra ngoài một lần là phần còn lại cũng ngoài
|
||||||
|
}
|
||||||
|
ti = i;
|
||||||
|
arc_ti = walk;
|
||||||
|
found = true;
|
||||||
|
if (params.exit_ahead > 0.0 && walk >= params.exit_ahead)
|
||||||
|
{
|
||||||
|
ahead = true;
|
||||||
|
break; // T ở quãng cố định dọc plan: chỉ tiến theo index, không lùi khi cửa sổ trôi
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
return r; // pose plan gần robot nhất cũng ngoài lưới: robot xa plan, việc của off_path
|
||||||
|
}
|
||||||
|
r.target = (ti + 1 == plan.size()) ? CostToGoResult::Target::kGoal
|
||||||
|
: ahead ? CostToGoResult::Target::kAhead
|
||||||
|
: CostToGoResult::Target::kExit;
|
||||||
|
|
||||||
|
// T phải là ô free; không thì lùi dọc plan tối đa target_backoff.
|
||||||
|
unsigned int tx = 0;
|
||||||
|
unsigned int ty = 0;
|
||||||
|
{
|
||||||
|
double qx = 0.0;
|
||||||
|
double qy = 0.0;
|
||||||
|
plan_to_grid.apply(plan[ti].pose.position.x, plan[ti].pose.position.y, qx, qy);
|
||||||
|
s.toCell(qx, qy, tx, ty);
|
||||||
|
}
|
||||||
|
if (!s.free[s.index(tx, ty)])
|
||||||
|
{
|
||||||
|
bool backed = false;
|
||||||
|
double back = 0.0;
|
||||||
|
double px = 0.0;
|
||||||
|
double py = 0.0;
|
||||||
|
plan_to_grid.apply(plan[ti].pose.position.x, plan[ti].pose.position.y, px, py);
|
||||||
|
for (std::size_t j = ti; j > from_index; --j)
|
||||||
|
{
|
||||||
|
double qx = 0.0;
|
||||||
|
double qy = 0.0;
|
||||||
|
plan_to_grid.apply(plan[j - 1].pose.position.x, plan[j - 1].pose.position.y, qx, qy);
|
||||||
|
back += std::hypot(qx - px, qy - py);
|
||||||
|
px = qx;
|
||||||
|
py = qy;
|
||||||
|
if (back > params.target_backoff)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
unsigned int mx = 0;
|
||||||
|
unsigned int my = 0;
|
||||||
|
if (s.toCell(qx, qy, mx, my) && s.free[s.index(mx, my)])
|
||||||
|
{
|
||||||
|
ti = j - 1;
|
||||||
|
arc_ti -= back;
|
||||||
|
tx = mx;
|
||||||
|
ty = my;
|
||||||
|
backed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bool snapped = false;
|
||||||
|
if (!backed && params.target_snap > 0.0)
|
||||||
|
{
|
||||||
|
// Lùi dọc plan không thoát khỏi vành (plan ôm tường suốt đoạn): dịch T ngang sang ô free gần nhất.
|
||||||
|
unsigned int fx = 0;
|
||||||
|
unsigned int fy = 0;
|
||||||
|
if (nearestFreeCell(s, tx, ty, params.target_snap, fx, fy))
|
||||||
|
{
|
||||||
|
tx = fx;
|
||||||
|
ty = fy;
|
||||||
|
snapped = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!backed && !snapped)
|
||||||
|
{
|
||||||
|
r.conclusive = true;
|
||||||
|
r.reachable = false;
|
||||||
|
r.target = CostToGoResult::Target::kBlockedExit;
|
||||||
|
r.target_index = ti;
|
||||||
|
r.arc_rest = total - arc_ti;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
r.target = backed ? CostToGoResult::Target::kBackedOff : CostToGoResult::Target::kSnapped;
|
||||||
|
}
|
||||||
|
r.target_index = ti;
|
||||||
|
r.arc_rest = std::max(0.0, total - arc_ti);
|
||||||
|
|
||||||
|
// Ô robot; đang ôm sát vật thì snap sang ô free gần nhất.
|
||||||
|
unsigned int rx = 0;
|
||||||
|
unsigned int ry = 0;
|
||||||
|
if (!s.toCell(robot_x, robot_y, rx, ry))
|
||||||
|
{
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
if (!s.free[s.index(rx, ry)])
|
||||||
|
{
|
||||||
|
r.robot_in_ring = true;
|
||||||
|
unsigned int fx = 0;
|
||||||
|
unsigned int fy = 0;
|
||||||
|
if (!nearestFreeCell(s, rx, ry, params.robot_snap, fx, fy))
|
||||||
|
{
|
||||||
|
return r; // thân đã nằm sâu trong dải cấm: không kết luận
|
||||||
|
}
|
||||||
|
rx = fx;
|
||||||
|
ry = fy;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t visited = 0;
|
||||||
|
computeGeodesic(tx, ty, s, &visited, s.index(rx, ry)); // dừng sớm tại ô robot
|
||||||
|
r.cells_visited = visited;
|
||||||
|
r.conclusive = true;
|
||||||
|
const float g = s.geo[s.index(rx, ry)];
|
||||||
|
r.reachable = std::isfinite(g);
|
||||||
|
r.geo_to_exit = g;
|
||||||
|
r.cost_to_go = r.reachable ? static_cast<double>(g) + r.arc_rest : std::numeric_limits<double>::infinity();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------- corridor
|
||||||
|
|
||||||
|
CorridorResult scanCorridor(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
||||||
|
std::size_t from_index, const CorridorParams& params,
|
||||||
|
const PlanarTransform& plan_to_grid, const GridScratch& s)
|
||||||
|
{
|
||||||
|
CorridorResult r;
|
||||||
|
r.plan_clear_ahead = params.horizon;
|
||||||
|
if (plan.size() < 2 || from_index >= plan.size() || s.edt.empty())
|
||||||
|
{
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
const double need = params.body_width + 2.0 * params.margin;
|
||||||
|
double walk = 0.0;
|
||||||
|
double px = 0.0;
|
||||||
|
double py = 0.0;
|
||||||
|
bool clear_set = false;
|
||||||
|
for (std::size_t i = from_index; i < plan.size(); ++i)
|
||||||
|
{
|
||||||
|
double qx = 0.0;
|
||||||
|
double qy = 0.0;
|
||||||
|
plan_to_grid.apply(plan[i].pose.position.x, plan[i].pose.position.y, qx, qy);
|
||||||
|
if (i > from_index)
|
||||||
|
{
|
||||||
|
walk += std::hypot(qx - px, qy - py);
|
||||||
|
}
|
||||||
|
const double prev_x = px;
|
||||||
|
const double prev_y = py;
|
||||||
|
px = qx;
|
||||||
|
py = qy;
|
||||||
|
if (walk > params.horizon)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
unsigned int mx = 0;
|
||||||
|
unsigned int my = 0;
|
||||||
|
if (!s.toCell(qx, qy, mx, my))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++r.scanned;
|
||||||
|
if (!clear_set && s.edt[s.index(mx, my)] < static_cast<float>(s.free_radius))
|
||||||
|
{
|
||||||
|
r.plan_clear_ahead = walk;
|
||||||
|
clear_set = true;
|
||||||
|
}
|
||||||
|
// Hướng tiếp tuyến: tới pose kế, hoặc từ pose trước nếu là pose cuối.
|
||||||
|
double tx = 0.0;
|
||||||
|
double ty = 0.0;
|
||||||
|
if (i + 1 < plan.size())
|
||||||
|
{
|
||||||
|
double nx = 0.0;
|
||||||
|
double ny = 0.0;
|
||||||
|
plan_to_grid.apply(plan[i + 1].pose.position.x, plan[i + 1].pose.position.y, nx, ny);
|
||||||
|
tx = nx - qx;
|
||||||
|
ty = ny - qy;
|
||||||
|
}
|
||||||
|
else if (i > from_index)
|
||||||
|
{
|
||||||
|
tx = qx - prev_x;
|
||||||
|
ty = qy - prev_y;
|
||||||
|
}
|
||||||
|
const double tn = std::hypot(tx, ty);
|
||||||
|
if (tn < 1e-6)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const double nx = -ty / tn;
|
||||||
|
const double ny = tx / tn;
|
||||||
|
double width = 0.0;
|
||||||
|
for (double sgn = 1.0; sgn >= -1.0; sgn -= 2.0)
|
||||||
|
{
|
||||||
|
double d = 0.0;
|
||||||
|
while (d < params.side_cap)
|
||||||
|
{
|
||||||
|
d += s.resolution;
|
||||||
|
unsigned int cx = 0;
|
||||||
|
unsigned int cy = 0;
|
||||||
|
if (!s.toCell(qx + sgn * nx * d, qy + sgn * ny * d, cx, cy))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (s.edt[s.index(cx, cy)] <= 0.0f)
|
||||||
|
{
|
||||||
|
break; // ô lethal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
width += std::min(d, params.side_cap);
|
||||||
|
}
|
||||||
|
r.min_width = std::min(r.min_width, width);
|
||||||
|
if (!std::isfinite(r.block_dist) && width < need)
|
||||||
|
{
|
||||||
|
r.block_dist = walk;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------- sweep
|
||||||
|
|
||||||
|
SweepResult sweepTrajectory(const std::vector<robot_geometry_msgs::PoseStamped>& trajectory,
|
||||||
|
double robot_x, double robot_y, double robot_yaw, double start_tol,
|
||||||
|
const SweepParams& params, const robot_costmap_2d::Costmap2D& grid,
|
||||||
|
GridScratch& s)
|
||||||
|
{
|
||||||
|
SweepResult r;
|
||||||
|
if (trajectory.size() < 2 || !s.ensure(grid))
|
||||||
|
{
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
if (std::hypot(trajectory.front().pose.position.x - robot_x,
|
||||||
|
trajectory.front().pose.position.y - robot_y) > start_tol)
|
||||||
|
{
|
||||||
|
return r; // quỹ đạo không bắt đầu ở robot: không phải chuyển động sắp xảy ra
|
||||||
|
}
|
||||||
|
const unsigned char* cost = grid.getCharMap();
|
||||||
|
const double front = params.front + params.padding;
|
||||||
|
const double rear = params.rear + params.padding;
|
||||||
|
const double side = params.side + params.padding;
|
||||||
|
auto lethal_at = [&](unsigned int mx, unsigned int my) {
|
||||||
|
const unsigned char c = cost[s.index(mx, my)];
|
||||||
|
return c >= params.lethal_threshold && c != robot_costmap_2d::NO_INFORMATION;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Baseline: ô lethal đang nằm dưới thân đệm tại pose hiện tại (duyệt diện tích, như checkMotion).
|
||||||
|
s.baseline.clear();
|
||||||
|
if (params.exclude_baseline)
|
||||||
|
{
|
||||||
|
const double c = std::cos(robot_yaw);
|
||||||
|
const double sn = std::sin(robot_yaw);
|
||||||
|
const double cx[4] = { front, front, -rear, -rear };
|
||||||
|
const double cy[4] = { side, -side, side, -side };
|
||||||
|
double min_x = std::numeric_limits<double>::max();
|
||||||
|
double min_y = min_x;
|
||||||
|
double max_x = std::numeric_limits<double>::lowest();
|
||||||
|
double max_y = max_x;
|
||||||
|
for (int i = 0; i < 4; ++i)
|
||||||
|
{
|
||||||
|
const double wx = robot_x + cx[i] * c - cy[i] * sn;
|
||||||
|
const double wy = robot_y + cx[i] * sn + cy[i] * c;
|
||||||
|
min_x = std::min(min_x, wx);
|
||||||
|
max_x = std::max(max_x, wx);
|
||||||
|
min_y = std::min(min_y, wy);
|
||||||
|
max_y = std::max(max_y, wy);
|
||||||
|
}
|
||||||
|
int mx0 = 0;
|
||||||
|
int my0 = 0;
|
||||||
|
int mx1 = 0;
|
||||||
|
int my1 = 0;
|
||||||
|
grid.worldToMapEnforceBounds(min_x, min_y, mx0, my0);
|
||||||
|
grid.worldToMapEnforceBounds(max_x, max_y, mx1, my1);
|
||||||
|
const double slack = 0.5 * s.resolution;
|
||||||
|
for (int my = my0; my <= my1; ++my)
|
||||||
|
{
|
||||||
|
for (int mx = mx0; mx <= mx1; ++mx)
|
||||||
|
{
|
||||||
|
if (!lethal_at(static_cast<unsigned int>(mx), static_cast<unsigned int>(my)))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
double wx = 0.0;
|
||||||
|
double wy = 0.0;
|
||||||
|
s.cellCenter(static_cast<unsigned int>(mx), static_cast<unsigned int>(my), wx, wy);
|
||||||
|
const double dx = wx - robot_x;
|
||||||
|
const double dy = wy - robot_y;
|
||||||
|
const double bx = dx * c + dy * sn;
|
||||||
|
const double by = -dx * sn + dy * c;
|
||||||
|
if (bx < -rear - slack || bx > front + slack || std::fabs(by) > side + slack)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
s.baseline.push_back(static_cast<std::uint32_t>(s.index(static_cast<unsigned int>(mx),
|
||||||
|
static_cast<unsigned int>(my))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::sort(s.baseline.begin(), s.baseline.end());
|
||||||
|
}
|
||||||
|
r.baseline_cells = static_cast<int>(s.baseline.size());
|
||||||
|
|
||||||
|
buildPerimeter(front, rear, side, 0.5 * s.resolution, s.perim_);
|
||||||
|
const std::size_t npts = s.perim_.size() / 3;
|
||||||
|
|
||||||
|
auto hit_at = [&](double px, double py, double yaw, int& edge) {
|
||||||
|
const double c = std::cos(yaw);
|
||||||
|
const double sn = std::sin(yaw);
|
||||||
|
for (std::size_t k = 0; k < npts; ++k)
|
||||||
|
{
|
||||||
|
const double bx = s.perim_[3 * k];
|
||||||
|
const double by = s.perim_[3 * k + 1];
|
||||||
|
const double wx = px + bx * c - by * sn;
|
||||||
|
const double wy = py + bx * sn + by * c;
|
||||||
|
unsigned int mx = 0;
|
||||||
|
unsigned int my = 0;
|
||||||
|
if (!s.toCell(wx, wy, mx, my) || !lethal_at(mx, my))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const std::uint32_t idx = static_cast<std::uint32_t>(s.index(mx, my));
|
||||||
|
if (!s.baseline.empty() && std::binary_search(s.baseline.begin(), s.baseline.end(), idx))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
edge = static_cast<int>(s.perim_[3 * k + 2]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
double walk = 0.0;
|
||||||
|
for (std::size_t i = 1; i < trajectory.size(); ++i)
|
||||||
|
{
|
||||||
|
const double ax = trajectory[i - 1].pose.position.x;
|
||||||
|
const double ay = trajectory[i - 1].pose.position.y;
|
||||||
|
const double bx = trajectory[i].pose.position.x;
|
||||||
|
const double by = trajectory[i].pose.position.y;
|
||||||
|
const double seg = std::hypot(bx - ax, by - ay);
|
||||||
|
const double yaw = yawOf(trajectory[i]);
|
||||||
|
// Chèn mẫu để bước dọc quỹ đạo không quá một ô: tường mỏng một ô không lọt giữa hai mẫu.
|
||||||
|
const int nsub = std::max(1, static_cast<int>(std::ceil(seg / s.resolution)));
|
||||||
|
for (int k = 1; k <= nsub; ++k)
|
||||||
|
{
|
||||||
|
const double t = static_cast<double>(k) / nsub;
|
||||||
|
const double px = ax + (bx - ax) * t;
|
||||||
|
const double py = ay + (by - ay) * t;
|
||||||
|
const double w = walk + seg * t;
|
||||||
|
if (w > params.lookahead)
|
||||||
|
{
|
||||||
|
r.conclusive = true;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
if (w < kSkipOwnFootprintDist)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
++r.scanned;
|
||||||
|
int edge = -1;
|
||||||
|
if (hit_at(px, py, yaw, edge))
|
||||||
|
{
|
||||||
|
r.conclusive = true;
|
||||||
|
r.blocked = true;
|
||||||
|
r.distance = w;
|
||||||
|
r.sample = i;
|
||||||
|
r.hit_edge = edge;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
walk += seg;
|
||||||
|
}
|
||||||
|
r.conclusive = r.scanned > 0;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace move_base2
|
||||||
208
src/io/env_logger.cpp
Normal file
208
src/io/env_logger.cpp
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* move_base2 — cài đặt EnvLogger.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
#include <move_base2/io/env_logger.h>
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
#include <chrono>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
namespace move_base2
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
/// JSON không có NaN/Inf: ghi null.
|
||||||
|
void appendNumber(std::string& out, double v)
|
||||||
|
{
|
||||||
|
char buf[32];
|
||||||
|
if (std::isfinite(v))
|
||||||
|
{
|
||||||
|
std::snprintf(buf, sizeof(buf), "%.4f", v);
|
||||||
|
out += buf;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
out += "null";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void appendField(std::string& out, const char* key, double v)
|
||||||
|
{
|
||||||
|
out += '"';
|
||||||
|
out += key;
|
||||||
|
out += "\":";
|
||||||
|
appendNumber(out, v);
|
||||||
|
out += ',';
|
||||||
|
}
|
||||||
|
|
||||||
|
void appendField(std::string& out, const char* key, bool v)
|
||||||
|
{
|
||||||
|
out += '"';
|
||||||
|
out += key;
|
||||||
|
out += "\":";
|
||||||
|
out += v ? "true" : "false";
|
||||||
|
out += ',';
|
||||||
|
}
|
||||||
|
|
||||||
|
void appendField(std::string& out, const char* key, const char* v)
|
||||||
|
{
|
||||||
|
out += '"';
|
||||||
|
out += key;
|
||||||
|
out += "\":\"";
|
||||||
|
for (const char* p = v; *p != '\0'; ++p)
|
||||||
|
{
|
||||||
|
if (*p == '"' || *p == '\\')
|
||||||
|
{
|
||||||
|
out += '\\';
|
||||||
|
}
|
||||||
|
out += *p;
|
||||||
|
}
|
||||||
|
out += "\",";
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
bool EnvLoggerConfig::validate(std::string& error) const
|
||||||
|
{
|
||||||
|
if (!enabled)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (log_dir.empty())
|
||||||
|
{
|
||||||
|
error = "env_monitor/logger/log_dir must not be empty when the logger is enabled";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!std::isfinite(flush_period) || flush_period <= 0.0)
|
||||||
|
{
|
||||||
|
error = "env_monitor/logger/flush_period must be > 0 [s]";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
EnvLogger::~EnvLogger()
|
||||||
|
{
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EnvLogger::open(const EnvLoggerConfig& config, std::string& error)
|
||||||
|
{
|
||||||
|
config_ = config;
|
||||||
|
if (!config.enabled)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (::mkdir(config.log_dir.c_str(), 0755) != 0 && errno != EEXIST)
|
||||||
|
{
|
||||||
|
error = "env_monitor/logger: cannot create log_dir '" + config.log_dir + "'";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const auto epoch =
|
||||||
|
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
|
||||||
|
.count();
|
||||||
|
path_ = config.log_dir + "/envmon_" + std::to_string(epoch) + ".jsonl";
|
||||||
|
out_.open(path_, std::ios::out | std::ios::trunc);
|
||||||
|
if (!out_.is_open())
|
||||||
|
{
|
||||||
|
error = "env_monitor/logger: cannot open '" + path_ + "' for writing";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
buffer_.reserve(64 * 1024);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnvLogger::record(const robot::Time& now, const EnvSample& s, const EnvVerdict& v,
|
||||||
|
const EnvGeometry* g, const char* state, std::uint64_t mission_id,
|
||||||
|
bool used_local_plan)
|
||||||
|
{
|
||||||
|
if (!out_.is_open())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::string& o = buffer_;
|
||||||
|
o += "{\"v\":1,";
|
||||||
|
appendField(o, "t", now.toSec());
|
||||||
|
o += "\"mission\":" + std::to_string(mission_id) + ",";
|
||||||
|
appendField(o, "state", state);
|
||||||
|
appendField(o, "heavy", s.geometry_valid);
|
||||||
|
appendField(o, "x", s.robot_x);
|
||||||
|
appendField(o, "y", s.robot_y);
|
||||||
|
appendField(o, "v_cmd", s.v_cmd);
|
||||||
|
appendField(o, "w_cmd", s.w_cmd);
|
||||||
|
appendField(o, "v_meas", s.v_meas);
|
||||||
|
appendField(o, "v_controller", s.v_controller);
|
||||||
|
appendField(o, "d_goal", s.d_goal);
|
||||||
|
appendField(o, "costmap_current", s.costmap_current);
|
||||||
|
appendField(o, "controller_valid", s.controller_valid);
|
||||||
|
appendField(o, "cost_to_go", s.cost_conclusive && s.reachable ? s.cost_to_go : std::numeric_limits<double>::quiet_NaN());
|
||||||
|
appendField(o, "cost_conclusive", s.cost_conclusive);
|
||||||
|
appendField(o, "reachable", s.reachable);
|
||||||
|
appendField(o, "edt_body_min", s.edt_body_min);
|
||||||
|
appendField(o, "contact_conclusive", s.contact_conclusive);
|
||||||
|
appendField(o, "traj_contact_dist", s.traj_contact_dist);
|
||||||
|
appendField(o, "used_local_plan", used_local_plan);
|
||||||
|
if (g != nullptr && g->valid)
|
||||||
|
{
|
||||||
|
appendField(o, "geo_to_exit", g->cost.geo_to_exit);
|
||||||
|
appendField(o, "arc_rest", g->cost.arc_rest);
|
||||||
|
appendField(o, "target_kind", static_cast<double>(static_cast<int>(g->cost.target)));
|
||||||
|
appendField(o, "cells_visited", static_cast<double>(g->cost.cells_visited));
|
||||||
|
appendField(o, "robot_in_ring", g->cost.robot_in_ring);
|
||||||
|
appendField(o, "plan_clear_ahead", g->corridor.plan_clear_ahead);
|
||||||
|
appendField(o, "corridor_min_width", g->corridor.min_width);
|
||||||
|
appendField(o, "corridor_block_dist", g->corridor.block_dist);
|
||||||
|
}
|
||||||
|
appendField(o, "eff_geo", v.eff_geo);
|
||||||
|
appendField(o, "cmd_window", v.cmd_window);
|
||||||
|
appendField(o, "brake_dist", v.brake_dist);
|
||||||
|
appendField(o, "v_cap", v.v_cap);
|
||||||
|
appendField(o, "stall_detected", v.stall_detected);
|
||||||
|
appendField(o, "stall_muted", v.stall_muted);
|
||||||
|
appendField(o, "unreachable_detected", v.unreachable_detected);
|
||||||
|
appendField(o, "unreachable_muted", v.unreachable_muted);
|
||||||
|
appendField(o, "contact_imminent", v.contact_imminent);
|
||||||
|
appendField(o, "brake", v.brake);
|
||||||
|
appendField(o, "contact_standdown", v.contact_standdown);
|
||||||
|
appendField(o, "cost_jump", v.cost_jump);
|
||||||
|
appendField(o, "no_progress", v.no_progress);
|
||||||
|
appendField(o, "path_blocked", v.path_blocked);
|
||||||
|
appendField(o, "collision", v.collision);
|
||||||
|
appendField(o, "replan_budget_left", v.replan_budget_left);
|
||||||
|
appendField(o, "net_disp", v.net_disp);
|
||||||
|
appendField(o, "wandering", v.wandering);
|
||||||
|
appendField(o, "idle", v.idle);
|
||||||
|
appendField(o, "confined", v.confined);
|
||||||
|
appendField(o, "region_radius", v.region_radius);
|
||||||
|
appendField(o, "event", v.event);
|
||||||
|
o.pop_back(); // dấu phẩy cuối
|
||||||
|
o += "}\n";
|
||||||
|
|
||||||
|
if (!has_last_flush_ || (now - last_flush_).toSec() >= config_.flush_period || buffer_.size() > 48 * 1024)
|
||||||
|
{
|
||||||
|
flush();
|
||||||
|
last_flush_ = now;
|
||||||
|
has_last_flush_ = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnvLogger::flush()
|
||||||
|
{
|
||||||
|
if (out_.is_open() && !buffer_.empty())
|
||||||
|
{
|
||||||
|
out_ << buffer_;
|
||||||
|
out_.flush();
|
||||||
|
buffer_.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace move_base2
|
||||||
@@ -65,79 +65,210 @@ bool CostmapPosePort::lookupPose(const std::string& frame,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CostmapPathCheck::checkPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
// ================================================================================================
|
||||||
std::size_t from_index, double horizon,
|
// CostmapEnvironment
|
||||||
PlanBlockResult& out) const
|
// ================================================================================================
|
||||||
|
|
||||||
|
namespace
|
||||||
{
|
{
|
||||||
if (costmap_ == nullptr || !tf_ || plan.empty() || from_index >= plan.size())
|
double yawOfPose(const robot_geometry_msgs::PoseStamped& pose)
|
||||||
|
{
|
||||||
|
const auto& q = pose.pose.orientation;
|
||||||
|
return std::atan2(2.0 * (q.w * q.z + q.x * q.y), 1.0 - 2.0 * (q.y * q.y + q.z * q.z));
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
robot_costmap_2d::Costmap2D* CostmapEnvironment::gridForRead(bool& live) const
|
||||||
|
{
|
||||||
|
live = true;
|
||||||
|
if (costmap_ == nullptr)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
robot_costmap_2d::Costmap2D* grid = costmap_->getCostmapForRead();
|
||||||
|
live = (grid == costmap_->getCostmap()); // snapshot tắt -> trả lưới sống -> phải khoá
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CostmapEnvironment::refresh()
|
||||||
|
{
|
||||||
|
if (costmap_ != nullptr)
|
||||||
|
{
|
||||||
|
costmap_->refreshSnapshot(); // no-op khi snapshot tắt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CostmapEnvironment::sampleGeometry(const EnvGeometryRequest& r, EnvGeometry& out)
|
||||||
|
{
|
||||||
|
out = EnvGeometry();
|
||||||
|
if (costmap_ == nullptr || !tf_ || r.plan == nullptr || r.sampling == nullptr || r.plan->size() < 2 ||
|
||||||
|
r.from_index >= r.plan->size())
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
bool live = true;
|
||||||
robot_costmap_2d::Costmap2D* grid = costmap_->getCostmap();
|
robot_costmap_2d::Costmap2D* grid = gridForRead(live);
|
||||||
if (grid == nullptr)
|
if (grid == nullptr)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
const std::string& grid_frame = costmap_->getGlobalFrameID();
|
||||||
PlanarTransform plan_to_grid;
|
PlanarTransform plan_to_grid;
|
||||||
if (!planarTransform(*tf_, plan[from_index].header.frame_id, costmap_->getGlobalFrameID(),
|
PlanarTransform pose_to_grid;
|
||||||
plan_to_grid))
|
if (!planarTransform(*tf_, (*r.plan)[r.from_index].header.frame_id, grid_frame, plan_to_grid) ||
|
||||||
|
!planarTransform(*tf_, r.robot_pose.header.frame_id, grid_frame, pose_to_grid))
|
||||||
|
{
|
||||||
|
return false; // không quy đổi được frame: không biết, không đoán
|
||||||
|
}
|
||||||
|
double gx = 0.0;
|
||||||
|
double gy = 0.0;
|
||||||
|
pose_to_grid.apply(r.robot_pose.pose.position.x, r.robot_pose.pose.position.y, gx, gy);
|
||||||
|
const double gyaw = yawOfPose(r.robot_pose) + std::atan2(pose_to_grid.sin_yaw, pose_to_grid.cos_yaw);
|
||||||
|
|
||||||
|
// Bán kính tâm = inscribed của thân, KHÔNG đệm: cùng biên với SBPL (luật R2).
|
||||||
|
const double free_radius = std::min(r.side, std::min(r.front, r.rear));
|
||||||
|
out.free_radius = free_radius;
|
||||||
|
|
||||||
|
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> lock(*(grid->getMutex()), boost::defer_lock);
|
||||||
|
if (live)
|
||||||
|
{
|
||||||
|
lock.lock();
|
||||||
|
}
|
||||||
|
if (!analyzeGrid(*grid, static_cast<unsigned char>(r.sampling->lethal_threshold), free_radius, scratch_))
|
||||||
{
|
{
|
||||||
// Không quy đổi được frame thì mọi kết luận đều vô nghĩa. Trả false = "không biết".
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
out.edt_body_min = bodyClearance(scratch_, gx, gy, gyaw, r.front, r.rear, r.side);
|
||||||
// Cùng giao thức khoá với map-update thread và planner: lượt quét chỉ đọc vài chục ô nên giữ
|
CostToGoParams cost_params;
|
||||||
// mutex trọn lượt là rẻ, còn đọc lưới trong lúc nó đang được ghi thì kết luận không đáng tin.
|
cost_params.target_backoff = r.sampling->target_backoff;
|
||||||
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> lock(*(grid->getMutex()));
|
cost_params.robot_snap = r.sampling->robot_snap;
|
||||||
out = move_base2::checkPlan(plan, from_index, horizon, *grid, plan_to_grid);
|
cost_params.exit_ahead = r.sampling->exit_ahead;
|
||||||
|
cost_params.target_snap = r.sampling->target_snap;
|
||||||
|
out.cost = costToGo(*r.plan, r.from_index, gx, gy, cost_params, plan_to_grid, scratch_);
|
||||||
|
CorridorParams corridor_params;
|
||||||
|
corridor_params.horizon = r.sampling->horizon;
|
||||||
|
corridor_params.body_width = 2.0 * r.side;
|
||||||
|
corridor_params.margin = r.sampling->corridor_margin;
|
||||||
|
corridor_params.side_cap = r.sampling->corridor_cap;
|
||||||
|
out.corridor = scanCorridor(*r.plan, r.from_index, corridor_params, plan_to_grid, scratch_);
|
||||||
|
out.valid = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CostmapPathCheck::checkMotion(const robot_geometry_msgs::PoseStamped& robot_pose, double v,
|
bool CostmapEnvironment::sweepTrajectory(const EnvTrajectoryRequest& r, SweepResult& out,
|
||||||
double omega, const MotionCheckParams& params,
|
bool& used_local_plan)
|
||||||
MotionBlockResult& out) const
|
|
||||||
{
|
{
|
||||||
if (costmap_ == nullptr || !tf_)
|
out = SweepResult();
|
||||||
|
used_local_plan = false;
|
||||||
|
if (costmap_ == nullptr || !tf_ || r.sampling == nullptr)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
robot_costmap_2d::Costmap2D* grid = costmap_->getCostmap();
|
bool live = true;
|
||||||
|
robot_costmap_2d::Costmap2D* grid = gridForRead(live);
|
||||||
if (grid == nullptr)
|
if (grid == nullptr)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
const std::string& grid_frame = costmap_->getGlobalFrameID();
|
||||||
// Pose robot ở frame của cổng pose (thường `map`), lưới ở `odom`: quy đổi cả vị trí lẫn hướng
|
|
||||||
// rồi tích phân cung ngay trong frame lưới — mỗi mẫu không phải tra TF lần nữa.
|
|
||||||
PlanarTransform pose_to_grid;
|
PlanarTransform pose_to_grid;
|
||||||
if (!planarTransform(*tf_, robot_pose.header.frame_id, costmap_->getGlobalFrameID(),
|
if (!planarTransform(*tf_, r.robot_pose.header.frame_id, grid_frame, pose_to_grid))
|
||||||
pose_to_grid))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
double gx = 0.0;
|
double gx = 0.0;
|
||||||
double gy = 0.0;
|
double gy = 0.0;
|
||||||
pose_to_grid.apply(robot_pose.pose.position.x, robot_pose.pose.position.y, gx, gy);
|
pose_to_grid.apply(r.robot_pose.pose.position.x, r.robot_pose.pose.position.y, gx, gy);
|
||||||
const auto& q = robot_pose.pose.orientation;
|
const double gyaw = yawOfPose(r.robot_pose) + std::atan2(pose_to_grid.sin_yaw, pose_to_grid.cos_yaw);
|
||||||
const double yaw_pose = std::atan2(2.0 * (q.w * q.z + q.x * q.y),
|
|
||||||
1.0 - 2.0 * (q.y * q.y + q.z * q.z));
|
|
||||||
const double yaw_grid = yaw_pose + std::atan2(pose_to_grid.sin_yaw, pose_to_grid.cos_yaw);
|
|
||||||
|
|
||||||
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> lock(*(grid->getMutex()));
|
SweepParams params;
|
||||||
out = move_base2::checkMotion(gx, gy, yaw_grid, v, omega, params, *grid, &baseline_scratch_);
|
params.front = r.front;
|
||||||
return true;
|
params.rear = r.rear;
|
||||||
|
params.side = r.side;
|
||||||
|
params.padding = r.sampling->padding;
|
||||||
|
params.lookahead = r.sampling->contact_lookahead;
|
||||||
|
params.lethal_threshold = static_cast<unsigned char>(r.sampling->lethal_threshold);
|
||||||
|
|
||||||
|
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> lock(*(grid->getMutex()), boost::defer_lock);
|
||||||
|
if (live)
|
||||||
|
{
|
||||||
|
lock.lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1) Quỹ đạo thật của local planner, quy về frame lưới.
|
||||||
|
if (r.sampling->use_local_plan != "false" && r.local_plan != nullptr && r.local_plan->poses.size() >= 2)
|
||||||
|
{
|
||||||
|
PlanarTransform traj_to_grid;
|
||||||
|
if (planarTransform(*tf_, r.local_plan->header.frame_id, grid_frame, traj_to_grid))
|
||||||
|
{
|
||||||
|
const double dyaw = std::atan2(traj_to_grid.sin_yaw, traj_to_grid.cos_yaw);
|
||||||
|
traj_scratch_.clear();
|
||||||
|
traj_scratch_.reserve(r.local_plan->poses.size());
|
||||||
|
for (const auto& p : r.local_plan->poses)
|
||||||
|
{
|
||||||
|
robot_geometry_msgs::PoseStamped ps;
|
||||||
|
traj_to_grid.apply(p.pose.x, p.pose.y, ps.pose.position.x, ps.pose.position.y);
|
||||||
|
const double yaw = p.pose.theta + dyaw;
|
||||||
|
ps.pose.orientation.z = std::sin(0.5 * yaw);
|
||||||
|
ps.pose.orientation.w = std::cos(0.5 * yaw);
|
||||||
|
traj_scratch_.push_back(ps);
|
||||||
|
}
|
||||||
|
out = move_base2::sweepTrajectory(traj_scratch_, gx, gy, gyaw, r.sampling->local_plan_start_tol,
|
||||||
|
params, *grid, scratch_);
|
||||||
|
if (out.conclusive)
|
||||||
|
{
|
||||||
|
used_local_plan = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (r.sampling->use_local_plan == "true")
|
||||||
|
{
|
||||||
|
return false; // ép dùng quỹ đạo mà không có -> không kết luận
|
||||||
|
}
|
||||||
|
// 2) Cung (v, omega) không đổi — cùng hình học với guard cũ, nhưng cùng bộ dò với quỹ đạo thật.
|
||||||
|
if (std::fabs(r.v) < 1e-3)
|
||||||
|
{
|
||||||
|
out.conclusive = true; // đứng yên / xoay tại chỗ: không có chuyển động tịnh tiến để dò
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const double res = grid->getResolution();
|
||||||
|
const double step = res > 0.0 ? res : 0.05;
|
||||||
|
const double direction = r.v < 0.0 ? -1.0 : 1.0;
|
||||||
|
const double kappa = r.omega / r.v;
|
||||||
|
traj_scratch_.clear();
|
||||||
|
const int n = static_cast<int>(std::ceil(r.sampling->contact_lookahead / step));
|
||||||
|
traj_scratch_.reserve(static_cast<std::size_t>(n) + 1);
|
||||||
|
for (int i = 0; i <= n; ++i)
|
||||||
|
{
|
||||||
|
const double s = direction * step * i;
|
||||||
|
robot_geometry_msgs::PoseStamped ps;
|
||||||
|
double yaw = gyaw;
|
||||||
|
if (std::fabs(kappa) < 1e-6)
|
||||||
|
{
|
||||||
|
ps.pose.position.x = gx + s * std::cos(gyaw);
|
||||||
|
ps.pose.position.y = gy + s * std::sin(gyaw);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
yaw = gyaw + kappa * s;
|
||||||
|
ps.pose.position.x = gx + (std::sin(yaw) - std::sin(gyaw)) / kappa;
|
||||||
|
ps.pose.position.y = gy - (std::cos(yaw) - std::cos(gyaw)) / kappa;
|
||||||
|
}
|
||||||
|
ps.pose.orientation.z = std::sin(0.5 * yaw);
|
||||||
|
ps.pose.orientation.w = std::cos(0.5 * yaw);
|
||||||
|
traj_scratch_.push_back(ps);
|
||||||
|
}
|
||||||
|
out = move_base2::sweepTrajectory(traj_scratch_, gx, gy, gyaw, 1.0, params, *grid, scratch_);
|
||||||
|
return out.conclusive;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CostmapPathCheck::robotBodyExtents(double& front, double& rear, double& side) const
|
bool CostmapEnvironment::robotBodyExtents(double& front, double& rear, double& side) const
|
||||||
{
|
{
|
||||||
if (costmap_ == nullptr)
|
if (costmap_ == nullptr)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Footprint KHÔNG đệm: guard có collision_footprint_padding riêng, cộng đệm costmap vào nữa là
|
|
||||||
// đếm biên hai lần. Đây cũng là hình mà SBPL/PRIEST dùng, nên ba bên cùng một thân.
|
|
||||||
const std::vector<robot_geometry_msgs::Point>& fp = costmap_->getUnpaddedRobotFootprint();
|
const std::vector<robot_geometry_msgs::Point>& fp = costmap_->getUnpaddedRobotFootprint();
|
||||||
if (fp.size() < 3)
|
if (fp.size() < 3)
|
||||||
{
|
{
|
||||||
@@ -158,7 +289,7 @@ bool CostmapPathCheck::robotBodyExtents(double& front, double& rear, double& sid
|
|||||||
}
|
}
|
||||||
if (!(max_x > 0.0) || !(min_x < 0.0) || !(max_abs_y > 0.0))
|
if (!(max_x > 0.0) || !(min_x < 0.0) || !(max_abs_y > 0.0))
|
||||||
{
|
{
|
||||||
return false; // suy biến hoặc base_link nằm ngoài thân — không tin được
|
return false;
|
||||||
}
|
}
|
||||||
front = max_x;
|
front = max_x;
|
||||||
rear = -min_x;
|
rear = -min_x;
|
||||||
@@ -248,9 +379,11 @@ bool NavigationRuntime::buildCostmaps(const std::shared_ptr<tf3::BufferCore>& tf
|
|||||||
// Costmap ĐIỀU KHIỂN (local) là nguồn của guard "không đi mù" — xem doc của CostmapStatusAdapter.
|
// Costmap ĐIỀU KHIỂN (local) là nguồn của guard "không đi mù" — xem doc của CostmapStatusAdapter.
|
||||||
costmap_status_.setCostmap(local_costmap_.get());
|
costmap_status_.setCostmap(local_costmap_.get());
|
||||||
|
|
||||||
// PathMonitor quét tuyến trên chính costmap local — cùng lưới mà detour lập đường vòng trên đó.
|
// EnvMonitor đọc costmap local — cùng lưới mà detour lập đường vòng trên đó; TF để quy plan (map) và
|
||||||
path_check_.setCostmap(local_costmap_.get());
|
// pose về frame lưới.
|
||||||
path_check_.setTf(tf_);
|
environment_.setCostmap(local_costmap_.get());
|
||||||
|
controller_.setSnapshotRefreshedByLoop(true); // ControlLoop làm mới snapshot một lần đầu cycle (P0b)
|
||||||
|
environment_.setTf(tf_);
|
||||||
|
|
||||||
// Gắn ngay: host có thể hỏi dữ liệu hiển thị bất cứ lúc nào sau initialize(), kể cả trước khi
|
// Gắn ngay: host có thể hỏi dữ liệu hiển thị bất cứ lúc nào sau initialize(), kể cả trước khi
|
||||||
// costmap có nội dung. Exporter tự trả về "chưa có gì" thay vì lưới rỗng.
|
// costmap có nội dung. Exporter tự trả về "chưa có gì" thay vì lưới rỗng.
|
||||||
@@ -367,29 +500,21 @@ bool NavigationRuntime::buildRunners(std::string& error)
|
|||||||
|
|
||||||
// --- 6. Mission layer --------------------------------------------------------------------------
|
// --- 6. Mission layer --------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// Không chặn: mission layer hỏng thì navigation vẫn phải chạy được. Nhưng phải LOG rõ, vì hai
|
// Mission layer luôn được dựng (2026-09-06, bỏ khoá `mission_layer_enabled`). Không chặn boot:
|
||||||
// trạng thái này cho hai hành vi khác hẳn nhau với cùng một order — cắt thành chặng, hay đi thẳng
|
// layer hỏng thì navigation vẫn phải chạy được, nhưng phải LOG rõ — hai trạng thái này cho hai
|
||||||
// xuống như một goal duy nhất.
|
// hành vi khác hẳn nhau với cùng một order: cắt thành chặng, hay đi thẳng xuống như một goal.
|
||||||
if (config_.mission_layer_enabled)
|
std::string mission_error;
|
||||||
|
if (mission_layer_.configure(root_nh, config_.mission_namespace, mission_error))
|
||||||
{
|
{
|
||||||
std::string mission_error;
|
mission_layer_.attach(mission_);
|
||||||
if (mission_layer_.configure(root_nh, config_.mission_namespace, mission_error))
|
robot::log_info("[move_base2] NavigationRuntime: mission layer ready (%zu source(s)) — "
|
||||||
{
|
"VDA5050 orders are split into legs.\n", mission_layer_.sourceCount());
|
||||||
mission_layer_.attach(mission_);
|
|
||||||
robot::log_info("[move_base2] NavigationRuntime: mission layer ready (%zu source(s)) — "
|
|
||||||
"VDA5050 orders are split into legs.\n", mission_layer_.sourceCount());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
robot::log_warning("[move_base2] NavigationRuntime: mission layer NOT started — %s. Orders "
|
|
||||||
"fall back to the direct path (one goal per order, no leg queue, no "
|
|
||||||
"released/orderUpdateId handling).\n", mission_error.c_str());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
robot::log_info("[move_base2] NavigationRuntime: mission layer disabled by config "
|
robot::log_warning("[move_base2] NavigationRuntime: mission layer NOT started — %s. Orders "
|
||||||
"(mission_layer_enabled: false) — orders go straight down as one goal.\n");
|
"fall back to the direct path (one goal per order, no leg queue, no "
|
||||||
|
"released/orderUpdateId handling).\n", mission_error.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 7. Kiểm cấu hình sau cùng -----------------------------------------------------------------
|
// --- 7. Kiểm cấu hình sau cùng -----------------------------------------------------------------
|
||||||
@@ -500,7 +625,7 @@ ControlLoopDeps NavigationRuntime::deps()
|
|||||||
deps.mission = &mission_;
|
deps.mission = &mission_;
|
||||||
deps.action = &action_;
|
deps.action = &action_;
|
||||||
deps.costmap_status = &costmap_status_;
|
deps.costmap_status = &costmap_status_;
|
||||||
deps.path_check = &path_check_;
|
deps.environment = &environment_;
|
||||||
return deps;
|
return deps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -80,6 +80,50 @@ bool planarTransform(const tf3::BufferCore& tf, const std::string& from, const s
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::size_t trackedNearestIndex(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
||||||
|
const robot_geometry_msgs::PoseStamped& pose, std::size_t hint,
|
||||||
|
double back_m, double ahead_m)
|
||||||
|
{
|
||||||
|
if (plan.size() < 2)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
const std::size_t last_seg = plan.size() - 2;
|
||||||
|
hint = std::min(hint, last_seg);
|
||||||
|
|
||||||
|
auto seg_len = [&](std::size_t i) {
|
||||||
|
return std::hypot(plan[i + 1].pose.position.x - plan[i].pose.position.x,
|
||||||
|
plan[i + 1].pose.position.y - plan[i].pose.position.y);
|
||||||
|
};
|
||||||
|
std::size_t lo = hint;
|
||||||
|
for (double walked = 0.0; lo > 0 && walked < back_m; --lo)
|
||||||
|
{
|
||||||
|
walked += seg_len(lo - 1);
|
||||||
|
}
|
||||||
|
std::size_t hi = hint;
|
||||||
|
for (double walked = 0.0; hi < last_seg && walked < ahead_m; ++hi)
|
||||||
|
{
|
||||||
|
walked += seg_len(hi);
|
||||||
|
}
|
||||||
|
|
||||||
|
const double px = pose.pose.position.x;
|
||||||
|
const double py = pose.pose.position.y;
|
||||||
|
std::size_t best = hint;
|
||||||
|
double best_distance = std::numeric_limits<double>::max();
|
||||||
|
for (std::size_t i = lo; i <= hi; ++i)
|
||||||
|
{
|
||||||
|
const double distance =
|
||||||
|
pointToSegment(px, py, plan[i].pose.position.x, plan[i].pose.position.y,
|
||||||
|
plan[i + 1].pose.position.x, plan[i + 1].pose.position.y);
|
||||||
|
if (distance < best_distance)
|
||||||
|
{
|
||||||
|
best_distance = distance;
|
||||||
|
best = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
std::size_t nearestIndex(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
std::size_t nearestIndex(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
||||||
const robot_geometry_msgs::PoseStamped& pose)
|
const robot_geometry_msgs::PoseStamped& pose)
|
||||||
{
|
{
|
||||||
@@ -134,284 +178,4 @@ double crossTrackError(const std::vector<robot_geometry_msgs::PoseStamped>& plan
|
|||||||
return best;
|
return best;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlanBlockResult checkPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
|
||||||
std::size_t from_index, double horizon,
|
|
||||||
const robot_costmap_2d::Costmap2D& grid,
|
|
||||||
const PlanarTransform& plan_to_grid)
|
|
||||||
{
|
|
||||||
PlanBlockResult result;
|
|
||||||
|
|
||||||
if (plan.empty() || from_index >= plan.size() || !(horizon > 0.0))
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
double travelled = 0.0; // [m]
|
|
||||||
|
|
||||||
for (std::size_t i = from_index; i < plan.size(); ++i)
|
|
||||||
{
|
|
||||||
if (i > from_index)
|
|
||||||
{
|
|
||||||
travelled += std::hypot(plan[i].pose.position.x - plan[i - 1].pose.position.x,
|
|
||||||
plan[i].pose.position.y - plan[i - 1].pose.position.y);
|
|
||||||
if (travelled > horizon)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
double gx = 0.0;
|
|
||||||
double gy = 0.0;
|
|
||||||
plan_to_grid.apply(plan[i].pose.position.x, plan[i].pose.position.y, gx, gy);
|
|
||||||
|
|
||||||
unsigned int mx = 0;
|
|
||||||
unsigned int my = 0;
|
|
||||||
if (!grid.worldToMap(gx, gy, mx, my))
|
|
||||||
{
|
|
||||||
// Ra ngoài cửa sổ lưới: phần còn lại của tuyến cũng nằm ngoài (lưới lăn theo robot), nên
|
|
||||||
// dừng hẳn thay vì quét tiếp cho tới hết horizon.
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
++result.scanned;
|
|
||||||
|
|
||||||
const unsigned char cost = grid.getCost(mx, my);
|
|
||||||
if (cost == robot_costmap_2d::NO_INFORMATION)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (cost >= robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE)
|
|
||||||
{
|
|
||||||
result.blocked = true;
|
|
||||||
result.index = i;
|
|
||||||
result.distance = travelled;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace
|
|
||||||
{
|
|
||||||
/// [m] Quãng đầu cung bị bỏ qua — chỗ robot đang đứng. Xem @ref checkMotion.
|
|
||||||
constexpr double kSkipOwnFootprintDist = 0.05;
|
|
||||||
|
|
||||||
/// Một điểm dò: ra ngoài lưới hay chưa có thông tin đều là KHÔNG chạm.
|
|
||||||
bool probeHits(const robot_costmap_2d::Costmap2D& grid, double x, double y,
|
|
||||||
unsigned char threshold, const std::vector<unsigned int>& baseline)
|
|
||||||
{
|
|
||||||
unsigned int mx = 0;
|
|
||||||
unsigned int my = 0;
|
|
||||||
if (!grid.worldToMap(x, y, mx, my))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const unsigned char cost = grid.getCost(mx, my);
|
|
||||||
if (cost == robot_costmap_2d::NO_INFORMATION || cost < threshold)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Ô đã nằm dưới thân tại pose hiện tại: lệnh này không "đưa thêm" nó vào thân.
|
|
||||||
return baseline.empty() || !std::binary_search(baseline.begin(), baseline.end(), grid.getIndex(mx, my));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Gom các ô >= @p threshold đang nằm dưới hình chữ nhật thân đệm tại pose hiện tại.
|
|
||||||
*
|
|
||||||
* Duyệt DIỆN TÍCH chứ không phải chu vi: ô nằm sâu trong thân mà chỉ lấy chu vi thì mẫu sau quét
|
|
||||||
* qua nó và tính oan là "mới". Ô được coi là dưới thân khi TÂM ô nằm trong hình chữ nhật nới thêm
|
|
||||||
* nửa ô — cùng độ rộng với những gì probe chu vi (bước nửa ô) có thể chạm tới ở s = 0. Kết quả
|
|
||||||
* được sort để probe tra bằng binary_search. ~600 ô ở lưới 0.05 m; kiểm cost trước khi quy đổi
|
|
||||||
* hình học nên phần lớn ô thoát sớm.
|
|
||||||
*/
|
|
||||||
void collectBaseline(const robot_costmap_2d::Costmap2D& grid, double x, double y, double yaw,
|
|
||||||
double front, double rear, double side, unsigned char threshold,
|
|
||||||
std::vector<unsigned int>& out, MotionBlockResult& result)
|
|
||||||
{
|
|
||||||
out.clear();
|
|
||||||
result.baseline_listed = 0;
|
|
||||||
const double c = std::cos(yaw);
|
|
||||||
const double sn = std::sin(yaw);
|
|
||||||
const double corners_x[4] = { front, front, -rear, -rear };
|
|
||||||
const double corners_y[4] = { side, -side, side, -side };
|
|
||||||
double min_wx = std::numeric_limits<double>::max();
|
|
||||||
double min_wy = std::numeric_limits<double>::max();
|
|
||||||
double max_wx = std::numeric_limits<double>::lowest();
|
|
||||||
double max_wy = std::numeric_limits<double>::lowest();
|
|
||||||
for (int i = 0; i < 4; ++i)
|
|
||||||
{
|
|
||||||
const double wx = x + corners_x[i] * c - corners_y[i] * sn;
|
|
||||||
const double wy = y + corners_x[i] * sn + corners_y[i] * c;
|
|
||||||
min_wx = std::min(min_wx, wx);
|
|
||||||
max_wx = std::max(max_wx, wx);
|
|
||||||
min_wy = std::min(min_wy, wy);
|
|
||||||
max_wy = std::max(max_wy, wy);
|
|
||||||
}
|
|
||||||
int mx0 = 0;
|
|
||||||
int my0 = 0;
|
|
||||||
int mx1 = 0;
|
|
||||||
int my1 = 0;
|
|
||||||
grid.worldToMapEnforceBounds(min_wx, min_wy, mx0, my0);
|
|
||||||
grid.worldToMapEnforceBounds(max_wx, max_wy, mx1, my1);
|
|
||||||
const double slack = 0.5 * grid.getResolution();
|
|
||||||
for (int my = my0; my <= my1; ++my)
|
|
||||||
{
|
|
||||||
for (int mx = mx0; mx <= mx1; ++mx)
|
|
||||||
{
|
|
||||||
const unsigned char cost = grid.getCost(static_cast<unsigned int>(mx), static_cast<unsigned int>(my));
|
|
||||||
if (cost == robot_costmap_2d::NO_INFORMATION || cost < threshold)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
double wx = 0.0;
|
|
||||||
double wy = 0.0;
|
|
||||||
grid.mapToWorld(static_cast<unsigned int>(mx), static_cast<unsigned int>(my), wx, wy);
|
|
||||||
const double dx = wx - x;
|
|
||||||
const double dy = wy - y;
|
|
||||||
const double bx = dx * c + dy * sn;
|
|
||||||
const double by = -dx * sn + dy * c;
|
|
||||||
if (bx < -rear - slack || bx > front + slack || std::fabs(by) > side + slack)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
out.push_back(grid.getIndex(static_cast<unsigned int>(mx), static_cast<unsigned int>(my)));
|
|
||||||
if (result.baseline_listed < MotionBlockResult::kMaxListedBaseline)
|
|
||||||
{
|
|
||||||
result.baseline_xy[result.baseline_listed][0] = bx;
|
|
||||||
result.baseline_xy[result.baseline_listed][1] = by;
|
|
||||||
++result.baseline_listed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::sort(out.begin(), out.end());
|
|
||||||
}
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
MotionBlockResult checkMotion(double x, double y, double yaw, double v, double omega,
|
|
||||||
const MotionCheckParams& params,
|
|
||||||
const robot_costmap_2d::Costmap2D& grid,
|
|
||||||
std::vector<unsigned int>* baseline_scratch)
|
|
||||||
{
|
|
||||||
MotionBlockResult result;
|
|
||||||
if (std::fabs(v) < params.min_speed || !(params.lookahead > 0.0) || params.max_samples < 1)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
const double direction = v < 0.0 ? -1.0 : 1.0;
|
|
||||||
// Độ cong của cung: omega / v. Lùi thì cung lật chiều cùng với v — công thức giữ nguyên dấu.
|
|
||||||
const double kappa = omega / v;
|
|
||||||
// Bước dò không được lớn hơn một ô lưới: với bước 0.13 m mũi robot ở mẫu này còn cách tường
|
|
||||||
// mỏng 1 ô, mẫu sau đã ở BÊN KIA tường — cả hai đều sạch, robot đâm xuyên. max_samples chỉ là
|
|
||||||
// trần thưa; lưới mịn hơn thì dò dày hơn, tối đa lookahead/resolution mẫu (16 ở 0.8 m / 0.05).
|
|
||||||
double step = params.lookahead / params.max_samples;
|
|
||||||
const double resolution = grid.getResolution();
|
|
||||||
if (resolution > 0.0 && step > resolution)
|
|
||||||
{
|
|
||||||
step = resolution;
|
|
||||||
}
|
|
||||||
const int samples = static_cast<int>(std::ceil(params.lookahead / step - 1.0e-9));
|
|
||||||
const double front = params.front_offset + params.padding;
|
|
||||||
const double rear = params.rear_offset + params.padding;
|
|
||||||
const double side = params.side_offset + params.padding;
|
|
||||||
const double sin0 = std::sin(yaw);
|
|
||||||
const double cos0 = std::cos(yaw);
|
|
||||||
|
|
||||||
std::vector<unsigned int> local_baseline;
|
|
||||||
std::vector<unsigned int>& baseline = baseline_scratch != nullptr ? *baseline_scratch : local_baseline;
|
|
||||||
baseline.clear();
|
|
||||||
if (params.exclude_baseline)
|
|
||||||
{
|
|
||||||
collectBaseline(grid, x, y, yaw, front, rear, side, params.cost_threshold, baseline, result);
|
|
||||||
}
|
|
||||||
result.baseline_cells = static_cast<int>(baseline.size());
|
|
||||||
|
|
||||||
for (int i = 1; i <= samples; ++i)
|
|
||||||
{
|
|
||||||
const double walk = std::min(step * i, params.lookahead); // [m] dọc cung, luôn dương
|
|
||||||
if (walk < kSkipOwnFootprintDist)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (std::fabs(kappa) * walk > params.lookahead_angle)
|
|
||||||
{
|
|
||||||
break; // quay quá góc này thì lệnh đã đổi — phần cung còn lại không phải tương lai thật
|
|
||||||
}
|
|
||||||
// Vị trí trên cung sau quãng `walk` theo chiều `direction`.
|
|
||||||
double px = 0.0;
|
|
||||||
double py = 0.0;
|
|
||||||
double pyaw = yaw;
|
|
||||||
const double s = direction * walk; // quãng có dấu
|
|
||||||
if (std::fabs(kappa) < 1.0e-6)
|
|
||||||
{
|
|
||||||
px = x + s * cos0;
|
|
||||||
py = y + s * sin0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
pyaw = yaw + kappa * s;
|
|
||||||
px = x + (std::sin(pyaw) - sin0) / kappa;
|
|
||||||
py = y - (std::cos(pyaw) - cos0) / kappa;
|
|
||||||
}
|
|
||||||
++result.scanned;
|
|
||||||
|
|
||||||
// Dò TOÀN BỘ CHU VI footprint đã đệm tại mẫu này, không phải 5 điểm rời. Năm điểm rời kéo dọc
|
|
||||||
// cung là năm đường quét, giữa chúng hở tới nửa bề rộng thân: hai trụ cách nhau 0.45 m nằm lọt
|
|
||||||
// giữa đường quét mũi (ngang 0) và đường quét hông (ngang ±0.53) — không đường nào chạm, robot
|
|
||||||
// ôm cả hai trụ mới dừng (đo 4/9: near=0.36 m @ +70° = góc trước-trái thân). Bỏ inflation thì
|
|
||||||
// trụ chỉ còn 2-3 ô, dò điểm rời chắc chắn lọt; "dò vật thật" bắt buộc đi kèm "dò kín thân".
|
|
||||||
// Chu vi lấy mẫu dày hơn một ô, và bước dọc cung không quá một ô, nên một ô vật cản muốn vào
|
|
||||||
// trong footprint bắt buộc phải cắt chu vi ở một mẫu nào đó — không còn khe hình học.
|
|
||||||
const double c = std::cos(pyaw);
|
|
||||||
const double sn = std::sin(pyaw);
|
|
||||||
const double spacing = resolution > 0.0 ? 0.5 * resolution : 0.025;
|
|
||||||
// Bốn đỉnh trong khung thân: x dọc heading, y sang trái.
|
|
||||||
const double x_min = -rear;
|
|
||||||
const double x_max = front;
|
|
||||||
const double y_max = side; // side đã gồm padding; side_offset = 0 -> dải rộng 2*padding
|
|
||||||
const double y_min = -side;
|
|
||||||
auto probe_body = [&](double bx, double by) {
|
|
||||||
const double wx = px + bx * c - by * sn;
|
|
||||||
const double wy = py + bx * sn + by * c;
|
|
||||||
if (!probeHits(grid, wx, wy, params.cost_threshold, baseline))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Ghi ô chạm theo frame robot HIỆN TẠI (không phải thân đã dịch tới mẫu s) để log đọc được.
|
|
||||||
const double dx = wx - x;
|
|
||||||
const double dy = wy - y;
|
|
||||||
result.hit_x = dx * cos0 + dy * sin0;
|
|
||||||
result.hit_y = -dx * sin0 + dy * cos0;
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
auto probe_edge = [&](int edge, double x0, double y0, double x1, double y1) {
|
|
||||||
const double len = std::hypot(x1 - x0, y1 - y0);
|
|
||||||
const int n = std::max(1, static_cast<int>(std::ceil(len / spacing)));
|
|
||||||
for (int e = 0; e <= n; ++e)
|
|
||||||
{
|
|
||||||
const double t = static_cast<double>(e) / n;
|
|
||||||
if (probe_body(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t))
|
|
||||||
{
|
|
||||||
result.hit_edge = edge;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
const bool hit = probe_edge(0, x_min, y_min, x_max, y_min) || // cạnh phải
|
|
||||||
probe_edge(1, x_max, y_min, x_max, y_max) || // cạnh mũi
|
|
||||||
probe_edge(2, x_max, y_max, x_min, y_max) || // cạnh trái
|
|
||||||
probe_edge(3, x_min, y_max, x_min, y_min); // cạnh đuôi
|
|
||||||
if (hit)
|
|
||||||
{
|
|
||||||
result.blocked = true;
|
|
||||||
result.sample = i;
|
|
||||||
result.distance = walk;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace move_base2
|
} // namespace move_base2
|
||||||
|
|||||||
@@ -366,7 +366,12 @@ bool ControllerRunner::setPlan(const std::vector<robot_geometry_msgs::PoseStampe
|
|||||||
if (costmap_lock_enabled_)
|
if (costmap_lock_enabled_)
|
||||||
{
|
{
|
||||||
if (costmap_->isSnapshotEnabled())
|
if (costmap_->isSnapshotEnabled())
|
||||||
costmap_->refreshSnapshot();
|
{
|
||||||
|
if (!snapshot_refreshed_by_loop_)
|
||||||
|
{
|
||||||
|
costmap_->refreshSnapshot();
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
|
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
|
||||||
*(costmap_->getCostmap()->getMutex()));
|
*(costmap_->getCostmap()->getMutex()));
|
||||||
@@ -526,7 +531,12 @@ bool ControllerRunner::computeVelocityCommands(robot_geometry_msgs::Twist& cmd)
|
|||||||
if (costmap_lock_enabled_)
|
if (costmap_lock_enabled_)
|
||||||
{
|
{
|
||||||
if (costmap_->isSnapshotEnabled())
|
if (costmap_->isSnapshotEnabled())
|
||||||
costmap_->refreshSnapshot();
|
{
|
||||||
|
if (!snapshot_refreshed_by_loop_)
|
||||||
|
{
|
||||||
|
costmap_->refreshSnapshot();
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
|
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
|
||||||
*(costmap_->getCostmap()->getMutex()));
|
*(costmap_->getCostmap()->getMutex()));
|
||||||
@@ -585,7 +595,12 @@ bool ControllerRunner::isGoalReached()
|
|||||||
if (costmap_lock_enabled_)
|
if (costmap_lock_enabled_)
|
||||||
{
|
{
|
||||||
if (costmap_->isSnapshotEnabled())
|
if (costmap_->isSnapshotEnabled())
|
||||||
costmap_->refreshSnapshot();
|
{
|
||||||
|
if (!snapshot_refreshed_by_loop_)
|
||||||
|
{
|
||||||
|
costmap_->refreshSnapshot();
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
|
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
|
||||||
*(costmap_->getCostmap()->getMutex()));
|
*(costmap_->getCostmap()->getMutex()));
|
||||||
@@ -647,6 +662,17 @@ void ControllerRunner::setMeasuredVelocity(const robot_geometry_msgs::Twist& vel
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
measured_velocity_ = velocity;
|
measured_velocity_ = velocity;
|
||||||
|
has_measured_velocity_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ControllerRunner::measuredVelocity(robot_geometry_msgs::Twist& out) const
|
||||||
|
{
|
||||||
|
if (!has_measured_velocity_)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out = measured_velocity_;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ControllerRunner::setTwistLinear(const robot_geometry_msgs::Vector3& linear)
|
bool ControllerRunner::setTwistLinear(const robot_geometry_msgs::Vector3& linear)
|
||||||
|
|||||||
@@ -624,7 +624,7 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
|||||||
{
|
{
|
||||||
last_valid_control_ = in.now;
|
last_valid_control_ = in.now;
|
||||||
|
|
||||||
// Phép đo 4 (PathMonitor): controller vẫn ra lệnh hợp lệ nhưng robot không tiến.
|
// kStall (EnvMonitor): controller vẫn ra lệnh hợp lệ nhưng robot không đi được đâu.
|
||||||
if (in.no_progress)
|
if (in.no_progress)
|
||||||
{
|
{
|
||||||
out.stop_planner = true;
|
out.stop_planner = true;
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
# --- Tham số runtime, dùng cho config_validation_test ----------------------------------------
|
# --- Tham số runtime, dùng cho config_validation_test ----------------------------------------
|
||||||
move_base2:
|
move_base2:
|
||||||
docking_requires_marker: false
|
|
||||||
controller_frequency: 20.0 # [Hz]
|
controller_frequency: 20.0 # [Hz]
|
||||||
planner_frequency: 0.0 # [Hz] 0 = chỉ lập plan khi cần
|
planner_frequency: 0.0 # [Hz] 0 = chỉ lập plan khi cần
|
||||||
planner_timeout: 5.0 # [s]
|
planner_timeout: 5.0 # [s]
|
||||||
@@ -70,7 +69,6 @@ root_profiles:
|
|||||||
controller_patience: 0.033333333
|
controller_patience: 0.033333333
|
||||||
max_planning_retries: 0
|
max_planning_retries: 0
|
||||||
recovery_behavior_enabled: true
|
recovery_behavior_enabled: true
|
||||||
docking_requires_marker: false
|
|
||||||
backup_global_planner: TestBackupGlobalPlanner
|
backup_global_planner: TestBackupGlobalPlanner
|
||||||
|
|
||||||
position:
|
position:
|
||||||
|
|||||||
@@ -55,33 +55,6 @@ TEST(MoveBase2Config, DefaultsAreValid)
|
|||||||
EXPECT_TRUE(config.validate(error)) << error;
|
EXPECT_TRUE(config.validate(error)) << error;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MoveBase2Config, RejectsAPartiallyDeclaredCollisionBody)
|
|
||||||
{
|
|
||||||
// Thân guard: cả ba offset = 0 (lấy từ footprint costmap) hoặc cả ba > 0. Khai hai bỏ một là mũi
|
|
||||||
// dò tâm hoặc hông không dò — config sai, không phải "mặc định".
|
|
||||||
MoveBase2Config config = minimalValid();
|
|
||||||
config.path_monitor.enabled = true;
|
|
||||||
config.path_monitor.collision_enabled = true;
|
|
||||||
config.path_monitor.collision_front_offset = 0.58; // [m]
|
|
||||||
config.path_monitor.collision_rear_offset = 0.58; // [m]
|
|
||||||
config.path_monitor.collision_side_offset = 0.0;
|
|
||||||
std::string error;
|
|
||||||
EXPECT_FALSE(config.validate(error));
|
|
||||||
EXPECT_NE(error.find("collision_side_offset"), std::string::npos) << error;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MoveBase2Config, AcceptsACollisionBodyTakenFromTheCostmapFootprint)
|
|
||||||
{
|
|
||||||
MoveBase2Config config = minimalValid();
|
|
||||||
config.path_monitor.enabled = true;
|
|
||||||
config.path_monitor.collision_enabled = true;
|
|
||||||
config.path_monitor.collision_front_offset = 0.0;
|
|
||||||
config.path_monitor.collision_rear_offset = 0.0;
|
|
||||||
config.path_monitor.collision_side_offset = 0.0;
|
|
||||||
std::string error;
|
|
||||||
EXPECT_TRUE(config.validate(error)) << error;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MoveBase2Config, RejectsRecoveryEnabledWithNoBehaviorLoaded)
|
TEST(MoveBase2Config, RejectsRecoveryEnabledWithNoBehaviorLoaded)
|
||||||
{
|
{
|
||||||
// Ràng buộc thứ tự khởi tạo: RecoveryRunner không nạp được behavior nào mà
|
// Ràng buộc thứ tự khởi tạo: RecoveryRunner không nạp được behavior nào mà
|
||||||
@@ -188,22 +161,21 @@ TEST(MoveBase2Config, ReadsEveryGroupFromYaml)
|
|||||||
EXPECT_EQ(config.global_frame, "map");
|
EXPECT_EQ(config.global_frame, "map");
|
||||||
EXPECT_EQ(config.robot_base_frame, "base_link");
|
EXPECT_EQ(config.robot_base_frame, "base_link");
|
||||||
EXPECT_EQ(config.recovery_namespace, "recovery");
|
EXPECT_EQ(config.recovery_namespace, "recovery");
|
||||||
EXPECT_FALSE(config.docking_requires_marker);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MoveBase2Config, MissionLayerIsEnabledByDefault)
|
TEST(MoveBase2Config, MissionNamespaceHasADefault)
|
||||||
{
|
{
|
||||||
// Mặc định bật: order VDA5050 đi qua mission layer và được cắt thành chặng. Đổi mặc định này là
|
// Mission layer LUÔN được dựng (2026-09-06 bỏ khoá bật/tắt), nên namespace mặc định là thứ duy
|
||||||
// đổi hành vi của mọi order trên robot, nên nó được khoá lại bằng test.
|
// nhất còn quyết định layer đọc `mission_sources` ở đâu. Đổi nó là đổi hành vi của mọi order.
|
||||||
const MoveBase2Config defaults;
|
const MoveBase2Config defaults;
|
||||||
EXPECT_TRUE(defaults.mission_layer_enabled);
|
|
||||||
EXPECT_EQ(defaults.mission_namespace, "mission_adapters");
|
EXPECT_EQ(defaults.mission_namespace, "mission_adapters");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MoveBase2Config, RejectsEnabledMissionLayerWithoutNamespace)
|
TEST(MoveBase2Config, RejectsAnEmptyMissionNamespace)
|
||||||
{
|
{
|
||||||
|
// Không còn đường "tắt layer để khỏi cần namespace": rỗng là boot với một layer không đọc được
|
||||||
|
// nguồn nào, tức order im lặng đi thẳng — phải chặn ở validate.
|
||||||
MoveBase2Config config = withRecoveryCount(loadFrom("move_base2"), 2);
|
MoveBase2Config config = withRecoveryCount(loadFrom("move_base2"), 2);
|
||||||
config.mission_layer_enabled = true;
|
|
||||||
config.mission_namespace.clear();
|
config.mission_namespace.clear();
|
||||||
|
|
||||||
std::string error;
|
std::string error;
|
||||||
@@ -211,16 +183,6 @@ TEST(MoveBase2Config, RejectsEnabledMissionLayerWithoutNamespace)
|
|||||||
EXPECT_NE(error.find("mission_namespace"), std::string::npos) << error;
|
EXPECT_NE(error.find("mission_namespace"), std::string::npos) << error;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MoveBase2Config, DisabledMissionLayerDoesNotNeedANamespace)
|
|
||||||
{
|
|
||||||
MoveBase2Config config = withRecoveryCount(loadFrom("move_base2"), 2);
|
|
||||||
config.mission_layer_enabled = false;
|
|
||||||
config.mission_namespace.clear();
|
|
||||||
|
|
||||||
std::string error;
|
|
||||||
EXPECT_TRUE(config.validate(error)) << error;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MoveBase2Config, ReadsProfileBindingsFromNestedNamespaces)
|
TEST(MoveBase2Config, ReadsProfileBindingsFromNestedNamespaces)
|
||||||
{
|
{
|
||||||
const MoveBase2Config config = loadFrom("move_base2");
|
const MoveBase2Config config = loadFrom("move_base2");
|
||||||
@@ -257,7 +219,6 @@ TEST(MoveBase2ConfigRootProfile, LoadsIndependentPlannerPairsWithoutTheLegacyAda
|
|||||||
ASSERT_NE(trolley, config.docking_marker_profiles.end());
|
ASSERT_NE(trolley, config.docking_marker_profiles.end());
|
||||||
EXPECT_EQ(trolley->second.global_planner_name, "TestTrolleyDockPlanner");
|
EXPECT_EQ(trolley->second.global_planner_name, "TestTrolleyDockPlanner");
|
||||||
EXPECT_EQ(trolley->second.local_planner_name, "TestTrolleyDockLocalPlanner");
|
EXPECT_EQ(trolley->second.local_planner_name, "TestTrolleyDockLocalPlanner");
|
||||||
EXPECT_FALSE(config.docking_requires_marker);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MoveBase2Config, LoadedConfigValidates)
|
TEST(MoveBase2Config, LoadedConfigValidates)
|
||||||
|
|||||||
1014
test/env_monitor_test.cpp
Normal file
1014
test/env_monitor_test.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -29,8 +29,8 @@
|
|||||||
#include <move_base2/ports/clock_port.h>
|
#include <move_base2/ports/clock_port.h>
|
||||||
#include <move_base2/ports/controller_port.h>
|
#include <move_base2/ports/controller_port.h>
|
||||||
#include <move_base2/ports/costmap_status_port.h>
|
#include <move_base2/ports/costmap_status_port.h>
|
||||||
|
#include <move_base2/ports/environment_port.h>
|
||||||
#include <move_base2/ports/mission_port.h>
|
#include <move_base2/ports/mission_port.h>
|
||||||
#include <move_base2/ports/path_check_port.h>
|
|
||||||
#include <move_base2/ports/planner_port.h>
|
#include <move_base2/ports/planner_port.h>
|
||||||
#include <move_base2/ports/pose_port.h>
|
#include <move_base2/ports/pose_port.h>
|
||||||
#include <move_base2/ports/recovery_port.h>
|
#include <move_base2/ports/recovery_port.h>
|
||||||
@@ -606,6 +606,11 @@ public:
|
|||||||
break;
|
break;
|
||||||
case RecoveryScript::kSucceeded:
|
case RecoveryScript::kSucceeded:
|
||||||
tick.status = RecoveryTick::Status::kSucceeded;
|
tick.status = RecoveryTick::Status::kSucceeded;
|
||||||
|
if (succeed_with_path_)
|
||||||
|
{
|
||||||
|
tick.has_path = true;
|
||||||
|
tick.path = success_path_;
|
||||||
|
}
|
||||||
active_ = false;
|
active_ = false;
|
||||||
break;
|
break;
|
||||||
case RecoveryScript::kFailed:
|
case RecoveryScript::kFailed:
|
||||||
@@ -616,6 +621,13 @@ public:
|
|||||||
return tick;
|
return tick;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Thành công thì kèm một detour path (như 'detour_path' thật) — cần cho stand-down của kContact.
|
||||||
|
void setSucceedWithPath(std::vector<robot_geometry_msgs::PoseStamped> path)
|
||||||
|
{
|
||||||
|
succeed_with_path_ = true;
|
||||||
|
success_path_ = std::move(path);
|
||||||
|
}
|
||||||
|
|
||||||
void cancel() override
|
void cancel() override
|
||||||
{
|
{
|
||||||
++cancel_count_;
|
++cancel_count_;
|
||||||
@@ -699,6 +711,8 @@ private:
|
|||||||
std::size_t index_ = 0;
|
std::size_t index_ = 0;
|
||||||
|
|
||||||
bool start_succeeds_ = true;
|
bool start_succeeds_ = true;
|
||||||
|
bool succeed_with_path_ = false;
|
||||||
|
std::vector<robot_geometry_msgs::PoseStamped> success_path_;
|
||||||
bool active_ = false;
|
bool active_ = false;
|
||||||
bool emits_velocity_ = false;
|
bool emits_velocity_ = false;
|
||||||
double recovery_speed_ = -0.1; ///< [m/s], âm = lùi
|
double recovery_speed_ = -0.1; ///< [m/s], âm = lùi
|
||||||
@@ -864,68 +878,50 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class FakePathCheckPort
|
* @class FakeEnvironmentPort
|
||||||
* @brief Cổng kiểm tuyến giả: kịch bản quyết định tuyến sạch hay bẩn, không cần costmap thật.
|
* @brief Cổng môi trường giả cho EnvMonitor: kịch bản quyết định cost_to_go / tiếp xúc, không cần costmap.
|
||||||
*
|
*
|
||||||
* `available_ = false` mô phỏng "không kết luận được" (thiếu TF/costmap) — bên gọi phải coi đó là
|
* Mặc định "không kết luận được" cho hình học và quỹ đạo (như cổng gốc thiếu costmap/TF) nhưng CÓ thân
|
||||||
* không biết, KHÔNG được suy thành sạch hay bẩn.
|
* (footprint) — để các test cũ chạy với EnvMonitor bật mà không sinh verdict nào.
|
||||||
*/
|
*/
|
||||||
class FakePathCheckPort final : public PathCheckPort
|
class FakeEnvironmentPort final : public EnvironmentPort
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
bool checkPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
bool sampleGeometry(const EnvGeometryRequest& request, EnvGeometry& out) override
|
||||||
std::size_t from_index, double horizon, PlanBlockResult& out) const override
|
|
||||||
{
|
{
|
||||||
(void)from_index;
|
(void)request;
|
||||||
(void)horizon;
|
++geometry_query_count_;
|
||||||
++query_count_;
|
if (!geometry_available_)
|
||||||
|
|
||||||
if (!available_)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
out = EnvGeometry();
|
||||||
out = PlanBlockResult();
|
out.valid = true;
|
||||||
out.blocked = blocked_;
|
out.free_radius = body_side_;
|
||||||
out.distance = distance_;
|
out.cost.conclusive = true;
|
||||||
out.scanned = plan.size();
|
out.cost.reachable = reachable_;
|
||||||
|
out.cost.cost_to_go = reachable_ ? cost_to_go_ : std::numeric_limits<double>::infinity();
|
||||||
|
out.edt_body_min = edt_body_min_;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @param distance [m] khoảng cách tới vật cản — bên gọi so với `min_detour_distance`.
|
bool sweepTrajectory(const EnvTrajectoryRequest& request, SweepResult& out, bool& used_local_plan) override
|
||||||
void setBlocked(bool blocked, double distance = 1.5)
|
|
||||||
{
|
{
|
||||||
blocked_ = blocked;
|
(void)request;
|
||||||
distance_ = distance;
|
++sweep_query_count_;
|
||||||
}
|
used_local_plan = false;
|
||||||
|
if (!sweep_available_)
|
||||||
/// Phép đo 3 (cung lệnh): kịch bản riêng, không dính `setBlocked` của phép đo 1.
|
|
||||||
bool checkMotion(const robot_geometry_msgs::PoseStamped& robot_pose, double v, double omega,
|
|
||||||
const MotionCheckParams& params, MotionBlockResult& out) const override
|
|
||||||
{
|
|
||||||
(void)robot_pose;
|
|
||||||
(void)omega;
|
|
||||||
(void)params;
|
|
||||||
++motion_query_count_;
|
|
||||||
if (!motion_available_)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
out = MotionBlockResult();
|
out = SweepResult();
|
||||||
out.scanned = std::fabs(v) >= params.min_speed ? 1 : 0;
|
out.conclusive = true;
|
||||||
out.blocked = out.scanned > 0 && motion_blocked_;
|
out.scanned = 1;
|
||||||
out.sample = out.blocked ? 1 : -1;
|
out.blocked = contact_blocked_;
|
||||||
out.distance = out.blocked ? motion_distance_ : 0.0;
|
out.distance = contact_blocked_ ? contact_distance_ : 0.0;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @param distance [m] quãng dọc cung tới điểm chạm — bên gọi so với `collision_stop_distance`.
|
|
||||||
void setMotionBlocked(bool blocked, double distance = 0.2)
|
|
||||||
{
|
|
||||||
motion_blocked_ = blocked;
|
|
||||||
motion_distance_ = distance;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool robotBodyExtents(double& front, double& rear, double& side) const override
|
bool robotBodyExtents(double& front, double& rear, double& side) const override
|
||||||
{
|
{
|
||||||
if (!body_available_)
|
if (!body_available_)
|
||||||
@@ -938,7 +934,33 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Giả lập footprint costmap; mặc định KHÔNG có (như PathCheckPort gốc).
|
/// @param distance [m] quãng dọc quỹ đạo tới điểm chạm.
|
||||||
|
void setContact(bool blocked, double distance = 0.1)
|
||||||
|
{
|
||||||
|
sweep_available_ = true;
|
||||||
|
contact_blocked_ = blocked;
|
||||||
|
contact_distance_ = distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setSweepAvailable(bool available)
|
||||||
|
{
|
||||||
|
sweep_available_ = available;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @param cost_to_go [m] chi phí còn lại; @p edt_body_min [m] khoảng trống thân.
|
||||||
|
void setGeometry(bool reachable, double cost_to_go, double edt_body_min = 1.0)
|
||||||
|
{
|
||||||
|
geometry_available_ = true;
|
||||||
|
reachable_ = reachable;
|
||||||
|
cost_to_go_ = cost_to_go;
|
||||||
|
edt_body_min_ = edt_body_min;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setGeometryAvailable(bool available)
|
||||||
|
{
|
||||||
|
geometry_available_ = available;
|
||||||
|
}
|
||||||
|
|
||||||
void setBodyExtents(double front, double rear, double side)
|
void setBodyExtents(double front, double rear, double side)
|
||||||
{
|
{
|
||||||
body_available_ = true;
|
body_available_ = true;
|
||||||
@@ -947,39 +969,35 @@ public:
|
|||||||
body_side_ = side;
|
body_side_ = side;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMotionAvailable(bool available)
|
void setBodyAvailable(bool available)
|
||||||
{
|
{
|
||||||
motion_available_ = available;
|
body_available_ = available;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t motionQueryCount() const
|
std::size_t sweepQueryCount() const
|
||||||
{
|
{
|
||||||
return motion_query_count_;
|
return sweep_query_count_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setAvailable(bool available)
|
std::size_t geometryQueryCount() const
|
||||||
{
|
{
|
||||||
available_ = available;
|
return geometry_query_count_;
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t queryCount() const
|
|
||||||
{
|
|
||||||
return query_count_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool blocked_ = false;
|
bool geometry_available_ = false;
|
||||||
bool available_ = true;
|
bool reachable_ = true;
|
||||||
double distance_ = 1.5; // [m]
|
double cost_to_go_ = 10.0; // [m]
|
||||||
mutable std::size_t query_count_ = 0;
|
double edt_body_min_ = 1.0; // [m]
|
||||||
bool motion_blocked_ = false;
|
bool sweep_available_ = false;
|
||||||
bool body_available_ = false;
|
bool contact_blocked_ = false;
|
||||||
double body_front_ = 0.0;
|
double contact_distance_ = 0.1; // [m]
|
||||||
double body_rear_ = 0.0;
|
bool body_available_ = true;
|
||||||
double body_side_ = 0.0;
|
double body_front_ = 0.58; // [m]
|
||||||
bool motion_available_ = true;
|
double body_rear_ = 0.58; // [m]
|
||||||
double motion_distance_ = 0.2; // [m]
|
double body_side_ = 0.48; // [m]
|
||||||
mutable std::size_t motion_query_count_ = 0;
|
std::size_t geometry_query_count_ = 0;
|
||||||
|
std::size_t sweep_query_count_ = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FakeMissionPort final : public MissionPort
|
class FakeMissionPort final : public MissionPort
|
||||||
|
|||||||
734
test/grid_analysis_test.cpp
Normal file
734
test/grid_analysis_test.cpp
Normal file
@@ -0,0 +1,734 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
*
|
||||||
|
* Software License Agreement (BSD License)
|
||||||
|
*
|
||||||
|
* Kiểm grid_analysis: EDT, mặt nạ tự do, trường địa trắc (cấm cắt góc), cost_to_go theo plan, hành
|
||||||
|
* lang vuông góc với plan, khoảng trống thân, kéo footprint dọc quỹ đạo.
|
||||||
|
*
|
||||||
|
* Lưới 10 x 10 m, ô 0.05 m — cùng resolution với local costmap runtime. Thân 1.166 x 0.96 m như
|
||||||
|
* footprint sim (front = rear = 0.583, side = 0.48), inscribed 0.48.
|
||||||
|
*
|
||||||
|
* Author: DuongTD
|
||||||
|
*********************************************************************/
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <limits>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <nav_test_harness/fake_costmap.h>
|
||||||
|
#include <robot_costmap_2d/cost_values.h>
|
||||||
|
|
||||||
|
#include <move_base2/core/grid_analysis.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
using move_base2::analyzeGrid;
|
||||||
|
using move_base2::bodyClearance;
|
||||||
|
using move_base2::computeGeodesic;
|
||||||
|
using move_base2::CorridorParams;
|
||||||
|
using move_base2::CorridorResult;
|
||||||
|
using move_base2::costToGo;
|
||||||
|
using move_base2::CostToGoParams;
|
||||||
|
using move_base2::CostToGoResult;
|
||||||
|
using move_base2::distanceAt;
|
||||||
|
using move_base2::GridScratch;
|
||||||
|
using move_base2::PlanarTransform;
|
||||||
|
using move_base2::scanCorridor;
|
||||||
|
using move_base2::SweepParams;
|
||||||
|
using move_base2::SweepResult;
|
||||||
|
using move_base2::sweepTrajectory;
|
||||||
|
|
||||||
|
constexpr double kRes = 0.05;
|
||||||
|
constexpr double kInscribed = 0.48;
|
||||||
|
constexpr unsigned char kLethal = robot_costmap_2d::LETHAL_OBSTACLE;
|
||||||
|
constexpr double kInf = std::numeric_limits<double>::infinity();
|
||||||
|
|
||||||
|
robot_geometry_msgs::PoseStamped makePose(double x, double y, double yaw = 0.0)
|
||||||
|
{
|
||||||
|
robot_geometry_msgs::PoseStamped pose;
|
||||||
|
pose.header.frame_id = "odom";
|
||||||
|
pose.pose.position.x = x;
|
||||||
|
pose.pose.position.y = y;
|
||||||
|
pose.pose.orientation.z = std::sin(0.5 * yaw);
|
||||||
|
pose.pose.orientation.w = std::cos(0.5 * yaw);
|
||||||
|
return pose;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<robot_geometry_msgs::PoseStamped> linePlan(double from_x, double to_x, double step,
|
||||||
|
double y = 0.0)
|
||||||
|
{
|
||||||
|
std::vector<robot_geometry_msgs::PoseStamped> plan;
|
||||||
|
for (double x = from_x; x <= to_x + 1e-9; x += step)
|
||||||
|
{
|
||||||
|
plan.push_back(makePose(x, y));
|
||||||
|
}
|
||||||
|
return plan;
|
||||||
|
}
|
||||||
|
|
||||||
|
nav_test_harness::FakeCostmap makeGrid()
|
||||||
|
{
|
||||||
|
return nav_test_harness::FakeCostmap::centered(10.0, kRes);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Vạch ô lethal thẳng từ (x0,y0) tới (x1,y1), bước nửa ô.
|
||||||
|
void lethalLine(nav_test_harness::FakeCostmap& grid, double x0, double y0, double x1, double y1)
|
||||||
|
{
|
||||||
|
const double len = std::hypot(x1 - x0, y1 - y0);
|
||||||
|
const int n = std::max(1, static_cast<int>(std::ceil(len / (0.5 * kRes))));
|
||||||
|
for (int i = 0; i <= n; ++i)
|
||||||
|
{
|
||||||
|
const double t = static_cast<double>(i) / n;
|
||||||
|
grid.setLethalAt(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cellOf(const GridScratch& s, double x, double y, unsigned int& mx, unsigned int& my)
|
||||||
|
{
|
||||||
|
return s.toCell(x, y, mx, my);
|
||||||
|
}
|
||||||
|
|
||||||
|
double geoAt(const GridScratch& s, double x, double y)
|
||||||
|
{
|
||||||
|
unsigned int mx = 0;
|
||||||
|
unsigned int my = 0;
|
||||||
|
if (!cellOf(s, x, y, mx, my))
|
||||||
|
{
|
||||||
|
return std::numeric_limits<double>::quiet_NaN();
|
||||||
|
}
|
||||||
|
return s.geo[s.index(mx, my)];
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------ EDT
|
||||||
|
|
||||||
|
TEST(GridAnalysisEdt, EmptyGridIsAllFreeAndInfinitelyFar)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
EXPECT_TRUE(std::isinf(distanceAt(s, 0.0, 0.0)));
|
||||||
|
unsigned int mx = 0;
|
||||||
|
unsigned int my = 0;
|
||||||
|
ASSERT_TRUE(cellOf(s, 2.0, -3.0, mx, my));
|
||||||
|
EXPECT_EQ(1, s.free[s.index(mx, my)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisEdt, DistanceToASingleLethalCellIsEuclidean)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalAt(0.0, 0.0);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
|
||||||
|
EXPECT_NEAR(0.0, distanceAt(s, 0.0, 0.0), 1e-6);
|
||||||
|
EXPECT_NEAR(1.0, distanceAt(s, 1.0, 0.0), 1.5 * kRes);
|
||||||
|
EXPECT_NEAR(std::hypot(0.5, 0.5), distanceAt(s, 0.5, 0.5), 1.5 * kRes);
|
||||||
|
EXPECT_NEAR(3.0, distanceAt(s, -3.0, 0.0), 1.5 * kRes);
|
||||||
|
|
||||||
|
// Mặt nạ tự do theo inscribed: 0.3 m thì tâm không đặt được, 0.6 m thì được.
|
||||||
|
unsigned int mx = 0;
|
||||||
|
unsigned int my = 0;
|
||||||
|
ASSERT_TRUE(cellOf(s, 0.3, 0.0, mx, my));
|
||||||
|
EXPECT_EQ(0, s.free[s.index(mx, my)]);
|
||||||
|
ASSERT_TRUE(cellOf(s, 0.6, 0.0, mx, my));
|
||||||
|
EXPECT_EQ(1, s.free[s.index(mx, my)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisEdt, UnknownAndInflationAreNotObstacles)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setCostAt(0.0, 0.0, robot_costmap_2d::NO_INFORMATION);
|
||||||
|
grid.setCostAt(1.0, 1.0, robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
EXPECT_TRUE(std::isinf(distanceAt(s, 0.0, 0.0)));
|
||||||
|
EXPECT_TRUE(std::isinf(distanceAt(s, 1.0, 1.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisEdt, OutsideTheGridIsNaN)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
EXPECT_TRUE(std::isnan(distanceAt(s, 7.0, 0.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------- geodesic
|
||||||
|
|
||||||
|
TEST(GridAnalysisGeodesic, OpenFieldMatchesOctileDistance)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
unsigned int tx = 0;
|
||||||
|
unsigned int ty = 0;
|
||||||
|
ASSERT_TRUE(cellOf(s, 0.0, 0.0, tx, ty));
|
||||||
|
std::size_t visited = 0;
|
||||||
|
ASSERT_TRUE(computeGeodesic(tx, ty, s, &visited));
|
||||||
|
EXPECT_GT(visited, 0u);
|
||||||
|
|
||||||
|
EXPECT_NEAR(0.0, geoAt(s, 0.0, 0.0), 1e-6);
|
||||||
|
EXPECT_NEAR(2.0, geoAt(s, 2.0, 0.0), 2.0 * kRes);
|
||||||
|
EXPECT_NEAR(2.0 * std::sqrt(2.0), geoAt(s, 2.0, 2.0), 2.0 * kRes);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisGeodesic, WallForcesTheWayAround)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
lethalLine(grid, 1.0, -1.5, 1.0, 1.5);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
unsigned int tx = 0;
|
||||||
|
unsigned int ty = 0;
|
||||||
|
ASSERT_TRUE(cellOf(s, 0.0, 0.0, tx, ty));
|
||||||
|
ASSERT_TRUE(computeGeodesic(tx, ty, s));
|
||||||
|
|
||||||
|
const double behind = geoAt(s, 2.0, 0.0);
|
||||||
|
EXPECT_TRUE(std::isfinite(behind));
|
||||||
|
// Thẳng là 2.0 m; phải vòng qua đầu tường (1.5 m + inscribed) nên dài hơn hẳn.
|
||||||
|
EXPECT_GT(behind, 3.5);
|
||||||
|
// Ô ngay trong dải cấm quanh tường không có đường (không free).
|
||||||
|
EXPECT_TRUE(std::isinf(geoAt(s, 1.0, 0.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisGeodesic, SealedRoomIsUnreachable)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
lethalLine(grid, -1.5, -1.5, 1.5, -1.5);
|
||||||
|
lethalLine(grid, 1.5, -1.5, 1.5, 1.5);
|
||||||
|
lethalLine(grid, 1.5, 1.5, -1.5, 1.5);
|
||||||
|
lethalLine(grid, -1.5, 1.5, -1.5, -1.5);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
unsigned int tx = 0;
|
||||||
|
unsigned int ty = 0;
|
||||||
|
ASSERT_TRUE(cellOf(s, 0.0, 0.0, tx, ty));
|
||||||
|
ASSERT_TRUE(computeGeodesic(tx, ty, s));
|
||||||
|
EXPECT_TRUE(std::isfinite(geoAt(s, 0.5, 0.5)));
|
||||||
|
EXPECT_TRUE(std::isinf(geoAt(s, 3.0, 3.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisGeodesic, DiagonalWallOfSingleCellsIsNotLeaked)
|
||||||
|
{
|
||||||
|
// Bán kính free ~0 để mặt nạ = "không lethal": tường chéo gồm các ô (i, i) chỉ chạm nhau ở góc.
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
for (int i = -100; i < 100; ++i) // trọn đường chéo của lưới 200 ô, không để hở hai đầu
|
||||||
|
{
|
||||||
|
grid.setLethalAt(i * kRes + 0.5 * kRes, i * kRes + 0.5 * kRes);
|
||||||
|
}
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, 0.01, s));
|
||||||
|
unsigned int tx = 0;
|
||||||
|
unsigned int ty = 0;
|
||||||
|
ASSERT_TRUE(cellOf(s, 1.0, -1.0, tx, ty));
|
||||||
|
ASSERT_TRUE(computeGeodesic(tx, ty, s));
|
||||||
|
// Không cắt góc: bên kia tường chéo phải là +inf, cùng bên thì tới được.
|
||||||
|
EXPECT_TRUE(std::isinf(geoAt(s, -1.0, 1.0)));
|
||||||
|
EXPECT_TRUE(std::isfinite(geoAt(s, 2.0, -2.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisGeodesic, TargetInsideTheForbiddenBandIsRejected)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalAt(0.0, 0.0);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
unsigned int tx = 0;
|
||||||
|
unsigned int ty = 0;
|
||||||
|
ASSERT_TRUE(cellOf(s, 0.2, 0.0, tx, ty));
|
||||||
|
EXPECT_FALSE(computeGeodesic(tx, ty, s));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------------- cost to go
|
||||||
|
|
||||||
|
TEST(GridAnalysisCostToGo, StraightPlanInTheWindowEqualsRemainingLength)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(-3.0, 3.0, 0.1);
|
||||||
|
|
||||||
|
CostToGoResult r = costToGo(plan, 0, -3.0, 0.0, CostToGoParams(), PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_TRUE(r.reachable);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kGoal, r.target);
|
||||||
|
EXPECT_NEAR(6.0, r.cost_to_go, 0.15);
|
||||||
|
EXPECT_NEAR(0.0, r.arc_rest, 1e-9);
|
||||||
|
|
||||||
|
// Robot giữa plan: còn 3 m.
|
||||||
|
r = costToGo(plan, 30, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_NEAR(3.0, r.cost_to_go, 0.15);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCostToGo, LateralDeviationCostsTheOctileDetour)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(-3.0, 3.0, 0.1);
|
||||||
|
// Robot lệch 1.5 m ngang tại x = 0: đường 8-liên-thông tới (3, 0) dài 1.5*sqrt2 + 1.5 = 3.62 m.
|
||||||
|
const CostToGoResult r = costToGo(plan, 30, 0.0, 1.5, CostToGoParams(), PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_TRUE(r.reachable);
|
||||||
|
EXPECT_NEAR(3.62, r.cost_to_go, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCostToGo, PlanLeavingTheWindowUsesTheExitPointPlusRemainingArc)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(0.0, 8.0, 0.1); // rời lưới ở x = 5
|
||||||
|
const CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_TRUE(r.reachable);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kExit, r.target);
|
||||||
|
EXPECT_GT(r.arc_rest, 2.9);
|
||||||
|
EXPECT_LT(r.arc_rest, 3.2);
|
||||||
|
EXPECT_NEAR(8.0, r.cost_to_go, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCostToGo, ExitAheadPinsTheTargetAtAFixedArcLength)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(0.0, 8.0, 0.1); // rời lưới ở x = 5
|
||||||
|
CostToGoParams params;
|
||||||
|
params.exit_ahead = 2.0;
|
||||||
|
const CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_TRUE(r.reachable);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kAhead, r.target);
|
||||||
|
EXPECT_EQ(20u, r.target_index); // pose ở x = 2.0
|
||||||
|
EXPECT_NEAR(6.0, r.arc_rest, 0.05); // 8 − 2
|
||||||
|
EXPECT_NEAR(8.0, r.cost_to_go, 0.2); // geo 2 + arc 6: cùng tổng với bản cũ khi đi thẳng
|
||||||
|
// Cửa sổ trôi không kéo T lùi: robot tiến 1 m -> from_index 10 -> T tiến theo (x = 3.0), arc_rest giảm đúng 1 m.
|
||||||
|
const CostToGoResult r2 = costToGo(plan, 10, 1.0, 0.0, params, PlanarTransform(), s);
|
||||||
|
EXPECT_EQ(30u, r2.target_index);
|
||||||
|
EXPECT_NEAR(5.0, r2.arc_rest, 0.05);
|
||||||
|
// Quãng cố định vượt cửa sổ thì vẫn kẹp ở điểm rời cửa sổ (kExit).
|
||||||
|
params.exit_ahead = 7.0;
|
||||||
|
const CostToGoResult r3 = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kExit, r3.target);
|
||||||
|
// Goal trong quãng thì T = goal.
|
||||||
|
params.exit_ahead = 2.0;
|
||||||
|
const CostToGoResult r4 = costToGo(linePlan(0.0, 1.0, 0.1), 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kGoal, r4.target);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCostToGo, BlockedExitBacksOffAlongThePlanOrReportsBlocked)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalCircle(3.0, 0.0, 0.3);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(0.0, 3.0, 0.1);
|
||||||
|
|
||||||
|
// T = (3, 0) nằm trong vật cản; ô free gần nhất dọc plan cách ~0.78 m > backoff 0.5 -> đích bị bịt.
|
||||||
|
CostToGoParams params;
|
||||||
|
params.target_backoff = 0.5;
|
||||||
|
CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_FALSE(r.reachable);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kBlockedExit, r.target);
|
||||||
|
|
||||||
|
// Cho lùi 1.0 m thì tìm được T free và có đường.
|
||||||
|
params.target_backoff = 1.0;
|
||||||
|
r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_TRUE(r.reachable);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kBackedOff, r.target);
|
||||||
|
EXPECT_GT(r.arc_rest, 0.7);
|
||||||
|
EXPECT_NEAR(3.0, r.cost_to_go, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCostToGo, TargetHuggingAWallSnapsSidewaysInsteadOfBlocking)
|
||||||
|
{
|
||||||
|
// Run 6/9 10:15: SBPL vẽ plan ôm mép vật cản trong hành lang rộng — T (2 m phía trước) nằm trong vành 0,48 m dù
|
||||||
|
// hành lang thừa chỗ. Lùi dọc plan không thoát (cả đoạn ôm tường); snap ngang 0,5 m là ra ô free -> có đường.
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
lethalLine(grid, 0.5, 0.45, 4.5, 0.45); // tường song song plan, cách 0,45 m < inscribed 0,48
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(0.0, 4.0, 0.1);
|
||||||
|
CostToGoParams params;
|
||||||
|
params.exit_ahead = 2.0;
|
||||||
|
params.target_backoff = 0.5;
|
||||||
|
params.target_snap = 0.0;
|
||||||
|
CostToGoResult r = costToGo(plan, 0, 0.0, -0.3, params, PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_FALSE(r.reachable);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kBlockedExit, r.target) << "bản cũ: ôm tường = 'đích bị bịt'";
|
||||||
|
params.target_snap = 0.5;
|
||||||
|
r = costToGo(plan, 0, 0.0, -0.3, params, PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_TRUE(r.reachable);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kSnapped, r.target);
|
||||||
|
EXPECT_EQ(20u, r.target_index); // vẫn là pose plan ở 2 m
|
||||||
|
EXPECT_NEAR(2.0, r.arc_rest, 0.05); // arc_rest theo index, không theo ô snap
|
||||||
|
EXPECT_NEAR(4.0, r.cost_to_go, 0.3); // geo ~2 m tới ô snap + arc 2 m
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCostToGo, BlockerAcrossTheCorridorStaysBlockedWhenSnapCannotEscape)
|
||||||
|
{
|
||||||
|
// Cụm trụ chắn ngang: ô free gần nhất quanh T cách 0,78 m > target_snap 0,5 -> vẫn là đích bị bịt (unreachable
|
||||||
|
// thật, nổ sớm từ đất sạch). Snap rộng hơn (1,0) thì ra ô bên cạnh và có đường vòng.
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalCircle(2.0, 0.0, 0.3);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(0.0, 4.0, 0.1);
|
||||||
|
CostToGoParams params;
|
||||||
|
params.exit_ahead = 2.0;
|
||||||
|
params.target_backoff = 0.5;
|
||||||
|
params.target_snap = 0.5;
|
||||||
|
CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_FALSE(r.reachable);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kBlockedExit, r.target);
|
||||||
|
params.target_snap = 1.0;
|
||||||
|
r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_TRUE(r.reachable);
|
||||||
|
EXPECT_EQ(CostToGoResult::Target::kSnapped, r.target);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCostToGo, RobotHuggingAnObstacleSnapsToTheNearestFreeCell)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
lethalLine(grid, -3.0, 0.4, 3.0, 0.4); // tường sát bên trái tuyến
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(-3.0, 3.0, 0.1, -0.6);
|
||||||
|
// Robot tại (0, 0.1): cách tường 0.3 < inscribed -> ô không free, snap trong 0.35 m.
|
||||||
|
const CostToGoResult r = costToGo(plan, 30, 0.0, 0.1, CostToGoParams(), PlanarTransform(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_TRUE(r.robot_in_ring);
|
||||||
|
EXPECT_TRUE(r.reachable);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCostToGo, EmptyPlanOrRobotOutsideIsInconclusive)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
std::vector<robot_geometry_msgs::PoseStamped> empty;
|
||||||
|
EXPECT_FALSE(costToGo(empty, 0, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s).conclusive);
|
||||||
|
const auto plan = linePlan(0.0, 3.0, 0.1);
|
||||||
|
EXPECT_FALSE(costToGo(plan, 0, 7.0, 0.0, CostToGoParams(), PlanarTransform(), s).conclusive);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCostToGo, TransformIsAppliedToThePlan)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
// Plan trong frame "map" = lưới dịch +10 m: (10..13, 0) map -> (0..3, 0) lưới.
|
||||||
|
const auto plan = linePlan(10.0, 13.0, 0.1);
|
||||||
|
PlanarTransform map_to_grid;
|
||||||
|
map_to_grid.x = -10.0;
|
||||||
|
const CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, CostToGoParams(), map_to_grid, s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_NEAR(3.0, r.cost_to_go, 0.15);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------- corridor
|
||||||
|
|
||||||
|
TEST(GridAnalysisCorridor, GapNarrowerThanTheBodyIsABlock)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalCircle(1.0, 0.6, 0.2); // mép trong ở y = +0.4
|
||||||
|
grid.setLethalCircle(1.0, -0.6, 0.2); // mép trong ở y = -0.4 -> khe 0.8 m < thân 0.96
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(-2.0, 3.0, 0.1);
|
||||||
|
const CorridorResult r = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s);
|
||||||
|
EXPECT_GT(r.scanned, 0u);
|
||||||
|
EXPECT_NEAR(0.8, r.min_width, 0.12);
|
||||||
|
EXPECT_NEAR(3.0, r.block_dist, 0.25);
|
||||||
|
// Tâm robot không đặt được ở khe (0.4 < 0.48): plan hết sạch trước chỗ khe.
|
||||||
|
EXPECT_LT(r.plan_clear_ahead, 3.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCorridor, GapWiderThanTheBodyIsNotABlock)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalCircle(1.0, 0.8, 0.2); // mép trong ở y = +0.6 -> khe 1.2 m
|
||||||
|
grid.setLethalCircle(1.0, -0.8, 0.2);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(-2.0, 3.0, 0.1);
|
||||||
|
const CorridorResult r = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s);
|
||||||
|
EXPECT_TRUE(std::isinf(r.block_dist));
|
||||||
|
EXPECT_NEAR(1.2, r.min_width, 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCorridor, WallOnOneSideWithRoomOnTheOtherIsNotABlock)
|
||||||
|
{
|
||||||
|
// Ca mission 1 ngày 5/9: plan sượt tường nhưng bên kia trống -> hành lang vẫn rộng.
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
lethalLine(grid, 0.0, 0.3, 2.0, 0.3);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(-2.0, 3.0, 0.1);
|
||||||
|
const CorridorResult r = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s);
|
||||||
|
EXPECT_TRUE(std::isinf(r.block_dist));
|
||||||
|
EXPECT_NEAR(0.3 + 1.5, r.min_width, 0.12); // trái 0.3 + phải chạm trần 1.5
|
||||||
|
// Tâm cách đầu tường (0, 0.3) đúng 0.48 m tại x = -sqrt(0.48^2 - 0.3^2) = -0.375 -> 1.625 m từ x = -2.
|
||||||
|
EXPECT_NEAR(1.625, r.plan_clear_ahead, 0.15);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisCorridor, HorizonLimitsTheScan)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalCircle(2.5, 0.6, 0.2);
|
||||||
|
grid.setLethalCircle(2.5, -0.6, 0.2);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const auto plan = linePlan(-2.0, 3.0, 0.1);
|
||||||
|
CorridorParams params;
|
||||||
|
params.horizon = 3.0; // khe ở 4.5 m dọc plan: ngoài tầm
|
||||||
|
const CorridorResult r = scanCorridor(plan, 0, params, PlanarTransform(), s);
|
||||||
|
EXPECT_TRUE(std::isinf(r.block_dist));
|
||||||
|
EXPECT_NEAR(3.0, r.plan_clear_ahead, 1e-9);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------------- body clearance
|
||||||
|
|
||||||
|
TEST(GridAnalysisBody, ClearanceBesideAWall)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
lethalLine(grid, -2.0, 0.7, 2.0, 0.7);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
// Cạnh trái thân ở y = 0.48, tường ở 0.7 -> 0.22 m.
|
||||||
|
EXPECT_NEAR(0.22, bodyClearance(s, 0.0, 0.0, 0.0, 0.583, 0.583, 0.48), 1.5 * kRes);
|
||||||
|
// Quay 90 độ: mũi (0.583) hướng về tường -> 0.117 m.
|
||||||
|
EXPECT_NEAR(0.117, bodyClearance(s, 0.0, 0.0, M_PI / 2.0, 0.583, 0.583, 0.48), 1.5 * kRes);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisBody, EmptyGridClearanceIsInfinite)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
EXPECT_TRUE(std::isinf(bodyClearance(s, 0.0, 0.0, 0.3, 0.583, 0.583, 0.48)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------- sweep
|
||||||
|
|
||||||
|
std::vector<robot_geometry_msgs::PoseStamped> straightTrajectory(double length, double step)
|
||||||
|
{
|
||||||
|
std::vector<robot_geometry_msgs::PoseStamped> traj;
|
||||||
|
for (double x = 0.0; x <= length + 1e-9; x += step)
|
||||||
|
{
|
||||||
|
traj.push_back(makePose(x, 0.0, 0.0));
|
||||||
|
}
|
||||||
|
return traj;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisSweep, StraightTrajectoryIntoAWallReportsTheNoseContact)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalAt(1.5, 0.0);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const SweepResult r = sweepTrajectory(straightTrajectory(2.0, 0.1), 0.0, 0.0, 0.0, 0.3,
|
||||||
|
SweepParams(), *grid.costmap(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_TRUE(r.blocked);
|
||||||
|
EXPECT_EQ(1, r.hit_edge); // mũi
|
||||||
|
// Mũi đệm 0.633 m chạm ô tại 1.5 m (mép ô ~1.475): còn ~0.84 m đường đi.
|
||||||
|
EXPECT_NEAR(0.84, r.distance, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisSweep, ClearTrajectoryIsNotBlocked)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalAt(1.5, 1.5);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const SweepResult r = sweepTrajectory(straightTrajectory(1.4, 0.1), 0.0, 0.0, 0.0, 0.3,
|
||||||
|
SweepParams(), *grid.costmap(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_FALSE(r.blocked);
|
||||||
|
EXPECT_GT(r.scanned, 0u);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisSweep, TrajectoryNotStartingAtTheRobotIsInconclusive)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
// Quỹ đạo bắt đầu ở (0,0) nhưng robot ở (1.0, 0): đây là global plan, không phải chuyển động sắp xảy ra.
|
||||||
|
const SweepResult r = sweepTrajectory(straightTrajectory(2.0, 0.1), 1.0, 0.0, 0.0, 0.3,
|
||||||
|
SweepParams(), *grid.costmap(), s);
|
||||||
|
EXPECT_FALSE(r.conclusive);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisSweep, LethalCellUnderTheBodyIsExcludedByTheBaseline)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalAt(0.3, 0.50); // kề hông trái, trong thân đệm (0.53)
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
const SweepResult r = sweepTrajectory(straightTrajectory(2.0, 0.1), 0.0, 0.0, 0.0, 0.3,
|
||||||
|
SweepParams(), *grid.costmap(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_GE(r.baseline_cells, 1);
|
||||||
|
EXPECT_FALSE(r.blocked); // đi thẳng: ô đó trượt ra sau, không "thêm" ô mới vào thân
|
||||||
|
|
||||||
|
SweepParams no_baseline;
|
||||||
|
no_baseline.exclude_baseline = false;
|
||||||
|
const SweepResult r2 = sweepTrajectory(straightTrajectory(2.0, 0.1), 0.0, 0.0, 0.0, 0.3,
|
||||||
|
no_baseline, *grid.costmap(), s);
|
||||||
|
EXPECT_TRUE(r2.blocked);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisSweep, TrajectoryCurvingAwayFromAWallIsClear)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
// Tường cách 1.3 m: đi thẳng thì mũi đệm (0.633) chạm sau ~0.65 m; cung R 0.4 quay 90 độ có góc
|
||||||
|
// mũi-phải quét xa nhất sqrt(0.633^2 + 0.93^2) = 1.13 m < 1.3 -> không chạm.
|
||||||
|
lethalLine(grid, 1.3, -2.0, 1.3, 2.0);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
// Quỹ đạo quay gắt sang trái: cung R = 0.4 m, 90 độ, kết thúc song song tường ở x ~ 0.4.
|
||||||
|
std::vector<robot_geometry_msgs::PoseStamped> traj;
|
||||||
|
for (int i = 0; i <= 20; ++i)
|
||||||
|
{
|
||||||
|
const double a = (M_PI / 2.0) * i / 20.0;
|
||||||
|
traj.push_back(makePose(0.4 * std::sin(a), 0.4 * (1.0 - std::cos(a)), a));
|
||||||
|
}
|
||||||
|
SweepParams params;
|
||||||
|
params.lookahead = 1.0;
|
||||||
|
const SweepResult straight = sweepTrajectory(straightTrajectory(1.5, 0.1), 0.0, 0.0, 0.0, 0.3,
|
||||||
|
params, *grid.costmap(), s);
|
||||||
|
const SweepResult curve = sweepTrajectory(traj, 0.0, 0.0, 0.0, 0.3, params, *grid.costmap(), s);
|
||||||
|
ASSERT_TRUE(straight.conclusive);
|
||||||
|
EXPECT_TRUE(straight.blocked);
|
||||||
|
ASSERT_TRUE(curve.conclusive);
|
||||||
|
EXPECT_FALSE(curve.blocked);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GridAnalysisSweep, LookaheadStopsTheSweep)
|
||||||
|
{
|
||||||
|
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||||
|
grid.setLethalAt(3.0, 0.0);
|
||||||
|
GridScratch s;
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||||
|
SweepParams params;
|
||||||
|
params.lookahead = 1.5;
|
||||||
|
const SweepResult r = sweepTrajectory(straightTrajectory(4.0, 0.1), 0.0, 0.0, 0.0, 0.3, params,
|
||||||
|
*grid.costmap(), s);
|
||||||
|
ASSERT_TRUE(r.conclusive);
|
||||||
|
EXPECT_FALSE(r.blocked);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------- scratch reuse
|
||||||
|
|
||||||
|
TEST(GridAnalysisScratch, ReusedAcrossGridsWithoutStaleState)
|
||||||
|
{
|
||||||
|
GridScratch s;
|
||||||
|
nav_test_harness::FakeCostmap a = makeGrid();
|
||||||
|
a.setLethalAt(0.0, 0.0);
|
||||||
|
ASSERT_TRUE(analyzeGrid(*a.costmap(), kLethal, kInscribed, s));
|
||||||
|
EXPECT_NEAR(0.0, distanceAt(s, 0.0, 0.0), 1e-6);
|
||||||
|
|
||||||
|
nav_test_harness::FakeCostmap b = makeGrid(); // trống
|
||||||
|
ASSERT_TRUE(analyzeGrid(*b.costmap(), kLethal, kInscribed, s));
|
||||||
|
EXPECT_TRUE(std::isinf(distanceAt(s, 0.0, 0.0)));
|
||||||
|
|
||||||
|
nav_test_harness::FakeCostmap c(100, 60, kRes, -2.5, -1.5); // kích thước khác
|
||||||
|
c.setLethalAt(1.0, 0.0);
|
||||||
|
ASSERT_TRUE(analyzeGrid(*c.costmap(), kLethal, kInscribed, s));
|
||||||
|
EXPECT_EQ(100u, s.width);
|
||||||
|
EXPECT_EQ(60u, s.height);
|
||||||
|
EXPECT_NEAR(1.0, distanceAt(s, 0.0, 0.0), 1.5 * kRes);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------- chi phí
|
||||||
|
|
||||||
|
TEST(GridAnalysisBench, ReportCostOnRuntimeSizedGrid)
|
||||||
|
{
|
||||||
|
// 160 x 160 @ 0.05 m như local costmap runtime, bãi 30 trụ rải ngẫu nhiên. Chỉ in số, không phán
|
||||||
|
// pass/fail: chi phí tuyệt đối phụ thuộc máy; plan (§2.5) dự tính EDT ~0.3 ms + Dijkstra <= 0.4 ms.
|
||||||
|
nav_test_harness::FakeCostmap grid = nav_test_harness::FakeCostmap::centered(8.0, kRes);
|
||||||
|
unsigned int seed = 12345u;
|
||||||
|
for (int i = 0; i < 30; ++i)
|
||||||
|
{
|
||||||
|
seed = seed * 1103515245u + 12345u;
|
||||||
|
const double x = -3.5 + 7.0 * ((seed >> 8) % 1000) / 1000.0;
|
||||||
|
seed = seed * 1103515245u + 12345u;
|
||||||
|
const double y = -3.5 + 7.0 * ((seed >> 8) % 1000) / 1000.0;
|
||||||
|
grid.setLethalCircle(x, y, 0.25);
|
||||||
|
}
|
||||||
|
// Dọn một hành lang trống dọc plan để T và ô robot luôn free -> Dijkstra chạy thật (thăm toàn bộ
|
||||||
|
// thành phần liên thông chứa T = trường hợp xấu nhất về chi phí).
|
||||||
|
// Lấy mẫu tại TÂM ô: bước đúng một ô nhưng đặt trên biên ô thì sai số float bỏ sót ô.
|
||||||
|
for (double x = -0.6 + 0.5 * kRes; x < 4.0; x += kRes)
|
||||||
|
{
|
||||||
|
for (double y = -0.8 + 0.5 * kRes; y < 0.8; y += kRes)
|
||||||
|
{
|
||||||
|
grid.setCostAt(x, y, robot_costmap_2d::FREE_SPACE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GridScratch s;
|
||||||
|
const auto plan = linePlan(0.0, 3.9, 0.1);
|
||||||
|
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); // ensure() cấp phát một lần
|
||||||
|
|
||||||
|
constexpr int kIters = 200;
|
||||||
|
auto t0 = std::chrono::steady_clock::now();
|
||||||
|
for (int i = 0; i < kIters; ++i)
|
||||||
|
{
|
||||||
|
analyzeGrid(*grid.costmap(), kLethal, kInscribed, s);
|
||||||
|
}
|
||||||
|
auto t1 = std::chrono::steady_clock::now();
|
||||||
|
CostToGoResult last;
|
||||||
|
for (int i = 0; i < kIters; ++i)
|
||||||
|
{
|
||||||
|
last = costToGo(plan, 0, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s);
|
||||||
|
}
|
||||||
|
auto t2 = std::chrono::steady_clock::now();
|
||||||
|
CorridorResult corr;
|
||||||
|
for (int i = 0; i < kIters; ++i)
|
||||||
|
{
|
||||||
|
corr = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s);
|
||||||
|
}
|
||||||
|
auto t3 = std::chrono::steady_clock::now();
|
||||||
|
SweepResult sw;
|
||||||
|
const auto traj = straightTrajectory(1.5, 0.1);
|
||||||
|
for (int i = 0; i < kIters; ++i)
|
||||||
|
{
|
||||||
|
sw = sweepTrajectory(traj, 0.0, 0.0, 0.0, 0.3, SweepParams(), *grid.costmap(), s);
|
||||||
|
}
|
||||||
|
auto t4 = std::chrono::steady_clock::now();
|
||||||
|
const auto us = [](auto a, auto b) {
|
||||||
|
return std::chrono::duration_cast<std::chrono::microseconds>(b - a).count() / static_cast<double>(kIters);
|
||||||
|
};
|
||||||
|
std::printf("[bench] 160x160: analyzeGrid %.0f us | costToGo %.0f us (visited %zu, reachable %d, "
|
||||||
|
"conclusive %d, in_ring %d, target %d, edt_robot %.2f, edt_T %.2f, cost %.2f) | "
|
||||||
|
"scanCorridor %.0f us (scanned %zu) | sweepTrajectory %.0f us\n",
|
||||||
|
us(t0, t1), us(t1, t2), last.cells_visited, last.reachable ? 1 : 0,
|
||||||
|
last.conclusive ? 1 : 0, last.robot_in_ring ? 1 : 0, static_cast<int>(last.target),
|
||||||
|
distanceAt(s, 0.0, 0.0), distanceAt(s, 3.9, 0.0), last.cost_to_go, us(t2, t3),
|
||||||
|
corr.scanned, us(t3, t4));
|
||||||
|
EXPECT_TRUE(last.conclusive);
|
||||||
|
EXPECT_TRUE(last.reachable);
|
||||||
|
EXPECT_GT(last.cells_visited, 1000u);
|
||||||
|
EXPECT_TRUE(sw.conclusive);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
testing::InitGoogleTest(&argc, argv);
|
||||||
|
return RUN_ALL_TESTS();
|
||||||
|
}
|
||||||
@@ -25,11 +25,10 @@
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
using move_base2::checkPlan;
|
|
||||||
using move_base2::crossTrackError;
|
using move_base2::crossTrackError;
|
||||||
using move_base2::nearestIndex;
|
using move_base2::nearestIndex;
|
||||||
|
using move_base2::trackedNearestIndex;
|
||||||
using move_base2::PlanarTransform;
|
using move_base2::PlanarTransform;
|
||||||
using move_base2::PlanBlockResult;
|
|
||||||
using move_base2::planarTransform;
|
using move_base2::planarTransform;
|
||||||
|
|
||||||
robot_geometry_msgs::PoseStamped makePose(double x, double y, const std::string& frame = "map")
|
robot_geometry_msgs::PoseStamped makePose(double x, double y, const std::string& frame = "map")
|
||||||
@@ -60,128 +59,6 @@ nav_test_harness::FakeCostmap makeGrid()
|
|||||||
return nav_test_harness::FakeCostmap::centered(10.0, 0.05);
|
return nav_test_harness::FakeCostmap::centered(10.0, 0.05);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PlanInspectorCheck, CleanPlanIsNotBlocked)
|
|
||||||
{
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
|
||||||
|
|
||||||
const PlanBlockResult result = checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform());
|
|
||||||
|
|
||||||
EXPECT_FALSE(result.blocked);
|
|
||||||
EXPECT_GT(result.scanned, 0u);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(PlanInspectorCheck, FreeSpaceValueIsNotBlocked)
|
|
||||||
{
|
|
||||||
// Bẫy của fork này: FREE_SPACE = 60, không phải 0. Đặt tường minh giá trị đó lên tuyến để khoá
|
|
||||||
// lại bất biến "so sánh phải dùng hằng số có tên".
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setCostAt(1.0, 0.0, robot_costmap_2d::FREE_SPACE));
|
|
||||||
|
|
||||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
|
||||||
const PlanBlockResult result = checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform());
|
|
||||||
|
|
||||||
EXPECT_FALSE(result.blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(PlanInspectorCheck, LethalCellOnThePlanBlocks)
|
|
||||||
{
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(1.5, 0.0));
|
|
||||||
|
|
||||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
|
||||||
const PlanBlockResult result = checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform());
|
|
||||||
|
|
||||||
ASSERT_TRUE(result.blocked);
|
|
||||||
EXPECT_NEAR(plan[result.index].pose.position.x, 1.5, 0.06);
|
|
||||||
EXPECT_NEAR(result.distance, 1.5, 0.06);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(PlanInspectorCheck, InscribedInflatedCountsAsBlocked)
|
|
||||||
{
|
|
||||||
// Ngưỡng là INSCRIBED_INFLATED_OBSTACLE chứ không phải LETHAL: ở đó tâm robot đặt vào là footprint
|
|
||||||
// đã đệm chạm vật cản.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setCostAt(1.0, 0.0, robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE));
|
|
||||||
|
|
||||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
|
||||||
const PlanBlockResult result = checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform());
|
|
||||||
|
|
||||||
EXPECT_TRUE(result.blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(PlanInspectorCheck, UnknownCellIsNotBlocked)
|
|
||||||
{
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setCostAt(1.0, 0.0, robot_costmap_2d::NO_INFORMATION));
|
|
||||||
|
|
||||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
|
||||||
const PlanBlockResult result = checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform());
|
|
||||||
|
|
||||||
// Vùng chưa quét mà tính là chắn thì robot dừng ở mọi tuyến đi vào chỗ chưa thấy.
|
|
||||||
EXPECT_FALSE(result.blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(PlanInspectorCheck, HorizonCutsTheScanShort)
|
|
||||||
{
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(2.5, 0.0));
|
|
||||||
|
|
||||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
|
||||||
|
|
||||||
EXPECT_FALSE(checkPlan(plan, 0, 1.0, *grid.costmap(), PlanarTransform()).blocked);
|
|
||||||
EXPECT_TRUE(checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform()).blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(PlanInspectorCheck, ScanStartsAtFromIndex)
|
|
||||||
{
|
|
||||||
// Vật cản NẰM SAU lưng (phần plan đã đi qua) không được tính.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(0.5, 0.0));
|
|
||||||
|
|
||||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
|
||||||
|
|
||||||
EXPECT_TRUE(checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform()).blocked);
|
|
||||||
EXPECT_FALSE(checkPlan(plan, 15, 3.0, *grid.costmap(), PlanarTransform()).blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(PlanInspectorCheck, OutOfGridStopsTheScanInsteadOfBlocking)
|
|
||||||
{
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid(); // ±5 m
|
|
||||||
const auto plan = linePlan(0.0, 20.0, 0.5);
|
|
||||||
|
|
||||||
const PlanBlockResult result = checkPlan(plan, 0, 20.0, *grid.costmap(), PlanarTransform());
|
|
||||||
|
|
||||||
EXPECT_FALSE(result.blocked);
|
|
||||||
EXPECT_LT(result.scanned, plan.size()); // dừng ở rìa lưới, không quét hết
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(PlanInspectorCheck, EmptyPlanAndBadArgumentsAreSafe)
|
|
||||||
{
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
|
||||||
|
|
||||||
EXPECT_FALSE(checkPlan({}, 0, 3.0, *grid.costmap(), PlanarTransform()).blocked);
|
|
||||||
EXPECT_FALSE(checkPlan(plan, 999, 3.0, *grid.costmap(), PlanarTransform()).blocked);
|
|
||||||
EXPECT_FALSE(checkPlan(plan, 0, 0.0, *grid.costmap(), PlanarTransform()).blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(PlanInspectorCheck, TransformIsAppliedToEveryPose)
|
|
||||||
{
|
|
||||||
// Plan ở "map", lưới ở "odom" lệch (2, 1): vật cản đặt theo toạ độ LƯỚI.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(-0.5, -1.0)); // = map (1.5, 0.0) sau khi trừ offset
|
|
||||||
|
|
||||||
PlanarTransform to_grid;
|
|
||||||
to_grid.x = -2.0;
|
|
||||||
to_grid.y = -1.0;
|
|
||||||
|
|
||||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
|
||||||
|
|
||||||
EXPECT_TRUE(checkPlan(plan, 0, 3.0, *grid.costmap(), to_grid).blocked);
|
|
||||||
EXPECT_FALSE(checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform()).blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(PlanInspectorNearest, ProjectsOntoSegmentsNotVertices)
|
TEST(PlanInspectorNearest, ProjectsOntoSegmentsNotVertices)
|
||||||
{
|
{
|
||||||
const auto plan = linePlan(0.0, 3.0, 1.0); // pose tại x = 0,1,2,3
|
const auto plan = linePlan(0.0, 3.0, 1.0); // pose tại x = 0,1,2,3
|
||||||
@@ -202,6 +79,30 @@ TEST(PlanInspectorNearest, PlanFoldingBackOnItselfPicksTheNearerLeg)
|
|||||||
EXPECT_EQ(nearestIndex(plan, makePose(1.0, 0.95)), 2u);
|
EXPECT_EQ(nearestIndex(plan, makePose(1.0, 0.95)), 2u);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PlanInspectorNearest, TrackedIndexStaysOnTheCurrentLegOfAFoldedPlan)
|
||||||
|
{
|
||||||
|
// Plan ra 4 m rồi vòng lại song song cách 0.6 m: robot đứng giữa hai đoạn (0.35 m tới đoạn đầu, 0.25 m tới
|
||||||
|
// đoạn về). nearestIndex toàn plan nhảy sang đoạn về (index 2); bám từ hint 0 với cửa sổ 1 m lùi / 3 m tiến
|
||||||
|
// thì đoạn về nằm ngoài 3 m dọc plan -> giữ đoạn đầu.
|
||||||
|
std::vector<robot_geometry_msgs::PoseStamped> plan{ makePose(0.0, 0.0), makePose(4.0, 0.0), makePose(4.0, 0.6),
|
||||||
|
makePose(0.0, 0.6) };
|
||||||
|
const auto robot = makePose(1.0, 0.35);
|
||||||
|
EXPECT_EQ(nearestIndex(plan, robot), 2u);
|
||||||
|
EXPECT_EQ(trackedNearestIndex(plan, robot, 0, 1.0, 3.0), 0u);
|
||||||
|
// Cửa sổ đủ dài thì bằng nearest toàn plan.
|
||||||
|
EXPECT_EQ(trackedNearestIndex(plan, robot, 0, 1.0, 20.0), 2u);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PlanInspectorNearest, TrackedIndexAdvancesAndClampsTheHint)
|
||||||
|
{
|
||||||
|
const auto plan = linePlan(0.0, 5.0, 0.5); // 11 pose, 10 đoạn
|
||||||
|
EXPECT_EQ(trackedNearestIndex(plan, makePose(2.3, 0.1), 2, 1.0, 3.0), 4u); // tiến
|
||||||
|
EXPECT_EQ(trackedNearestIndex(plan, makePose(1.2, 0.0), 4, 1.0, 3.0), 2u); // lùi trong 1 m
|
||||||
|
EXPECT_EQ(trackedNearestIndex(plan, makePose(0.1, 0.0), 8, 1.0, 3.0), 6u); // lùi bị chặn ở 1 m: đoạn 6
|
||||||
|
EXPECT_EQ(trackedNearestIndex(plan, makePose(4.9, 0.0), 99, 1.0, 3.0), 9u); // hint vượt cỡ -> kẹp
|
||||||
|
EXPECT_EQ(trackedNearestIndex({}, makePose(0.0, 0.0), 3, 1.0, 3.0), 0u);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PlanInspectorNearest, ShortPlansAreSafe)
|
TEST(PlanInspectorNearest, ShortPlansAreSafe)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(nearestIndex({}, makePose(0.0, 0.0)), 0u);
|
EXPECT_EQ(nearestIndex({}, makePose(0.0, 0.0)), 0u);
|
||||||
@@ -280,263 +181,3 @@ int main(int argc, char** argv)
|
|||||||
::testing::InitGoogleTest(&argc, argv);
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
return RUN_ALL_TESTS();
|
return RUN_ALL_TESTS();
|
||||||
}
|
}
|
||||||
|
|
||||||
// =================================================================================================
|
|
||||||
// Phép đo 3: cung lệnh — thân robot có sắp chạm vật cản THẬT không (2026-09-03).
|
|
||||||
// =================================================================================================
|
|
||||||
namespace
|
|
||||||
{
|
|
||||||
using move_base2::checkMotion;
|
|
||||||
using move_base2::MotionBlockResult;
|
|
||||||
using move_base2::MotionCheckParams;
|
|
||||||
|
|
||||||
MotionCheckParams bodyParams()
|
|
||||||
{
|
|
||||||
MotionCheckParams p;
|
|
||||||
p.lookahead = 0.8;
|
|
||||||
p.max_samples = 6;
|
|
||||||
p.front_offset = 0.58;
|
|
||||||
p.rear_offset = 0.58;
|
|
||||||
p.side_offset = 0.48;
|
|
||||||
p.padding = 0.05;
|
|
||||||
p.cost_threshold = robot_costmap_2d::LETHAL_OBSTACLE;
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, ClearArcIsNotBlocked)
|
|
||||||
{
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
|
|
||||||
EXPECT_FALSE(r.blocked);
|
|
||||||
// Bước dò bị kẹp về một ô lưới (0.05 m) nên 0.8 m là 16 mẫu, không phải max_samples = 6.
|
|
||||||
EXPECT_EQ(r.scanned, 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, ThinWallBetweenCoarseSamplesIsStillCaught)
|
|
||||||
{
|
|
||||||
// Tường dày đúng MỘT ô tại x=1.30. Với bước thưa 0.8/6 = 0.133 m, mũi (0.63 m trước tâm) ở
|
|
||||||
// mẫu 5 là 1.297 (chưa tới), mẫu 6 là 1.43 (đã qua) — cả hai sạch, robot đâm xuyên. Kẹp bước
|
|
||||||
// về một ô lưới thì không mẫu nào nhảy qua được.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
for (double y = -0.6; y <= 0.6; y += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(1.30, y));
|
|
||||||
}
|
|
||||||
MotionCheckParams p = bodyParams();
|
|
||||||
p.max_samples = 6;
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, p, *grid.costmap());
|
|
||||||
ASSERT_TRUE(r.blocked);
|
|
||||||
EXPECT_NEAR(r.distance, 1.30 - 0.63, 0.06);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, LethalAheadOfTheNoseBlocksWithDistance)
|
|
||||||
{
|
|
||||||
// Tường tại x=1.2. Mũi robot vươn 0.58+0.05 = 0.63 m trước tâm, nên tâm chỉ cần đi ~0.57 m là
|
|
||||||
// mũi chạm — phải báo ở mẫu ~0.53–0.67 m, KHÔNG phải khi tâm tới 1.2 m.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
for (double y = -0.6; y <= 0.6; y += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(1.2, y));
|
|
||||||
}
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
|
|
||||||
ASSERT_TRUE(r.blocked);
|
|
||||||
EXPECT_GT(r.distance, 0.4);
|
|
||||||
EXPECT_LT(r.distance, 0.8);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, InflationOnlyIsNotBlocked)
|
|
||||||
{
|
|
||||||
// Đây là toàn bộ lý do phép đo này tồn tại: ô INSCRIBED (253) là biên cho TÂM robot, đặt lên mũi
|
|
||||||
// là đếm biên hai lần. Phủ 253 kín đường đi — robot vẫn phải được chạy.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
for (double x = 0.0; x <= 2.0; x += 0.05)
|
|
||||||
{
|
|
||||||
for (double y = -0.7; y <= 0.7; y += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setCostAt(x, y, robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
|
|
||||||
EXPECT_FALSE(r.blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, LethalUnderTheBodyDoesNotBlockParallelMotion)
|
|
||||||
{
|
|
||||||
// Người kề hông trái: hàng ô LETHAL ở y=0.51 (ngoài thân 0.48, trong thân đệm 0.53), dọc x từ
|
|
||||||
// -0.2 tới 0.3 — đúng ca sim 4/9 làm guard báo 0.05 m mãi. Đi thẳng thì mép trái chỉ quét lại
|
|
||||||
// đúng những ô đã nằm dưới thân -> không có ô mới -> phải cho đi.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
for (double x = -0.2; x <= 0.3; x += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(x, 0.51));
|
|
||||||
}
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
|
|
||||||
EXPECT_FALSE(r.blocked) << "blocked at " << r.distance << " m";
|
|
||||||
EXPECT_GE(r.baseline_cells, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, LethalUnderTheBodyStillBlocksWhenTheArcAddsNewCells)
|
|
||||||
{
|
|
||||||
// Khối 3 hàng x 9 cột ở hông trái-sau (y=0.51/0.56/0.61, x từ -0.6 tới -0.2). Hàng trong cùng là
|
|
||||||
// baseline; rẽ phải làm góc trái-sau văng ra ngoài lên hai hàng còn lại -> ô MỚI -> vẫn chặn, và
|
|
||||||
// ở khoảng cách thật (> mẫu đầu 0.05 m) chứ không phải "0.05 m" máy móc.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
for (double x = -0.6; x <= -0.2; x += 0.05)
|
|
||||||
{
|
|
||||||
for (double y = 0.51; y <= 0.62; y += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(x, y));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, -0.1, bodyParams(), *grid.costmap());
|
|
||||||
ASSERT_TRUE(r.blocked);
|
|
||||||
EXPECT_GT(r.distance, 0.05);
|
|
||||||
EXPECT_LT(r.distance, 0.5);
|
|
||||||
EXPECT_GE(r.baseline_cells, 1);
|
|
||||||
// Chi tiết debug: chạm bằng cạnh TRÁI, ô chạm nằm bên trái phía sau tâm, baseline liệt kê ô bên trái.
|
|
||||||
EXPECT_EQ(r.hit_edge, 2);
|
|
||||||
EXPECT_GT(r.hit_y, 0.4);
|
|
||||||
EXPECT_LT(r.hit_x, 0.0);
|
|
||||||
ASSERT_GE(r.baseline_listed, 1);
|
|
||||||
EXPECT_GT(r.baseline_xy[0][1], 0.4);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, LethalAheadOfTheNoseIsUnaffectedByBaseline)
|
|
||||||
{
|
|
||||||
// Có người kề hông (baseline) VÀ tường trước mũi: tường là ô mới, phải chặn y như không có baseline.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(0.0, 0.51));
|
|
||||||
for (double y = -0.6; y <= 0.6; y += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(1.2, y));
|
|
||||||
}
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
|
|
||||||
ASSERT_TRUE(r.blocked);
|
|
||||||
EXPECT_GT(r.distance, 0.4);
|
|
||||||
EXPECT_LT(r.distance, 0.8);
|
|
||||||
EXPECT_GE(r.baseline_cells, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, LookaheadAngleTruncatesTheArc)
|
|
||||||
{
|
|
||||||
// R = v/omega = 0.1 m: 0.8 m cung là quay 8 rad. Cắt ở 1.0 rad -> chỉ còn 0.1 m cung = 2 mẫu;
|
|
||||||
// không cắt (pi) -> 0.31 m = 6 mẫu. Không có vật cản: chỉ đo số mẫu.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
MotionCheckParams p = bodyParams();
|
|
||||||
p.lookahead_angle = 1.0;
|
|
||||||
const MotionBlockResult cut = checkMotion(0.0, 0.0, 0.0, 0.4, 4.0, p, *grid.costmap());
|
|
||||||
EXPECT_FALSE(cut.blocked);
|
|
||||||
EXPECT_EQ(cut.scanned, 2);
|
|
||||||
p.lookahead_angle = M_PI;
|
|
||||||
const MotionBlockResult full = checkMotion(0.0, 0.0, 0.0, 0.4, 4.0, p, *grid.costmap());
|
|
||||||
EXPECT_FALSE(full.blocked);
|
|
||||||
EXPECT_GE(full.scanned, 6);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, BaselineDisabledKeepsTheOldVerdict)
|
|
||||||
{
|
|
||||||
// Đường lùi: tắt cờ thì ca "người kề hông + đi thẳng" lại bị chặn ngay mẫu đầu như trước 4/9.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
for (double x = -0.2; x <= 0.3; x += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(x, 0.51));
|
|
||||||
}
|
|
||||||
MotionCheckParams p = bodyParams();
|
|
||||||
p.exclude_baseline = false;
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, p, *grid.costmap());
|
|
||||||
ASSERT_TRUE(r.blocked);
|
|
||||||
EXPECT_LE(r.distance, 0.1);
|
|
||||||
EXPECT_EQ(r.baseline_cells, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, ArcCurvingAwayFromTheWallIsClear)
|
|
||||||
{
|
|
||||||
// Tường ngay trước mặt tại x=1.0 nhưng lệnh rẽ gắt trái (kappa = 1.5/0.3 = 5 1/m, R=0.2 m):
|
|
||||||
// cung quay đi trước khi mũi tới tường. Gate quỹ đạo giấy cũng nói vậy — và đó là hành vi ĐÚNG
|
|
||||||
// của phép đo này: nó đo lệnh thật, không đoán.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
for (double y = -0.3; y <= 0.3; y += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(1.0, y));
|
|
||||||
}
|
|
||||||
MotionCheckParams p = bodyParams();
|
|
||||||
p.side_offset = 0.0; // chỉ tâm/mũi/đuôi để test hình học cung, không dính probe hông
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.3, 1.5, p, *grid.costmap());
|
|
||||||
EXPECT_FALSE(r.blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, ThinPillarAtTheFrontCornerIsCaught)
|
|
||||||
{
|
|
||||||
// Trụ mảnh (một ô) tại x=1.0, y=+0.30: nằm giữa đường quét mũi (ngang 0) và đường quét hông
|
|
||||||
// (ngang 0.53) của bản 5 điểm — bản đó không probe nào chạm, robot ôm trụ bằng góc trước-trái
|
|
||||||
// (đo 4/9: near=0.36 m @ +70°). Chu vi bắt được ở mẫu mà cạnh mũi đi qua trụ.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(1.0, 0.30));
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
|
|
||||||
ASSERT_TRUE(r.blocked);
|
|
||||||
// Cạnh mũi ở 0.58 + 0.05 = 0.63 trước tâm -> chạm khi tâm đi được ~0.37 m.
|
|
||||||
EXPECT_NEAR(r.distance, 1.0 - 0.63, 0.06);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, PillarBetweenTwoWheelsIsCaughtByTheFrontEdge)
|
|
||||||
{
|
|
||||||
// Hai trụ cách nhau 0.45 m, robot chui giữa: bản 5 điểm để lọt cả hai; chu vi mũi chạm ngay.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(1.2, 0.225));
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(1.2, -0.225));
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
|
|
||||||
EXPECT_TRUE(r.blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, SideProbeCatchesAWallBesideTheArc)
|
|
||||||
{
|
|
||||||
// Tường dọc bên trái tại y=+0.50, robot đi thẳng: chu vi với nửa rộng 0.48 + 0.05 = 0.53 chạm;
|
|
||||||
// thu side_offset về 0 (dải rộng 2 x padding) thì không.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
for (double x = 0.0; x <= 1.5; x += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(x, 0.50));
|
|
||||||
}
|
|
||||||
MotionCheckParams p = bodyParams();
|
|
||||||
const MotionBlockResult with_side = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, p, *grid.costmap());
|
|
||||||
EXPECT_TRUE(with_side.blocked);
|
|
||||||
p.side_offset = 0.0;
|
|
||||||
const MotionBlockResult no_side = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, p, *grid.costmap());
|
|
||||||
EXPECT_FALSE(no_side.blocked);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, StandingStillScansNothing)
|
|
||||||
{
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(0.3, 0.0));
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.0, 0.5, bodyParams(), *grid.costmap());
|
|
||||||
EXPECT_FALSE(r.blocked);
|
|
||||||
EXPECT_EQ(r.scanned, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, ReverseMotionProbesBehind)
|
|
||||||
{
|
|
||||||
// Tường SAU lưng tại x=-1.2, lệnh lùi: đuôi (0.58+0.05) chạm trước khi tâm tới.
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
for (double y = -0.6; y <= 0.6; y += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setLethalAt(-1.2, y));
|
|
||||||
}
|
|
||||||
const MotionBlockResult fwd = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
|
|
||||||
EXPECT_FALSE(fwd.blocked);
|
|
||||||
const MotionBlockResult rev = checkMotion(0.0, 0.0, 0.0, -0.4, 0.0, bodyParams(), *grid.costmap());
|
|
||||||
ASSERT_TRUE(rev.blocked);
|
|
||||||
EXPECT_LT(rev.distance, 0.8);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MotionCheck, UnknownCellsAreNotBlocked)
|
|
||||||
{
|
|
||||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
|
||||||
for (double x = 0.0; x <= 2.0; x += 0.05)
|
|
||||||
{
|
|
||||||
ASSERT_TRUE(grid.setCostAt(x, 0.0, robot_costmap_2d::NO_INFORMATION));
|
|
||||||
}
|
|
||||||
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
|
|
||||||
EXPECT_FALSE(r.blocked);
|
|
||||||
}
|
|
||||||
} // namespace
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ using move_base2::testing::FakeClockPort;
|
|||||||
using move_base2::testing::FakeControllerPort;
|
using move_base2::testing::FakeControllerPort;
|
||||||
using move_base2::testing::FakeCostmapStatusPort;
|
using move_base2::testing::FakeCostmapStatusPort;
|
||||||
using move_base2::testing::FakeMissionPort;
|
using move_base2::testing::FakeMissionPort;
|
||||||
using move_base2::testing::FakePathCheckPort;
|
using move_base2::testing::FakeEnvironmentPort;
|
||||||
using move_base2::testing::FakePlannerPort;
|
using move_base2::testing::FakePlannerPort;
|
||||||
using move_base2::testing::FakePosePort;
|
using move_base2::testing::FakePosePort;
|
||||||
using move_base2::testing::FakeRecoveryPort;
|
using move_base2::testing::FakeRecoveryPort;
|
||||||
@@ -129,7 +129,7 @@ public:
|
|||||||
deps_.mission = &mission_;
|
deps_.mission = &mission_;
|
||||||
deps_.action = &action_;
|
deps_.action = &action_;
|
||||||
deps_.costmap_status = &costmap_status_;
|
deps_.costmap_status = &costmap_status_;
|
||||||
deps_.path_check = &path_check_;
|
deps_.environment = &environment_;
|
||||||
|
|
||||||
std::string error;
|
std::string error;
|
||||||
EXPECT_TRUE(loop_.configure(config, deps_, error)) << error;
|
EXPECT_TRUE(loop_.configure(config, deps_, error)) << error;
|
||||||
@@ -178,7 +178,7 @@ public:
|
|||||||
FakeMissionPort mission_;
|
FakeMissionPort mission_;
|
||||||
FakeActionPort action_;
|
FakeActionPort action_;
|
||||||
FakeCostmapStatusPort costmap_status_;
|
FakeCostmapStatusPort costmap_status_;
|
||||||
FakePathCheckPort path_check_;
|
FakeEnvironmentPort environment_;
|
||||||
ControlLoopDeps deps_;
|
ControlLoopDeps deps_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -334,23 +334,11 @@ TEST(ControlLoop, DockingMarkerProfileOverridesBothPlannersAndFallsBackToDefault
|
|||||||
EXPECT_EQ(fixture.controller_.activeController(), "FakeDockPlanner");
|
EXPECT_EQ(fixture.controller_.activeController(), "FakeDockPlanner");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ControlLoop, RejectsDockingRequestWithoutMarker)
|
TEST(ControlLoop, AllowsGoalFrameStyleDockingWithoutMarker)
|
||||||
{
|
|
||||||
Fixture fixture;
|
|
||||||
|
|
||||||
NavigationRequest docking = makeRequest(1.0);
|
|
||||||
docking.profile = MotionProfile::kDocking;
|
|
||||||
// marker cố ý bỏ trống — bản cũ cũng chặn tại cửa dockTo.
|
|
||||||
|
|
||||||
std::string reason;
|
|
||||||
EXPECT_FALSE(fixture.loop_.submit(docking, reason));
|
|
||||||
EXPECT_NE(reason.find("marker"), std::string::npos) << reason;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(ControlLoop, AllowsGoalFrameStyleDockingWithoutMarkerWhenConfigured)
|
|
||||||
{
|
{
|
||||||
|
// Marker là tuỳ chọn từ 2026-09-06: docking chạy bằng goal_frame đã quy về pose tuyệt đối, local
|
||||||
|
// planner không đọc `maker_name`. Request không marker phải được nhận, không phải bị từ chối.
|
||||||
ControlLoopConfig config = baseConfig();
|
ControlLoopConfig config = baseConfig();
|
||||||
config.docking_requires_marker = false;
|
|
||||||
Fixture fixture(config);
|
Fixture fixture(config);
|
||||||
|
|
||||||
NavigationRequest docking = makeRequest(1.0);
|
NavigationRequest docking = makeRequest(1.0);
|
||||||
@@ -1400,7 +1388,7 @@ TEST(StaleCostmap, NullPortMeansNoGuard)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ================================================================================================
|
// ================================================================================================
|
||||||
// PathMonitor (contract 2026-08-04d)
|
// Trợ giúp chung cho các ca có mission (submit + kiểm nhận).
|
||||||
// ================================================================================================
|
// ================================================================================================
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
@@ -1414,359 +1402,8 @@ bool submitOk(Fixture& fixture, const NavigationRequest& request)
|
|||||||
return accepted;
|
return accepted;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Config có giám sát tuyến BẬT, ngưỡng ngắn để test không phải tua thời gian dài.
|
|
||||||
ControlLoopConfig monitorConfig()
|
|
||||||
{
|
|
||||||
ControlLoopConfig config = baseConfig();
|
|
||||||
config.state_machine.recovery_routes.planning_failed = { 0 };
|
|
||||||
config.state_machine.recovery_routes.controlling_failed = { 0 };
|
|
||||||
config.state_machine.recovery_routes.oscillation = { 0 };
|
|
||||||
config.state_machine.recovery_routes.path_blocked = { 1 };
|
|
||||||
config.state_machine.recovery_routes.off_path = { 1 };
|
|
||||||
config.state_machine.recovery_routes.collision = { 1 };
|
|
||||||
|
|
||||||
config.path_monitor.enabled = true;
|
|
||||||
config.path_monitor.check_period = 0.0; // quét mỗi cycle: test điều khiển thời gian bằng advance
|
|
||||||
config.path_monitor.confirm_time = 0.2; // [s]
|
|
||||||
config.path_monitor.horizon = 3.0; // [m]
|
|
||||||
config.path_monitor.min_detour_distance = 0.5; // [m]
|
|
||||||
// Phép đo lệch tuyến TẮT ở config cơ sở: pose giả đứng yên tại gốc còn plan giả nằm ở goal, nên
|
|
||||||
// cross-track luôn lớn và sẽ át hết mọi ca về "tuyến bị chắn". Ca lệch tuyến có config riêng.
|
|
||||||
config.path_monitor.max_cross_track = 0.0; // [m]
|
|
||||||
config.path_monitor.grace_after_new_plan = 0.0; // tắt để test khai hoả được ngay
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, BlockedPathStopsTheRobotAfterConfirmTime)
|
|
||||||
{
|
|
||||||
Fixture fixture(monitorConfig());
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
// Recovery chạy dài để test quan sát được trạng thái DỪNG, thay vì thấy nó thoáng qua trong một
|
|
||||||
// cycle rồi quay lại kControlling.
|
|
||||||
fixture.recovery_.setScript(std::vector<RecoveryScript>(20, RecoveryScript::kRunning));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce(); // kPlanning
|
|
||||||
fixture.stepOnce(); // kControlling
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
|
|
||||||
fixture.path_check_.setBlocked(true, 1.5);
|
|
||||||
|
|
||||||
// Chưa đủ confirm_time: robot vẫn chạy.
|
|
||||||
fixture.stepOnce();
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
|
|
||||||
for (int i = 0; i < 6; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
|
||||||
EXPECT_NEAR(fixture.loop_.lastCommand().linear.x, 0.0, 1e-9);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, PathBlockedCanBeDisabledOnItsOwn)
|
|
||||||
{
|
|
||||||
// Công tắc riêng (2026-08-06): tắt phép đo "tuyến bị chắn" mà không phải đặt `horizon` về 0 —
|
|
||||||
// ngưỡng giữ nguyên để bật lại chỉ cần đổi một cờ, và đọc config thấy rõ ý định.
|
|
||||||
ControlLoopConfig config = monitorConfig();
|
|
||||||
config.path_monitor.path_blocked_enabled = false;
|
|
||||||
|
|
||||||
Fixture fixture(config);
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
|
|
||||||
fixture.path_check_.setBlocked(true, 1.5);
|
|
||||||
for (int i = 0; i < 10; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "cycle " << i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, FlickeringObstacleDoesNotStopTheRobot)
|
|
||||||
{
|
|
||||||
// Người đi ngang qua đúng một lượt quét không được làm robot dừng — đó là cả lý do có debounce.
|
|
||||||
Fixture fixture(monitorConfig());
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
fixture.path_check_.setBlocked(i % 2 == 0, 1.5);
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "cycle " << i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, ObstacleTooCloseIsLeftToTheControllerPath)
|
|
||||||
{
|
|
||||||
// Vật cản sát nách: detour hầu như không ra nghiệm, và ca đó đã có controller_patience lo.
|
|
||||||
Fixture fixture(monitorConfig());
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.path_check_.setBlocked(true, 0.2); // [m] < min_detour_distance
|
|
||||||
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, StaleCostmapSuppressesTheMonitor)
|
|
||||||
{
|
|
||||||
// Costmap quá hạn thì mọi kết luận về vật cản đều là quá khứ — không được khai hoả dựa vào nó.
|
|
||||||
Fixture fixture(monitorConfig());
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
|
|
||||||
fixture.path_check_.setBlocked(true, 1.5);
|
|
||||||
fixture.costmap_status_.setCurrent(false);
|
|
||||||
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, UnavailableCheckIsTreatedAsUnknown)
|
|
||||||
{
|
|
||||||
// Cổng trả false = "không kết luận được". Không được suy thành bẩn (robot dừng oan mỗi lần TF trễ).
|
|
||||||
Fixture fixture(monitorConfig());
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
|
|
||||||
fixture.path_check_.setBlocked(true, 1.5);
|
|
||||||
fixture.path_check_.setAvailable(false);
|
|
||||||
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, DockingLegIsNeverMonitored)
|
|
||||||
{
|
|
||||||
// Đường cong vào dock là một thể thống nhất; cắt ghép nó là phá chính hình học đó.
|
|
||||||
ControlLoopConfig config = monitorConfig();
|
|
||||||
config.docking_requires_marker = false;
|
|
||||||
Fixture fixture(config);
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
|
|
||||||
NavigationRequest request = makeRequest(3.0);
|
|
||||||
request.profile = MotionProfile::kDocking;
|
|
||||||
ASSERT_TRUE(submitOk(fixture, request));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.path_check_.setBlocked(true, 1.5);
|
|
||||||
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, DriftingOffThePlanStopsTheRobot)
|
|
||||||
{
|
|
||||||
// Local planner tự né vật cản nhỏ (tầng 0) có thể kéo robot ra xa tuyến mà plan vẫn "sạch" và
|
|
||||||
// controller vẫn ra lệnh — không trigger nào khác nhìn thấy tình huống này.
|
|
||||||
ControlLoopConfig config = monitorConfig();
|
|
||||||
config.path_monitor.max_cross_track = 0.4; // [m]
|
|
||||||
Fixture fixture(config);
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
fixture.recovery_.setScript(std::vector<RecoveryScript>(20, RecoveryScript::kRunning));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
|
|
||||||
// Plan giả chỉ có pose goal (3, 0); robot bám sát nó thì cross-track nhỏ.
|
|
||||||
fixture.pose_.setPosition(3.0, 0.1);
|
|
||||||
fixture.stepOnce();
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
|
|
||||||
// Dạt sang bên quá ngưỡng: sau `confirm_time` thì dừng.
|
|
||||||
fixture.pose_.setPosition(3.0, 0.9);
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
|
||||||
EXPECT_NEAR(fixture.loop_.lastCommand().linear.x, 0.0, 1e-9);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, OffPathCanBeDisabledOnItsOwn)
|
|
||||||
{
|
|
||||||
// Đối xứng với `PathBlockedCanBeDisabledOnItsOwn`: ngưỡng `max_cross_track` vẫn để nguyên 0.4 m,
|
|
||||||
// chỉ hạ cờ — cấu hình đã đo được giữ lại, bật lại là một dòng.
|
|
||||||
ControlLoopConfig config = monitorConfig();
|
|
||||||
config.path_monitor.max_cross_track = 0.4; // [m] ngưỡng thật, giữ nguyên
|
|
||||||
config.path_monitor.off_path_enabled = false;
|
|
||||||
|
|
||||||
Fixture fixture(config);
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
|
|
||||||
fixture.pose_.setPosition(3.0, 5.0); // lệch rất xa
|
|
||||||
for (int i = 0; i < 10; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "cycle " << i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, GracePeriodSuppressesTheOffPathCheck)
|
|
||||||
{
|
|
||||||
// Ngay sau khi đổi plan, pose robot có thể lệch nhất thời so với plan vừa tới (rõ nhất lúc
|
|
||||||
// preempt/đổi chặng). Đo lệch tuyến lúc đó là khai hoả oan.
|
|
||||||
ControlLoopConfig config = monitorConfig();
|
|
||||||
config.path_monitor.max_cross_track = 0.4; // [m]
|
|
||||||
config.path_monitor.grace_after_new_plan = 5.0; // [s] dài hơn hẳn kịch bản
|
|
||||||
Fixture fixture(config);
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.pose_.setPosition(3.0, 5.0); // lệch rất xa
|
|
||||||
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, EmptyRouteDisablesTheMonitorInsteadOfAborting)
|
|
||||||
{
|
|
||||||
// Route rỗng nghĩa là behavior detour không nạp được (hoặc chưa khai). Giám sát mà không có
|
|
||||||
// đường xử lý sẽ biến một cơ chế đang tắt thành ABORT ngay lần đầu thấy vật cản — đúng lỗi đã
|
|
||||||
// gặp trên sim 2026-08-05. Đúng: không khai hoả, để controller_patience lo như trước.
|
|
||||||
ControlLoopConfig config = monitorConfig();
|
|
||||||
config.state_machine.recovery_routes.path_blocked.clear();
|
|
||||||
config.state_machine.recovery_routes.off_path.clear();
|
|
||||||
config.state_machine.recovery_routes.collision.clear();
|
|
||||||
Fixture fixture(config);
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.path_check_.setBlocked(true, 1.5);
|
|
||||||
|
|
||||||
for (int i = 0; i < 10; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
EXPECT_GT(std::abs(fixture.loop_.lastCommand().linear.x), 0.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, OffPathRouteEmptyDisablesOnlyThatCheck)
|
|
||||||
{
|
|
||||||
// Hai phép đo độc lập: thiếu route của cái này không được làm câm cái kia.
|
|
||||||
ControlLoopConfig config = monitorConfig();
|
|
||||||
config.path_monitor.max_cross_track = 0.4; // [m]
|
|
||||||
config.state_machine.recovery_routes.off_path.clear();
|
|
||||||
Fixture fixture(config);
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
fixture.recovery_.setScript(std::vector<RecoveryScript>(20, RecoveryScript::kRunning));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
|
|
||||||
// Lệch rất xa nhưng route off_path rỗng -> không khai hoả.
|
|
||||||
fixture.pose_.setPosition(3.0, 5.0);
|
|
||||||
for (int i = 0; i < 6; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
|
|
||||||
// Còn path_blocked vẫn còn route thì vẫn phải chạy bình thường.
|
|
||||||
fixture.path_check_.setBlocked(true, 1.5);
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonPathMonitor, BlockedFreshPlanDoesNotReplaceACleanOne)
|
|
||||||
{
|
|
||||||
// Luật "không thay plan sạch bằng plan bẩn": không có nó thì planner mù ghi đè đường vòng mỗi
|
|
||||||
// nhịp replan nền và robot dừng-detour-chạy-dừng liên tục.
|
|
||||||
ControlLoopConfig config = monitorConfig();
|
|
||||||
config.planner_frequency = 1000.0; // ép replan nền nổ mỗi cycle
|
|
||||||
Fixture fixture(config);
|
|
||||||
fixture.planner_.setScript(std::vector<PlannerScript>(20, PlannerScript::kOk));
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(40, ControllerScript::kOk));
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(3.0)));
|
|
||||||
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
|
|
||||||
const std::size_t accepted_before = fixture.controller_.setPlanCount();
|
|
||||||
|
|
||||||
// Plan mới về đều đặn nhưng đều bị chắn -> controller không được nhận thêm plan nào.
|
|
||||||
fixture.path_check_.setBlocked(true, 1.5);
|
|
||||||
for (int i = 0; i < 3; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.controller_.setPlanCount(), accepted_before);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
testing::InitGoogleTest(&argc, argv);
|
testing::InitGoogleTest(&argc, argv);
|
||||||
@@ -1774,46 +1411,11 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// =================================================================================================
|
// =================================================================================================
|
||||||
// PathMonitor — phép đo 3: thân sắp chạm vật cản thật trên cung lệnh (2026-09-03).
|
// EnvMonitor kContact — phanh theo quỹ đạo thật, cờ collision, leo thang khi route khoá (P4, 2026-09-05).
|
||||||
|
// PathMonitor cũ (4 phép đo, PathCheckPort) đã xoá 6/9/2026 — xem Test/ENV_MONITOR_PLAN.md §4.19.
|
||||||
// =================================================================================================
|
// =================================================================================================
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
ControlLoopConfig collisionConfig()
|
|
||||||
{
|
|
||||||
ControlLoopConfig config = monitorConfig();
|
|
||||||
// Chỉ phép đo 3 sống: hai phép đo cũ tắt để chứng minh nó tự giơ được cờ path_blocked một mình.
|
|
||||||
config.path_monitor.path_blocked_enabled = false;
|
|
||||||
config.path_monitor.off_path_enabled = false;
|
|
||||||
config.path_monitor.collision_enabled = true;
|
|
||||||
config.path_monitor.collision_lookahead = 0.8; // [m]
|
|
||||||
config.path_monitor.collision_stop_distance = 0.35; // [m]
|
|
||||||
config.path_monitor.collision_confirm_cycles = 2;
|
|
||||||
config.path_monitor.collision_front_offset = 0.58; // [m]
|
|
||||||
config.path_monitor.collision_rear_offset = 0.58; // [m]
|
|
||||||
config.path_monitor.collision_side_offset = 0.48; // [m]
|
|
||||||
config.path_monitor.collision_footprint_padding = 0.1; // [m]
|
|
||||||
config.path_monitor.collision_brake_decel = 0.0;
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// @brief Chỉ Phép đo 4 sống. Cửa sổ/confirm rút ngắn để test chạy vài chục cycle.
|
|
||||||
ControlLoopConfig progressConfig(const std::string& action = "recover")
|
|
||||||
{
|
|
||||||
ControlLoopConfig config = monitorConfig();
|
|
||||||
config.path_monitor.path_blocked_enabled = false;
|
|
||||||
config.path_monitor.off_path_enabled = false;
|
|
||||||
config.path_monitor.collision_enabled = false;
|
|
||||||
config.path_monitor.progress_enabled = true;
|
|
||||||
config.path_monitor.progress_action = action;
|
|
||||||
config.path_monitor.progress_window = 1.0; // [s] = 20 cycle
|
|
||||||
config.path_monitor.progress_min_commanded = 0.05; // [m]
|
|
||||||
config.path_monitor.progress_min_efficiency = 0.30;
|
|
||||||
config.path_monitor.progress_clear_efficiency = 0.60;
|
|
||||||
config.path_monitor.progress_confirm_time = 0.3; // [s] = 6 cycle
|
|
||||||
config.path_monitor.progress_goal_zone = 0.6; // [m]
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
void driveToControlling(Fixture& fixture)
|
void driveToControlling(Fixture& fixture)
|
||||||
{
|
{
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
fixture.planner_.setScript({ PlannerScript::kOk });
|
||||||
@@ -1829,256 +1431,163 @@ void driveToControlling(Fixture& fixture)
|
|||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, BrakesImmediatelyThenReplansAfterConfirm)
|
namespace
|
||||||
{
|
{
|
||||||
Fixture fixture(collisionConfig());
|
/// @brief P4 (5/9): guard cũ tắt hẳn, kContact của EnvMonitor phanh thật và giơ cờ collision.
|
||||||
|
ControlLoopConfig envContactConfig()
|
||||||
|
{
|
||||||
|
ControlLoopConfig config = baseConfig();
|
||||||
|
config.state_machine.recovery_routes.planning_failed = { 0 };
|
||||||
|
config.state_machine.recovery_routes.controlling_failed = { 0 };
|
||||||
|
config.state_machine.recovery_routes.oscillation = { 0 };
|
||||||
|
config.state_machine.recovery_routes.path_blocked = { 1 };
|
||||||
|
config.state_machine.recovery_routes.collision = { 1 };
|
||||||
|
config.env_monitor.enabled = true;
|
||||||
|
config.env_monitor.stall.action = "log";
|
||||||
|
config.env_monitor.unreachable.action = "log";
|
||||||
|
config.env_monitor.contact.action = "recover";
|
||||||
|
config.env_monitor.contact.apply_command = true;
|
||||||
|
config.env_monitor.contact.confirm_cycles = 2;
|
||||||
|
config.env_monitor.contact.trigger_static_time = 0.3; // [s] = 6 cycle, đủ ngắn cho test
|
||||||
|
config.env_monitor.contact.clean_start_margin = 0.2; // [m]
|
||||||
|
config.env_sampling.check_period = 0.01; // [s] < chu kỳ 0.05 -> tick nặng mỗi cycle
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(WalkingSkeletonEnvContact, BrakesAfterConfirmThenRaisesCollisionAfterStaticTime)
|
||||||
|
{
|
||||||
|
Fixture fixture(envContactConfig());
|
||||||
driveToControlling(fixture);
|
driveToControlling(fixture);
|
||||||
|
|
||||||
// Chạm trong quãng dừng: lệnh ra bằng 0 NGAY cycle này — trước cả khi cờ được giơ.
|
// Tiếp xúc 0.10 m trên quỹ đạo, trong clean_start_margin: cycle 1 đếm, cycle 2 phanh -> lệnh 0.
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.2);
|
fixture.environment_.setContact(true, 0.10);
|
||||||
|
fixture.stepOnce();
|
||||||
fixture.stepOnce();
|
fixture.stepOnce();
|
||||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
|
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
|
||||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().angular.z, 0.0);
|
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().angular.z, 0.0);
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "cycle 1: chưa đủ xác nhận";
|
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "phanh nhưng chưa giơ cờ";
|
||||||
|
|
||||||
// Cycle 2 xác nhận -> cờ path_blocked vào state machine ở cycle 3.
|
// Kề mũi đứng yên đủ trigger_static_time -> cờ collision -> route collision.
|
||||||
fixture.stepOnce();
|
int held = 0;
|
||||||
fixture.stepOnce();
|
for (int i = 0; i < 12 && fixture.loop_.state() == NavigationState::kControlling; ++i)
|
||||||
|
{
|
||||||
|
fixture.stepOnce();
|
||||||
|
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0) << "cycle " << i;
|
||||||
|
++held;
|
||||||
|
}
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
||||||
EXPECT_EQ(fixture.recovery_.lastTrigger(), RecoveryTrigger::kCollision);
|
EXPECT_EQ(fixture.recovery_.lastTrigger(), RecoveryTrigger::kCollision);
|
||||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
|
EXPECT_GE(held, 4) << "cờ phải chờ đủ trigger_static_time, không nổ cùng nhịp với phanh";
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, HeldByTheGuardAfterTheRouteIsSpentEscalatesToControllingFailed)
|
TEST(WalkingSkeletonEnvContact, HeldWithTheRouteSpentEscalatesToControllingFailed)
|
||||||
{
|
{
|
||||||
// Run 1 sim 2026-09-04: detour không ra nghiệm (start nằm trong vành 253), route path_blocked cạn,
|
// Cùng chuỗi bắt buộc như guard cũ: cạn route collision -> khoá -> brake giữ 0 nhưng controller
|
||||||
// guard giữ lệnh 0 nhưng vẫn báo command_valid -> controller_patience không chạy -> robot đứng
|
// phải bị báo no_valid_command -> controller_patience -> controlling_failed. Không có bước này
|
||||||
// chết 20 s không một dòng log. Chuỗi bắt buộc: cạn route -> mute -> guard báo no_valid_command
|
// robot đứng chết vô hạn với controller "ổn".
|
||||||
// -> controller_patience -> controlling_failed. Guard giữ 0 suốt chuỗi.
|
Fixture fixture(envContactConfig());
|
||||||
Fixture fixture(collisionConfig());
|
|
||||||
driveToControlling(fixture);
|
driveToControlling(fixture);
|
||||||
fixture.recovery_.setScript({ RecoveryScript::kFailed });
|
fixture.recovery_.setScript({ RecoveryScript::kFailed });
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.2);
|
fixture.environment_.setContact(true, 0.10);
|
||||||
for (int i = 0; i < 3; ++i)
|
for (int i = 0; i < 12 && fixture.loop_.state() == NavigationState::kControlling; ++i)
|
||||||
{
|
{
|
||||||
fixture.stepOnce(); // phanh, xác nhận, giơ cờ
|
fixture.stepOnce();
|
||||||
}
|
}
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
ASSERT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
||||||
ASSERT_EQ(fixture.recovery_.lastTrigger(), RecoveryTrigger::kCollision);
|
ASSERT_EQ(fixture.recovery_.lastTrigger(), RecoveryTrigger::kCollision);
|
||||||
|
|
||||||
// Behavior tick ở cycle sau khi start, kết quả vào state machine ở cycle sau nữa.
|
|
||||||
for (int i = 0; i < 4 && fixture.loop_.state() == NavigationState::kRecovering; ++i)
|
for (int i = 0; i < 4 && fixture.loop_.state() == NavigationState::kRecovering; ++i)
|
||||||
{
|
{
|
||||||
fixture.stepOnce();
|
fixture.stepOnce();
|
||||||
}
|
}
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling)
|
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling)
|
||||||
<< "detour thất bại phải quay về kControlling với route path_blocked đã cạn";
|
<< "detour thất bại phải quay về kControlling với route collision đã cạn";
|
||||||
|
|
||||||
|
// onRecoveryFinished xoá chuỗi phanh: tối đa confirm_cycles cycle lệnh có thể khác 0 rồi phanh lại.
|
||||||
bool escalated = false;
|
bool escalated = false;
|
||||||
for (int i = 0; i < 40 && !escalated; ++i) // controller_patience 0.5 s = 10 cycle, dư địa rộng
|
int nonzero_after_settle = 0;
|
||||||
|
for (int i = 0; i < 60 && !escalated; ++i)
|
||||||
{
|
{
|
||||||
fixture.stepOnce();
|
fixture.stepOnce();
|
||||||
ASSERT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0) << "cycle " << i;
|
if (i >= 3 && fixture.loop_.lastCommand().linear.x != 0.0)
|
||||||
|
{
|
||||||
|
++nonzero_after_settle;
|
||||||
|
}
|
||||||
escalated = fixture.loop_.state() == NavigationState::kRecovering &&
|
escalated = fixture.loop_.state() == NavigationState::kRecovering &&
|
||||||
fixture.recovery_.lastTrigger() == RecoveryTrigger::kControllingFailed;
|
fixture.recovery_.lastTrigger() == RecoveryTrigger::kControllingFailed;
|
||||||
}
|
}
|
||||||
EXPECT_TRUE(escalated) << "guard held the robot but nothing escalated";
|
EXPECT_TRUE(escalated) << "EnvMonitor held the robot but nothing escalated";
|
||||||
|
EXPECT_EQ(nonzero_after_settle, 0) << "brake must hold once re-confirmed";
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, BodyComesFromTheCostmapFootprintWhenOffsetsAreZero)
|
TEST(WalkingSkeletonEnvContact, DetourFromContactThatDidNotMoveTheRobotArmsTheStandDown)
|
||||||
{
|
{
|
||||||
// Ba offset = 0: thân lấy từ footprint costmap qua cổng. Có footprint -> guard dò và dừng như khai
|
// Run 6/9 16:30 (đám đông): người đứng 7 cm trước mũi -> cờ collision -> detour -> chạm lại ngay -> cờ -> detour ...
|
||||||
// tay; hình thân đúng là việc của costmap, không phải của người chép số vào hai file.
|
// 4 lần rồi ABORT, vì stand-down không được vũ trang (trigger đọc ở cycle tick là mặc định). Chuỗi đúng: sau
|
||||||
ControlLoopConfig config = collisionConfig();
|
// detour do cờ collision mà robot không dịch, chạm lại thì kContact ĐỨNG RA — không phanh, không cờ thứ hai.
|
||||||
config.path_monitor.collision_front_offset = 0.0;
|
Fixture fixture(envContactConfig());
|
||||||
config.path_monitor.collision_rear_offset = 0.0;
|
|
||||||
config.path_monitor.collision_side_offset = 0.0;
|
|
||||||
Fixture fixture(config);
|
|
||||||
fixture.path_check_.setBodyExtents(0.58, 0.58, 0.48);
|
|
||||||
driveToControlling(fixture);
|
driveToControlling(fixture);
|
||||||
|
std::vector<robot_geometry_msgs::PoseStamped> detour{ makeRequest(0.0).goal, makeRequest(3.0).goal };
|
||||||
|
fixture.recovery_.setScript({ RecoveryScript::kSucceeded });
|
||||||
|
fixture.recovery_.setSucceedWithPath(detour);
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.2);
|
fixture.environment_.setContact(true, 0.10);
|
||||||
fixture.stepOnce();
|
for (int i = 0; i < 12 && fixture.loop_.state() == NavigationState::kControlling; ++i)
|
||||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
|
||||||
EXPECT_EQ(fixture.recovery_.lastTrigger(), RecoveryTrigger::kCollision);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, NoBodyGeometryIsInconclusiveNotAStop)
|
|
||||||
{
|
|
||||||
// Offset = 0 mà cổng không có footprint: guard KHÔNG biết thân ở đâu thì không dừng, không giơ cờ,
|
|
||||||
// chỉ kêu lỗi — cùng contract với TF/costmap hỏng. Dừng mù là biến lỗi cấu hình thành robot chết.
|
|
||||||
ControlLoopConfig config = collisionConfig();
|
|
||||||
config.path_monitor.collision_front_offset = 0.0;
|
|
||||||
config.path_monitor.collision_rear_offset = 0.0;
|
|
||||||
config.path_monitor.collision_side_offset = 0.0;
|
|
||||||
Fixture fixture(config); // fake không setBodyExtents
|
|
||||||
driveToControlling(fixture);
|
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.2);
|
|
||||||
for (int i = 0; i < 10; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "cycle " << i;
|
|
||||||
ASSERT_GT(fixture.loop_.lastCommand().linear.x, 0.0) << "cycle " << i;
|
|
||||||
}
|
|
||||||
EXPECT_EQ(fixture.path_check_.motionQueryCount(), 0u); // chưa có thân thì không dò
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, SlowTurnFarFromContactIsCappedNotStopped)
|
|
||||||
{
|
|
||||||
// Run 16:26: xoay bò v 0.04 ω −0.5 (R 0.08 m), chạm ở 0.15 m cung = quay 1.9 rad, còn 3.8 s.
|
|
||||||
// Chưa "sắp" theo thời gian lẫn góc -> không dừng, không recovery, chỉ kẹp v <= (0.15−0.1)/1.0.
|
|
||||||
Fixture fixture(collisionConfig()); // padding 0.1, stop_time 1.0, stop_angle 0.35 mặc định
|
|
||||||
driveToControlling(fixture);
|
|
||||||
fixture.controller_.setNominalSpeed(0.04);
|
|
||||||
fixture.controller_.setNominalTurnRate(-0.5);
|
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.15);
|
|
||||||
for (int i = 0; i < 10; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "cycle " << i;
|
|
||||||
}
|
|
||||||
EXPECT_EQ(fixture.recovery_.startCount(), 0u);
|
|
||||||
EXPECT_GT(fixture.loop_.lastCommand().linear.x, 0.0);
|
|
||||||
EXPECT_LE(fixture.loop_.lastCommand().linear.x, 0.05 + 1e-9);
|
|
||||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().angular.z, -0.5); // guard không đụng omega
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, SlowTurnCloseToContactStops)
|
|
||||||
{
|
|
||||||
// Cùng cú xoay nhưng chỉ còn 0.02 m cung = 0.25 rad <= stop_angle -> sắp chạm thật -> dừng.
|
|
||||||
Fixture fixture(collisionConfig());
|
|
||||||
driveToControlling(fixture);
|
|
||||||
fixture.controller_.setNominalSpeed(0.04);
|
|
||||||
fixture.controller_.setNominalTurnRate(0.5);
|
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.02);
|
|
||||||
fixture.stepOnce();
|
|
||||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
|
||||||
EXPECT_EQ(fixture.recovery_.lastTrigger(), RecoveryTrigger::kCollision);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, StraightCreepStillStops)
|
|
||||||
{
|
|
||||||
// Bò thẳng 0.05 m/s, chạm ở 0.15 m: 3 s nữa nhưng góc = 0 -> "sắp" theo góc -> dừng như trước.
|
|
||||||
Fixture fixture(collisionConfig());
|
|
||||||
driveToControlling(fixture);
|
|
||||||
fixture.controller_.setNominalSpeed(0.05);
|
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.15);
|
|
||||||
fixture.stepOnce();
|
|
||||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, FastCurvedApproachStillStops)
|
|
||||||
{
|
|
||||||
// Lần nổ 115.1 của run 16:26: v 0.36 ω −0.27, chạm ở 0.20 m = 0.55 s -> sắp theo thời gian.
|
|
||||||
Fixture fixture(collisionConfig());
|
|
||||||
driveToControlling(fixture);
|
|
||||||
fixture.controller_.setNominalSpeed(0.36);
|
|
||||||
fixture.controller_.setNominalTurnRate(-0.27);
|
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.20);
|
|
||||||
fixture.stepOnce();
|
|
||||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
|
|
||||||
fixture.stepOnce();
|
|
||||||
fixture.stepOnce();
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, ContactBeyondStopDistanceDoesNotStop)
|
|
||||||
{
|
|
||||||
Fixture fixture(collisionConfig());
|
|
||||||
driveToControlling(fixture);
|
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.7); // [m] > stop_distance 0.35
|
|
||||||
for (int i = 0; i < 10; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "cycle " << i;
|
|
||||||
ASSERT_GT(fixture.loop_.lastCommand().linear.x, 0.0) << "cycle " << i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, CapsSpeedByRemainingDistanceWhenBrakeDecelIsSet)
|
|
||||||
{
|
|
||||||
ControlLoopConfig config = collisionConfig();
|
|
||||||
config.path_monitor.collision_brake_decel = 0.1; // [m/s^2] -> cap sqrt(2*0.1*0.35) = 0.26 m/s
|
|
||||||
Fixture fixture(config);
|
|
||||||
driveToControlling(fixture);
|
|
||||||
const double free_speed = fixture.loop_.lastCommand().linear.x;
|
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.7);
|
|
||||||
for (int i = 0; i < 10; ++i)
|
|
||||||
{
|
{
|
||||||
fixture.stepOnce();
|
fixture.stepOnce();
|
||||||
}
|
}
|
||||||
const double cap = std::sqrt(2.0 * 0.1 * (0.7 - 0.35));
|
ASSERT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
||||||
EXPECT_LE(fixture.loop_.lastCommand().linear.x, cap + 1e-9);
|
ASSERT_EQ(fixture.recovery_.lastTrigger(), RecoveryTrigger::kCollision);
|
||||||
EXPECT_GT(fixture.loop_.lastCommand().linear.x, 0.0);
|
for (int i = 0; i < 4 && fixture.loop_.state() == NavigationState::kRecovering; ++i)
|
||||||
EXPECT_LT(fixture.loop_.lastCommand().linear.x, free_speed);
|
{
|
||||||
|
fixture.stepOnce();
|
||||||
|
}
|
||||||
|
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
||||||
|
|
||||||
|
// Vẫn chạm 0.10 m, robot (pose giả) không dịch: không được có RECOVERING thứ hai trong 1.5 s, và lệnh phải
|
||||||
|
// được trả về controller (đứng ra = không phanh).
|
||||||
|
int recovering_again = 0;
|
||||||
|
double max_cmd = 0.0;
|
||||||
|
for (int i = 0; i < 30; ++i)
|
||||||
|
{
|
||||||
|
fixture.stepOnce();
|
||||||
|
if (fixture.loop_.state() == NavigationState::kRecovering)
|
||||||
|
{
|
||||||
|
++recovering_again;
|
||||||
|
}
|
||||||
|
max_cmd = std::max(max_cmd, fixture.loop_.lastCommand().linear.x);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(0, recovering_again) << "cờ collision nổ lại từ chính chỗ vừa detour";
|
||||||
|
EXPECT_GT(max_cmd, 0.0) << "stand-down phải nhường lệnh cho controller";
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(WalkingSkeletonEnvContact, ContactBeyondTheStoppingDistanceCapsSpeedWithoutBraking)
|
||||||
|
{
|
||||||
|
Fixture fixture(envContactConfig());
|
||||||
|
driveToControlling(fixture);
|
||||||
|
const double v_free = fixture.loop_.lastCommand().linear.x;
|
||||||
|
ASSERT_GT(v_free, 0.27);
|
||||||
|
|
||||||
|
// 2.0 m phía trước: bao sqrt(2·0.35·1.7) = 1.09 m/s > lệnh -> không đổi, không phanh.
|
||||||
|
fixture.environment_.setContact(true, 2.0);
|
||||||
|
for (int i = 0; i < 6; ++i)
|
||||||
|
{
|
||||||
|
fixture.stepOnce();
|
||||||
|
}
|
||||||
|
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, v_free);
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, OneCycleFlickerBrakesButDoesNotReplan)
|
// 0.40 m: bao sqrt(2·0.35·(0.40 − margin 0.2 − dư 0.1)) = 0.265 m/s. Lệnh bị kẹp xuống đúng bao và GIỮ ở đó:
|
||||||
{
|
// quãng phanh ở tốc độ đã kẹp = 0.10 m -> còn 0.30 m > margin -> không phải "sắp chạm", không phanh, không cờ.
|
||||||
// Một nhịp costmap nhấp nháy: lệnh 0 đúng một cycle (an toàn), nhưng không kéo recovery ra.
|
fixture.environment_.setContact(true, 0.40);
|
||||||
Fixture fixture(collisionConfig());
|
for (int i = 0; i < 8; ++i)
|
||||||
driveToControlling(fixture);
|
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.2);
|
|
||||||
fixture.stepOnce();
|
|
||||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
|
|
||||||
fixture.path_check_.setMotionBlocked(false);
|
|
||||||
for (int i = 0; i < 6; ++i)
|
|
||||||
{
|
{
|
||||||
fixture.stepOnce();
|
fixture.stepOnce();
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "cycle " << i;
|
EXPECT_NEAR(fixture.loop_.lastCommand().linear.x, std::sqrt(2.0 * 0.35 * 0.10), 1e-3) << "cycle " << i;
|
||||||
}
|
|
||||||
EXPECT_GT(fixture.loop_.lastCommand().linear.x, 0.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, DisabledFlagLeavesTheCommandAlone)
|
|
||||||
{
|
|
||||||
ControlLoopConfig config = collisionConfig();
|
|
||||||
config.path_monitor.collision_enabled = false;
|
|
||||||
Fixture fixture(config);
|
|
||||||
driveToControlling(fixture);
|
|
||||||
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.2);
|
|
||||||
for (int i = 0; i < 6; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "cycle " << i;
|
|
||||||
ASSERT_GT(fixture.loop_.lastCommand().linear.x, 0.0) << "cycle " << i;
|
|
||||||
}
|
|
||||||
EXPECT_EQ(fixture.path_check_.motionQueryCount(), 0u);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonCollisionGuard, UnknownVerdictNeitherStopsNorClearsTheStreak)
|
|
||||||
{
|
|
||||||
Fixture fixture(collisionConfig());
|
|
||||||
driveToControlling(fixture);
|
|
||||||
|
|
||||||
fixture.path_check_.setMotionAvailable(false); // thiếu TF/costmap: "không biết"
|
|
||||||
fixture.path_check_.setMotionBlocked(true, 0.2);
|
|
||||||
for (int i = 0; i < 6; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "cycle " << i;
|
|
||||||
ASSERT_GT(fixture.loop_.lastCommand().linear.x, 0.0) << "cycle " << i;
|
|
||||||
}
|
}
|
||||||
|
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------
|
||||||
@@ -2098,63 +1607,3 @@ void runStalled(Fixture& fixture, int max_cycles)
|
|||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
TEST(WalkingSkeletonProgressMonitor, StalledRobotEscalatesToOscillationRecovery)
|
|
||||||
{
|
|
||||||
Fixture fixture(progressConfig("recover"));
|
|
||||||
driveToControlling(fixture);
|
|
||||||
ASSERT_GT(fixture.loop_.lastCommand().linear.x, 0.0) << "controller phải đang đòi đi";
|
|
||||||
|
|
||||||
// Cần: 1 mẫu đủ tuổi window (20 cycle) + confirm 6 cycle + 1 cycle để state machine thấy cờ.
|
|
||||||
runStalled(fixture, 60);
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
|
||||||
EXPECT_EQ(fixture.recovery_.lastTrigger(), RecoveryTrigger::kOscillation);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonProgressMonitor, LogModeNeverEscalates)
|
|
||||||
{
|
|
||||||
Fixture fixture(progressConfig("log"));
|
|
||||||
driveToControlling(fixture);
|
|
||||||
|
|
||||||
runStalled(fixture, 80);
|
|
||||||
|
|
||||||
// Cùng tình huống kẹt, mode "log" chỉ in cảnh báo: state machine không được thấy cờ.
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonProgressMonitor, NearTheGoalIsNotMeasured)
|
|
||||||
{
|
|
||||||
Fixture fixture(progressConfig("recover"));
|
|
||||||
fixture.planner_.setScript({ PlannerScript::kOk });
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(400, ControllerScript::kOk));
|
|
||||||
fixture.recovery_.setScript(std::vector<RecoveryScript>(20, RecoveryScript::kRunning));
|
|
||||||
// Goal cách 0.3 m < progress_goal_zone 0.6 m: dừng chính xác / rot2goal có lệnh nhỏ mà không
|
|
||||||
// tiến là hợp lệ, không được coi là kẹt.
|
|
||||||
ASSERT_TRUE(submitOk(fixture, makeRequest(0.3)));
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
|
|
||||||
runStalled(fixture, 80);
|
|
||||||
|
|
||||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(WalkingSkeletonProgressMonitor, ZeroCommandIsWaitingNotStalling)
|
|
||||||
{
|
|
||||||
Fixture fixture(progressConfig("recover"));
|
|
||||||
driveToControlling(fixture);
|
|
||||||
|
|
||||||
// Controller ngừng ra lệnh (hold chờ người): lệnh sau trọng tài về 0 -> cmd < min_commanded ->
|
|
||||||
// KHÔNG đo. Phép đo 4 không được nhầm "chờ" với "kẹt".
|
|
||||||
fixture.controller_.setScript(std::vector<ControllerScript>(400, ControllerScript::kFail));
|
|
||||||
for (int i = 0; i < 60 && fixture.loop_.state() == NavigationState::kControlling; ++i)
|
|
||||||
{
|
|
||||||
fixture.stepOnce();
|
|
||||||
}
|
|
||||||
// Có thể rời kControlling vì controller_patience (kFail) — nhưng KHÔNG phải vì kOscillation.
|
|
||||||
EXPECT_NE(fixture.recovery_.lastTrigger(), RecoveryTrigger::kOscillation);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user