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...
About ROS 2 Interfaces 关于ROS 2接口
Table of Contents 目录
2. Message Description Specification 消息说明规范
2.1 Fields 字段
2.1.1 Field Types 字段类型
2.1.2 Field Names 字段名称
2.1.3 Field Default Value 字段默认值
3. Service Description Specification 服务描述规范
1. Background 背景
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文件。
2. Message Description Specification
消息说明规格
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文件由两部分组成:字段和常量。
2.1 Fields 字段
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 |
|||
bool |
bool |
builtins.bool |
boolean |
byte |
uint8_t |
builtins.bytes* |
octet |
char |
char |
builtins.str* |
char |
float32 |
float |
builtins.float* |
float |
float64 |
double |
builtins.float* |
double |
int8 |
int8_t |
builtins.int* |
octet |
uint8 |
uint8_t |
builtins.int* |
octet |
int16 |
int16_t |
builtins.int* |
short |
uint16 |
uint16_t |
builtins.int* |
unsigned short |
int32 |
int32_t |
builtins.int* |
long |
uint32 |
uint32_t |
builtins.int* |
unsigned long |
int64 |
int64_t |
builtins.int* |
long long |
uint64 |
uint64_t |
builtins.int* |
unsigned long long |
string |
std::string |
builtins.str |
string |
Every built-in-type can be used to define arrays: 每个内置类型都可用于定义数组:
Type name |
|||
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> |
builtins.list* |
sequence<T, N> |
bounded string |
std::string |
builtins.str* |
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. 字段名称必须是带有下划线的小写字母数字字符,用于分隔单词。它们必须以字母字符开头,它们不能以下划线结尾,也不能有两个连续的下划线。
2.1.3 Field Default Value 字段默认值
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
For example: 例如:
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 当前字符串值不会被转义
2.2 Constants 常数
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
For example: 例如:
int32 X=123
int32 Y=-123
string FOO="foo"
string EXAMPLE='bar'
Note 注意
Constants names have to be UPPERCASE 常量名称必须是大写的
3. Service Description Specification
服务描述规范
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
---
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.
无法在服务中嵌入其他服务。
文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。
原文链接:zhangrelay.blog.csdn.net/article/details/100773009
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)