1 接口的数据类型

类型名称 C++ Python DDS 类型
bool bool bool boolean
byte uint8_t bytes octet
char char str char
float32 float float float
float64 double float double
int8 int8_t int octet
uint8 uint8_t int octet
int16 int16_t int short
uint16 uint16_t int unsigned short
int32 int32_t int long
uint32 uint32_t int unsigned long
int64 int64_t int long long
uint64 uint64_t int unsigned long long
string std::string str string
wstring std::u16string str wstring

1.1 命名规则

1.字段名称必须是小写字母和数字字符,并带有下划线以分隔单词。
2.字段名称必须以字母字符开头,不能以下划线结尾,且永远不能有两个连续的下划线。

1.2 设置默认值

uint8 x 42
int16 y -2000
string full_name "John Doe"
int32[] samples [-200, -100, 0, 100, 200]

1.3 设置常量

int32 X=123
int32 Y=-123
string FOO="foo"
string EXAMPLE='bar'

2 创建接口包

ros2 pkg create --build-type ament_cmake tutorial_interfaces

3 创建接口目录

mkdir msg
mkdir srv
mkdir action

4 定义接口

1.msg 目录下的 Num.msg 文件

int num

2.srv 目录下的 AddThreeInts.srv 文件

int64 a
int64 b
int64 c
---
int64 sum

3.action 目录下的 Fibonacci.action 文件

int32 order
---
int32[] sequence
---
int32[] partial_sequence

5 CMakeLists.txt 构建

find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "msg/Num.msg"
  "srv/AddThreeInts.srv"
  "action/Fibonacci.action"
)

6 package.xml 添加依赖

<build_depend>rosidl_default_generators</build_depend>

<exec_depend>rosidl_default_runtime</exec_depend>

<depend>action_msgs</depend>

<member_of_group>rosidl_interface_packages</member_of_group>

7 构建软件包

colcon build --packages-select tutorial_interfaces

results matching ""

    No results matching ""