【ROS2原理13】IDL接口映射

发布于:2023-01-16 ⋅ 阅读:(1022) ⋅ 点赞:(0)

1 提要:

        何为IDL,就是(Interface Definition and Language )的缩写,​​​ 这描述了使用接口定义语言 (IDL) 的子集定义接口。本文确定了接口定义语言 (IDL) 的一个子集,可用于描述组件之间的接口。此外,它还描述了如何使用接口在 C、C++ 和 Python 中生成代码。

        所谓接口(Interface)就是两个环境之间需要交换的数据转化约定。

2 支持的 IDL 子集

        ROS 2 支持 OMG IDL 4.2 规范的一个子集。目前可能不支持下面未列出的任何内容(例如枚举)。

2.1 词汇约定

1) Comments注释

        支持行注释 (//) 和块注释 (/* ... */)。

2)dentifier 标识符

        标识符必须以 ASCII 字母字符开头,后跟任意数量的 ASCII 字母、数字和下划线 (_) 字符。

3)数类型  :

  • Integer   整数
  • Character  字符
  • String       字符串
  • Floating-point   浮点数
  • Fixed-point      定点数

2.2 预处理(Preprocessing)

        目前在读取 .idl 文件时没有进行预处理。

2.3 构建块(Building Blocks)

1)包含 Imports

        必须使用导入来引用声明此 .idl 文件中使用的数据类型的其他 .idl 文件。

2)模块Modules

        每个模块必须至少包含一个定义,并且它们可以嵌套。对于 ROS 接口,第一个模块级别通常表示包名称,第二个模块级别区分接口的类型(msg、srv、action)。

3)常量 Constants

4)结构 Structures

        一个结构必须至少包含一个成员。

三、基本数字类型

3.1 整数 Integer Types

IDL type Value range
short -2^15 … 2^15 - 1
unsigned short 0 … 2^16 - 1
long -2^31 … 2^31 - 1
unsigned long 0 … 2^32 - 1
long long -2^63 … 2^63 - 1
unsigned long long 0 … 2^64 - 1

3.2 浮点数 Floating-Point Types

IDL type Format
float IEEE single-precision floating point number
double IEEE double-precision floating point number
long double IEEE double-extended floating-point number

3.3 字符类型Char Type

IDL type Value range
char A 8-bit single-byte character with a numerical value between 0 and 255 (see 7.2.6.2.1)

        该类型可以存储来自任何面向字节的代码集的单字节字符,或者当在数组中使用时,对来自多字节代码集的多字节字符进行编码。

3.4 宽字符类型( Wide Char Type)

IDL type Value range
wchar A 16-bit wide character

        虽然 IDL 规范仅将 wchar 的大小定义为依赖于实现,但 DDS-XTypes 规范 1.2 将其定义为 16 位。

3.5 布尔类型 Boolean Type

IDL type Value range
boolean One of the values TRUE and FALSE

3.6 八位字节类型( Octet Types)

IDL type Value range
octet opaque 8-bit

3.7  8-bits Integers

IDL type Value range
int8 -2^7 … 2^7 - 1
uint8 0 … 2^8 - 1

3.8 明确名称整数( Explicitly-named Integer Types)

IDL type Equivalent IDL type
int16 short
uint16 unsigned short
int32 long
uint32 unsigned long
int64 long long
uint64 unsigned long long

3.9 模板类Template Types

1)序列 Sequences

IDL type Value range
sequence<type_spec> sequence of items of the specific type_spec
the sequence is unbounded and no maximum size is specified
sequence<type_spec, N> sequence of of up to N items of the specified type_spec
the sequence is bounded and contain between 0 and N items

2)字符串 Strings

IDL type Value range
string sequence of char except null

3)宽字符串Wstrings

IDL type Value range
wstring sequence of wchar except null

4)结构类型Constructed Types

  •  Structures:结构最少要有一个元素.

5)枚举 Enumerations

        枚举类型由枚举数的有序列表组成。

6)数组Arrays

        多维、固定大小的数组由每个项目的类型和每个维度的显式大小定义。不过,目前只支持一维、固定大小的数组。

7)注释
        支持任意注释的语法。如何处理每种注释类型取决于下面描述的代码生成。

四、代码生成(Code Generation)

4.1约束检查( Constraint Checking)

        为消息生成的代码只保证在消息发布时强制执行约束。例如,当限制为固定大小数组的字段分配有错误大小的数组时,可能不会出现错误。这可能在客户端库实现中不一致,具体取决于语言特性和性能成本。将来,任何客户端库都可以在设置或修改字段时添加额外的约束检查。生成的消息代码的用户应该假设可以随时强制执行约束以与此类更改兼容。

4.2 类型映射(Type Mapping)

下表定义了 IDL 类型如何映射到以下编程语言:

  • C11 (or higher)
  • C++11 (or higher)
  • Python 3 (or higher)
IDL type C type C++ type Python type
float float float float
double double double float
long double long double long double2 float
char unsigned char unsigned char str with length 1
wchar char16_t char16_t str with length 1
boolean _Bool bool bool
octet unsigned char std::byte1 bytes with length 1
int8 int8_t int8_t int
uint8 uint8_t uint8_t int
int16 int16_t int16_t int
uint16 uint16_t uint16_t int
int32 int32_t int32_t int
uint32 uint32_t uint32_t int
int64 int64_t int64_t int
uint64 uint64_t uint64_t int
  1. 如果 std::byte 不可用,则使用 unsigned char 代替。
  2. 用户应根据自己选择的中间件和平台研究对 long double 的支持。例如,使用 Visual Studio 时只有 64 位。

        下表定义了 IDL 模板化和构造类型如何映射到编程语言(除非下面另有说明)。当指定 T 是上述类型之一或 IDL 结构时。 N 是有界类型的上限。

IDL type C type C++ type Python type
T[N] T[N] std::array<T, N> list
sequence<T> struct {size_t, T * } std::vector<T> list
sequence<T, N> struct {size_t, T * }, size_t N std::vector<T> list
string char * std::string str
string<N> char * std::string str
wstring char16_t * std::u16string str
wstring<N> char16_t * std::u16string str

这些数组和序列类型具有特殊的映射。如果单元格为空白,则使用默认映射。

IDL type C type C++ type Python type
T[N] for numeric types T:
float, double,
int8, uint8,
int16, uint16,
int32, uint32,
int64, uint64
- - numpy.ndarray(
  shape=(N, ),
  dtype=numpy.DT)
where DT is determined by T:
float -> float32,
double -> float64,
intX -> intX,
uintX -> uintX
sequence<T>
sequence<T, N>
for numeric types T:
float, double,
int8, uint8,
int16, uint16,
int32, uint32,
int64, uint64
- - array.array(typecode=TC) where TC is determined by T:
float -> f,
double -> d,
int8 -> b, uint8 -> B,
int16 -> h, uint16 -> H,
int32 -> l, uint32 -> L,
int64 -> q, uint64 -> Q
octet[N] - - bytes
sequence<octet> - - bytes
sequence<octet, N> - - bytes

4.3 标准化注释

1)  @key Annotation

        虽然关键注释不会直接影响生成的 ROS 数据类型,但它正在传递给 (DDS) 供应商特定的代码生成器。

2) @default Annotation

        默认值用于初始化结构成员。

3)@verbatim Annotation

        当值注释作为语言参数传递时,文本参数用作跨所有支持的编程语言的注释元素的文档块。

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到