Files
costmap_2d/CMakeLists.txt
duongtd 40488b7632 feat(costmap_2d): direction zones and map readiness
- DirectionalLayer rewritten as a DirectionFieldProvider. It no longer
  derives from StaticLayer and never writes the master grid. It takes an
  OccupancyGrid of direction codes (0 = no constraint, 1..8 =
  direction_index + 1) on direction_topic, validates it against the
  master geometry and global frame, and publishes an immutable
  DirectionField snapshot with a DISABLED / PENDING / READY state and a
  generation counter. Every static map on map_topic returns the layer to
  PENDING until a grid for that map arrives.
- direction_field.h is the core API for planners: grid validation,
  bounds-checked codeAt(), a half-open-cell DDA (traverseGridSegment),
  allowedDirectionMask() and checkSegment().
  LayeredCostmap::getDirectionFieldSnapshot() exposes the snapshot
  without adding data members.
- max_deviation_deg (default 90, valid 0 < x < 180). The field carries
  the value so every planner uses the same rule; 90 keeps the previous
  masks bit for bit.
- StaticLayer ignores an OccupancyGrid addressed to another layer's
  topic instead of logging two unthrottled errors per push.
- StaticLayer and its subclasses report current only after updateCosts
  has written the newest map (markMapWritten, also used by
  CriticalLayer). Readiness flags are written under the master mutex;
  invalid partial updates are rejected with a throttled error.

Full history: branch feature/direction-zone.
2026-11-04 13:24:16 +07:00

359 lines
11 KiB
CMake

cmake_minimum_required(VERSION 3.0.2)
project(robot_costmap_2d VERSION 1.0.0 LANGUAGES CXX)
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
set(BUILDING_WITH_CATKIN TRUE)
message(STATUS "Building robot_costmap_2d with Catkin")
else()
set(BUILDING_WITH_CATKIN FALSE)
message(STATUS "Building robot_costmap_2d with Standalone CMake")
endif()
# C++ Standard - must be set before find_package
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Find dependencies
find_package(Eigen3 REQUIRED)
find_package(Boost REQUIRED COMPONENTS system thread filesystem)
# GTest is only needed by the test executables, which the robot deployment build
# does not produce. QUIET, not REQUIRED: a robot without GTest installed must
# still configure. The test block below requires it where it is actually used.
find_package(GTest QUIET)
find_package(yaml-cpp REQUIRED)
# DepthCameraObstacleLayer only (voxel grid, kd-tree clearing, Euclidean clustering); the core
# library and the other layers do not use PCL.
find_package(PCL REQUIRED COMPONENTS common filters kdtree search segmentation)
if (NOT BUILDING_WITH_CATKIN)
# Enable Position Independent Code
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Cấu hình RPATH để tránh cycle trong runtime search path
set(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_BUILD_RPATH "${CMAKE_BINARY_DIR}")
set(PACKAGES_DIR
robot_std_msgs
robot_sensor_msgs
geometry_msgs
robot_nav_msgs
robot_map_msgs
robot_laser_geometry
robot_visualization_msgs
robot_voxel_grid
tf3
robot_tf3_geometry_msgs
robot_tf3_sensor_msgs
data_convert
robot_xmlrpcpp
robot_cpp
robot_time
)
find_library(TF3_LIBRARY NAMES tf3 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
else()
# ========================================================
# Catkin specific configuration
# ========================================================
find_package(catkin REQUIRED COMPONENTS
robot_std_msgs
robot_sensor_msgs
geometry_msgs
robot_nav_msgs
robot_map_msgs
robot_laser_geometry
robot_visualization_msgs
robot_voxel_grid
robot_tf3_geometry_msgs
robot_tf3_sensor_msgs
data_convert
robot_xmlrpcpp
robot_cpp
robot_time
)
find_library(TF3_LIBRARY NAMES tf3)
catkin_package(
INCLUDE_DIRS include
LIBRARIES robot_costmap_2d plugins
CATKIN_DEPENDS robot_std_msgs robot_sensor_msgs geometry_msgs robot_nav_msgs robot_map_msgs robot_laser_geometry robot_visualization_msgs robot_voxel_grid robot_tf3_geometry_msgs robot_tf3_sensor_msgs data_convert robot_xmlrpcpp robot_cpp robot_time
DEPENDS Boost
)
include_directories(
include
${catkin_INCLUDE_DIRS}
${EIGEN3_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${GTEST_INCLUDE_DIRS}
${TF3_INCLUDE_DIR}
)
endif()
# Define macro để dùng trong code
add_definitions(-DROBOT_COSTMAP_2D_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
# Eigen definitions
add_definitions(${EIGEN3_DEFINITIONS})
# ========================================================
# Core library: robot_costmap_2d
# ========================================================
add_library(robot_costmap_2d SHARED
src/costmap_2d_robot.cpp
src/array_parser.cpp
src/costmap_2d.cpp
src/observation_buffer.cpp
src/layer.cpp
src/layered_costmap.cpp
src/direction_field.cpp
src/costmap_math.cpp
src/footprint.cpp
src/costmap_layer.cpp
)
if(BUILDING_WITH_CATKIN)
add_dependencies(robot_costmap_2d ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_include_directories(robot_costmap_2d
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
${EIGEN3_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${TF3_INCLUDE_DIR}
)
target_link_libraries(robot_costmap_2d
PUBLIC ${catkin_LIBRARIES}
PRIVATE Boost::boost Boost::system Boost::thread Boost::filesystem
PRIVATE yaml-cpp
PRIVATE dl
PRIVATE ${TF3_LIBRARY}
)
else()
target_include_directories(robot_costmap_2d
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
${EIGEN3_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${TF3_INCLUDE_DIR}
)
target_link_libraries(robot_costmap_2d
PUBLIC
${PACKAGES_DIR}
PRIVATE
Boost::boost Boost::system Boost::thread Boost::filesystem
yaml-cpp
dl
${TF3_LIBRARY}
)
set_target_properties(robot_costmap_2d PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
BUILD_RPATH "${CMAKE_BINARY_DIR}"
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
)
endif()
# ========================================================
# Plugin library: plugins
# ========================================================
add_library(plugins SHARED
plugins/static_layer.cpp
plugins/obstacle_layer.cpp
plugins/inflation_layer.cpp
plugins/voxel_layer.cpp
plugins/critical_layer.cpp
plugins/directional_layer.cpp
plugins/preferred_layer.cpp
plugins/unpreferred_layer.cpp
plugins/depth_camera_obstacle_layer.cpp
)
# PCL stays private to the plugin library: no PCL type in public headers.
target_include_directories(plugins SYSTEM PRIVATE ${PCL_INCLUDE_DIRS})
target_compile_options(plugins PRIVATE ${PCL_DEFINITIONS})
target_link_libraries(plugins PRIVATE ${PCL_LIBRARIES})
if(BUILDING_WITH_CATKIN)
add_dependencies(plugins ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_include_directories(plugins
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(plugins
PUBLIC robot_costmap_2d
PRIVATE ${catkin_LIBRARIES}
PRIVATE Boost::boost Boost::system Boost::thread Boost::filesystem
PRIVATE yaml-cpp
PRIVATE ${TF3_LIBRARY}
)
else()
target_include_directories(plugins
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(plugins
PUBLIC robot_costmap_2d
PRIVATE Boost::boost Boost::system Boost::thread Boost::filesystem
PRIVATE yaml-cpp
PRIVATE robot_time
PRIVATE robot_cpp
PRIVATE ${TF3_LIBRARY}
)
set_target_properties(plugins PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
BUILD_RPATH "${CMAKE_BINARY_DIR}"
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
)
endif()
# ========================================================
# Install
# ========================================================
if(BUILDING_WITH_CATKIN)
## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
install(TARGETS robot_costmap_2d plugins
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)
install(FILES config/directional_layer_params.yaml
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/config)
## Mark cpp header files for installation
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
PATTERN ".svn" EXCLUDE
)
else()
install(TARGETS robot_costmap_2d plugins
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
install(FILES config/directional_layer_params.yaml
DESTINATION share/${PROJECT_NAME}/config)
# Export targets
install(EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION lib/cmake/${PROJECT_NAME}
)
## Mark cpp header files for installation
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION include
FILES_MATCHING PATTERN "*.h"
PATTERN ".svn" EXCLUDE
)
# Print configuration info
message(STATUS "=================================")
message(STATUS "Project: ${PROJECT_NAME}")
message(STATUS "Version: ${PROJECT_VERSION}")
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "Libraries: robot_costmap_2d, plugins")
message(STATUS "Dependencies: robot_std_msgs, robot_sensor_msgs, geometry_msgs, robot_nav_msgs, robot_map_msgs, robot_laser_geometry, robot_visualization_msgs, robot_voxel_grid, tf3, robot_tf3_geometry_msgs, robot_tf3_sensor_msgs, data_convert, robot_xmlrpcpp, robot_cpp, robot_time, Eigen3, Boost, yaml-cpp")
message(STATUS "=================================")
endif()
# ========================================================
# Test executables
# ========================================================
# Default follows the build mode, not a fixed value: a catkin workspace is a
# development machine and gets the tests, while the standalone CMake build is how
# the robot is deployed and must not need GTest installed at all. Force it on with
# -DBUILD_COSTMAP_TESTS=ON to run the tests from a standalone build.
option(BUILD_COSTMAP_TESTS "Build robot_costmap_2d test executables"
${BUILDING_WITH_CATKIN})
if(BUILD_COSTMAP_TESTS)
find_package(GTest REQUIRED)
find_package(Boost REQUIRED COMPONENTS system thread filesystem)
find_library(TF3_LIBRARY NAMES tf3 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
add_executable(direction_field_test test/direction_field_test.cpp)
target_link_libraries(direction_field_test PRIVATE robot_costmap_2d GTest::GTest GTest::Main)
add_executable(directional_layer_test test/directional_layer_test.cpp)
target_link_libraries(directional_layer_test PRIVATE plugins robot_costmap_2d GTest::GTest GTest::Main Boost::thread ${TF3_LIBRARY})
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/array_parser_test.cpp)
add_executable(test_array_parser test/array_parser_test.cpp)
target_link_libraries(test_array_parser PRIVATE
robot_costmap_2d
GTest::GTest
GTest::Main
Boost::system Boost::thread
${TF3_LIBRARY}
)
endif()
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/coordinates_test.cpp)
add_executable(test_costmap test/coordinates_test.cpp)
target_link_libraries(test_costmap PRIVATE
plugins
robot_costmap_2d
GTest::GTest
GTest::Main
Boost::system Boost::thread
${TF3_LIBRARY}
)
endif()
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/static_layer_test.cpp)
add_executable(test_plugin test/static_layer_test.cpp)
target_link_libraries(test_plugin PRIVATE
robot_costmap_2d
Boost::boost Boost::filesystem Boost::system
yaml-cpp
dl
Boost::system Boost::thread
robot_time
GTest::GTest
GTest::Main
${TF3_LIBRARY}
)
endif()
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/depth_camera_obstacle_layer_test.cpp)
add_executable(depth_camera_obstacle_layer_test test/depth_camera_obstacle_layer_test.cpp)
target_link_libraries(depth_camera_obstacle_layer_test PRIVATE
plugins
robot_costmap_2d
yaml-cpp
GTest::GTest
GTest::Main
)
endif()
endif()