天天看點

ROS2程式設計基礎課程--接口

About ROS 2 Interfaces 關于ROS 2接口

Table of Contents 目錄

1. Background 背景

2. Message Description Specification 消息說明規範

2.1 Fields 字段

2.1.1 Field Types 字段類型

2.1.2 Field Names 字段名稱

2.1.3 Field Default Value 字段預設值

2.2 Constants 常數

3. Service Description Specification 服務描述規範

ROS applications typically communicate through interfaces of one of two types: messages and services. ROS uses a simplified description language to describe these interfaces. This description makes it easy for ROS tools to automatically generate source code for the interface type in several target languages. 

ROS應用程式通常通過以下兩種類型之一的接口進行通信:消息和服務。ROS使用簡化的描述語言來描述這些接口。此描述使ROS工具可以輕松地為多種目智語言中的接口類型自動生成源代碼。

In this document we will describe the supported types and how to create your own msg/srv files. 

在本節将介紹支援的類型以及如何建立自定義的msg / srv檔案。

Messages description are defined in .msg files in the msg/ directory of a ROS package. .msg files are composed of two parts: fields and constants. 

消息描述定義在ROS包msg/目錄的.msg檔案中。 .msg檔案由兩部分組成:字段和常量。

Each field consists of a type and a name, separated by a space, i.e: 

每個字段由一個類型和一個名稱組成,用空格分隔,即:

fieldtype1 fieldname1

fieldtype2 fieldname2

fieldtype3 fieldname3

For example: 例如:

int32 my_int

string my_string

Field types can be: 字段類型可以是:

a built-in-type 

内置式

names of Message descriptions defined on their own, such as “geometry_msgs/PoseStamped” 

自定義的消息描述的名稱,例如“geometry_msgs/PoseStamped”

Built-in-types currently supported: 目前支援的内置類型:

Type name

C++

Python

DDS type

bool

builtins.bool

boolean

byte

uint8_t

builtins.bytes*

octet

char

builtins.str*

float32

float

builtins.float*

float64

double

int8

int8_t

builtins.int*

uint8

int16

int16_t

short

uint16

uint16_t

unsigned short

int32

int32_t

long

uint32

uint32_t

unsigned long

int64

int64_t

long long

uint64

uint64_t

unsigned long long

string

std::string

builtins.str

Every built-in-type can be used to define arrays: 每個内置類型都可用于定義數組:

static array

std::array<T, N>

builtins.list*

T[N]

unbounded dynamic array

std::vector

builtins.list

sequence

bounded dynamic array

custom_class<T, N>

sequence<T, N>

bounded string

All types that are more permissive than their ROS definition enforce the ROS constraints in range and length by software 

所有比ROS定義更寬松的類型都通過軟體強制執行ROS範圍和長度限制!

Example of message definition using arrays and bounded types: 使用數組和有界類型的消息定義示例:

int32[] unbounded_integer_array

int32[5] five_integers_array

int32[<=5] up_to_five_integers_array

string string_of_unbounded_size

string<=10 up_to_ten_characters_string

string[<=5] up_to_five_unbounded_strings

string<=10[] unbounded_array_of_string_up_to_ten_characters each

string<=10[<=5] up_to_five_strings_up_to_ten_characters_each

Field names must be lowercase alphanumeric characters with underscores for separating words. They must start with an alphabetic character, they must not end with an underscore and never have two consecutive underscores. 字段名稱必須是帶有下劃線的小寫字母數字字元,用于分隔單詞。它們必須以字母字元開頭,它們不能以下劃線結尾,也不能有兩個連續的下劃線。

Default values can be set to any field in the message type. Currently default values are not supported for string arrays and complex types (i.e. types not present in the built-in-types table above, that applies to all nested messages) 

可以将預設值設定為消息類型中的任何字段。目前字元串數組和複雜類型(即上面内置類型表中不存在的類型,不适用于所有嵌套消息)不支援預設值

Defining a default value is done by adding a third element to the field definition line, i.e: 

通過向字段定義行添加第三個元素來定義預設值,即:

fieldtype fieldname fielddefaultvalue

uint8 x 42

int16 y -2000

string full_name "John Doe"

int32[] samples [-200, -100, 0, 100, 200]

Note: 注意:

string values must be defined in single ' or double quotes " 字元串值必須用單引号'或雙引号"定義

currently string values are not escaped 目前字元串值不會被轉義

Each constant definition is like a field description with a default value, except that this value can never be changed programatically. This value assignment is indicated by use of an equal ‘=’ sign, e.g. 

每個常量定義類似于具有預設值的字段描述,但該值永遠不能以程式設計方式更改。通過使用等于'='的符号來完成該值指派,例如

constanttype CONSTANTNAME=constantvalue

int32 X=123

int32 Y=-123

string FOO="foo"

string EXAMPLE='bar'

Note 注意

Constants names have to be UPPERCASE 常量名稱必須是大寫的

Services description are defined in .srv files in the srv/ directory of a ROS package. 

服務描述在ROS包srv/目錄中的.srv檔案中定義。

A service description file consists of a request and a response msg type, separated by ‘—’. Any two .msg files concatenated together with a ‘—’ are a legal service description. 

服務描述檔案由請求和響應消息類型組成,以“ - ”分隔。任何兩個.msg檔案與' --- '連接配接在一起是合法的服務描述。

Here is a very simple example of a service that takes in a string and returns a string: 

下面是一個非常簡單的服務示例,它接受一個字元串并傳回一個字元串:

string str

---

We can of course get much more complicated (if you want to refer to a message from the same package you must not mention the package name): 

當然可以實作更複雜的(如果想引用來自同一個包的消息,不能提到包名):

#request constants

int8 FOO=1

int8 BAR=2

#request fields

int8 foobar

another_pkg/AnotherMessage msg

#response constants

uint32 SECRET=123456

#response fields

another_pkg/YetAnotherMessage val

CustomMessageDefinedInThisPackage value

uint32 an_integer

You cannot embed another service inside of a service. 

無法在服務中嵌入其他服務。