ROS Message 类型总结
ROS使用简化的消息描述语言来描述ROS节点发布的数据(即消息)。消息描述存储在ROS包的msg /子目录中的.msg文件中。
.msg文件包含两部分:字段和常量。字段是在消息内部发送的数据。常数定义了可用于解释这些字段的有用值(例如,整数值类似枚举的常数)。
了解消息类型的方式为官方文档和API,通过官方文档可以查看详细说明,通过API可直接查看消息类型的定义。
内置类型1
这些内置类型在ROS中对应的包名为 std_msgs。内置类型与 C++ 和 Python 中的对应关系如下:
Primitive Type | Serialization | C++ | Python2 / Python3 |
---|---|---|---|
bool | unsigned 8-bit int | uint8_t | bool |
int8 | signed 8-bit int | int8_t | int |
uint8 | unsigned 8-bit int | uint8_t | int |
int16 | signed 16-bit int | int16_t | int |
uint16 | unsigned 16-bit int | uint16_t | int |
int32 | signed 32-bit int | int32_t | int |
uint32 | unsigned 32-bit int | uint32_t | int |
int64 | signed 64-bit int | int64_t | long int |
uint64 | unsigned 64-bit int | uint64_t | long int |
float32 | 32-bit IEEE float | float | float |
float64 | 64-bit IEEE float | double | float |
string | ascii string | std::string | str bytes |
time | secs/nsecs unsigned 32-bit ints | ros::Time | rospy.Time |
duration | secs/nsecs signed 32-bit ints | ros::Duration | rospy.Duration |
对于内置类型的数组的对应关系如下:
Primitive Type | Serialization | C++ | Python2 / Python3 |
---|---|---|---|
fixed-length | no extra serialization | boost::array<T, length>/std::vector | tuple |
variable-length | uint32 length prefix | std::vector | tuple |
uint8[] | see above | as above | str bytes |
bool[] | see above | std::vector<uint8_t> | list of bool |
以上为ROS的消息内置类型,且据文档显示,ROS官方目前没有添加其它类型的打算。
常见类型2
除了内置类型,在 ROS 中已经将常用到的一些消息如传感器消息、导航消息等写成了不同的包,方便直接使用。common_msgs 是各种常用的消息包的集合,主要包括:
- actionlib_msgs
- diagnostic_msgs
- geometry_msgs
- nav_msgs
- sensor_msgs
- shape_msgs
- stereo_msgs
- trajectory_msgs
- visualization_msgs
不同类型的具体使用和具体内容见文档和API,具体的实现为基本内置类型的组合。常见类型相当于基本内置类型组合的一个封装,消息类型的表示为包资源名称加上.msg文件的名称。例如,文件geometry_msgs / msg / Twist.msg通常表示为geometry_msgs / Twist。
geometry_msgs/Twist twist_example
自定义msg消息
1. 添加 .msg 文件
在Package中添加msg文件夹,且添加自定义的 .msg 文件,这种语言的格式很简单,在不同的行上,添加消息描述即可。
如:
int32 x
int32 y
2. package.xml
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
3. CMakeLists.txt
不用增加以下的内容,找到对应的代码块进行修改即可:
# Do not just add this line to your CMakeLists.txt, modify the existing line
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation)
catkin_package(
...
CATKIN_DEPENDS message_runtime ...
...)
以下两个步骤为取消原来的注释(去掉#),然后添加你自定义的 .msg 文件/依赖即可:
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )
# generate_messages(
# DEPENDENCIES
# std_msgs # Or other packages containing msgs
# )