ROS220-ROS2工具:坐标变换(二)内部常用接口消息
2026/1/28...大约 2 分钟
简介
由上一篇文章所述来看,我们不难发现对于坐标变换的实现,其本质是基于话题通信的发布订阅模型。发布方可以发布坐标系之间的相对关系,订阅方则可以监听这些消息,并实现不同坐标系之间的变换。根据“ROS2-004-通信机制:话题通信” 文章内针对消息接口的介绍,在话题通信中,接口消息 作为数据载体在整个通信模型中是比较重要的一部分。
本章将会介绍坐标变换中常用的两种接口消息:geometry_msgs/msg/TransformStamped 和 geometry_msgs/msg/PointStamped。
前者用于描述某一时刻两个坐标系之间相对关系的接口,后者用于描述某一时刻坐标系内某个坐标点的位置的接口。在之后的坐标变换中,我们会经常性的使用到坐标系相对关系以及坐标点相关信息。
具体定义格式
我们可以使用 ros2 interface show 指令来确认这两个接口的相关定义:
ros2 interface show geometry_msgs/msg/TransformStamped
ros2 interface show geometry_msgs/msg/PointStamped相关定义如下所示:
TransformStamped
# This expresses a transform from coordinate frame header.frame_id
# to the coordinate frame child_frame_id at the time of header.stamp
#
# This message is mostly used by the
# <a href="https://docs.ros.org/en/rolling/p/tf2/">tf2</a> package.
# See its documentation for more information.
#
# The child_frame_id is necessary in addition to the frame_id
# in the Header to communicate the full reference for the transform
# in a self contained message.
# The frame id in the header is used as the reference frame of this transform.
std_msgs/Header header
builtin_interfaces/Time stamp # Time stamp
int32 sec
uint32 nanosec
string frame_id
# The frame id of the child frame to which this transform points.
string child_frame_id
# Translation and rotation in 3-dimensions of child_frame_id from header.frame_id.
Transform transform
Vector3 translation
float64 x
float64 y
float64 z
Quaternion rotation
float64 x 0
float64 y 0
float64 z 0
float64 w 1注:这里的Quaternion rotation 指的是 四元数旋转,其虽与欧拉角的描述不太相同,但是能够能高效的描述旋转问题。在 TF2 内部有集成两种不同描述方式的转换函数,因此在坐标系旋转时输入欧拉角即可,无需手动换算成四元数。
PointStamped
# This represents a Point with reference coordinate frame and timestamp
std_msgs/Header header
builtin_interfaces/Time stamp
int32 sec
uint32 nanosec
string frame_id
Point point
float64 x
float64 y
float64 z