古月21讲/2.6_通信接口
官方文档:Interfaces — ROS 2 Documentation: Humble documentation
官方接口代码实战:https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Single-Package-Define-And-Use-Interface.html
ROS 2使用简化的描述语言,即接口定义语言( interface definition language,IDL)来描述这些接口。此描述使ROS工具可以轻松地以几种目标语言为接口类型生成源代码 (自动跨语言)。。不同通信接口,接口文件后缀不同,
话题(.msg),
服务(.srv),
动作(.action)
接口语言定义语法
所有的语言,都有统一的内置类型(built-in-type)表示
也可单独定义类型名称,例如“ geometry_msgs/posestamped”
内部固有类型 (与跨语言对应关系)
Type name | C++ | Python | DDS type |
---|---|---|---|
bool | bool | builtins.bool | boolean |
byte | uint8_t | builtins.bytes* | octet |
char | char | builtins.int* | 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 |
wstring | std::u16string | builtins.str | wstring |
话题(topics 发布/订阅信息模型)
消息是ROS 2节点将网络上的数据发送到其他ROS节点的一种方式,而没有响应(单向信息传递)。例如,如果一个ROS 2节点从传感器读取温度数据,则可以使用温度消息在ROS 2网络上发布该数据。 ROS 2网络上的其他节点可以订阅该数据并接收温度消息。
每个字段由一个类型和名称组成,被空格隔开,即
fieldtype1 fieldname1
fieldtype2 fieldname2
例如
int32 my_int
string my_string
带有默认值的示例
uint8 x 42
int16 y -2000
string full_name "John Doe"
int32[] samples [-200, -100, 0, 100, 200]
常数示例,无法更改(有=号)
int32 X=123
int32 Y=-123
string FOO="foo"
string EXAMPLE='bar'
服务接口
在ROS软件包的 srv/ 路径中,在.srv文件中描述和定义了服务。
服务描述文件由请求和响应MSG类型组成,该类型由使用 --- 区分
语法
string str
---
string str
示例
# 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
动作通信接口
有三个参数需要定义
使用2个 --- 和 --- 区分
<request_type> <request_fieldname>
---
<response_type> <response_fieldname>
---
<feedback_type> <feedback_fieldname>
示例
int32 order
---
int32[] sequence
---
int32[] sequence