Files
move_base2/CMakeLists.txt
duongtd 8bc4895a60 feat: lifecycle contract — cancel/pause must reach true quiescence before reporting done
Root cause (from a C++ host log, 2026-09-11): cancel during PLANNING only set
a discard flag on the planner thread, which ran to allocated_time before
CANCELLED could be reported; a queued next mission landed on a still-busy
planner and timed out too. Same class of bug existed for pause, recovery, and
implicitly for a legitimate ABORTED read while a component was still
unwinding. Full audit, decisions, and phased design in
docs/PLAN_LIFECYCLE_CANCEL_PAUSE_RESUME.md; old-vs-new behaviour per state in
docs/LIFECYCLE_BEHAVIOUR_MATRIX.md.

Runtime quiescence, not just "command sent":
- kPausing (new state) mirrors kCancelling: PAUSED/CANCELLED is only entered
  once planner and recovery are idle, the action reports terminal, and the
  robot is stopped per odometry (robot_stopped, stopped_measured_linear/
  angular_eps) — not merely "the command we sent was zero". settle_timeout
  bounds the wait; exceeding it still finishes but logs an ungated error
  naming which component was still busy, so a plugin that doesn't honour
  cancel/pause is visible instead of silently swallowed.
- PlannerRunner::running_planner_ (new): the instance the planner thread is
  actually inside makePlan() with, tracked separately from active_ — active_
  can already point at a different instance after swapPlanner() while the
  old attempt is still running. cancelPlan(), swapPlanner() (when a job is
  running_ or only pending_ and not yet picked up), and the destructor all
  preempt through this pointer now; the old code preempted through active_
  and could silently preempt the wrong (idle) instance after a profile swap.
- ComponentStatusBoard + per-component ComponentPhase (idle/busy/stopping/
  paused/hung), read by PlannerRunner, RecoveryRunner, ActionRunner and
  ControlLoop instead of ad hoc bools — this is what makes "did everything
  actually stop" answerable at all, and what the new Watchdog thread reads
  to flag a component busy-without-progress as hung (logged once at the
  edge) without the control loop needing to detect its own stall.
- Resume: PLANNING replans from the current pose. CONTROLLING re-issues
  setPlan() with the plan already in hand (decided: setPlan again, not a
  fresh plan) — the local planner needs nothing else since it already runs
  once per control cycle; a fresh plan on resume was considered and
  rejected as unnecessary work for a plan that's still valid.
- Command-to-zero on cancel/pause stays immediate (no deceleration ramp) —
  existing VelocityArbiter behaviour, left as is per a scoped decision.

Depends on: Test/sbpl (bd89924), pnkx_nav_core (2b0993b, robot_nav_core::
Preemptable + component_status_probe.h), Test/sbpl_lattice_planner (7456e99),
Test/recovery_core (254c7fc, onCancelRequested + ABI guard), Test/
sbpl_recovery (ea79686, worker-thread cancel), Test/action_core (669c3f7,
pause/resume + kCancelled), Test/dock_pattern_action (cc1ea74).

Verified: 13 gtest binaries / 528 tests, 0 failures. Runtime on the dev
machine (Gazebo + real ROS host + VDA5050 over MQTT), 6 scenarios — cancel
during PLANNING, pause/resume during PLANNING and CONTROLLING (incl.
submit-while-still-winding-down), pause-then-cancel during PLANNING and
CONTROLLING: no ABORTED/RECOVERING in any run, preempt lands within
hundredths of a millisecond of entering CANCELLING/PAUSING, PAUSED/CANCELLED
only reported once the robot is actually stopped per odometry. Full numbers
in the plan doc section 6.

Excluded from this commit (pre-existing, unrelated):
config/runtime/recovery_behaviors_params.yaml (allocated_time 1.0->2.0,
SBPLDetourPlanner tuning predating this work).

Not yet done (see plan doc "Trạng thái"): config tuning (phase 0, pending
per a scoping decision); runtime scenarios for cancel mid-detour and a 30 s
pause mid-action; commit to rb_ws is a separate step, not done here.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-12 22:07:34 +07:00

534 lines
15 KiB
CMake

cmake_minimum_required(VERSION 3.0.2)
project(move_base2 VERSION 0.1.0 LANGUAGES CXX)
# ========================================================
# Build mode detection
# ========================================================
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
set(BUILDING_WITH_CATKIN TRUE)
message(STATUS "Building move_base2 with Catkin")
else()
set(BUILDING_WITH_CATKIN FALSE)
message(STATUS "Building move_base2 with Standalone CMake")
endif()
# ========================================================
# C++ Standard
# ========================================================
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# ========================================================
# Common dependencies
# ========================================================
find_package(Boost REQUIRED COMPONENTS
system
thread
filesystem
)
find_package(Threads REQUIRED)
find_package(yaml-cpp REQUIRED)
# ========================================================
# Standalone configuration
# ========================================================
if(NOT BUILDING_WITH_CATKIN)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_BUILD_RPATH "${CMAKE_BINARY_DIR}")
# Gói này build được ở hai vị trí:
# Test/<pkg> (dev, sibling của pnkx_nav_core) -> ../../pnkx_nav_core/src
# src/Navigations/Packages/<pkg> (in-tree) -> ../../.. chính là src
# Có thể ép qua -DPNKX_NAV_CORE_SRC_DIR khi layout khác chuẩn.
if(NOT DEFINED PNKX_NAV_CORE_SRC_DIR)
set(PNKX_NAV_CORE_SRC_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/../../pnkx_nav_core/src"
)
if(NOT IS_DIRECTORY "${PNKX_NAV_CORE_SRC_DIR}")
set(PNKX_NAV_CORE_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../..")
endif()
endif()
# Test/<pkg> -> T800_ws/devel/lib
set(WORKSPACE_DEVEL_LIB_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/../../../../devel/lib"
)
file(GLOB STANDALONE_PACKAGE_INCLUDE_DIRS
LIST_DIRECTORIES true
${PNKX_NAV_CORE_SRC_DIR}/*/include
${PNKX_NAV_CORE_SRC_DIR}/*/*/include
${PNKX_NAV_CORE_SRC_DIR}/*/*/*/include
${PNKX_NAV_CORE_SRC_DIR}/*/*/*/*/include
)
# Glob rỗng nghĩa là PNKX_NAV_CORE_SRC_DIR trỏ trượt — fail ngay tại configure thay vì để
# compiler báo "No such file or directory" khó lần.
if(NOT STANDALONE_PACKAGE_INCLUDE_DIRS)
message(FATAL_ERROR
"move_base2: no package include dirs found under "
"'${PNKX_NAV_CORE_SRC_DIR}'. Set -DPNKX_NAV_CORE_SRC_DIR=<pnkx_nav_core>/src.")
endif()
set(STANDALONE_INCLUDE_DIRS
${STANDALONE_PACKAGE_INCLUDE_DIRS}
# These runtime packages are siblings while they still live below Test/.
# The explicit paths keep `cmake ..` in move_base2 working before the
# packages are moved below pnkx_nav_core/src.
${CMAKE_CURRENT_SOURCE_DIR}/../recovery_core/include
${CMAKE_CURRENT_SOURCE_DIR}/../action_core/include
${CMAKE_CURRENT_SOURCE_DIR}/../mission_adapters/include
${CMAKE_CURRENT_SOURCE_DIR}/../robot_sensor_ports/include
${CMAKE_CURRENT_SOURCE_DIR}/../nav_test_harness/include
/usr/local/include
)
set(PACKAGES_DIR
robot_costmap_2d
robot_nav_2d_utils
robot_cpp
robot_time
robot_xmlrpcpp
# move_base2 public headers and runners use these packages directly.
# When configured from the pnkx_nav_core root, these names resolve to
# CMake targets and propagate their public include directories.
recovery_core
action_core
mission_adapters
# Kho cảm biến thô dùng chung. NavigationServer ghi vào, runner phát cổng đọc ra plugin.
robot_sensor_ports
# nav_test_harness KHÔNG nằm ở đây: chỉ test (nhánh catkin) dùng nó. Link vào lib
# production làm build standalone đòi -lnav_test_harness trên máy không build gói đó.
)
find_library(TF3_LIBRARY
NAMES tf3
PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu
)
if(NOT TF3_LIBRARY)
message(FATAL_ERROR "tf3 not found — install tf3 (/usr/local/lib/libtf3.so) before building")
endif()
if(EXISTS ${WORKSPACE_DEVEL_LIB_DIR})
link_directories(${WORKSPACE_DEVEL_LIB_DIR})
endif()
link_directories(/usr/local/lib)
# ========================================================
# Catkin configuration
# ========================================================
else()
find_package(catkin REQUIRED COMPONENTS
move_base_core
robot_nav_core
robot_costmap_2d
robot_cpp
robot_time
robot_geometry_msgs
robot_std_msgs
robot_nav_msgs
robot_nav_2d_msgs
robot_nav_2d_utils
robot_sensor_msgs
robot_map_msgs
robot_protocol_msgs
robot_xmlrpcpp
# SensorGateway là chỗ duy nhất trong gói này lọc laser trước khi vào costmap.
laser_filter
# RecoveryRunner include thẳng recovery_core: đó là chỗ duy nhất trong gói này biết tới nó.
recovery_core
# ActionRunner include thẳng action_core: chỗ duy nhất trong gói này biết tới nó.
action_core
# Kho cảm biến thô dùng chung. NavigationServer ghi vào, runner phát cổng đọc ra plugin.
robot_sensor_ports
# `bridges/` include thẳng mission_adapters: biên duy nhất trong gói này biết tới nó
# (MissionAdapterBridge dịch contract, MissionLayer lắp ráp framework).
mission_adapters
# scenario_test chạy kịch bản khai báo qua khung của nav_test_harness.
nav_test_harness
)
find_library(TF3_LIBRARY
NAMES tf3
PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu
)
if(NOT TF3_LIBRARY)
message(FATAL_ERROR "tf3 not found — install tf3 (/usr/local/lib/libtf3.so) before building")
endif()
catkin_package(
INCLUDE_DIRS
include
LIBRARIES
move_base2_core
move_base2
CATKIN_DEPENDS
move_base_core
robot_nav_core
robot_costmap_2d
robot_cpp
robot_time
robot_geometry_msgs
robot_std_msgs
robot_nav_msgs
robot_nav_2d_msgs
robot_nav_2d_utils
robot_sensor_msgs
robot_map_msgs
robot_protocol_msgs
robot_xmlrpcpp
DEPENDS
Boost
)
include_directories(include)
# Dependency vào dạng SYSTEM: `robot/robot.h` kéo theo `console.h`, file này khai vài chục hằng màu
# ở namespace scope và sinh đúng số đó warning -Wunused-variable. Để chúng lẫn vào output sẽ chôn
# mất warning THẬT của gói — mà cờ -Wall -Wextra ở dưới có mặt chính là để thấy những warning đó.
include_directories(SYSTEM
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
endif()
# ========================================================
# Core library — logic thuần, không I/O.
#
# Tách riêng khỏi plugin để test lõi không phải kéo theo contract host, và để bất kỳ ai muốn nhúng
# state machine vào runtime khác cũng dùng lại được.
# ========================================================
add_library(move_base2_core SHARED
src/navigation_state.cpp
src/component_status.cpp
src/state_machine.cpp
src/velocity_arbiter.cpp
src/plan_inspector.cpp
src/grid_analysis.cpp
src/env_monitor.cpp
src/control_loop.cpp
src/config/move_base2_config.cpp
src/runners/recovery_runner.cpp
src/runners/action_runner.cpp
src/runners/planner_runner.cpp
src/runners/controller_runner.cpp
src/io/runtime_stats.cpp
src/io/watchdog.cpp
src/io/sensor_gateway.cpp
src/io/sensor_store_types.cpp
src/io/costmap_exporter.cpp
src/io/env_logger.cpp
src/bridges/mission_adapter_bridge.cpp
src/bridges/mission_layer.cpp
src/navigation_runtime.cpp
)
# Gói này KHÔNG kế thừa cờ -w của pnkx_nav_core: warning ở đây phải nhìn thấy được.
target_compile_options(move_base2_core PRIVATE -Wall -Wextra)
target_include_directories(move_base2_core
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
# ========================================================
# Plugin library — facade BaseNavigation + export Boost.DLL.
# ========================================================
add_library(move_base2 SHARED
src/navigation_server.cpp
src/move_base2_plugin.cpp
)
target_compile_options(move_base2 PRIVATE -Wall -Wextra)
target_include_directories(move_base2
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
set_target_properties(move_base2 PROPERTIES POSITION_INDEPENDENT_CODE ON)
# ========================================================
# Linking
# ========================================================
if(BUILDING_WITH_CATKIN)
add_dependencies(move_base2_core
${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS}
)
add_dependencies(move_base2
move_base2_core
${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS}
)
target_link_libraries(move_base2_core
PUBLIC
${catkin_LIBRARIES}
PRIVATE
Boost::boost
Threads::Threads
${TF3_LIBRARY}
)
target_link_libraries(move_base2
PUBLIC
move_base2_core
${catkin_LIBRARIES}
PRIVATE
Boost::boost
Boost::system
Boost::filesystem
Threads::Threads
${CMAKE_DL_LIBS}
${TF3_LIBRARY}
)
else()
target_include_directories(move_base2_core
PRIVATE
${STANDALONE_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
target_include_directories(move_base2
PRIVATE
${STANDALONE_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
target_link_libraries(move_base2_core
PUBLIC
${PACKAGES_DIR}
PRIVATE
Boost::boost
Threads::Threads
${TF3_LIBRARY}
)
target_link_libraries(move_base2
PUBLIC
move_base2_core
${PACKAGES_DIR}
PRIVATE
Boost::boost
Boost::system
Boost::filesystem
Threads::Threads
${CMAKE_DL_LIBS}
${TF3_LIBRARY}
)
set_target_properties(move_base2_core move_base2 PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
BUILD_RPATH "${CMAKE_BINARY_DIR}"
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
)
endif()
# ========================================================
# Install
# ========================================================
if(BUILDING_WITH_CATKIN)
install(TARGETS move_base2_core move_base2
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
)
install(DIRECTORY launch config
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)
else()
install(TARGETS move_base2_core move_base2
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
install(EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION lib/cmake/${PROJECT_NAME}
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION include
FILES_MATCHING PATTERN "*.h"
)
endif()
# ========================================================
# Tests
# ========================================================
option(BUILD_MOVE_BASE2_TESTS "Build move_base2 tests" ON)
if(BUILD_MOVE_BASE2_TESTS AND BUILDING_WITH_CATKIN)
if(NOT COMMAND catkin_add_gtest)
message(FATAL_ERROR "catkin_add_gtest NOT FOUND")
endif()
# Plugin global planner chỉ dùng cho test — nạp qua Boost.DLL đúng đường runtime đi, không link
# thẳng. Đặt trong nhánh test để không bao giờ lọt vào bản cài đặt.
foreach(test_plugin move_base2_test_global_planner move_base2_test_local_planner)
if(test_plugin STREQUAL "move_base2_test_global_planner")
set(test_plugin_src test/plugins/test_global_planner.cpp)
else()
set(test_plugin_src test/plugins/test_local_planner.cpp)
endif()
add_library(${test_plugin} SHARED ${test_plugin_src})
target_compile_options(${test_plugin} PRIVATE -Wall -Wextra)
target_include_directories(${test_plugin}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)
set_target_properties(${test_plugin} PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(${test_plugin}
PRIVATE
${catkin_LIBRARIES}
Boost::boost
${CMAKE_DL_LIBS}
)
endforeach()
set(MOVE_BASE2_TESTS
component_status_test
watchdog_test
state_machine_test
velocity_arbiter_test
plan_inspector_test
grid_analysis_test
env_monitor_test
walking_skeleton_test
config_validation_test
action_runner_test
recovery_runner_test
sensor_gateway_test
runtime_stats_test
navigation_server_test
planner_runner_test
controller_runner_test
mission_adapter_bridge_test
mission_layer_test
deferred_goal_test
move_base2_scenario_test # tên có tiền tố gói: nav_test_harness đã có target scenario_test
)
foreach(test_name ${MOVE_BASE2_TESTS})
catkin_add_gtest(${test_name} test/${test_name}.cpp)
if(TARGET ${test_name})
# catkin_add_gtest đặt target EXCLUDE_FROM_ALL; mở ra để binary có mặt sau `catkin_make`
# thường, đúng quy trình verify của repo (chạy thẳng ./devel/lib/<pkg>/<test>).
set_target_properties(${test_name} PROPERTIES EXCLUDE_FROM_ALL FALSE)
target_compile_options(${test_name} PRIVATE -Wall -Wextra)
target_include_directories(${test_name}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/test
)
# SYSTEM phải đứng ngay sau tên target, nếu không bị hiểu là path tương đối.
target_include_directories(${test_name} SYSTEM
PRIVATE
${catkin_INCLUDE_DIRS}
)
# catkin_add_gtest đã dùng target_link_libraries dạng plain, nên phần bổ sung cũng phải plain.
target_link_libraries(${test_name}
move_base2_core
${catkin_LIBRARIES}
${Boost_LIBRARIES}
yaml-cpp
${CMAKE_DL_LIBS}
)
# Plugin recovery được nạp qua Boost.DLL từ devel/lib, không link thẳng: đúng đường runtime đi.
# ctest không mang theo PNKX_NAV_CORE_* của shell nên binary tự trỏ.
target_compile_definitions(${test_name} PRIVATE
MOVE_BASE2_TEST_CONFIG_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test/config"
MOVE_BASE2_TEST_LIBRARY_DIR="${CATKIN_DEVEL_PREFIX}/lib"
# Kịch bản là DỮ LIỆU nằm ở nav_test_harness; test không được đoán đường dẫn tới nó.
MOVE_BASE2_SCENARIO_DIR="${CMAKE_CURRENT_SOURCE_DIR}/../nav_test_harness/scenarios"
)
endif()
endforeach()
# `NavigationServer` nằm trong thư viện plugin `move_base2`, không phải trong `move_base2_core`.
if(TARGET navigation_server_test)
target_link_libraries(navigation_server_test move_base2)
endif()
# Plugin phải có mặt trong devel/lib trước khi test chạy — nó được nạp qua Boost.DLL lúc chạy.
if(TARGET planner_runner_test)
add_dependencies(planner_runner_test move_base2_test_global_planner)
endif()
if(TARGET controller_runner_test)
add_dependencies(controller_runner_test move_base2_test_local_planner)
endif()
endif()