天天看點

ROS2程式設計基礎課程--安裝

Installation 安裝

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

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

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

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. 

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

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 

We provide ROS 2 binary packages for the following platforms: 

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

Linux (Ubuntu Bionic(18.04))

Debian packages

"fat" archive

OS X

Windows

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

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

Linux

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使用者有兩種安裝二進制包的選項:

“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。

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

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'

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_bridge、TurtleBot軟體包或其他RMW軟體包的資訊,請參考下面的特定部分。

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

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

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 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也提供大學,購買或試用選項。

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 install ros-dashing-ros1-bridge

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

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

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>

class MinimalSubscriber : public rclcpp::Node

  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());

      });

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

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

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

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