From 80bde38f4d5dbb66bab5a8bd5c2c3faaa870aa9f Mon Sep 17 00:00:00 2001 From: HiepLM Date: Wed, 7 Jan 2026 09:26:36 +0700 Subject: [PATCH] ros --- CMakeLists.txt | 136 ++++++++++++++++++++++++++++++++++++++++--------- package.xml | 46 ++++++++++++++++- 2 files changed, 157 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 579968c..fbd6423 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,19 @@ # --- CMake version và project name --- cmake_minimum_required(VERSION 3.10) + +# ======================================================== +# Dual-mode CMakeLists.txt: Supports both Catkin and Standalone CMake +# ======================================================== + +# Detect if building with Catkin +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() + project(robot_costmap_2d) # --- C++ standard và position independent code --- @@ -22,6 +36,39 @@ find_package(Boost REQUIRED COMPONENTS system thread filesystem) # Boost: syste find_package(GTest REQUIRED) # Google Test cho unit test find_package(PCL REQUIRED COMPONENTS common io) # Point Cloud Library +if(BUILDING_WITH_CATKIN) + 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 + tf3 + robot_tf3_geometry_msgs + robot_tf3_sensor_msgs + data_convert + robot_xmlrpcpp + robot_cpp + robot_time + ) +endif() + +# ======================================================== +# Catkin specific configuration +# ======================================================== + +if(BUILDING_WITH_CATKIN) + 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 tf3 robot_tf3_geometry_msgs robot_tf3_sensor_msgs data_convert robot_xmlrpcpp robot_cpp robot_time + DEPENDS Eigen3 PCL + ) +endif() + # --- Define macro để dùng trong code --- add_definitions(-DROBOT_COSTMAP_2D_DIR="${CMAKE_CURRENT_SOURCE_DIR}") @@ -36,6 +83,10 @@ include_directories( ) link_directories(${PCL_LIBRARY_DIRS}) # Thêm thư viện PCL vào linker path +if(BUILDING_WITH_CATKIN) + include_directories(${catkin_INCLUDE_DIRS}) +endif() + # --- Eigen và PCL definitions --- add_definitions(${EIGEN3_DEFINITIONS} ${PCL_DEFINITIONS}) @@ -54,25 +105,39 @@ add_library(robot_costmap_2d ) # --- Link các thư viện phụ thuộc --- -target_link_libraries(robot_costmap_2d - ${Boost_LIBRARIES} # Boost - 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 # XMLRPC - yaml-cpp - dl - robot_cpp -) +if(BUILDING_WITH_CATKIN) + target_link_libraries(robot_costmap_2d + ${catkin_LIBRARIES} + ${Boost_LIBRARIES} # Boost + yaml-cpp + dl + ${PCL_LIBRARIES} + ) +else() + # Standalone mode: link dependencies with PUBLIC visibility to propagate include paths + target_link_libraries(robot_costmap_2d + PUBLIC + 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 # XMLRPC + robot_cpp + PRIVATE + ${Boost_LIBRARIES} # Boost + yaml-cpp + dl + ${PCL_LIBRARIES} + ) +endif() # --- Include directories cho target --- target_include_directories(robot_costmap_2d @@ -82,6 +147,10 @@ target_include_directories(robot_costmap_2d $ # Khi install ) +if(BUILDING_WITH_CATKIN) + add_dependencies(robot_costmap_2d ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) +endif() + # --- Cài đặt thư viện vào hệ thống khi chạy make install --- install(TARGETS robot_costmap_2d EXPORT robot_costmap_2d-targets @@ -103,9 +172,11 @@ install(EXPORT robot_costmap_2d-targets ) # --- Cài đặt headers --- -install(DIRECTORY include/${PROJECT_NAME}/ - DESTINATION include/${PROJECT_NAME} -) +if(NOT BUILDING_WITH_CATKIN) + install(DIRECTORY include/${PROJECT_NAME}/ + DESTINATION include/${PROJECT_NAME} + ) +endif() # --- Plugin libraries --- # Tạo các plugin shared library @@ -121,19 +192,36 @@ add_library(plugins plugins/unpreferred_layer.cpp ) -target_link_libraries(plugins +if(BUILDING_WITH_CATKIN) + target_link_libraries(plugins PRIVATE robot_costmap_2d + ${catkin_LIBRARIES} + ${Boost_LIBRARIES} + yaml-cpp + ) +else() + # In standalone mode, robot_costmap_2d already has PUBLIC links to all dependencies + # so we just need to link it with PUBLIC visibility to get the include paths + target_link_libraries(plugins + PUBLIC + robot_costmap_2d + PRIVATE ${Boost_LIBRARIES} yaml-cpp robot_time robot_cpp - ) + ) +endif() set_target_properties(plugins PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} ) +if(BUILDING_WITH_CATKIN) + add_dependencies(plugins ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) +endif() + install(TARGETS plugins EXPORT plugins-targets ARCHIVE DESTINATION lib # Thư viện tĩnh .a diff --git a/package.xml b/package.xml index be108a7..8998e79 100644 --- a/package.xml +++ b/package.xml @@ -20,7 +20,51 @@ catkin libconsole-bridge-dev - libconsole-bridge-dev + robot_std_msgs + robot_std_msgs + + robot_sensor_msgs + robot_sensor_msgs + + geometry_msgs + geometry_msgs + + robot_nav_msgs + robot_nav_msgs + + robot_map_msgs + robot_map_msgs + + robot_laser_geometry + robot_laser_geometry + + robot_visualization_msgs + robot_visualization_msgs + + robot_voxel_grid + robot_voxel_grid + + tf3 + tf3 + + robot_tf3_geometry_msgs + robot_tf3_geometry_msgs + + robot_tf3_sensor_msgs + robot_tf3_sensor_msgs + + data_convert + data_convert + + robot_xmlrpcpp + robot_xmlrpcpp + + robot_cpp + robot_cpp + + robot_time + robot_time + \ No newline at end of file