ROS2編程基礎課程–colcon

  • 2019 年 10 月 5 日
  • 筆記

版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/ZhangRelay/article/details/100773358

Using colcon to build packages

使用colcon編譯包

This is a brief tutorial of how to create and build a ROS 2 workspace with colcon. It is a practical tutorial and not designed to replace the core documentation.

這是一個使用colcon如何創建和編譯ROS 2工作區的簡要教程。這是一個實用的教程,並非旨在取代核心文檔。

ROS 2 releases before Bouncy used ament_tools described in the ament tutorial.

如果使用Bouncy或之前發行版本的ROS 2參考ament教程中ament_tools使用說明。

在Crystal和Dashing中均使用colcon進行編譯。

Background 背景

colcon is an iteration on the ROS build tools catkin_make, catkin_make_isolated, catkin_tools and ament_tools. For more information on the design of colcon see this document.

colcon是對ROS編譯(編譯)工具catkin_make,catkin_make_isolated,catkin_tools和ament_tools迭代。有關colcon設計的更多信息,請參考此文檔

The source code can be found in the colcon GitHub organization.

源代碼可以在colcon GitHub組織中找到

Prerequisites 預備基礎

Install colcon 安裝colcon 

Linux

sudo apt install python3-colcon-common-extensions

python3-colcon-common-extensions is already the newest version (0.2.0-2).

OS X

python3 -m pip install colcon-common-extensions

Windows

pip install -U colcon-common-extensions

Install ROS 2 安裝ROS2

To build the samples, you will need to install ROS 2.

編譯示例前,需要先安裝ROS 2。

Follow the installation instructions.

請參考安裝說明

Attention 注意

If installing from Debian packages, this tutorial requires the desktop installation.

如果從Debian軟件包安裝,本教程需要桌面安裝

Basics 基礎知識

A ROS workspace is a directory with a particular structure. Commonly there is a src subdirectory. Inside that subdirectory is where the source code of ROS packages will be located. Typically the directory starts otherwise empty.

ROS工作空間是具有特定結構的目錄。通常有一個src子目錄。這個src子目錄中是ROS包的源代碼所在的位置。通常,通常都從這個目錄開始,否則為空。

colcon does out of source builds. By default it will create the following directories as peers of the src directory:

colcon完成源代碼編譯。默認情況下,它將創建以下目錄作為src目錄的對等項:

The build directory will be where intermediate files are stored. For each package a subfolder will be created in which e.g. CMake is being invoked.

其中build目錄將是存儲中間文件的位置。對於每個包,將創建一個子文件夾,例如調用CMake。

The install directory is where each package will be installed to. By default each package will be installed into a separate subdirectory.

其中install目錄是每個軟件包將安裝到的目錄。默認情況下,每個包都將安裝到單獨的子目錄中。

The log directory contains various logging information about each colcon invocation.

其中log目錄包含有關每個colcon調用的各種日誌記錄信息。

Note 注意

Compared to catkin there is no devel directory. 與catkin相比,沒有devel目錄。

Create a workspace 創建工作區

First, create a directory (ros2_example_ws) to contain our workspace:

首先,創建一個目錄(ros2_example_ws)來包含自定義的工作區:

Linux/OS X

mkdir -p ~/ros2_example_ws/src

cd ~/ros2_example_ws

Windows

md devros2_example_wssrc

cd devros2_example_ws

At this point the workspace contains a single empty directory src:

此時工作空間包含一個空目錄文件夾src:

.

└── src

1 directory, 0 files

Add some sources 添加一些來源

Let』s clone the examples repository into the src directory of the workspace:

示例庫克隆到工作區的src目錄中:

git clone https://github.com/ros2/examples src/examples

Attention 注意

It is recommended to checkout a branch that is compatible with the version of ROS that was installed (e.g. crystal、dashing).

建議檢查與已安裝的ROS版本兼容的分支(例如crystal、dashing)。

cd ~/ros2_example_ws/src/examples/

git checkout $ROS_DISTRO

cd ~/ros2_example_ws

Now the workspace should have the source code to the ROS 2 examples:

現在工作區應該有ROS 2示例的源代碼:

.

└── src

└── examples

├── CONTRIBUTING.md

├── LICENSE

├── rclcpp

├── rclpy

└── README.md

4 directories, 3 files

Source an underlay 導入底層

It is important that we have sourced the environment for an existing ROS 2 installation that will provide our workspace with the necessary build dependencies for the example packages. This is achieved by sourcing the setup script provided by a binary installation or a source installation, ie. another colcon workspace (see Installation). We call this environment an underlay.

重要的是,為現有的ROS 2安裝提供環境,這將為編譯工作區示例包提供必要的依賴性。通過獲取二進制安裝或源安裝提供的安裝腳本來實現的,即:另一個colcon工作區(請參考安裝)。通常將此環境稱為底層

Our workspace, ros2_examples_ws, will be an overlay on top of the existing ROS 2 installation. In general, it is recommended to use an overlay when you plan to iterate on a small number of packages, rather than putting all of your packages into the same workspace.

工作空間ros2_examples_ws將疊加在現有的ROS 2安裝之上。通常,建議在計劃迭代少量軟件包時使用覆蓋,而不是將所有軟件包放在同一個工作區中。

Build the workspace 編譯工作區

Attention 注意

To build packages on Windows you need to be in a Visual Studio environment, see Building the ROS 2 Code for more details.

要在Windows上編譯軟件包,需要在Visual Studio環境中,請參考編譯ROS 2代碼以獲取更多詳細信息。

In the root of the workspace, run colcon build. Since build types such as ament_cmake do not support the concept of the devel space and require the package to be installed, colcon supports the option –symlink-install. This allows the installed files to be changed by changing the files in the source space (e.g. Python files or other not compiled resourced) for faster iteration.

在工作區的根目錄中,運行colcon build。由於編譯類型ament_cmake(例如不支持devel的概念並且需要安裝包),因此colcon支持選項–symlink-install。這允許通過更改source空間中的文件(例如Python文件或其他未編譯的資源)來更改已安裝的文件,以便更快地進行迭代。

colcon build –symlink-install

After the build is finished, we should see the build, install, and log directories:

編譯完成後,可以看到build,install和log文件夾目錄:

.

├── build

├── install

├── log

└── src

4 directories, 0 files

Run tests 運行測試

To run tests for the packages we just built, run the following:

為剛剛編譯的包運行測試,請運行以下命令:

colcon test

Source the environment 導入環境

When colcon has completed building successfully, the output will be in the install directory. Before you can use any of the installed executables or libraries, you will need to add them to your path and library paths. colcon will have generated bash/bat files in the install directory to help setup the environment. These files will add all of the required elements to your path and library paths as well as provide any bash or shell commands exported by packages.

當colcon成功完成編譯後,輸出將在install目錄中。在使用任何已安裝的可執行文件或庫之前,需要將它們添加到路徑和庫路徑中。colcon將在install目錄中生成bash / bat文件以幫助設置環境。這些文件將向路徑和庫路徑添加所有必需元素,並提供由包導出的任何bash或shell命令。

Linux/OS X

. install/setup.bash

Or 或者

source install/setup.bash

Windows

call installsetup.bat

Try a demo 試試示例

With the environment sourced we can run executables built by colcon. Let』s run a subscriber node from the examples:

在環境導入後,可以運行colcon編譯的可執行文件。從示例中運行訂閱器節點如下:

ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function

In another terminal, let』s run a publisher node (don』t forget to source the setup script):

在另一個終端中,運行一個發佈器節點(不要忘記導入安裝腳本):

ros2 run examples_rclcpp_minimal_publisher publisher_member_function

You should see messages from the publisher and subscriber with numbers incrementing.

應該看到來自發佈器和訂閱器的消息,並且數字會遞增。

Create your own package 創建自定義的包

colcon uses the package.xml specification defined in REP 149 (format 2 is also supported).

colcon使用REP 149中的規範定義package.xml(也支持格式2)。

colcon supports multiple build types. The recommended build types are ament_cmake and ament_python. Also supported are pure cmake packages.

colcon支持多種編譯類型。推薦的編譯類型是ament_cmake和ament_python。也支持純cmake包。

An example of an ament_python build is the ament_index_python package , where the setup.py is the primary entry point for building.

ament_python編譯的一個示例是ament_index_python包,其中setup.py是編譯的主要入口點。

A package such as demo_nodes_cpp uses the ament_cmake build type, and uses CMake as the build tool.

諸如demo_nodes_cpp包使用ament_cmake編譯類型,並使用CMake作為編譯工具。

For convenience, you can use the tool ros2 pkg create to create a new package based on a template.

為方便起見,可以使用該工具ros2 pkg create基於模板創建新包。

Note 注意

For catkin users, this is the equivalent of catkin_create_package.

對於catkin用戶來說,這相當於catkin_create_package。

注意區別,ROS1和ROS2,創建自定義包的區別。

Tips 注意事項

If you do not want to build a specific package place an empty file named COLCON_IGNORE in the directory and it will not be indexed.

如果不想構建特定的包,請在目錄中指定一個COLCON_IGNORE空文件,將不會編入索引。

If you want to avoid configuring and building tests in CMake packages you can pass: –cmake-args -DBUILD_TESTING=0.

如果想避免在CMake軟件包中配置和構建測試,可以通過:–cmake-args -DBUILD_TESTING=0。

If you want to run a single particular test from a package:

如果要從包中運行單個特定測試:

colcon test –packages-select YOUR_PKG_NAME –ctest-args -R YOUR_TEST_IN_PKG

Using Ament 使用Ament

Warning 警告

As of ROS 2 Bouncy the recommended build tool is “colcon“ described in the colcon tutorial. The current default branch as well as releases after Bouncy do not include ament_tools anymore.

從ROS 2 Bouncy開始,推薦的編譯工具是 colcon教程中提及的“colcon“。當前的默認發行版以及Bouncy之後的版本不再將ament_tools包括在內。

由於介紹的教程針對Crystal和Dashing發行版本的ROS 2,所以設計ament的章節就省略了,如果需要了解更多細節,請查閱官方文檔。