ROS Message 类型总结

ROS Message 类型总结

ROS使用简化的消息描述语言来描述ROS节点发布的数据(即消息)。消息描述存储在ROS包的msg /子目录中的.msg文件中。

.msg文件包含两部分:字段和常量。字段是在消息内部发送的数据。常数定义了可用于解释这些字段的有用值(例如,整数值类似枚举的常数)。

了解消息类型的方式为官方文档和API,通过官方文档可以查看详细说明,通过API可直接查看消息类型的定义。

内置类型1

这些内置类型在ROS中对应的包名为 std_msgs。内置类型与 C++ 和 Python 中的对应关系如下:

Primitive TypeSerializationC++Python2 / Python3
boolunsigned 8-bit intuint8_tbool
int8signed 8-bit intint8_tint
uint8unsigned 8-bit intuint8_tint
int16signed 16-bit intint16_tint
uint16unsigned 16-bit intuint16_tint
int32signed 32-bit intint32_tint
uint32unsigned 32-bit intuint32_tint
int64signed 64-bit intint64_tlong int
uint64unsigned 64-bit intuint64_tlong int
float3232-bit IEEE floatfloatfloat
float6464-bit IEEE floatdoublefloat
stringascii stringstd::stringstr bytes
timesecs/nsecs unsigned 32-bit intsros::Timerospy.Time
durationsecs/nsecs signed 32-bit intsros::Durationrospy.Duration

对于内置类型的数组的对应关系如下:

Primitive TypeSerializationC++Python2 / Python3
fixed-lengthno extra serializationboost::array<T, length>/std::vectortuple
variable-lengthuint32 length prefixstd::vectortuple
uint8[]see aboveas abovestr bytes
bool[]see abovestd::vector<uint8_t>list of bool

以上为ROS的消息内置类型,且据文档显示,ROS官方目前没有添加其它类型的打算。

常见类型2

除了内置类型,在 ROS 中已经将常用到的一些消息如传感器消息、导航消息等写成了不同的包,方便直接使用。common_msgs 是各种常用的消息包的集合,主要包括:

  1. actionlib_msgs
  2. diagnostic_msgs
  3. geometry_msgs
  4. nav_msgs
  5. sensor_msgs
  6. shape_msgs
  7. stereo_msgs
  8. trajectory_msgs
  9. 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
# )

  1. 内置类型 ↩︎

  2. 常见类型 ↩︎

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值