ROS2工程移植指南

该博客是ROS2工程的移植指南,涵盖了CMakeList、Package.xml和Launch文件的编写,强调了从ROS1到ROS2代码移植的细节,如Node、publisher/subscriber、service/client、timer和parameter的实现变化。特别指出,ROS2中service/client的异步请求处理可能导致死锁,需要采用多线程或多callback group的方式避免。同时,msg/srv的namespace、LOG、时间戳等方面也有所改变。
摘要由CSDN通过智能技术生成

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')
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值