详解使用ament_cmake构建ROS2功能包

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_2dCmakeList.txt来进行介绍

  1. 指定cmake的最低版本(3.5)和功能包的名字
cmake_minimum_required(VERSION 3.5)
project(nav2_costmap_2d)
  1. 查找ament_cmake
find_package(ament_cmake REQUIRED)
  1. 查找构建此功能包所需的依赖-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)
  1. 查找构建此功能包所需的系统依赖-非ROS2功能包
find_package(Eigen3 REQUIRED)
include_directories(
  include
  ${EIGEN3_INCLUDE_DIRS}
)
  1. 使用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
)
  1. 设置变量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
)
  1. 通过ament_target_dependencies添加ROS2功能包依赖,它将依赖的库、头文件以及自身依赖添加到目标中
ament_target_dependencies(nav2_costmap_2d_core
  ${dependencies}
)
  1. 通过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
)
  1. 安装库
install(TARGETS
  nav2_costmap_2d_core
  layers
  filters
  nav2_costmap_2d_client
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)
  1. 安装可执行文件,注意这里的可执行文件路径为: lib/${PROJECT_NAME}而不是bin
install(TARGETS
  nav2_costmap_2d
  nav2_costmap_2d_markers
  nav2_costmap_2d_cloud
  RUNTIME DESTINATION lib/${PROJECT_NAME}
)
  1. 导出头文件
install(DIRECTORY include/
  DESTINATION include/
)
  1. 导出launch参数文件
install(
  DIRECTORY launch params
  DESTINATION share/${PROJECT_NAME}
)
  1. 通过ament_export_include_directories导出该功能包的头文件,以便其他功能包依赖此功能包时,能够找到对应的头文件
ament_export_include_directories(include)
  1. 通过ament_export_libraries导出该功能包构建的库,以便其他功能包能够顺利链接到
ament_export_libraries(layers filters nav2_costmap_2d_core nav2_costmap_2d_client)
  1. 通过ament_export_dependencies导出此功能包所有的ROS相关的依赖,依赖其他功能包依赖此功能包时,不必再通过find_package重复添加这些依赖
ament_export_dependencies(${dependencies})
  1. 最后调用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时,同时添加自己的环境变量,可

  1. 在功能包中添加一个env-hooks文件夹,在文件夹中添加一个project_name.sh.in文件
ament_prepend_unique_value UESER_ENV_PATH "$AMENT_CURRENT_PREFIX/XXXX"
  1. 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_cmakerosidl_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>

参考

  • 9
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ament_cmake_autoConfig.cmakeament_cmake_auto-config.cmake都是用于自动配置ament_cmakeCMake模块文件。这些文件含了一些CMake变量和函数,用于设置ament_cmake构建和安装过程中所需的依赖项、编译选项和安装目录等信息。这些模块文件通常由ament_cmake提供,可以在CMakeLists.txt文件中使用include()命令来加载它们,以便使用其中定义的功能变量。具体来说,ament_cmake_autoConfig.cmake用于配置ament_cmake构建,而ament_cmake_auto-config.cmake用于配置ament_cmake的安装。这两个文件都是通过在CMake的扩展点系统中使用的,以实现代码的模块化和灵活性。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【ROS2原理3】:构建系统“ament_cmake”和构建工具“ament_tools”](https://blog.csdn.net/gongdiwudu/article/details/126192244)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [ROS2学习(十六).ROS概念 - 构建系统](https://blog.csdn.net/amuro_ray027/article/details/120201983)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火柴的初心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值