A Gentle Introduction to ROS

更新时间:2023-05-02 10:17:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

A Gentle Introduction to ROS Jason M.O’Kane

Jason M.O’Kane

University of South Carolina

Department of Computer Science and Engineering

315Main Street

Columbia,SC29208

b4a500885ef7ba0d4a733b9b/~jokane

?2014,Jason Matthew O’Kane.All rights reserved.

This is version2.0.2(61e988a),generated on January15,2014. Typeset by the author using L A T E X and memoir.cls.

ISBN978-14-92143-23-9

Contents in Brief

Contents in Brief iii Contents v 1Introduction1 In which we introduce ROS,describe how it can be useful,and pre-

view the remainder of the book.

2Getting started11 In which we install ROS,introduce some basic ROS concepts,and in-

teract with a working ROS system.

3Writing ROS programs37 In which we write ROS programs to publish and subscribe to mes-

sages.

4Log messages59 In which we generate and view log messages.

5Graph resource names75 In which we learn how ROS resolves the names of nodes,topics,pa-

rameters,and services.

iii

C ONTENTS IN B RIEF

6Launch?les83 In which we con?gure and run many nodes at once using launch?les.

7Parameters105 In which we con?gure nodes using parameters.

8Services117 In which we call services and respond to service requests.

9Recording and replaying messages133 In which we use bag?les to record and replay messages.

10Conclusion141 In which we preview some additional topics.

Index145 iv

Contents

Contents in Brief iii Contents v

1Introduction1

1.1Why ROS? (1)

Distributed computation (2)

Software reuse (2)

Rapid testing (3)

ROS is not (4)

1.2What to expect (4)

Chapters and dependencies (5)

Intended audience (5)

1.3Conventions (7)

1.4Getting more information (7)

Distributions (8)

Build systems (9)

1.5Looking forward (9)

2Getting started11

2.1Installing ROS (11)

v

C ONTENTS

Adding the ROS repository (11)

Installing the package authentication key (12)

Downloading the package lists (12)

Installing the ROS packages (12)

Installing turtlesim (12)

Setting up rosdep systemwide (13)

2.2Con?guring your account (13)

Setting up rosdep in a user account (13)

Setting environment variables (14)

2.3A minimal example using turtlesim (15)

Starting turtlesim (15)

2.4Packages (16)

Listing and locating packages (17)

Inspecting a package (18)

2.5The master (19)

2.6Nodes (20)

Starting nodes (21)

Listing nodes (21)

Inspecting a node (23)

Killing a node (23)

2.7Topics and messages (23)

2.7.1Viewing the graph (24)

2.7.2Messages and message types (26)

Listing topics (26)

Echoing messages (27)

Measuring publication rates (27)

Inspecting a topic (27)

Inspecting a message type (29)

Publishing messages from the command line (30)

Understanding message type names (33)

2.8A larger example (33)

2.8.1Communication via topics is many-to-many (35)

2.8.2Nodes are loosely coupled (35)

2.9Checking for problems (36)

2.10Looking forward (36)

3Writing ROS programs37

3.1Creating a workspace and a package (37)

vi

Contents

Creating a workspace (37)

Creating a package (38)

Editing the manifest (39)

3.2Hello,ROS! (39)

3.2.1A simple program (39)

3.2.2Compiling the Hello program (42)

Declaring dependencies (42)

Declaring an executable (42)

Building the workspace (43)

Sourcing setup.bash (44)

3.2.3Executing the hello program (45)

3.3A publisher program (45)

3.3.1Publishing messages (45)

Including the message type declaration (47)

Creating a publisher object (47)

Creating and?lling in the message object (49)

Publishing the message (49)

Formatting the output (50)

3.3.2The publishing loop (50)

Checking for node shutdown (50)

Controlling the publishing rate (51)

3.3.3Compiling pubvel (52)

Declaring message type dependencies (52)

3.3.4Executing pubvel (52)

3.4A subscriber program (53)

Writing a callback function (53)

Creating a subscriber object (55)

Giving ROS control (57)

3.4.1Compiling and executing subpose (58)

3.5Looking forward (58)

4Log messages59

4.1Severity levels (59)

4.2An example program (60)

4.3Generating log messages (60)

Generating simple log messages (60)

Generating one-time log messages (63)

Generating throttled log messages (63)

vii

C ONTENTS

4.4Viewing log messages (65)

4.4.1Console (66)

Formatting console messages (66)

4.4.2Messages on rosout (67)

4.4.3Log?les (70)

Finding the run id (70)

Checking and purging log?les (70)

4.5Enabling and disabling log messages (71)

Setting the logger level from the command line (72)

Setting the logger level from a GUI (73)

Setting the logger level from C++code (73)

4.6Looking forward (74)

5Graph resource names75

5.1Global names (75)

5.2Relative names (76)

Resolving relative names (77)

Setting the default namespace (77)

Understanding the purpose of relative names (78)

5.3Private names (78)

5.4Anonymous names (79)

5.5Looking forward (80)

6Launch?les83

6.1Using launch?les (83)

Executing launch?les (83)

Requesting verbosity (85)

Ending a launched session (85)

6.2Creating launch?les (86)

6.2.1Where to place launch?les (86)

6.2.2Basic ingredients (86)

Inserting the root element (86)

Launching nodes (87)

Finding node log?les (88)

Directing output to the console (88)

Requesting respawning (89)

Requiring nodes (89)

Launching nodes in their own windows (90)

viii

Contents

6.3Launching nodes inside a namespace (91)

6.4Remapping names (93)

6.4.1Creating remappings (93)

6.4.2Reversing a turtle (95)

6.5Other launch?le elements (97)

6.5.1Including other?les (97)

6.5.2Launch arguments (99)

Declaring arguments (100)

Assigning argument values (101)

Accessing argument values (101)

Sending argument values to included launch?les (101)

6.5.3Creating groups (102)

6.6Looking forward (104)

7Parameters105

7.1Accessing parameters from the command line (105)

Listing parameters (105)

Querying parameters (106)

Setting parameters (107)

Creating and loading parameter?les (107)

7.2Example:Parameters in turtlesim (108)

Reading the background color (109)

Setting the background color (109)

7.3Accessing parameters from C++ (110)

7.4Setting parameters in launch?les (113)

Setting parameters (113)

Setting private parameters (113)

Reading parameters from a?le (114)

7.5Looking forward (115)

8Services117

8.1Terminology for services (117)

8.2Finding and calling services from the command line (118)

Listing all services (118)

Listing services by node (119)

Finding the node offering a service (120)

Finding the data type of a service (120)

Inspecting service data types (121)

ix

C ONTENTS

Calling services from the command line (122)

8.3A client program (123)

Declaring the request and response types (123)

Creating a client object (123)

Creating request and response objects (125)

Calling the service (125)

Declaring a dependency (127)

8.4A server program (127)

Writing a service callback (127)

Creating a server object (129)

Giving ROS control (130)

8.4.1Running and improving the server program (130)

8.5Looking ahead (131)

9Recording and replaying messages133

9.1Recording and replaying bag?les (133)

Recording bag?les (133)

Replaying bag?les (134)

Inspecting bag?les (134)

9.2Example:A bag of squares (135)

Drawing squares (135)

Recording a bag of squares (135)

Replaying the bag of squares (136)

9.3Bags in launch?les (139)

9.4Looking forward (140)

10Conclusion141

10.1What next? (141)

Running ROS over a network (141)

Writing cleaner programs (142)

Visualizing data with rviz (142)

Creating message and service types (142)

Managing coordinate frames with tf (142)

Simulating with Gazebo (143)

10.2Looking forward (143)

Index145 x

Chapter1

Introduction In which we introduce ROS,describe how it can be useful,and preview the re-

mainder of the book.

1.1Why ROS?

The robotics community has made impressive progress in recent years.Reliable and in-expensive robot hardware—from land-based mobile robots,to quadrotor helicopters,to humanoids—is more widely available than ever before.Perhaps even more impressively, the community has also developed algorithms that help those robots run with increasing levels of autonomy.

In spite of(or,some might argue,because of)this rapid progress,robots do still present some signi?cant challenges for software developers.This book introduces a software plat-form called Robot Operating System,or ROS,1that is intended to ease some of these dif-?culties.The of?cial description of ROS is:

ROS is an open-source,meta-operating system for your robot.It provides the

services you would expect from an operating system,including hardware ab-

straction,low-level device control,implementation of commonly-used func-

tionality,message-passing between processes,and package management.It

1When spoken aloud,the name“ROS”is nearly always pronounced as a single word that rhymes with “moss,”and almost never spelled out“arrr-oh-ess.”

1

1.I NTRODUCTION

also provides tools and libraries for obtaining,building,writing,and running

1

code across multiple computers.

This description is accurate—and it correctly emphasizes that ROS does not replace,but instead works alongside a traditional operating system—but it may leave you wondering what the real advantages are for software that uses ROS.After all,learning to use a new framework,particularly one as complex and diverse as ROS,can take quite a lot of time and mental energy,so one should be certain that the investment will be worthwhile.Here are a few speci?c issues in the development of software for robots that ROS can help to resolve.

Distributed computation Many modern robot systems rely on software that spans many different processes and runs across several different computers.For example: Some robots carry multiple computers,each of which controls a subset of the robot’s sensors or actuators.

Even within a single computer,it’s often a good idea to divide the robot’s software into small,stand-alone parts that cooperate to achieve the overall goal.This ap-proach is sometimes called“complexity via composition.”

When multiple robots attempt to cooperate on a shared task,they often need to communicate with one another to coordinate their efforts.

Human users often send commands to a robot from a laptop,a desktop computer, or mobile device.We can think of this human interface as an extension of the robot’s software.

The common thread through all of these cases is a need for communication between mul-tiple processes that may or may not live on the same computer.ROS provides two relatively simple,seamless mechanisms for this kind of communication.We’ll discuss the details in Chapters3and8.

Software reuse The rapid progress of robotics research has resulted in a growing collec-tion of good algorithms for common tasks such as navigation,motion planning,mapping, and many others.Of course,the existence of these algorithms is only truly useful if there is a way to apply them in new contexts,without the need to reimplement each algorithm for each new system.ROS can help to prevent this kind of pain in at least two important ways.

1

b4a500885ef7ba0d4a733b9b/ROS/Introduction

2

1.1.Why ROS?

ROS’s standard packages provide stable,debugged implementations of many impor-tant robotics algorithms.

ROS’s message passing interface is becoming a de facto standard for robot software interoperability,which means that ROS interfaces to both the latest hardware and to

implementations of cutting edge algorithms are quite often available.For example,

2 the ROS website lists dozens of repositories of publicly-available ROS packages.

This sort of uniform interface greatly reduces the need to write“glue”code to con-

nect existing parts.

As a result,developers that use ROS can expect—after,of course,climbing ROS’s initial

learning curve—to focus more time on experimenting with new ideas,and less time rein-

venting wheels.

Rapid testing One of the reasons that software development for robots is often more

challenging than other kinds of development is that testing can be time consuming and

error-prone.Physical robots may not always be available to work with,and when they

are,the process is sometimes slow and?nicky.Working with ROS provides two effective

workarounds to this problem.

Well-designed ROS systems separate the low-level direct control of the hardware and high-level processing and decision making into separate programs.Because of this separation,we can temporarily replace those low-level programs(and their corre-sponding hardware)with a simulator,to test the behavior of the high-level part of the system.

ROS also provides a simple way to record and play back sensor data and other kinds of messages.This facility means that we can obtain more leverage from the time we do spend operating a physical robot.By recording the robot’s sensor data,we can replay it many times to test different ways of processing that same data.In ROS parlance,these recordings are called“bags”and a tool called rosbag is used to record and replay them.See Chapter9.

A crucial point for both of these features is that the change is seamless.Because the real

robot,the simulator,and the bag playback mechanism can all provide identical(or at least

very similar)interfaces,your software does not need to be modi?ed to operate in these

distinct scenarios,and indeed need not even“know”whether it is talking to a real robot or

to something else.

2

b4a500885ef7ba0d4a733b9b/Repositories

3

1.I NTRODUCTION

Of course,ROS is not the only platform that offers these capabilities.What is unique about ROS,at least in the author’s judgment,is the level of widespread support for ROS across the robotics community.This “critical mass”of support makes it reasonable to pre-dict that ROS will continue to evolve,expand,and improve in the future.

ROS is not...

Finally,let’s take a moment to review a few things that are not true about

ROS.ROS is not a programming language.In fact,ROS programs are routinely written in C ++, 3and this book has some explicit instructions on how to do that.Client libraries are also available for Python, 4Java, 5Lisp, 6and a handful of other lan-guages. 7

ROS is not (only)a library.Although ROS does include client libraries,it also includes (among other things),a central server,a set of command-line tools,a set of graphical tools,and a build system.

ROS is not an integrated development environment.Although ROS does not pre-scribe any particular development environment,it can be used with most popular IDEs. 8It is also quite reasonable (and,indeed,it is the author’s personal prefer-ence)to use ROS from a text editor and the command line,without any IDE.

1.2What to expect

The goal of this book is to provide an integrated overview of the concepts and techniques you’ll need to know to write ROS software.This goal places a few important constraints on the content of the book.

This is not an introduction to programming.We won’t discuss basic programming concepts in any great detail.This book assumes that you’ve studied C ++in suf?cient depth to read,write,and understand code in that language.

3b4a500885ef7ba0d4a733b9b/roscpp 4b4a500885ef7ba0d4a733b9b/rospy 5b4a500885ef7ba0d4a733b9b/rosjava 6b4a500885ef7ba0d4a733b9b/roslisp 7b4a500885ef7ba0d4a733b9b/ClientLibraries 8b4a500885ef7ba0d4a733b9b/IDEs

4

1.2.What to expect

This is not a reference manual.There is plenty of detailed information about ROS,including both tutorials 9and exhaustive reference 10material,available online.This book makes no attempt to replace those resources.Instead,we present a se-lected subset of ROS features that,in the author’s view,represents a useful starting point for using ROS.

This is not a textbook on robotics algorithms.The study of robots,especially the study of algorithms for controlling autonomous robots,can be quite fascinating.A dizzy-ing variety of algorithms have been developed for various parts of this problem.This book will not teach you any of those algorithms.2Our focus is on a speci?c tool,namely ROS,that can ease the implementation and testing of those algorithms.Chapters and dependencies Figure 1.1shows the organization of the book.Chapters are shown as rectangles;arrows show the major dependencies between them.It should be fairly reasonable to read this book in any order that follows those constraints.

Intended audience This book should be useful for both students in robotics courses and for researchers or hobbyists that want to get a quick start with ROS.We’ll assume that read-ers are comfortable with Linux (including tasks like using the command line,installing software,editing ?les,and setting environment variables),are familiar with C ++,and want to write software to control robots.Generally,we’ll assume that you are using Ubuntu Linux 13.04(the newest version that is,at this writing,of?cially supported)and the bash shell.However,there are relatively few instances where these choices matter;other Linux distributions and other shells will not usually be problematic.

2...but you should learn them anyway.

9b4a500885ef7ba0d4a733b9b/ROS/Tutorials 10b4a500885ef7ba0d4a733b9b/APIs

5

1.I

NTRODUCTION

6

1.3.Conventions

1.3Conventions

Throughout the book,we’ll attempt to anticipate some of the most common sources of problems.These kinds of warnings,which are worthy of your attention,especially if things are not working as expected,are marked like this:

This“dangerous bend”sign indicates a common source of problems.

In addition,some sections include explanations that will be of interest to some readers, but are not crucial for understanding the concepts at hand.These comments are marked like this:

This“fast forward”symbol indicates information that can be safely skipped,espe-cially on a?rst reading.

1.4Getting more information

As alluded to above,this book makes no attempt to be a comprehensive reference for ROS. It’s all but certain that you will need additional details to do anything interesting.Fortu-nately,online information about ROS is abundant.

Most importantly,the developers of ROS maintain extensive documentation, 11in-cluding a set of tutorials.This book includes links,each marked with a ,to many of the corresponding pages in this documentation.If you are reading an electronic version of the book in a reasonably modern PDF viewer,you should be able to click these links directly to open them in your browser.

When unexpected things happen—and chances are quite good that they will—there

12 is a question and answer site(in the style of Stack Exchange)devoted to ROS.

11

b4a500885ef7ba0d4a733b9b

12

b4a500885ef7ba0d4a733b9b

7

1.I NTRODUCTION

It may also be valuable to subscribe to the ros-users mailing list, 13on which an-nouncements sometimes appear.

Here are two important details that will help you make sense of some of the documenta-tion,but are not always fully explained in context there.

Distributions Major versions of ROS are called distributions ,and are named using ad-jectives that start with with successive letters of the alphabet. 14(This is,for compari-son,very similar to the naming schemes used for other large software projects,including Ubuntu and Android.)At the time of this writing,the current version is hydro .The next version,named indigo ,is due in May 2014. 15Older distributions include groovy ,fuerte ,electric ,diamondback ,C Turtle ,and box turtle .These version names appear in many places throughout the documentation.

To keep things as simple and up-to-date as possible,this book assumes that you are using hydro .

If,for some reason,you need to use groovy instead of hydro ,most of the book’s

content still applies without modi?cation.However,one change is particularly im-portant:In hydro (and,therefore,in this book),velocity commands for the turtle-sim simulator have been changed to use a standard message type and topic name that happen to be shared with many real mobile robots.

Distribution Topic name Message type

groovy

/turtle1/command_velocity turtlesim/Velocity hydro /turtle1/cmd_vel geometry_msgs/Twist

This change has a few practical implications:

When adding dependencies to your package (see page 42),you’ll need a de-

pendency on turtlesim ,instead of on geometry_msgs .

The relevant header ?le (see page 47)is

13https://b4a500885ef7ba0d4a733b9b/mailman/listinfo/ros-users 14b4a500885ef7ba0d4a733b9b/wiki/Distributions 15b4a500885ef7ba0d4a733b9b/indigo/Planning

8

1.5.Looking forward

turtlesim/Velocity.h

rather than

geometry_msgs/Twist.h

The turtlesim/Velocity message type has only two?elds,called linear and

angular.These?elds play the same roles as the linear.x and angular.z?elds

of geometry_msgs/Twist.This change applies both on the command line

(see page30)and in C++code(see page49).

Build systems Starting with the groovy distribution,ROS made some major changes to the way software is compiled.Older,pre-groovy distributions used a build system called rosbuild,but more recent versions have begun to replace rosbuild with a new build sys-tem called catkin.It is important to know about this change because a few of the tutorials have separate versions,depending on whether you’re using rosbuild or catkin.These sep-arate versions are selected using a pair of buttons near the top of the tutorial.This book describes catkin,but there may be some cases in which rosbuild is a better choice. 16

1.5Looking forward

In the next chapter,we’ll get started working with ROS,learning some basic concepts and tools.

16b4a500885ef7ba0d4a733b9b/catkin_or_rosbuild

9

本文来源:https://www.bwwdw.com/article/fjse.html

Top