详解使用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_cmakebuildtool_depend>
  • 依赖的ROS2功能包
  <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

  • format的版本需要为3
<package format="3">
  • 编译工具依赖ament_cmakerosidl_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>

参考

  • 详细分析一个ROS2 CMakeLists.txt文件
  • ament_cmake user documentation

你可能感兴趣的:(ROS,人工智能,ROS,机器人,SLAM)