步骤

1 在windows上安装ROS2 FOXY。

参考:windows安装ros
安装时将对应版本名改为foxy即可。安装时最好保证已经安装了chocolatey和visual studio.

  • 快速安装chocolatey:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
  • 快速安装ros2和编译工具colcon(注意要用管理员模式启动):
set ChocolateyInstall=d:\
choco source add -n=ros-win -s="https://aka.ms/ros/public" --priority=1
choco install ros-foxy-desktop -y --execution-timeout=0  

2 编译

大部分教程中都使用ament编译。
本文则使用colcon。

colcon 的设计理念可以在这里查到。概括来说,colcon 的目标就是成为一个通用的编译工具,现在主要用来编译 ROS,ROS 2 以及 Gazebo,未来可能使用更广泛。所以尽管 colcon 最早的开发动力来自 ROS 2,但它的定位并不是 ROS 2 的附属品。 colcon 有非常详细的文档,可以在这里查阅。

在 ROS 2 Ardent 版本中编译工具是 ament_tools,从 ROS 2 Bouncy 版本开始,colcon 就成了默认的编译工具。

1 创建包

创建包:

mkdir ros2_ws\src
cd ros2_ws\src
ros2 pkg create <package_name>

创建cpp包:

ros2 pkg create --build-type ament_cmake <package_name>

创建Python包:

ros2 pkg create --build-type ament_python <package_name>

创建包的时候顺便创建节点名,可以添加 --node-name <node_name>
例如:

ros2 pkg create --node-name test_node test_package 

示例:
在这里插入图片描述

2 CMakeList

cmakelist撰写顺序

  1. Required CMake Version (cmake_minimum_required) //cmake版本

  2. Package Name (project()) //包的名字,也是项目名字,并且可以在之后需要使用项目名称时用${PROJECT_NAME}
    替代,好处是,改变项目名字时,只需要改变这里的名字,后续的都会改变。

  3. Find other CMake/Catkin packages needed for build (find_package()) //查找依赖包,并且给出找到包的导出路径、库文件等。

  4. Enable Python module support (colcon_python_setup())

  5. Message/Service/Action Generators(add_message_files(), add_service_files(), add_action_files())

  6. Invoke message/service/action generation (generate_messages())

  7. Specify package build info export (colcon_package())

  8. Libraries/Executables to build (add_library()/add_executable()/target_link_libraries())

  9. Tests to build (colcon_add_gtest())

  10. Install rules (install())

catkin_package:

catkin_package的相关信息是一个catkin提供的CMake宏,需要对build系统具体说明catkin的信息用来产生pkg-config和CMake文件。
catkin_package()必须写在add_library() 或add_executable()之前,有5个可选参数:

  • INCLUDE_DIRS – The exported include paths (i.e. cflags) for the package

  • LIBRARIES – The exported libraries from the project

  • CATKIN_DEPENDS – Other catkin projects that this project depends on

  • DEPENDS – Non-catkin CMake projects that this project depends on. For a better understanding, see this explanation.

  • CFG_EXTRAS – Additional configuration options

CmakeLists.txt自动寻找依赖包:

find_package(catkin REQUIRED COMPONENTS nodelet)

配置运行包到CmakeLists.txt:

add_executable(<PACKAGE_NAME> src/main.cpp src/file1.cpp src/file2.cpp)

配置依赖到CmakeLists.txt:

在package.xml文件中,添加编译和运行的ROS依赖库,例如msg包

<build_depend>xx_msgs</build_depend>
<run_depend>xx_msgs</run_depend>

如果不是ros固有的包,而是其他cpp文件。
将cpp文件添加至每个包的src目录下,h文件添加至每个包的include目录下。(无论是运行还是依赖)
add_library是默认建立共享库。
target_link_libraries指定要链接的库,一般放到add_executable()之后。

## include中如果有h头文件
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)
## 需要添加的库
add_library(base64
    include/<PACKAGE_NAME>/XX.h
    src/XX.cpp
)

add_dependencies(XX ${${PROJECT_NAME}_EXPORTED_TARGETS} ${colcon_EXPORTED_TARGETS})
target_link_libraries(<需要调用XX的PACKAGE_NAME> XX ${colcon_LIBRARIES})

3 编译运行

  1. –symlink-install :使用符号链接而不是复制文件,如
    以动态链接库为例,会在install目录中使用符号链接,指向build目录下生成的库文件(如 *.so).
    没有该选项,则两个目录都会有该库文件

  2. –packages-select :只编译指定包,如
    colcon build –packages-select autoware_map_msgs vector_map_msgs

  3. –packages-ignore : 忽略指定包,同上

  4. –continue-on-error :在编译出错之后继续编译其他模块

  5. –cmake-args ,–ament-cmake-args, –catkin-cmake-args :传递参数给对应的package

编译src下所有的包:

colcon build --symlink-install

编译指定的包:

colcon build --packages-select  <package1> <package2>

运行
.\setup.bat 或者 .\setup.ps1

 .\setup.bat
ros2 run <package_name> <file_name or execute_name>

参考:
1、ros2框架与colcon
2、colcon相关教程


版权声明:本文为u013013023原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u013013023/article/details/117320992