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
文件中的最后一个调用。
ROS2功能包
一个功能包的构建信息包含在
CMakeLists.txt
:描述了如何构建此功能包package.xml
:包含该功能包的依赖信息,它可以帮助编译工具colcon确定多个功能包编译的顺序
CMakeList.txt
接下来,以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)
注意事项
- 相比ROS1, ROS2没有了
devel
,只有install
- ROS2中没有了
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
时,同时添加自己的环境变量,可
- 在功能包中添加一个env-hooks文件夹,在文件夹中添加一个
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
对于package.xml
主要需要添加如下信息:
- 编译的工具依赖:
<buildtool_depend>ament_cmake</buildtool_depend>
- 依赖的ROS2功能包
<depend>geometry_msgs</depend>
<depend>laser_geometry</depend>
<depend>map_msgs</depend>
<depend>message_filters</depend>
<depend>nav2_msgs</depend>
<depend>nav2_util</depend>
<depend>nav2_voxel_grid</depend>
<depend>nav_msgs</depend>
<depend>pluginlib</depend>
<depend>rclcpp</depend>
<depend>rclcpp_lifecycle</depend>
<depend>sensor_msgs</depend>
<depend>std_msgs</depend>
<depend>std_srvs</depend>
<depend>tf2</depend>
<depend>tf2_geometry_msgs</depend>
<depend>tf2_ros</depend>
<depend>tf2_sensor_msgs</depend>
<depend>visualization_msgs</depend>
<depend>angles</depend>
- 导出依赖:
<export>
<build_type>ament_cmake</build_type>
</export>
完整的package.xml
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format ="3">
<name>nav2_costmap_2d</name>
<version>1.2.0</version>
<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="stevenmacenski@gmail.com">Steve Macenski</maintainer>
<license>BSD-3-Clause</license>
<license>Apache-2.0</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<build_depend>nav2_common</build_depend>
<depend>geometry_msgs</depend>
<depend>laser_geometry</depend>
<depend>map_msgs</depend>
<depend>message_filters</depend>
<depend>nav2_msgs</depend>
<depend>nav2_util</depend>
<depend>nav2_voxel_grid</depend>
<depend>nav_msgs</depend>
<depend>pluginlib</depend>
<depend>rclcpp</depend>
<depend>rclcpp_lifecycle</depend>
<depend>sensor_msgs</depend>
<depend>std_msgs</depend>
<depend>std_srvs</depend>
<depend>tf2</depend>
<depend>tf2_geometry_msgs</depend>
<depend>tf2_ros</depend>
<depend>tf2_sensor_msgs</depend>
<depend>visualization_msgs</depend>
<depend>angles</depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>nav2_map_server</test_depend>
<test_depend>ament_cmake_gtest</test_depend>
<test_depend>launch</test_depend>
<test_depend>launch_testing</test_depend>
<test_depend>nav2_lifecycle_manager</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
注意:如果是自定义的msg或者srv
- format的版本需要为
3
<package format="3">
- 编译工具依赖
ament_cmake
和rosidl_default_generators
<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>rosidl_default_generators</buildtool_depend>
- 另外需要添加
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
- 导出
<export>
<build_type>ament_cmake</build_type>
</export>