• 【ROS2原理】IDL接口映射


    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 typeValue range
    short-2^15 … 2^15 - 1
    unsigned short0 … 2^16 - 1
    long-2^31 … 2^31 - 1
    unsigned long0 … 2^32 - 1
    long long-2^63 … 2^63 - 1
    unsigned long long0 … 2^64 - 1

    3.2 浮点数 Floating-Point Types

    IDL typeFormat
    floatIEEE single-precision floating point number
    doubleIEEE double-precision floating point number
    long doubleIEEE double-extended floating-point number

    3.3 字符类型Char Type

    IDL typeValue range
    charA 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 typeValue range
    wcharA 16-bit wide character

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

    3.5 布尔类型 Boolean Type

    IDL typeValue range
    booleanOne of the values TRUE and FALSE

    3.6 八位字节类型( Octet Types)

    IDL typeValue range
    octetopaque 8-bit

    3.7  8-bits Integers

    IDL typeValue range
    int8-2^7 … 2^7 - 1
    uint80 … 2^8 - 1

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

    IDL typeEquivalent IDL type
    int16short
    uint16unsigned short
    int32long
    uint32unsigned long
    int64long long
    uint64unsigned long long

    3.9 模板类Template Types

    1)序列 Sequences

    IDL typeValue range
    sequencesequence of items of the specific type_spec
    the sequence is unbounded and no maximum size is specified
    sequencesequence 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 typeValue range
    stringsequence of char except null

    3)宽字符串Wstrings

    IDL typeValue range
    wstringsequence 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 typeC typeC++ typePython type
    floatfloatfloatfloat
    doubledoubledoublefloat
    long doublelong doublelong double2float
    charunsigned charunsigned charstr with length 1
    wcharchar16_tchar16_tstr with length 1
    boolean_Boolboolbool
    octetunsigned charstd::byte1bytes with length 1
    int8int8_tint8_tint
    uint8uint8_tuint8_tint
    int16int16_tint16_tint
    uint16uint16_tuint16_tint
    int32int32_tint32_tint
    uint32uint32_tuint32_tint
    int64int64_tint64_tint
    uint64uint64_tuint64_tint
    1. 如果 std::byte 不可用,则使用 unsigned char 代替。
    2. 用户应根据自己选择的中间件和平台研究对 long double 的支持。例如,使用 Visual Studio 时只有 64 位。

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

    IDL typeC typeC++ typePython type
    T[N]T[N]std::arraylist
    sequencestruct {size_t, T * }std::vectorlist
    sequencestruct {size_t, T * }, size_t Nstd::vectorlist
    stringchar *std::stringstr
    stringchar *std::stringstr
    wstringchar16_t *std::u16stringstr
    wstringchar16_t *std::u16stringstr

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

    IDL typeC typeC++ typePython 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
    sequence
    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--bytes
    sequence--bytes

    4.3 标准化注释

    1)  @key Annotation

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

    2) @default Annotation

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

    3)@verbatim Annotation

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

  • 相关阅读:
    利用Semaphore实现多线程调用接口A且限制接口A的每秒QPS为10
    Aspose.total 企业案例:W3K 支持使用 Aspose.Total for .NET API 将 CAD 绘图和 Office 文档转换为 PDF
    量化投资学习——股指期货研究(四)
    STM32实战总结:HAL之GUI
    【FreeRTOS】FreeRTOS删除任务vTaskDelete()
    我的创作纪念日
    【数据结构】线性表之顺序表
    20-SpringCloudAlibaba-2
    关于 arduino 中的 random()和randomSeed()的使用
    接口自动化测试如何实现?5个步骤轻松拿捏!
  • 原文地址:https://blog.csdn.net/gongdiwudu/article/details/126263555