6. 核心消息类型
本文详述 builtin_interfaces、std_msgs、geometry_msgs 三个基础消息包。
6.1 builtin_interfaces
头文件:autonomy/commsgs/builtin_interfaces.hpp
时间与时长是全部 Stamped 消息的基础。
6.1.1 Time
字段 |
类型 |
说明 |
|---|---|---|
|
|
Unix epoch 秒 |
|
|
纳秒分量 [0, 10⁹) |
方法 |
说明 |
|---|---|
|
当前系统时间 |
|
合并为 64 位纳秒 |
|
浮点秒 |
|
总纳秒(32 位,注意溢出) |
|
边界值 |
auto t = commsgs::builtin_interfaces::Time::Now();
auto t2 = commsgs::builtin_interfaces::Time(100, 500000000); // 100.5s
6.1.2 Duration
字段 |
类型 |
说明 |
|---|---|---|
|
|
内部纳秒存储 |
支持与 std::chrono::duration 互转,提供 + - * 运算符(带溢出检查)。
auto d = commsgs::builtin_interfaces::Duration::FromSeconds(1.5);
auto elapsed = commsgs::builtin_interfaces::Time::Now() - start_time;
6.2 std_msgs
头文件:autonomy/commsgs/std_msgs.hpp
6.2.1 Header
字段 |
类型 |
说明 |
|---|---|---|
|
|
时间戳 |
|
|
坐标系名称 |
所有 *Stamped 消息的公共前缀。
6.2.2 其他类型
类型 |
字段 |
用途 |
|---|---|---|
|
|
可视化颜色 |
|
|
字符串包装 |
|
|
多维浮点数组 |
|
|
数组布局描述 |
|
|
单维描述 |
MultiArray*系列自 Foxy 起标记为 deprecated,建议使用语义化消息替代。
6.2.3 转换完整度
类型 |
ToProto |
FromProto |
|---|---|---|
|
✅ |
✅ |
|
✅ |
✅ |
|
✅ |
✅ |
|
✅ |
✅ |
6.3 geometry_msgs
头文件:autonomy/commsgs/geometry_msgs.hpp
导航栈中使用最频繁的消息包。
6.3.1 基础向量与点
类型 |
字段 |
语义 |
|---|---|---|
|
|
自由向量(锚定原点,变换时仅旋转) |
|
|
空间点(变换时平移+旋转) |
|
|
32 位精度点 |
|
|
四元数旋转 |
|
|
多边形顶点 |
6.3.2 位姿与运动
类型 |
组成 |
用途 |
|---|---|---|
|
|
6-DOF 位姿 |
|
|
平面位姿 |
|
|
6-DOF 速度 |
|
|
平面速度 |
|
|
加速度 |
|
|
力/力矩 |
6.3.3 Stamped 变体
类型 |
额外字段 |
典型 topic |
|---|---|---|
|
|
|
|
|
平面目标 |
|
|
|
|
|
平面速度命令 |
|
|
标注点 |
|
|
方向 |
|
|
力/加速度向量 |
6.3.4 协方差变体
类型 |
协方差大小 |
顺序 |
|---|---|---|
|
36 (6×6) |
x, y, z, roll, pitch, yaw |
|
36 (6×6) |
同上 |
|
36 (6×6) |
同上 |
对应 *Stamped 变体:PoseWithCovarianceStamped、TwistWithCovarianceStamped 等。
6.3.5 变换
类型 |
字段 |
说明 |
|---|---|---|
|
|
刚体变换 |
|
|
TF 边 |
|
|
批量变换 |
|
|
体坐标系速度 |
TF 约定:
header.frame_id = 父坐标系(reference frame)
child_frame_id = 子坐标系(target frame)
transform = 从 child 到 parent 的变换
6.3.6 集合类型
类型 |
组成 |
用途 |
|---|---|---|
|
|
粒子滤波器粒子 |
|
|
平面粒子 |
6.3.7 Apollo 扩展
类型 |
字段 |
用途 |
|---|---|---|
|
|
UTM/ENU 坐标 |
|
|
经纬高(WGS84) |
|
|
刚体惯性张量 |
|
|
带时间戳惯性 |
6.3.8 转换完整度
类别 |
完整度 |
备注 |
|---|---|---|
Vector3 / Point / Quaternion |
✅ 完整 |
|
Pose / Twist / Transform |
✅ 完整 |
|
*Stamped 系列 |
✅ 大部分完整 |
|
*WithCovariance |
✅ 完整 |
|
Polygon |
⏳ 部分 |
|
PoseArray |
⏳ 部分 |
header 转换待补全 |
PointENU / PointLLH |
✅ 完整 |
6.4 使用频率排序
导航栈中最常用的 geometry_msgs 类型:
优先级 |
类型 |
使用模块 |
|---|---|---|
★★★ |
|
planning, control, navigator |
★★★ |
|
control |
★★★ |
|
transform, localization |
★★☆ |
|
checker, 几何计算 |
★★☆ |
|
localization, odom |
★☆☆ |
|
costmap 禁区 |
★☆☆ |
|
高精地图 |
6.5 代码示例:完整位姿链路
// 1. 里程计位姿(带协方差)
commsgs::geometry_msgs::PoseWithCovarianceStamped loc;
loc.header.stamp = commsgs::builtin_interfaces::Time::Now();
loc.header.frame_id = "map";
loc.pose.pose.position.x = 1.0;
loc.pose.covariance.resize(36, 0.0);
loc.pose.covariance[0] = 0.01; // x 方差
loc.pose.covariance[7] = 0.01; // y 方差
loc.pose.covariance[35] = 0.05; // yaw 方差
// 2. 提取 PoseStamped 用于规划
commsgs::geometry_msgs::PoseStamped current;
current.header = loc.header;
current.pose = loc.pose.pose;
// 3. 计算速度命令
commsgs::geometry_msgs::TwistStamped cmd;
cmd.header.stamp = commsgs::builtin_interfaces::Time::Now();
cmd.header.frame_id = "base_link";
cmd.twist.linear.x = 0.5;
cmd.twist.angular.z = 0.1;