ROS2工程移植指南
用于记录现有工程向ROS2移植的常见问题,以及ROS2开发的一些注意事项
ROS2的官方教程参见http://docs.ros.org/en/rolling/Tutorials/
1 工程构建
1.1 CMakeList的编写
ROS2采用ament cmake系统,最主要的区别是原先的catkin Cmake宏被取代
find_package(catkin REQUIRED COMPONENTS ...
catkin_package(
INCLUDE_DIRS
LIBRARIES
CATKIN_DEPENDS)
############################################ 改变为
find_package(ament_cmake REQUIRED)
find_package(... REQUIRED)
ament_export_include_directories
ament_export_targets
ament_export_dependencies
ament_target_dependencies(${PROJECT_NAME} ${DEPS})
ament_package()
若想构建一个兼容ROS1/2的CMakeList.txt,在不同ROS版本有区别的部分通过环境变量触发条件编译
if($ENV{ROS_VERSION} EQUAL 2)
########################
## BULD VERSION: ROS2 ##
########################
else()
########################
## BULD VERSION: ROS1 ##
########################
endif()
1.2 Package.xml
<buildtool_depend>catkin</buildtool_depend>
<build_type>catkin</build_type>
##################
<buildtool_depend>ament_cmake</buildtool_depend>
<build_type>ament_cmake</build_type>
若想构建一个兼容ROS1/2的Package.xml,在不同ROS版本有区别的部分通过环境变量设置条件
<export>
<build_type condition="$ROS_VERSION == 1">catkin</build_type>
<build_type condition="$ROS_VERSION == 2">ament_cmake</build_type>
</export>
1.3 Launch 文件
ROS2支持xml和python两种类型的launch file。以python版本为例
from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
import os
def generate_launch_description():
para_dir = os.path.join(get_package_share_directory('localizer'), 'config', 'localizer.yaml')