ROS2的构建系统叫做ament_cmake
,它是基于CMake
改进而来的。接下来将详细介绍一下ament_cmake
常用的一些命令:
ament_target_dependencies
:添加重要的头文件、库以及相关依赖ament_export_dependencies
:导出依赖给下游的功能包,以至于使用该库时,不必再调用find_package
添加这些依赖ament_export_include_directories
:标记该功能包的头文件位置,以便其他功能包要依赖该功能包时能顺利找到对应的头文件ament_export_libraries
:标记该功能包需要导出的库,以便其他的功能包能链接到这些库。ament_package
:每个功能都必须执行一次调用,会为该功能生成一个ament
索引,以及cmake相关配置文件,以便其他功能包能够通过find_package
找到该功能包。由于ament_package()
会从CMakeLists.txt
文件中收集大量信息,因此它应该是CMakeLists.txt
文件中的最后一个调用。一个功能包的构建信息包含在
CMakeLists.txt
:描述了如何构建此功能包package.xml
:包含该功能包的依赖信息,它可以帮助编译工具colcon确定多个功能包编译的顺序接下来,以nav2中一个比较有代表性的功能包nav2_costmap_2d
的CmakeList.txt
来进行介绍
cmake
的最低版本(3.5
)和功能包的名字cmake_minimum_required(VERSION 3.5)
project(nav2_costmap_2d)
ament_cmake
包find_package(ament_cmake REQUIRED)
ROS2功能包
find_package(geometry_msgs REQUIRED)
find_package(laser_geometry REQUIRED)
find_package(map_msgs REQUIRED)
find_package(message_filters REQUIRED)
find_package(nav2_common REQUIRED)
find_package(nav2_msgs REQUIRED)
find_package(nav2_util)
find_package(nav2_voxel_grid REQUIRED)
find_package(nav_msgs REQUIRED)
find_package(pluginlib REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(rmw REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(std_msgs REQUIRED)
find_package(std_srvs REQUIRED)
find_package(tf2_geometry_msgs REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_ros REQUIRED)
find_package(tf2_sensor_msgs REQUIRED)
find_package(visualization_msgs REQUIRED)
find_package(angles REQUIRED)
非ROS2功能包
find_package(Eigen3 REQUIRED)
include_directories(
include
${EIGEN3_INCLUDE_DIRS}
)
add_library
构建库add_library(nav2_costmap_2d_core SHARED
src/array_parser.cpp
src/costmap_2d.cpp
src/layer.cpp
src/layered_costmap.cpp
src/costmap_2d_ros.cpp
src/costmap_2d_publisher.cpp
src/costmap_math.cpp
src/footprint.cpp
src/costmap_layer.cpp
src/observation_buffer.cpp
src/clear_costmap_service.cpp
src/footprint_collision_checker.cpp
plugins/costmap_filters/costmap_filter.cpp
)
dependencies
,这里主要添加的是ROS2功能包
set(dependencies
geometry_msgs
laser_geometry
map_msgs
message_filters
nav2_msgs
nav2_util
nav2_voxel_grid
nav_msgs
pluginlib
rclcpp
rclcpp_lifecycle
sensor_msgs
std_msgs
std_srvs
tf2
tf2_geometry_msgs
tf2_ros
tf2_sensor_msgs
visualization_msgs
angles
)
ament_target_dependencies
添加ROS2功能包依赖
,它将依赖的库、头文件以及自身依赖添加到目标中ament_target_dependencies(nav2_costmap_2d_core
${dependencies}
)
add_executable
构建可执行文件,同时也需要使用ament_target_dependencies
添加相关依赖add_executable(nav2_costmap_2d_markers src/costmap_2d_markers.cpp)
target_link_libraries(nav2_costmap_2d_markers
nav2_costmap_2d_core
)
ament_target_dependencies(nav2_costmap_2d_markers
${dependencies}
)
add_executable(nav2_costmap_2d_cloud src/costmap_2d_cloud.cpp)
target_link_libraries(nav2_costmap_2d_cloud
nav2_costmap_2d_core
)
add_executable(nav2_costmap_2d src/costmap_2d_node.cpp)
ament_target_dependencies(nav2_costmap_2d
${dependencies}
)
target_link_libraries(nav2_costmap_2d
nav2_costmap_2d_core
layers
filters
)
install(TARGETS
nav2_costmap_2d_core
layers
filters
nav2_costmap_2d_client
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
lib/${PROJECT_NAME}
而不是bin
install(TARGETS
nav2_costmap_2d
nav2_costmap_2d_markers
nav2_costmap_2d_cloud
RUNTIME DESTINATION lib/${PROJECT_NAME}
)
头文件
install(DIRECTORY include/
DESTINATION include/
)
launch
和参数
文件install(
DIRECTORY launch params
DESTINATION share/${PROJECT_NAME}
)
ament_export_include_directories
导出该功能包的头文件,以便其他功能包依赖此功能包时,能够找到对应的头文件ament_export_include_directories(include)
ament_export_libraries
导出该功能包构建的库,以便其他功能包能够顺利链接到ament_export_libraries(layers filters nav2_costmap_2d_core nav2_costmap_2d_client)
ament_export_dependencies
导出此功能包所有的ROS相关的依赖,依赖其他功能包依赖此功能包时,不必再通过find_package
重复添加这些依赖ament_export_dependencies(${dependencies})
ament_package
ament_package()
注意ament_package
是可以带参数的:
ament_package(CONFIG_EXTRAS cmake/xxxx.cmake)
devel
,只有install
CATKIN_DEVEL_PREFIX
等变量CMAKE_INSTALL_PREFIX
指的是xxx/install/${package_name}
而不是xxx/install
为了获得xxx/install
,可以通过如下方法:
set(INSTALL_DIR ${CMAKE_INSTALL_PREFIX})
string(REPLACE "/${PROJECT_NAME}" "" INSTALL_DIR ${INSTALL_DIR})
如果想在执行source install/setup.bash
时,同时添加自己的环境变量,可
project_name.sh.in
文件ament_prepend_unique_value UESER_ENV_PATH "$AMENT_CURRENT_PREFIX/XXXX"
CMakeLists.txt
中添加ament_environment_hooks("${CMAKE_CURRENT_SOURCE_DIR}/env-hooks/${PROJECT_NAME}.sh.in")
对于package.xml
主要需要添加如下信息:
<buildtool_depend>ament_cmakebuildtool_depend>
<depend>geometry_msgsdepend>
<depend>laser_geometrydepend>
<depend>map_msgsdepend>
<depend>message_filtersdepend>
<depend>nav2_msgsdepend>
<depend>nav2_utildepend>
<depend>nav2_voxel_griddepend>
<depend>nav_msgsdepend>
<depend>pluginlibdepend>
<depend>rclcppdepend>
<depend>rclcpp_lifecycledepend>
<depend>sensor_msgsdepend>
<depend>std_msgsdepend>
<depend>std_srvsdepend>
<depend>tf2depend>
<depend>tf2_geometry_msgsdepend>
<depend>tf2_rosdepend>
<depend>tf2_sensor_msgsdepend>
<depend>visualization_msgsdepend>
<depend>anglesdepend>
<export>
<build_type>ament_cmakebuild_type>
export>
完整的package.xml
<package format ="3">
<name>nav2_costmap_2dname>
<version>1.2.0version>
<description>
This package provides an implementation of a 2D costmap that takes in sensor
data from the world, builds a 2D or 3D occupancy grid of the data (depending
on whether a voxel based implementation is used), and inflates costs in a
2D costmap based on the occupancy grid and a user specified inflation radius.
This package also provides support for map_server based initialization of a
costmap, rolling window based costmaps, and parameter based subscription to
and configuration of sensor topics.
description>
<maintainer email="[email protected]">Steve Macenskimaintainer>
<license>BSD-3-Clauselicense>
<license>Apache-2.0license>
<buildtool_depend>ament_cmakebuildtool_depend>
<build_depend>nav2_commonbuild_depend>
<depend>geometry_msgsdepend>
<depend>laser_geometrydepend>
<depend>map_msgsdepend>
<depend>message_filtersdepend>
<depend>nav2_msgsdepend>
<depend>nav2_utildepend>
<depend>nav2_voxel_griddepend>
<depend>nav_msgsdepend>
<depend>pluginlibdepend>
<depend>rclcppdepend>
<depend>rclcpp_lifecycledepend>
<depend>sensor_msgsdepend>
<depend>std_msgsdepend>
<depend>std_srvsdepend>
<depend>tf2depend>
<depend>tf2_geometry_msgsdepend>
<depend>tf2_rosdepend>
<depend>tf2_sensor_msgsdepend>
<depend>visualization_msgsdepend>
<depend>anglesdepend>
<test_depend>ament_lint_commontest_depend>
<test_depend>ament_lint_autotest_depend>
<test_depend>nav2_map_servertest_depend>
<test_depend>ament_cmake_gtesttest_depend>
<test_depend>launchtest_depend>
<test_depend>launch_testingtest_depend>
<test_depend>nav2_lifecycle_managertest_depend>
<export>
<build_type>ament_cmakebuild_type>
export>
package>
注意:如果是自定义的msg或者srv
3
<package format="3">
ament_cmake
和rosidl_default_generators
<buildtool_depend>ament_cmakebuildtool_depend>
<buildtool_depend>rosidl_default_generatorsbuildtool_depend>
<exec_depend>rosidl_default_runtimeexec_depend>
<member_of_group>rosidl_interface_packagesmember_of_group>
<export>
<build_type>ament_cmakebuild_type>
export>