22
1 入门 ROS 机器人应用程序开发 机器人学是正在改变世界的技术之一。机器人能够在很多方面替代人,所以人们害怕 它们抢走我们的工作。但有一点是肯定的:机器人将是未来具有影响力的技术之一。随着 新技术不断发展,这一领域的机遇也随之增加。这意味着机器人和自动化将来可以带来大 量的就业机会。 机器人软件开发是机器人技术的主要领域之一,可以提供大量的工作机会。众所周知, 软件给机器人或任何机器带来了“生命”。我们可以通过软件扩展机器人的功能。如果这个 机器人存在,就可以使用软件实现其控制、感知和智能等功能。 机器人软件涉及计算机视觉、人工智能和控制理论等相关技术。简而言之,为机器人 开发软件并不是一项简单的任务,这可能需要许多领域的专业知识。 如果你正在 iOS Android 中进行移动应用程序开发,则可以使用软件开发工具包 Software Development KitSDK )来构建应用程序,但是机器人呢?有没有通用的软件框 架可用呢?答案是肯定的,有!机器人操作系统( Robot Operating SystemROS )就是最流 行的机器人软件框架之一。 在本章中,我们将简要学习 ROS 的基本概念以及安装方法。本书针对的是基于 ROS 的项目,因此本章将是这些项目的上手指南。 本章将介绍以下内容: ROS 入门 为什么使用 ROS ROS 的基本概念 支持 ROS 的机器人、传感器和执行器 安装 ROS ROS 在工业和研究领域的应用 因此,让我们开启 ROS 之旅吧!

第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

  • Upload
    others

  • View
    18

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

第 1 章

入门 ROS 机器人应用程序开发

机器人学是正在改变世界的技术之一。机器人能够在很多方面替代人,所以人们害怕

它们抢走我们的工作。但有一点是肯定的:机器人将是未来具有影响力的技术之一。随着

新技术不断发展,这一领域的机遇也随之增加。这意味着机器人和自动化将来可以带来大

量的就业机会。

机器人软件开发是机器人技术的主要领域之一,可以提供大量的工作机会。众所周知,

软件给机器人或任何机器带来了“生命”。我们可以通过软件扩展机器人的功能。如果这个

机器人存在,就可以使用软件实现其控制、感知和智能等功能。

机器人软件涉及计算机视觉、人工智能和控制理论等相关技术。简而言之,为机器人

开发软件并不是一项简单的任务,这可能需要许多领域的专业知识。

如果你正在 iOS 或 Android 中进行移动应用程序开发,则可以使用软件开发工具包

(Software Development Kit,SDK)来构建应用程序,但是机器人呢?有没有通用的软件框

架可用呢?答案是肯定的,有!机器人操作系统(Robot Operating System,ROS)就是最流

行的机器人软件框架之一。

在本章中,我们将简要学习 ROS 的基本概念以及安装方法。本书针对的是基于 ROS的项目,因此本章将是这些项目的上手指南。

本章将介绍以下内容:

● ROS 入门

● 为什么使用 ROS ● ROS 的基本概念

● 支持 ROS 的机器人、传感器和执行器

● 安装 ROS ● ROS 在工业和研究领域的应用

因此,让我们开启 ROS 之旅吧!

Page 2: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

2  第 1 章

1.1 ROS 入门

ROS 是用于机器人编程的开放源码、灵活的软件框架。ROS 提供了硬件抽象层,开发

人员使用 ROS 构建机器人应用程序,同时无须为底层硬件而操心。ROS 还提供不同的可视

化和调试机器人数据的软件工具。ROS 框架的核心是消息传递中间件,即使在不同的机器

上运行,进程之间也可以相互通信和交换数据。ROS 消息传递可以是同步的,也可以是异

步的。

在 ROS 中通过功能包的形式来组织软件,并提供良好的模块化和可重用性。通过使用

ROS 消息传递中间件以及硬件抽象层,开发人员可以创建大量的机器人功能,如(在移动

机器人中)建图和导航。ROS 中几乎所有的功能都将与机器人无关,以便各种机器人都可

以使用它。新的机器人可以直接使用此功能包,而无须修改包中的任何代码。

ROS 在大学中有广泛的合作关系,许多开发人员也为此做出了贡献。可以说,ROS 是

由全球开发人员及组织支持的社区驱动项目。活跃的开发人员生态系统是 ROS 与其他机器

人框架的主要区别。

简而言之,ROS 是管道(或通信)、工具、功能和生态系统的组合,这些功能如图 1-1所示。

工具 功能 生态系统管道

图 1-1 ROS 方程式

ROS 项目于 2007 年在斯坦福大学启动,名称为 Switchyard。接着在 2008 年,这个

项目在一个名叫 Willow Garage 的机器人研究机构下进一步发展。ROS 的重大转变就是在

Willow Garage 中发生的。在 2013 年,Willow Garage 的研究人员组建了开源机器人基金会

(Open Source Robotics Foundation,OSRF)。现在 ROS 由 OSRF 积极维护。

网站的链接如下:

● Willow Garage: http: //www.willowgarage.com/ ● OSRF: http: //www.osrfoundation.org/

1.1.1 ROS 发行版

ROS 发行版与 Linux 发行版非常相似,也就是 ROS 功能包的版本集。每个发行版都保

持一套稳定的核心包,直到维护周期的终结(End Of Life,EOL)。

ROS 发行版与 Ubuntu 完全兼容,大多数 ROS 发行版根据各自的 Ubuntu 版本进行

规划。

Page 3: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

入门 ROS 机器人应用程序开发  3

图 1-2 是 ROS 网 站(http: //wiki.ros.org/Distributions) 上 推 荐 使 用 的 一 些 ROS 最 新

版本。

图 1-2 最新的 ROS 发行版

最新的 ROS 发行版是 Kinetic Kame。这个配置的支持一直到 2021 年 5 月。最新的

ROS 发行版的一个问题是大部分功能包都不可用,这是因为需要时间将它们从之前的版本

中移植过来。如果你正在寻找稳定的发行版,可以使用 ROS Indigo Igloo,因为该版本于

2015 年开始发布,大部分功能包均可在此发行版中使用。ROS Jade Turtle 发行版将于 2017年 5 月停止支持,所以建议不要使用它。

1.1.2 支持 ROS 的操作系统

支持 ROS 的操作系统主要是 Ubuntu。根据 Ubuntu 的发行版来规划 ROS 的发行

版。 除 Ubuntu 之 外,Ubuntu ARM、Debian、Gentoo、Mac OS X、Arch Linux、Android、Windows 和 Open Embedded 都部分支持 ROS,如图 1-3 所示。

图 1-3 支持 ROS 的操作系统

下表列出了新的 ROS 发行版和支持的操作系统的特定版本:

Page 4: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

4  第 1 章

ROS 发行版 支持的操作系统

Kinetic Kame (LTS)Ubuntu 16.04 (LTS) 和 Ubuntu 15.10、Debian 8、OS X (Homebrew)、Gentoo 和 Ubuntu

ARM

Jade TurtleUbuntu 15.04、14.10 和 14.04、Ubuntu ARM、OS X (Homebrew)、Gentoo、Arch

Linux、Android NDK 和 Debian 8

Indigo Igloo (LTS)Ubuntu 14.04 (LTS) 和 Ubuntu13.10、Ubuntu ARM、OS X (Homebrew)、Gentoo、Arch

Linux、Android NDK 和 Debian 7

ROS Indigo 和 Kinetic 是 LTS 版本对应 Ubuntu 的长期支持(Long-Term Support,LTS)发行版。使用 LTS 发行版的优点是获得最长的使用寿命和最多的支持。

1.1.3 支持 ROS 的机器人和传感器

ROS 框架是成功的机器人框架之一,全球各地的大学都对此做出了贡献。由于活跃的

生态系统和开源性质,ROS 可以在大多数机器人中使用,并且与主流的机器人硬件和软件

兼容。这里有一些完全运行在 ROS 上的知名机器人,如图 1-4 所示。

(a)

(b)

(c)

(d)

(e)

图 1-4 ROS 支持的主流机器人

图中列出的机器人的名称是 Pepper(a)、REEM-C(b)、TurtleBot(c)、Robonaut(d)、Universal Robotic arms(e)。

此链接列出了 ROS 支持的机器人:http: //wiki.ros.org/Robots(现在已经更新为 http: //

Page 5: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

入门 ROS 机器人应用程序开发  5

robots.ros.org/)。

下面是获取 ROS 功能包的链接:

● Pepper:http: //wiki.ros.org/Robots/Pepper ● REEM-C:http: //wiki.ros.org/Robots/Robonaut2 ● TurtleBot 2:http: //wiki.ros.org/Robots/TurtleBot ● Robonaut:http: //wiki.ros.org/Robots/Robonaut2 ● Universal Robotic arms:http: //wiki.ros.org/universal_robot

一些支持 ROS 的主流传感器如图 1-5 所示。

(a)

(d)

(b)

(e)

(c)

(f)

图 1-5 支持 ROS 的热门机器人传感器

图中列出的传感器的名称是 Velodyne(a)、ZED 摄像头 (b)、Teraranger(c)、Xsens(d)、Hokuyo 激光雷达 (e) 和 Intel RealSense(f)。

支持 ROS 的传感器列表可在此链接 http: //wiki.ros.org/Sensors 获得。

这些传感器的 ROS Wiki 页面的链接如下:

● Velodyne(a):http: //wiki.ros.org/Velodyne ● ZED 摄像头 (b):http: //wiki.ros.org/zed-ros-wrapper ● Teraranger(c):http: //wiki.ros.org/terarangerone ● Xsens(d):http: //wiki.ros.org/ terarangerone ● Hokuyo 激光雷达 (e):http: //wiki.ros.org/hokuyo_node ● Intel Realsense(f):http: //wiki.ros.org/realsense_camera

1.1.4 为什么选择 ROS构建 ROS 框架的主要目的就是使之成为机器人的通用软件框架。因为在 ROS 之前开

展的机器人研究,大多数软件都是自己的机器人所独有的。这些软件可能是开源的,但是

很难重用。

Page 6: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

6  第 1 章

与现有的机器人框架相比,ROS 在以下方面表现优异:

● 协作开发:正如我们所提到的,ROS 是开源的,可以自由地用于工业和研究领域。

开发人员可以通过添加功能包来扩展 ROS 的功能。几乎所有的 ROS 包都在硬件抽

象层上工作,可以轻松地为其他机器人重用。因此,如果一所大学在移动导航、机

械臂或其他方面做得很好,可以向 ROS 社区做出贡献,其他开发人员可以重用其功

能包并构建新的应用程序。

● 语言支持:ROS 通信框架可以轻松地以任何现代编程语言实现。它已经支持流行的

语言,如 C ++、Python 和 Lisp,它还有 Java 和 Lua 的实验库。

● 库集成:ROS 具有许多第三方机器人库的接口,如开源计算机视觉(Open Source Computer Vision,Open-CV)、点云库(Point Cloud Library,PCL)、Open-NI、Open-Rave 和 Orocos。开发人员可以没有顾虑地使用这些库。

● 集成化仿真器:ROS 还与开源仿真器(如 Gazebo) 有着紧密的联系,并且具有堪比

专业仿真器的良好界面,如 Webots 和 V-REP。 ● 代码测试:ROS 提供了一个名为 rostest 的内置测试框架来发现代码错误,提高代码

质量。

● 可扩展性:ROS 框架的宗旨就是可扩展性。我们可以通过在云端或异构集群上让使

用 ROS 的机器人执行大量的计算任务。

● 可定制性:正如前面所述,ROS 是完全开放源代码且免费的,可以根据机器人的要

求自定义框架。如果只想使用 ROS 消息传递平台,可以删除所有其他组件,甚至可

以为特定机器人定制 ROS 以获得更好的性能。

● 社区:ROS 是社区驱动的项目,主要由 OSRF 领导。庞大的社区支持对于 ROS 来说

是一个很好的加强,人们可以轻松地开始机器人应用开发。

下面是与 ROS 集成的库和仿真器的网址:

● Open-CV:http: //wiki.ros.org/vision_opencv ● PCL:http: //wiki.ros.org/pcl_ros ● Open-NI:http: //wiki.ros.org/openni_launch ● Open-Rave:http: //openrave.org/ ● Orocos:http: //www.orocos.org/ ● Webots:https: //www.cyberbotics.com/overview ● V-REP:http: //www.coppeliarobotics.com/

下面来了解一下 ROS 的一些基本概念,有帮助于入门 ROS 项目。

1.2 ROS 基础

了解 ROS 的基本工作原理及术语可以帮助你了解现有的 ROS 应用程序并构建自己的

Page 7: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

入门 ROS 机器人应用程序开发  7

ROS 应用程序。本部分将介绍在后续章节中使用到的重要概念。本章忽略的一些内容,将

会在相应的后续章节中介绍。

ROS 有三个不同的概念,现在来了解一下。

1.2.1 文件系统级

文件系统级(见图 1-6)解释了 ROS 文件是如何在硬盘上组织的。

ROS 文件系统级

综合功能包(元功能包集)

功能包

功能包清单 杂项文件

消息 服务 代码

图 1-6 ROS 文件系统级

从图 1-6 中可以看出,ROS 的文件系统主要可以分为综合功能包、功能包、功能包清

单、消息、服务、代码和杂项文件。下面是每个组件的简短描述:

● 综合功能包:综合功能包将特定应用程序包的列表组合在一起。例如,在 ROS 中,

有一个称为导航(navigation)的综合功能包用于移动机器人导航。它可以保存相关

功能包的信息,并有助于在自己的安装过程中安装这些功能包。

● 功能包:ROS 软件主要以 ROS 功能包的形式组织。可以说 ROS 功能包是 ROS 的

元构建单元。一个功能包的单个模块中可能包括 ROS 节点 / 进程、数据集和配置

文件。

● 功能包清单:每个包都有一个名为 package.xml 的清单文件。该文件包括包的名称、

版本、作者、许可证和依赖项等信息。综合功能包的 package.xml 文件由相关功能

包的名称组成。

● 消息(msg):ROS 通过发送 ROS 消息进行通信。可以在具有 .msg 扩展名的文件

中定义消息数据的类型。这些文件称为消息文件。按照约定,消息文件保存在 our_package/msg/message_files.msg 下。

● 服务(srv):计算图级概念之一是服务。与 ROS 消息类似,约定服务定义放在 our_package/srv/ service_files.srv 下。

Page 8: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

8  第 1 章

1.2.2 计算图级

ROS 计算图(见图 1-7)是 ROS 进程的对等网络,并能处理数据。ROS 计算图概念包

括节点、主题、消息、主节点(节点管理器)、参数服务器、服务和消息记录包。

ROS 计算图级

节点 1

主题

服务 服务消息 消息

主题

节点 2

节点管理器

参数服务器

消息记录包

图 1-7 ROS 计算图级概念图

图 1-7 显示了 ROS 计算图级中的各种概念。每个概念的简短描述如下:

● 节点:ROS 节点是一个使用 ROS API 进行通信的进程。机器人可能有许多节点来执

行计算。例如,自主移动机器人具有用于硬件接口、读取激光扫描以及定位和地图

构建的节点。我们可以使用 ROS 客户端库(如 roscpp 和 rospy)创建 ROS 节点,这

些将在下一节讨论。

● 节点管理器:ROS 节点管理器作为一个中间节点,用于在不同 ROS 节点之间建立连

接。节点管理器具有 ROS 环境中运行的所有节点的全部细节。它将与另一个节点交

换细节,建立它们之间的连接。交换信息后,这两个 ROS 节点将开始通信。

● 参数服务器:参数服务器在 ROS 中非常有用。节点可以在参数服务器中存储变量,

并设置其私有属性。如果参数具有全局域,则可以由其他所有节点访问。ROS 参数

与 ROS 节点管理器一起运行。

● 消息:ROS 节点可以通过多种方式相互通信。在所有方法中,节点以 ROS 消息的形

式发送和接收数据。ROS 消息是 ROS 节点用于交换数据的数据结构。

● 主题:ROS 主题是两个 ROS 节点之间通信和交换 ROS 消息的方法之一。主题是命

名的总线,使用 ROS 消息交换数据。每个主题都有一个特定的名称,一个节点会将

数据发布到主题,另一个节点可以通过订阅从主题中读取。

● 服务:服务是不同于主题的另一种通信方式。主题使用发布或订阅通信,但在服务

中,使用的是请求或回复的方法。一个节点将作为服务提供者,运行服务例程,客

户端节点可从服务器节点请求服务。服务器将执行服务程序并将结果发送给客户端。

Page 9: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

入门 ROS 机器人应用程序开发  9

客户端节点等待服务器响应结果。

● 消息记录包:消息记录包是 ROS 中用于录制和播放 ROS 主题的实用工具。在机器

人工作时,可能会出现某些情况,我们需要在没有实际硬件的情况下工作。使用

rosbag,可以记录传感器数据,并可以将消息记录包文件复制到其他计算机,通过

回放来检查数据。

1.2.3 ROS 社区级

ROS 社区级(见图 1-8)包括 ROS 软件和知识等共享资源。

ROS 社区级

发行版博客

ROS 维基

邮件列表

错误跟踪系统ROS 问答软件库

图 1-8 ROS 社区级

每个部分的简要说明如下:

● 发行版:ROS 发行版是 ROS 包的版本集合,如同 Linux 发行版。

● 软件库:与 ROS 相关的功能包和文件取决于版本控制系统(Version-Control System, VCS),例如 Git、SVN 和 Mercurial,使用来自世界各地的开发人员贡献的功能包。

● ROS 维基:ROS 社区维基是 ROS 的知识中心,任何人都可以为其创建文档。你可

以从 ROS 维基找到有关 ROS 的标准文档和教程。

● 邮件列表:订阅 ROS 邮件列表使用户能够获得关于 ROS 功能包的更新推送,同时

他们也可以在此提问有关 ROS 的问题(http: //wiki.ros.org/Mailing%20Lists)。

● ROS 问答:ROS 问答网站是 ROS 的问题反馈(Stack Overflow)。用户可以询问有

关 ROS 和相关领域的问题(http: //answers.ros.org/questions/)。

● 博客:ROS 博客通过照片和视频等定期更新有关 ROS 社区的最新动态(http: //www.ros.org/news)。

1.2.4 ROS 通信

先来看看两个节点如何使用 ROS 主题相互通信,其实现过程如图 1-9 所示。

Page 10: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

10  第 1 章

(3)

(1) (2)

“Hello World”

ROS Master

talkernode

listenernode

Publish Strin

g

Messag

e

Topic: /ta

lker

Messag

e:

std_msgs/S

tring

Subscribe String

MessageTopic: /talkerMessage:

std_msgs/String

/talker

图 1-9 ROS 节点之间通过主题相互通信

可以看到,有两个节点,名为 talker 和 listener。talker 节点将“ Hello World”的字符

串消息发布到名为 /talker 的主题中,并且 listener 节点订阅了此主题。现来看看在每个阶段

发生了什么,标记为(1)、(2)和(3):(1)在运行 ROS 的任何节点之前,应该先启动 ROS 节点管理器。在启动之后,它将

等待节点。当 talker 节点(发布者)开始运行时,它将首先连接到 ROS 节点管理器并与主

机交换发布主题的详细信息。这包括主题名称、消息类型和发布节点 URI。主机的 URI 是一个全局值,所有节点都可以连接到它。节点管理器维护连接到它的发布者的列表。每当

发布者的详细信息更改时,列表会自动更新。

(2)当启动 listener 节点(订阅者)时,它将连接到主节点并交换节点的细节,例如要

订阅的主题、其消息类型和节点 URI。节点管理器也维护订阅表,类似于发布者。

(3)只要有相同主题的订阅者和发布者,主节点将交换订阅者和发布者的 URI。这将

有助于两个节点彼此连接并交换 数据。在它们相互连接之后,节点管理器没有任何作用。

数据没有流经节点管理器。相反,节点是相互直连并交换消息的。

1.3 ROS 客户端库

ROS 客户端库用于编写 ROS 节点。所有的 ROS 概念都在客户端库中实现,所以我们

可以使用它,而不是从头开始实现所有的东西,例如,可以使用发布者和订阅者实现 ROS节点,可以使用客户端库编写服务回调等。

主要的 ROS 客户端库是基于 C ++ 和 Python 的。以下是常用的 ROS 客户端库列表:

● roscpp:这是构建 ROS 节点最受推荐和广泛使用的 ROS 客户端库之一。该客户端

Page 11: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

入门 ROS 机器人应用程序开发  11

库实现了大部分 ROS 概念,可用于高性能应用程序。

● rospy :这是 ROS 客户端库的 Python 纯实现。这个库的优点是易于进行原型设计,

因此开发时间更短,可完美适用于非关键任务,但不推荐用于高性能应用。

● roslisp:这是 LISP 的客户端库,通常用于构建机器人规划库。

所有的客户端 ROS 库的详细信息在链接 http: //wiki.ros.org/Client%20Libraries 中给出。

1.4 ROS 工具

ROS 有各种 GUI 和命令行工具,用于检查和调试消息。现来了解一些常用的工具。

1.4.1 Rviz(ROS 可视化)

Rviz(http: //wiki.ros.org/rviz)是 ROS 中可用的 3D 可视化程序之一,可以将 ROS 主

题和参数的数值进行 2D 和 3D 可视化。Rviz 用于实现数据可视化(见图 1-10),如机器人

模型、机器人 3D 变换数据(Transform Data, TF)、点云、激光、图像以及各种不同的传感

器数据。

图 1-10 Rviz 中可视化的点云数据

1.4.2 rqt_plotrqt_plot 程序(http: //wiki.ros.org/rqt_plot)是按照 ROS 主题的格式来绘制标量值的工

具。可以在主题框中输入主题名称,如图 1-11 所示。

Page 12: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

12  第 1 章

图 1-11 rqt_plot

1.4.3 rqt_graphrqt_graph(http: //wiki.ros.org/rqt_graph),即 ROS GUI 工具,用于显示 ROS 节点之间

的互连图,如图 1-12 所示。

图 1-12 rqt_graph

ROS 的全部工具列表参考链接为:http://wiki.ros.org/Tools。

Page 13: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

入门 ROS 机器人应用程序开发  13

1.5 ROS 仿真器

Gazebo(http: //gazebosim.org)是与 ROS 深度集成的开源机器人仿真器之一。Gazebo(见图 1-13)是一个动态的机器人仿真器,带有多种机器人模型和广泛的传感器支持。可以

通过插件添加 Gazebo 的功能。ROS 可通过主题、参数和服务访问传感器值。如果需要与

ROS 完全兼容的仿真器,则可以使用 Gazebo。大多数机器人仿真器是专有和昂贵的。如果

不想付费,也可以直接使用 Gazebo,这一点毫无疑问。

Gazebo 的 ROS 接口参考链接为:http://wiki.ros.org/gazebo。

图 1-13 Gazebo 仿真器

1.6 在 Ubuntu 16.04 LTS 上安装 ROS Kinetic

正如所讨论的,有各种各样的 ROS 发行版可供下载和安装,所以选择满足确定需求的

发行版可能会有些困惑。下面是选择发行版时的一些常见问题的解答。

● 应该选择哪种发行版以获得最大的支持?

■ 答:如果有兴趣获得最大的支持,请选择 LTS 版本。如果选择了第二个最新的

LTS 发行版,那就更好了。

● 需要 ROS 的最新功能,那么应该选哪个?

Page 14: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

14  第 1 章

■ 答:最新版本,你可能在新版本发布后不会立即获取完整的最新的包,而需要等

待几个月,这是因为从一个发行版到另一个发行版迁移需要时间。

本书正在使用两个 LTS 发行版:ROS Indigo(稳定版本)和 ROS Kinetic(最新的

版本)。

开始安装

进入 ROS 网站(http: //www.ros.org/),然后浏览 Getting Started | Install(入门 | 安装)。

你将看到列出最新 ROS 发行版的屏幕如图 1-14 所示。

图 1-14 网站上最新的 ROS 发行版

如果点击 Install(安装)按钮,可以获得每个发行版的完整的安装说明。

现在将逐步介绍如何安装最新的 ROS 发行版。

1. 配置 Ubuntu 软件源

我们将从 ROS 功能包库中下载并在 Ubuntu 上安装 ROS。软件源有 .deb 格式的 ROS的预构建二进制文件。为了能够使用 ROS 软件源中的功能包,必须首先配置 Ubuntu 的软

件源选项。

更多 Ubuntu 软件源不同版本的细节请参考链接:https://help.ubuntu.com/community/Repositories/Ubuntu。

要配置软件源,请首先在 Ubuntu 搜索栏中搜索 Software & Updates(软件和更新),如

图 1-15 所示。

Page 15: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。
Page 16: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

16  第 1 章

Getting Started with ROS Robotics Application Development

[ 30 ]

Setting up keysWhen a new repository is added to Ubuntu, we should add the keys to make it trusted andto be able to validate the origin of the packages. The following key should be added toUbuntu before starting installation:

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 0xB01FA116

Now we are sure that we are downloading from an authorized server.

Installing ROSNow, we are ready to install ROS packages on Ubuntu. The first step is to update the list ofpackages on Ubuntu. You can use the following command to update the list:

$ sudo apt-get update

This will fetch all packages from the servers that are in source.list.

After getting the package list, we have to install the entire ROS package suite using thefollowing command:

ROS Kinect:

$ sudo apt-get install ros-kinetic-desktop-full

ROS Jade:

$ sudo apt-get install ros-jade-desktop-full

ROS Indigo:

$ sudo apt-get install ros-indigo-desktop-full

This will install most of the important packages in ROS. You will need at least 15 GB ofspace in your root Ubuntu partition to install and work with ROS.

Initializing rosdepThe rosdep tool in ROS helps us easily install dependencies of packages that we are goingto compile. This tool is also necessary for some core components of ROS.

现在可以确信我们将从授权的服务器下载软件包。

4. 安装 ROS现在,准备在 Ubuntu 上安装 ROS 包。第一步是更新 Ubuntu 上的功能包列表。你可以

使用下面的命令更新列表:

Getting Started with ROS Robotics Application Development

[ 30 ]

Setting up keysWhen a new repository is added to Ubuntu, we should add the keys to make it trusted andto be able to validate the origin of the packages. The following key should be added toUbuntu before starting installation:

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 0xB01FA116

Now we are sure that we are downloading from an authorized server.

Installing ROSNow, we are ready to install ROS packages on Ubuntu. The first step is to update the list ofpackages on Ubuntu. You can use the following command to update the list:

$ sudo apt-get update

This will fetch all packages from the servers that are in source.list.

After getting the package list, we have to install the entire ROS package suite using thefollowing command:

ROS Kinect:

$ sudo apt-get install ros-kinetic-desktop-full

ROS Jade:

$ sudo apt-get install ros-jade-desktop-full

ROS Indigo:

$ sudo apt-get install ros-indigo-desktop-full

This will install most of the important packages in ROS. You will need at least 15 GB ofspace in your root Ubuntu partition to install and work with ROS.

Initializing rosdepThe rosdep tool in ROS helps us easily install dependencies of packages that we are goingto compile. This tool is also necessary for some core components of ROS.

这将获取 source.list 中所有服务器上的软件包信息。

获取功能包列表后,必须使用下面的命令安装全部的 ROS 功能包:

● ROS Kinect:

Getting Started with ROS Robotics Application Development

[ 30 ]

Setting up keysWhen a new repository is added to Ubuntu, we should add the keys to make it trusted andto be able to validate the origin of the packages. The following key should be added toUbuntu before starting installation:

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 0xB01FA116

Now we are sure that we are downloading from an authorized server.

Installing ROSNow, we are ready to install ROS packages on Ubuntu. The first step is to update the list ofpackages on Ubuntu. You can use the following command to update the list:

$ sudo apt-get update

This will fetch all packages from the servers that are in source.list.

After getting the package list, we have to install the entire ROS package suite using thefollowing command:

ROS Kinect:

$ sudo apt-get install ros-kinetic-desktop-full

ROS Jade:

$ sudo apt-get install ros-jade-desktop-full

ROS Indigo:

$ sudo apt-get install ros-indigo-desktop-full

This will install most of the important packages in ROS. You will need at least 15 GB ofspace in your root Ubuntu partition to install and work with ROS.

Initializing rosdepThe rosdep tool in ROS helps us easily install dependencies of packages that we are goingto compile. This tool is also necessary for some core components of ROS.

● ROS Jade:

Getting Started with ROS Robotics Application Development

[ 30 ]

Setting up keysWhen a new repository is added to Ubuntu, we should add the keys to make it trusted andto be able to validate the origin of the packages. The following key should be added toUbuntu before starting installation:

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 0xB01FA116

Now we are sure that we are downloading from an authorized server.

Installing ROSNow, we are ready to install ROS packages on Ubuntu. The first step is to update the list ofpackages on Ubuntu. You can use the following command to update the list:

$ sudo apt-get update

This will fetch all packages from the servers that are in source.list.

After getting the package list, we have to install the entire ROS package suite using thefollowing command:

ROS Kinect:

$ sudo apt-get install ros-kinetic-desktop-full

ROS Jade:

$ sudo apt-get install ros-jade-desktop-full

ROS Indigo:

$ sudo apt-get install ros-indigo-desktop-full

This will install most of the important packages in ROS. You will need at least 15 GB ofspace in your root Ubuntu partition to install and work with ROS.

Initializing rosdepThe rosdep tool in ROS helps us easily install dependencies of packages that we are goingto compile. This tool is also necessary for some core components of ROS.

● ROS Indigo:

Getting Started with ROS Robotics Application Development

[ 30 ]

Setting up keysWhen a new repository is added to Ubuntu, we should add the keys to make it trusted andto be able to validate the origin of the packages. The following key should be added toUbuntu before starting installation:

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 0xB01FA116

Now we are sure that we are downloading from an authorized server.

Installing ROSNow, we are ready to install ROS packages on Ubuntu. The first step is to update the list ofpackages on Ubuntu. You can use the following command to update the list:

$ sudo apt-get update

This will fetch all packages from the servers that are in source.list.

After getting the package list, we have to install the entire ROS package suite using thefollowing command:

ROS Kinect:

$ sudo apt-get install ros-kinetic-desktop-full

ROS Jade:

$ sudo apt-get install ros-jade-desktop-full

ROS Indigo:

$ sudo apt-get install ros-indigo-desktop-full

This will install most of the important packages in ROS. You will need at least 15 GB ofspace in your root Ubuntu partition to install and work with ROS.

Initializing rosdepThe rosdep tool in ROS helps us easily install dependencies of packages that we are goingto compile. This tool is also necessary for some core components of ROS.

这将在 ROS 中安装大部分重要的功能包。Ubuntu 根分区中至少需要 15GB 的空间来安

装和使用 ROS。5. 初始化 rosdepROS 中的 rosdep 工具有助于我们轻松地安装需要编译的功能包的依赖项。ROS 的一些

核心组件也需要这个工具。

启动 rosdep 命令如下:

Getting Started with ROS Robotics Application Development

[ 31 ]

This command launches rosdep:

$ sudo rosdep init $ rosdep update

Setting the ROS environmentCongratulations! We are done with the ROS installation, but what next?

The ROS installation mainly consists of scripts and executables, which are mostly installedto /opt/ros/<ros_version>.

To get access to these commands and scripts, we should add ROS environment variables tothe Ubuntu Terminal. It's easy to do this. To access ROS commands from inside theTerminal, we have to source the following bash file:

/opt/ros/<ros_version>/setup.bash

Here's the command to do so:

$ source /opt/ros/kinetic/setup.bash

But in order to get the ROS environment in multiple Terminals, we should add thecommand to the .bashrc script, which is in the home folder. The .bashrc script will besourced whenever a new Terminal opens.

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

We can install multiple ROS distributions on Ubuntu. If there are multiple distributions, wecan switch to each ROS distribution by changing the distribution name in the precedingcommand.

Getting rosinstallLast but not least, there is the ROS command-line tool, called rosinstall, for installingsource trees for particular ROS packages. The tool is based on Python, and you can install itusing the following command:

$ sudo apt-get install python-rosinstall

We are done with the ROS installation. Just check whether the installation is proper, byrunning the following commands.

6. 设置 ROS 环境

现在已经完成了 ROS 的安装,下一步该做什么呢?

ROS 安装的内容主要包括脚本和可执行文件,主要安装到 / opt / ros / <ros_version>。要访问这些命令和脚本,我们需要将 ROS 环境变量添加到 Ubuntu 终端系统。这一点

很容易实现,为了在终端里运行 ROS 命令,我们必须 source 如下的 bash 文件:

Getting Started with ROS Robotics Application Development

[ 31 ]

This command launches rosdep:

$ sudo rosdep init $ rosdep update

Setting the ROS environmentCongratulations! We are done with the ROS installation, but what next?

The ROS installation mainly consists of scripts and executables, which are mostly installedto /opt/ros/<ros_version>.

To get access to these commands and scripts, we should add ROS environment variables tothe Ubuntu Terminal. It's easy to do this. To access ROS commands from inside theTerminal, we have to source the following bash file:

/opt/ros/<ros_version>/setup.bash

Here's the command to do so:

$ source /opt/ros/kinetic/setup.bash

But in order to get the ROS environment in multiple Terminals, we should add thecommand to the .bashrc script, which is in the home folder. The .bashrc script will besourced whenever a new Terminal opens.

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

We can install multiple ROS distributions on Ubuntu. If there are multiple distributions, wecan switch to each ROS distribution by changing the distribution name in the precedingcommand.

Getting rosinstallLast but not least, there is the ROS command-line tool, called rosinstall, for installingsource trees for particular ROS packages. The tool is based on Python, and you can install itusing the following command:

$ sudo apt-get install python-rosinstall

We are done with the ROS installation. Just check whether the installation is proper, byrunning the following commands.

实现命令如下所示:

Getting Started with ROS Robotics Application Development

[ 31 ]

This command launches rosdep:

$ sudo rosdep init $ rosdep update

Setting the ROS environmentCongratulations! We are done with the ROS installation, but what next?

The ROS installation mainly consists of scripts and executables, which are mostly installedto /opt/ros/<ros_version>.

To get access to these commands and scripts, we should add ROS environment variables tothe Ubuntu Terminal. It's easy to do this. To access ROS commands from inside theTerminal, we have to source the following bash file:

/opt/ros/<ros_version>/setup.bash

Here's the command to do so:

$ source /opt/ros/kinetic/setup.bash

But in order to get the ROS environment in multiple Terminals, we should add thecommand to the .bashrc script, which is in the home folder. The .bashrc script will besourced whenever a new Terminal opens.

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

We can install multiple ROS distributions on Ubuntu. If there are multiple distributions, wecan switch to each ROS distribution by changing the distribution name in the precedingcommand.

Getting rosinstallLast but not least, there is the ROS command-line tool, called rosinstall, for installingsource trees for particular ROS packages. The tool is based on Python, and you can install itusing the following command:

$ sudo apt-get install python-rosinstall

We are done with the ROS installation. Just check whether the installation is proper, byrunning the following commands.

但是为了在多个终端中获取 ROS 环境,我们需要将该命令添加到 home 文件夹中

Page 17: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

Getting Started with ROS Robotics Application Development

[ 31 ]

This command launches rosdep:

$ sudo rosdep init $ rosdep update

Setting the ROS environmentCongratulations! We are done with the ROS installation, but what next?

The ROS installation mainly consists of scripts and executables, which are mostly installedto /opt/ros/<ros_version>.

To get access to these commands and scripts, we should add ROS environment variables tothe Ubuntu Terminal. It's easy to do this. To access ROS commands from inside theTerminal, we have to source the following bash file:

/opt/ros/<ros_version>/setup.bash

Here's the command to do so:

$ source /opt/ros/kinetic/setup.bash

But in order to get the ROS environment in multiple Terminals, we should add thecommand to the .bashrc script, which is in the home folder. The .bashrc script will besourced whenever a new Terminal opens.

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

We can install multiple ROS distributions on Ubuntu. If there are multiple distributions, wecan switch to each ROS distribution by changing the distribution name in the precedingcommand.

Getting rosinstallLast but not least, there is the ROS command-line tool, called rosinstall, for installingsource trees for particular ROS packages. The tool is based on Python, and you can install itusing the following command:

$ sudo apt-get install python-rosinstall

We are done with the ROS installation. Just check whether the installation is proper, byrunning the following commands.

Getting Started with ROS Robotics Application Development

[ 31 ]

This command launches rosdep:

$ sudo rosdep init $ rosdep update

Setting the ROS environmentCongratulations! We are done with the ROS installation, but what next?

The ROS installation mainly consists of scripts and executables, which are mostly installedto /opt/ros/<ros_version>.

To get access to these commands and scripts, we should add ROS environment variables tothe Ubuntu Terminal. It's easy to do this. To access ROS commands from inside theTerminal, we have to source the following bash file:

/opt/ros/<ros_version>/setup.bash

Here's the command to do so:

$ source /opt/ros/kinetic/setup.bash

But in order to get the ROS environment in multiple Terminals, we should add thecommand to the .bashrc script, which is in the home folder. The .bashrc script will besourced whenever a new Terminal opens.

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

We can install multiple ROS distributions on Ubuntu. If there are multiple distributions, wecan switch to each ROS distribution by changing the distribution name in the precedingcommand.

Getting rosinstallLast but not least, there is the ROS command-line tool, called rosinstall, for installingsource trees for particular ROS packages. The tool is based on Python, and you can install itusing the following command:

$ sudo apt-get install python-rosinstall

We are done with the ROS installation. Just check whether the installation is proper, byrunning the following commands.

Page 18: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

18  第 1 章

能使用 ROS 吗?可以的,只需要使用一个名为 VirtualBox 的工具(https: //www.virtualbox.org/)。VirtualBox 允许安装客户机操作系统,而不影响主机操作系统。虚拟操作系统可以

在虚拟计算机给定的规范中与主机操作系统一起工作,例如处理器数量和 RAM 以及硬盘

大小。

可以下载 VirtualBox 的常用系统版本,链接为:https://www.virtualbox.org/wiki/Downloads。然后参考互联网上的教程在 VirtualBox 上安装 Ubuntu。

图 1-18 是 VirtualBox GUI 的屏幕截图。左侧是虚拟操作系统列表,右侧是虚拟 PC 配

置。在顶部面板上有用于创建新的虚拟操作系统和启动现有 VirtualBox 的按钮。最佳虚拟

PC 配置如图 1-18 所示。

图 1-18 VirtualBox 配置

Page 19: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

Getting Started with ROS Robotics Application Development

[ 34 ]

Here are the main specifications of the virtual PC:

Number of CPUs: 1RAM: 4 GBDisplay | Video Memory: 128 MBAcceleration: 3DStorage: 20 GB to 30 GBNetwork adapter on NAT

In order to have hardware acceleration, you should install drivers from the VirtualBoxGuest add-ons disc. After booting into the Ubuntu desktop, navigate to Devices | InsertGuest Addition CD Image. This will mount the CD image in Ubuntu and ask the user torun the script to install drivers. If we allow it, it will automatically install all the drivers.After a reboot, you will get full acceleration on the Ubuntu guest.

There is no difference in ROS installation on VirtualBox .If the virtual network adapter is inNAT mode, the Internet connection of the host OS will be shared with the guest OS. So theguest can work the same as the real OS.

Setting the ROS workspaceAfter setting ROS on a real PC or VirtualBox, the next step is to create a workspace in ROS.The ROS workspace is a place where we keep ROS packages. In the latest ROS distribution,we use a catkin-based workspace to build and install ROS packages. The catkin system(http://wiki.ros.org/catkin) is the official build system of ROS, which helps us build thesource code into a target executable or libraries inside the ROS workspace.

Building an ROS workspace is an easy task; just open a Terminal and follow theseinstructions:

The first step is to create an empty workspace folder and another folder called1.src to store the ROS package in. The following command will do this job. Theworkspace folder name here is catkin_ws.

$ mkdir -p ~/catkin_ws/src

Page 20: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。
Page 21: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

入门 ROS 机器人应用程序开发  21

图 1-21 使用 ROS 的公司

图 1-22 是在应聘 Fetch Robotics(http: //fetchrobotics.com/careers/)机器人应用程序开

发工程师的工作要求。

图 1-22 ROS 应用工程师的典型工作要求

了解 ROS 将有助于你轻松胜任机器人应用工程开发的工作。如果浏览任何与机器人相

关的工作的技能要求列表,就一定会在其中发现 ROS。在大学和行业有独立的课程和研讨会讲授机器人的 ROS 开发。了解 ROS,你将有机会

成为诸如 CMU 机器人研究所(http: //www.ri.cmu.edu/)和 UPenn 的 GRAP 实验室(https: // www.grasp.upenn.edu/)等知名机器人机构的实习生以及硕士、博士和博士后。

后续章节将帮助你获得 ROS 的实践基础和核心技能。

Page 22: 第1 章 入门 ROS 机器人应用程序开发images.china-pub.com/ebook8000001-8005000/8004538/ch01.pdf · ros 是用于机器人编程的开放源码、灵活的软件框架。

22  第 1 章

1.10 问题

● ROS 的主要组成部分是什么?

● ROS 与其他机器人框架相比有什么优势? ● ROS 的不同概念是什么?

● ROS 计算图的不同概念是什么?

1.11 本章总结

本章是开始 ROS 机器人应用程序开发的入门篇。本章的主要目的是通过安装和了解

ROS 来开始学习。本章可用作 ROS 应用程序开发的入门指南,帮助你了解接下来的章节,

我们将主要介绍基于 ROS 的应用程序开发。在本章结尾,我们看到了与 ROS 相关的工作

和研究机会,并且还发现许多公司和大学正在寻找 ROS 开发人员进行不同的机器人应用程

序开发。从下一章开始,将详细介绍基于 ROS 的各种项目。