ROS2編程基礎課程–安裝

  • 2019 年 10 月 5 日
  • 筆記

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

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

Installation 安裝

ROS 2 Installation Options ROS 2安裝選項

Multiple distributions of ROS 2 are supported at a time. We recommend using the most recent release available when possible.

一次同時支援多個ROS 2發行版,但是建議儘可能使用最新版本(Dashing)。

Select your ROS distribution 選擇ROS 2發行版本

ROS 2 Bouncy Bolson

ROS 2 Crystal Clemmys

ROS 2 Dashing Diademata

Released July 2018

Released December 2018

Released May 2019

Supported until July 2019

Supported until December 2019

Supported until May 2021

For more detailed descriptions of each release see REP-2000 有關每個版本的更詳細說明,請參考REP-2000

Why you might want an older distribution

為什麼可能需要一個舊的發行版?

You may want to install an older distribution if you need ROS 2 to:

考慮到如下一些需求可能需要安裝較舊的ROS 2發行版:

  • Operate on an older platform

在較舊的平台上運行

  • Support an older package that hasn』t been optimized for the latest release

尚未支援針對最新版本進行優化的舊軟體包

  • Be supported for a longer period of time (the latest release isn』t necessarily supported the longest)

得到更長時間的支援(最新版本不一定支援時間最長)

General users with no special considerations should use the most recent release available.

沒有特殊注意事項的一般用戶應使用最新版本。

Contributing to ROS 2 core? 對ROS 2核心貢獻一份力量?

If you plan to contribute directly to ROS 2 core packages, you can install the latest development from source.

如果計劃直接為ROS 2核心軟體包做出貢獻,可以從源程式碼安裝最新的開發版。

Installing ROS 2 Dashing Diademata

安裝ROS 2

Binary packages 二進位包

We provide ROS 2 binary packages for the following platforms:

以下平台可直接使用ROS 2二進位包進行安裝:

  • Linux (Ubuntu Bionic(18.04))

Debian packages

"fat" archive

Building from source 從源碼編譯安裝

We support building ROS 2 from source on the following platforms:

支援在以下平台上從源程式碼編譯ROS 2:

Which install should you choose?

應該選擇哪種安裝方式?

Installing from binary packages or from source will both result in a fully-functional and usable ROS 2 install. Differences between the options depend on what you plan to do with ROS 2. Binary packages are for general use and provide an already-built install of ROS 2. This is great for people who want to dive in and start using ROS 2 as-is, right away.

從二進位包或源程式碼安裝完全功能和可用的ROS 2安裝。選項之間的差異取決於將如何使用ROS 2。二進位包是普遍用途,並提供已經編譯好的ROS 2安裝包。這對於想要進入並立即開始使用ROS 2的人來說非常棒的選擇。推薦使用二進位包進行安裝!

Linux users have two options for installing binary packages: Linux用戶有兩種安裝二進位包的選項:

  • Debian packages
  • 「fat」 archive

Installing from Debian packages is the recommended method. It』s more convenient because it installs its necessary dependencies automatically. It also updates alongside regular system updates. However, you need root access in order to install Debian packages. If you don』t have root access, the 「fat」 archive is the next best choice.

建議使用Debian軟體包進行安裝。因為它更方便,並會自動安裝必要的依賴項,還會與常規系統更新一起更新。但是,需要root訪問許可權才能安裝Debian軟體包。如果沒有root訪問許可權,那麼「fat」檔案包是另一個最佳選擇。

OS X and Windows users who choose to install from binary packages only have the 「fat」 archive option (Debian packages are exclusive to Ubuntu/Debian).

選擇從二進位包安裝的OS X和Windows用戶只有「fat」檔案包選項(Debian軟體包是Ubuntu/Debian獨有的)。

Building from source is meant for developers looking to alter or explicitly omit parts of ROS 2』s base. It is also recommended for platforms that don』t support binaries. Building from source also gives you the option to install the absolute latest version of ROS 2.

從源程式碼編譯適用於希望改變或明確省略部ROS 2基礎的開發人員。對於不支援二進位文件安裝的平台,也建議使用源碼編譯安裝。從源程式碼編譯還可以選擇安裝最新版本的ROS 2。

Installing ROS2 via Debian Packages

通過Debian軟體包安裝ROS 2

Debian packages for ROS 2 Dashing Diademata are available for Ubuntu Bionic.

用於ROS 2 Dashing Diademata的Debian軟體包可用於Ubuntu Bionic。

Setup Locale 設置區域設置

Make sure you have a locale which supports UTF-8. If you are in a minimal environment, such as a docker container, the locale may be something minimal like POSIX. We test with the following settings. It should be fine if you』re using a different UTF-8 supported locale.

確保語言環境支援UTF-8。如果處於最小的環境中,例如docker容器,則語言環境可能與POSIX一樣極小。使用以下設置進行測試。如果使用不同的支援UTF-8的語言環境,應該沒問題。

sudo locale-gen en_US en_US.UTF-8

sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8

export LANG=en_US.UTF-8

Setup Sources 設置源

You will need to add the ROS 2 apt repositories to your system. To do so, first authorize our gpg key with apt like this:

需要將ROS 2 apt庫添加到系統中。為此,首先使用以下方式授權gpg密鑰:

sudo apt update && sudo apt install curl gnupg2 lsb-release

curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add –

And then add the repository to your sources list:

然後將庫添加到源列表:

sudo sh -c 'echo "deb [arch=amd64,arm64] http://packages.ros.org/ros2/ubuntu `lsb_release -cs` main" > /etc/apt/sources.list.d/ros2-latest.list'

Install ROS 2 packages 安裝ROS 2包

Update your apt repository caches after setting up the repositories. 設置庫後更新apt庫快取。

sudo apt update

Desktop Install (Recommended): ROS, RViz, demos, tutorials. 桌面安裝(推薦):ROS、RViz、示例、教程。

sudo apt install ros-dashing-desktop

ROS-Base Install (Bare Bones): Communication libraries, message packages, command line tools. No GUI tools. ROS-Base安裝(精簡):通訊庫、消息包、命令行工具。沒有GUI工具。

sudo apt install ros-dashing-ros-base

See specific sections below for how to also install the ros1_bridge, TurtleBot packages, or alternative RMW packages.

有關如何安裝ros1_bridgeTurtleBot軟體包其他RMW軟體包資訊,請參考下面的特定部分。

Environment setup 環境設置

(optional) Install argcomplete (可選)安裝argcomplete

ROS 2 command line tools use argcomplete to autocompletion. So if you want autocompletion, installing argcomplete is necessary.

ROS 2命令行工具使用argcomplete完成自動補全功能(Tab補全命令的功能)。因此,如果想要自動完成,則需要安裝argcomplete。

sudo apt install python3-argcomplete

Sourcing the setup script 導入設置腳本

Set up your environment by sourcing the following file. 通過導入以下文件設置環境。

source /opt/ros/dashing/setup.bash

You may want to add this to your .bashrc. 想將此添加到.bashrc。

echo "source /opt/ros/dashing/setup.bash" >> ~/.bashrc

Install additional RMW implementations

安裝其他RMW實現

By default the RMW implementation FastRTPS is used. If using Ardent OpenSplice is also installed.

默認情況下,使用RMW實現FastRTPS。如果還使用了Ardent OpenSplice。

To install support for OpenSplice or RTI Connext on Bouncy:

在Bouncy上安裝對OpenSplice或RTI Connext的支援:

sudo apt update

sudo apt install ros-dashing-rmw-opensplice-cpp # for OpenSplice

sudo apt install ros-dashing-rmw-connext-cpp # for RTI Connext (requires license agreement)

By setting the environment variable RMW_IMPLEMENTATION=rmw_opensplice_cpp you can switch to use OpenSplice instead. For ROS 2 releases Bouncy and newer, RMW_IMPLEMENTATION=rmw_connext_cpp can also be selected to use RTI Connext.

通過設置環境變數RMW_IMPLEMENTATION=rmw_opensplice_cpp,可以切換為使用OpenSplice。對於ROS 2 Bouncy或更新版本,如Dashing,RMW_IMPLEMENTATION=rmw_connext_cpp也可以選擇使用RTI Connext。

If you want to install the Connext DDS-Security plugins please refer to this page.

如果要安裝Connext DDS-Security插件,請參考此網頁

University, purchase or evaluation options are also available for RTI Connext.

RTI Connext也提供大學,購買或試用選項。

Install additional packages using ROS 1 packages

使用ROS 1包安裝其他軟體包

The ros1_bridge as well as the TurtleBot demos are using ROS 1 packages. To be able to install them please start by adding the ROS 1 sources as documented here.

在ros1_bridge還有TurtleBot示例等使用ROS 1包。為了能夠安裝它們,請首先添加ROS 1源,如此處所述

If you』re using Docker for isolation you can start with the image ros:melodic or osrf/ros:melodic-desktop (or Kinetic if using Ardent). This will also avoid the need to setup the ROS sources as they will already be integrated. 如果使用Docker進行隔離,則可以從鏡像ros:melodic或者osrf/ros:melodic-desktop開始,(如果使用Ardent則使用Kinetic)。這也將避免設置ROS源的需要,因為它們已經被集成。

Now you can install the remaining packages: 現在可以安裝剩餘的包:

sudo apt update

sudo apt install ros-dashing-ros1-bridge

The turtlebot2 packages are not currently available in Dashing. 目前,Dashing中不提供turtlebot2軟體包。

However, Dashing supports the turtlebot3 package. 但是,Dashing支援turtlebot3軟體包。

Build your own packages 編譯自定義的包

If you would like to build your own packages, refer to the tutorial "Using Colcon to build packages".

如果您想編譯自定義的包,請參考教程「使用Colcon編譯包」

測試一下,ROS Dashing是否安裝配置正常吧,分別在不同終端輸入如下命令:

ros2 run examples_rclcpp_minimal_subscriber subscriber_lambda

ros2 run examples_rclcpp_minimal_publisher publisher_lambda

命令分別打開一個訂閱器和一個發布器,用於收發hello world消息。

使用rqt圖形化工具可以看到:

簡要看一下如下程式碼,和常見C++或ROS 1程式碼相比,有何相同和不同之處?

發布器:

#include <chrono>

#include <memory>

#include "rclcpp/rclcpp.hpp"

#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;

/* This example creates a subclass of Node and uses a fancy C++11 lambda

* function to shorten the callback syntax, at the expense of making the

* code somewhat more difficult to understand at first glance. */

class MinimalPublisher : public rclcpp::Node

{

public:

MinimalPublisher()

: Node("minimal_publisher"), count_(0)

{

publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);

auto timer_callback =

[this]() -> void {

auto message = std_msgs::msg::String();

message.data = "Hello, world! " + std::to_string(this->count_++);

RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());

this->publisher_->publish(message);

};

timer_ = this->create_wall_timer(500ms, timer_callback);

}

private:

rclcpp::TimerBase::SharedPtr timer_;

rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;

size_t count_;

};

int main(int argc, char * argv[])

{

rclcpp::init(argc, argv);

rclcpp::spin(std::make_shared<MinimalPublisher>());

rclcpp::shutdown();

return 0;

}

接收器:

#include <iostream>

#include <memory>

#include "rclcpp/rclcpp.hpp"

#include "std_msgs/msg/string.hpp"

class MinimalSubscriber : public rclcpp::Node

{

public:

MinimalSubscriber()

: Node("minimal_subscriber")

{

subscription_ = this->create_subscription<std_msgs::msg::String>(

"topic",

10,

[this](std_msgs::msg::String::UniquePtr msg) {

RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());

});

}

private:

rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;

};

int main(int argc, char * argv[])

{

rclcpp::init(argc, argv);

rclcpp::spin(std::make_shared<MinimalSubscriber>());

rclcpp::shutdown();

return 0;

}

試一試ROS 2的小烏龜案例吧:

Turtlesim(https://blog.csdn.net/ZhangRelay/article/details/99932959)