2. 快速开始
2.1 三步使用
在 C++ 源文件中
#include对应消息头文件构造 C++ struct,填充字段
通过 Autolink 发布/订阅时,在边界调用
ToProto/FromProto
commsgs 无独立 CMake 目标,随
libautonomy一并编译安装;无需额外配置。
2.2 构造并填充消息
#include "autonomy/commsgs/geometry_msgs.hpp"
#include "autonomy/commsgs/planning_msgs.hpp"
#include "autonomy/commsgs/std_msgs.hpp"
using namespace autonomy::commsgs;
// 构造 PoseStamped
geometry_msgs::PoseStamped pose;
pose.header.stamp = builtin_interfaces::Time::Now();
pose.header.frame_id = "map";
pose.pose.position.x = 1.0;
pose.pose.position.y = 2.0;
pose.pose.orientation.w = 1.0;
// 构造 Path
planning_msgs::Path path;
path.header = pose.header;
path.poses.push_back(pose);
2.3 Autolink 发布(Writer)
#include "autonomy/commsgs/sensor_msgs.hpp"
auto writer = node->CreateWriter<commsgs::sensor_msgs::Imu>("/sensor/imu");
commsgs::sensor_msgs::Imu imu;
imu.header.stamp = commsgs::builtin_interfaces::Time::Now();
imu.header.frame_id = "imu_link";
imu.angular_velocity.z = 0.1;
imu.linear_acceleration.x = 9.8;
writer->Write(imu); // 框架内部处理序列化
2.4 Autolink 订阅(Reader)
auto callback = [](const std::shared_ptr<commsgs::map_msgs::OccupancyGrid>& map) {
const auto& info = map->info;
// 使用 map->data 访问栅格数据
};
auto reader = node->CreateReader<commsgs::map_msgs::OccupancyGrid>("/map", callback);
2.5 Service 边界转换
PlannerServer 在 Autolink 服务边界使用 proto 类型,内部转换为 C++ struct:
#include "autonomy/commsgs/planning_msgs.hpp"
namespace nav_proto = commsgs::proto::nav_msgs;
// 服务回调:request 为 proto,内部转 C++
const auto path = commsgs::planning_msgs::FromProto(request->path());
bool valid = planner->IsPathValid(path);
response->set_is_valid(valid);
2.6 手动 Proto 序列化
#include "autonomy/commsgs/geometry_msgs.hpp"
commsgs::geometry_msgs::PoseStamped pose;
// ... 填充 pose
// C++ → Proto → 字节流
auto proto = commsgs::geometry_msgs::ToProto(pose);
std::string bytes;
proto.SerializeToString(&bytes);
// 字节流 → Proto → C++
commsgs::proto::geometry_msgs::PoseStamped proto_in;
proto_in.ParseFromString(bytes);
auto pose_out = commsgs::geometry_msgs::FromProto(proto_in);
2.7 PointCloud2 快速示例
#include "autonomy/commsgs/sensor_msgs.hpp"
#include "autonomy/commsgs/point_cloud2_iterator.hpp"
commsgs::sensor_msgs::PointCloud2 cloud;
commsgs::sensor_msgs::PointCloud2Modifier modifier(cloud);
modifier.setPointCloud2FieldsByString(3, "x", "y", "z");
modifier.resize(100);
commsgs::sensor_msgs::PointCloud2Iterator<float> iter_x(cloud, "x");
commsgs::sensor_msgs::PointCloud2Iterator<float> iter_y(cloud, "y");
commsgs::sensor_msgs::PointCloud2Iterator<float> iter_z(cloud, "z");
for (size_t i = 0; i < 100; ++i, ++iter_x, ++iter_y, ++iter_z) {
*iter_x = static_cast<float>(i);
*iter_y = 0.0f;
*iter_z = 0.0f;
}
2.8 头文件速查
需求 |
头文件 |
|---|---|
时间戳 |
|
Header / 颜色 |
|
位姿 / 速度 / TF |
|
传感器数据 |
|
地图 / 代价地图 |
|
路径 / 里程计 |
|
Nav2 Action proto |
|
错误码 |
|
PointCloud2 工具 |
|