feat(Point-LIO): 累积全景点云同时携带各点的原始光心坐标

新增 publishAccumulatedFullCloudWorldLidar():一条 PointCloud2 同时装
x,y,z(世界系)与 lidar_x,lidar_y,lidar_z(同一批点的原始光心坐标),
point_step = 6*sizeof(float),顺序与 full_cloud_world 严格锁步。

为什么必须成对:障碍分支要在光心坐标上做自身回波判断,
用里程计位姿把世界点反变换回雷达系、或事后做最近邻匹配,都不被允许。
数量不匹配时只 RCLCPP_ERROR 并放弃发布,不做静默降级。
This commit is contained in:
walunt5
2026-11-05 11:58:47 +08:00
parent 5a67417fbe
commit f6bbae78bd
@@ -13,6 +13,7 @@
#include <tf2_ros/transform_listener.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <cmath>
@@ -31,6 +32,7 @@
#include "li_initialization.h"
#include "runtime_statistics.h"
#include "sensor_time_rate_limiter.h"
#include "runtime_update_trace.h"
using namespace std;
@@ -94,6 +96,12 @@ std::deque<FullCloudPoint> full_cloud_queue;
bool has_full_cloud_anchor = false;
StateSnapshot full_cloud_anchor;
PointCloudXYZI::Ptr full_cloud_world(new PointCloudXYZI());
// Original optical-centre coordinates of the SAME points, pushed in lockstep with
// full_cloud_world. The obstacle branch classifies these directly instead of
// inverse-transforming the world point with an odometry pose.
std::vector<std::array<float, 3>> full_cloud_world_lidar;
// Set once at node initialisation; the publish helpers below are free functions.
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr full_cloud_lidar_pub;
uint64_t full_cloud_dropped_points_this_scan = 0;
uint64_t last_full_cloud_enqueued_points = 0;
uint64_t last_full_cloud_out_of_order = 0;
@@ -286,6 +294,8 @@ void processFullCloudSegmentWithSnapshot(
const PointType point_world = transformFullPointForward(full_cloud_anchor, full_point);
if (isFinitePoint(point_world)) {
full_cloud_world->push_back(point_world);
full_cloud_world_lidar.push_back({full_point.point.x, full_point.point.y,
full_point.point.z});
}
full_cloud_queue.pop_front();
}
@@ -294,6 +304,54 @@ void processFullCloudSegmentWithSnapshot(
full_cloud_anchor = current_snapshot;
}
// One message carrying world xyz plus the original optical-centre xyz of the very
// same point, in the same order. The obstacle branch needs that pairing; deriving
// the lidar coordinate afterwards by nearest-neighbour matching is not allowed.
void publishAccumulatedFullCloudWorldLidar(
const rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr & pub,
double stamp)
{
if (!pub || !full_cloud_world || full_cloud_world->empty()) {
return;
}
const size_t count = full_cloud_world->size();
if (full_cloud_world_lidar.size() != count) {
RCLCPP_ERROR(LOGGER,
"[Point-LIO][FullCloud] original-coordinate array has %zu entries for %zu points; "
"not publishing the paired cloud", full_cloud_world_lidar.size(), count);
return;
}
sensor_msgs::msg::PointCloud2 cloud;
cloud.header.stamp = get_ros_time(stamp);
cloud.header.frame_id = "camera_init";
cloud.height = 1;
cloud.width = static_cast<uint32_t>(count);
cloud.is_bigendian = false;
cloud.is_dense = false;
cloud.point_step = 6 * sizeof(float);
cloud.row_step = cloud.point_step * cloud.width;
const char * names[6] = {"x", "y", "z", "lidar_x", "lidar_y", "lidar_z"};
cloud.fields.resize(6);
for (size_t i = 0; i < 6; ++i) {
cloud.fields[i].name = names[i];
cloud.fields[i].offset = static_cast<uint32_t>(i * sizeof(float));
cloud.fields[i].datatype = sensor_msgs::msg::PointField::FLOAT32;
cloud.fields[i].count = 1;
}
cloud.data.resize(cloud.row_step);
float * out = reinterpret_cast<float *>(cloud.data.data());
for (size_t i = 0; i < count; ++i) {
const auto & world = full_cloud_world->points[i];
out[i * 6 + 0] = world.x;
out[i * 6 + 1] = world.y;
out[i * 6 + 2] = world.z;
out[i * 6 + 3] = full_cloud_world_lidar[i][0];
out[i * 6 + 4] = full_cloud_world_lidar[i][1];
out[i * 6 + 5] = full_cloud_world_lidar[i][2];
}
pub->publish(cloud);
}
void publishAccumulatedFullCloudWorld(
const rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr & pub,
double stamp)
@@ -323,7 +381,10 @@ void publishAccumulatedFullCloudWorld(
static_cast<unsigned long long>(last_full_cloud_out_of_order));
}
publishAccumulatedFullCloudWorldLidar(full_cloud_lidar_pub, stamp);
full_cloud_world->clear();
full_cloud_world_lidar.clear();
full_cloud_dropped_points_this_scan = 0;
}
@@ -486,6 +547,57 @@ void publish_frame_body(
pubLaserCloudFull_body->publish(laserCloudmsg);
}
// ---- default-off update trace -------------------------------------------
// File-scope so publish_odometry() can reach it; scan/batch ids are advanced
// once per scan and once per point batch inside processingLoop().
point_lio::RuntimeUpdateTrace g_update_trace;
uint64_t g_trace_scan_id = 0;
uint64_t g_trace_batch_id = 0;
void trace_fill(const state_output & s, double * pos, double * vel)
{
for (int i = 0; i < 3; ++i) {
pos[i] = s.pos(i);
vel[i] = s.vel(i);
}
}
// Only an actually published message is recorded, so this runs after the rate
// limiter has let the publish through.
void trace_publish(const nav_msgs::msg::Odometry & odom, double t)
{
if (!g_update_trace.enabled()) {
return;
}
point_lio::RuntimeUpdateTrace::Record r;
r.event = point_lio::RuntimeUpdateTrace::kPublish;
r.scan_id = g_trace_scan_id;
r.batch_id = g_trace_batch_id;
r.t_before = t;
r.t_after = t;
r.dt = 0.0;
r.ok = 1;
r.state_dof = static_cast<int>(state_output::DOF);
r.pos_before[0] = r.pos_after[0] = odom.pose.pose.position.x;
r.pos_before[1] = r.pos_after[1] = odom.pose.pose.position.y;
r.pos_before[2] = r.pos_after[2] = odom.pose.pose.position.z;
// publish_odometry() has already rotated the velocity into the body frame, but
// the CSV declares world. Rotate it back with the reported orientation instead
// of relabelling a body-frame value.
const double q[4] = {odom.pose.pose.orientation.x, odom.pose.pose.orientation.y,
odom.pose.pose.orientation.z, odom.pose.pose.orientation.w};
const double v_body[3] = {odom.twist.twist.linear.x, odom.twist.twist.linear.y,
odom.twist.twist.linear.z};
double v_world[3] = {0.0, 0.0, 0.0};
point_lio::rotate_body_to_world(q, v_body, v_world);
for (int i = 0; i < 3; ++i) {
r.vel_before[i] = v_world[i];
r.vel_after[i] = v_world[i];
}
g_update_trace.record(r);
}
template <typename T> void set_posestamp(T & out)
{
// Static variable, initialized to true, only effective on the first call
@@ -638,6 +750,7 @@ void publish_odometry(const rclcpp::Publisher<nav_msgs::msg::Odometry>::SharedPt
}
pubOdomAftMapped->publish(odomAftMapped);
trace_publish(odomAftMapped, odom_sensor_time);
const double publish_time =
std::chrono::duration<double>(std::chrono::system_clock::now().time_since_epoch()).count();
point_lio::RuntimeStatistics::instance().recordOdomPublish(odom_sensor_time, publish_time);
@@ -730,7 +843,21 @@ public:
~LaserMappingNode() override
{
// Order matters: no thread may still be recording when the trace is closed.
stopWorker();
g_update_trace.shutdown();
if (g_update_trace.attempted() > 0) {
RCLCPP_INFO(get_logger(),
"[Point-LIO][UpdateTrace] stopped: attempted=%llu written=%llu dropped=%llu "
"(contended=%llu full=%llu) write_failures=%llu incomplete=%d",
static_cast<unsigned long long>(g_update_trace.attempted()),
static_cast<unsigned long long>(g_update_trace.written()),
static_cast<unsigned long long>(g_update_trace.dropped()),
static_cast<unsigned long long>(g_update_trace.contended_drops()),
static_cast<unsigned long long>(g_update_trace.full_drops()),
static_cast<unsigned long long>(g_update_trace.write_failures()),
static_cast<int>(g_update_trace.output_incomplete()));
}
if (!pcl_wait_save->empty() && pcd_save_en) {
string file_name = string("scans.pcd");
string all_points_dir(string(string(ROOT_DIR) + "PCD/") + file_name);
@@ -810,6 +937,27 @@ private:
log_dir.c_str());
runtime_pos_log = false;
}
// Default-off update trace. Silently absent unless the parameter is set, and
// a failure here only disables the trace, never the filter.
g_update_trace.set_error_sink([](const std::string & message) {
RCLCPP_ERROR(LOGGER, "[Point-LIO][UpdateTrace] %s", message.c_str());
});
if (runtime_update_trace_enable && use_imu_as_input) {
// The trace records the 30-dimension output-state filter. Silently
// reporting state_dof=30 while the 24-dimension input filter runs would be
// wrong, so refuse instead.
RCLCPP_ERROR(LOGGER,
"[Point-LIO][UpdateTrace] use_imu_as_input=true is not supported by the "
"trace; trace disabled, filtering continues");
} else if (runtime_update_trace_enable) {
if (g_update_trace.initialize(true, log_dir)) {
RCLCPP_INFO(LOGGER, "[Point-LIO][UpdateTrace] enabled -> %s",
g_update_trace.path().c_str());
} else {
RCLCPP_ERROR(LOGGER,
"[Point-LIO][UpdateTrace] could not start; trace disabled, filtering continues");
}
}
if (runtime_pos_log && print_cloud_input_fps) {
RCLCPP_INFO(get_logger(),
@@ -835,6 +983,10 @@ private:
pub_laser_cloud_full_res_ = create_publisher<sensor_msgs::msg::PointCloud2>("cloud_registered", 20);
pub_laser_cloud_full_world_ = create_publisher<sensor_msgs::msg::PointCloud2>(
"/cloud_registered_full", rclcpp::SensorDataQoS().keep_last(1));
// World xyz + original optical-centre xyz, same points, for the obstacle branch.
pub_laser_cloud_full_world_lidar_ = create_publisher<sensor_msgs::msg::PointCloud2>(
"/cloud_registered_full_lidar", rclcpp::SensorDataQoS().keep_last(1));
full_cloud_lidar_pub = pub_laser_cloud_full_world_lidar_;
pub_laser_cloud_full_res_body_ =
create_publisher<sensor_msgs::msg::PointCloud2>("cloud_registered_body", 20);
pub_laser_cloud_map_ = create_publisher<sensor_msgs::msg::PointCloud2>("Laser_map", 20);
@@ -869,6 +1021,7 @@ private:
rclcpp::Subscription<sensor_msgs::msg::Imu>::SharedPtr sub_imu_;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pub_laser_cloud_full_res_;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pub_laser_cloud_full_world_;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pub_laser_cloud_full_world_lidar_;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pub_laser_cloud_full_res_body_;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pub_laser_cloud_map_;
rclcpp::Publisher<nav_msgs::msg::Odometry>::SharedPtr pub_odom_aft_mapped_;
@@ -888,6 +1041,89 @@ private:
void LaserMappingNode::processingLoop()
{
rclcpp::Rate rate(500);
// ---- update trace wrappers ---------------------------------------------
// When the trace is off each wrapper is one relaxed atomic test followed by
// the original call: same arguments, same order, same return value.
auto trace_predict = [this](
double dt, bool predict_state, bool prop_cov, double t_ref) {
if (!g_update_trace.enabled()) {
kf_output.predict(dt, Q_output_, input_in, predict_state, prop_cov);
return;
}
point_lio::RuntimeUpdateTrace::Record r;
r.event = point_lio::RuntimeUpdateTrace::kPredict;
r.scan_id = g_trace_scan_id;
r.batch_id = g_trace_batch_id;
// Only a state prediction advances the state time. A covariance-only call
// leaves the state where it was; the dt is an explanatory field, not an
// interval over which the state moved.
if (predict_state) {
r.t_after = t_ref;
r.t_before = t_ref - dt;
} else {
r.t_before = t_ref;
r.t_after = t_ref;
}
r.dt = dt;
r.ok = 1;
r.state_dof = static_cast<int>(state_output::DOF);
r.predict_state = predict_state ? 1 : 0;
r.prop_cov = prop_cov ? 1 : 0;
trace_fill(kf_output.get_x(), r.pos_before, r.vel_before);
kf_output.predict(dt, Q_output_, input_in, predict_state, prop_cov);
trace_fill(kf_output.get_x(), r.pos_after, r.vel_after);
g_update_trace.record(r);
};
auto trace_imu_update = [this](double t_ref) {
if (!g_update_trace.enabled()) {
kf_output.update_iterated_dyn_share_IMU();
return;
}
point_lio::RuntimeUpdateTrace::Record r;
r.event = point_lio::RuntimeUpdateTrace::kImuUpdate;
r.scan_id = g_trace_scan_id;
r.batch_id = g_trace_batch_id;
r.t_before = r.t_after = t_ref;
r.dt = 0.0;
r.ok = 1;
r.state_dof = static_cast<int>(state_output::DOF);
trace_fill(kf_output.get_x(), r.pos_before, r.vel_before);
kf_output.update_iterated_dyn_share_IMU();
trace_fill(kf_output.get_x(), r.pos_after, r.vel_after);
g_update_trace.record(r);
};
auto trace_lidar_update = [this](double t_ref) -> bool {
if (!g_update_trace.enabled()) {
return kf_output.update_iterated_dyn_share_modified();
}
point_lio::RuntimeUpdateTrace::Record r;
r.event = point_lio::RuntimeUpdateTrace::kLidarUpdate;
r.scan_id = g_trace_scan_id;
r.batch_id = g_trace_batch_id;
r.t_before = r.t_after = t_ref;
r.dt = 0.0;
trace_fill(kf_output.get_x(), r.pos_before, r.vel_before);
const bool ok = kf_output.update_iterated_dyn_share_modified();
trace_fill(kf_output.get_x(), r.pos_after, r.vel_after);
r.ok = ok ? 1 : 0;
if (ok) {
r.state_dof = kf_output.last_update_state_dof();
r.effective_m = kf_output.last_update_effective_m();
r.branch = kf_output.last_update_information_branch() ? 1 : 0;
} else {
// A failed update selected no branch and consumed no measurement; writing
// 0 here would claim the covariance form ran.
r.state_dof = static_cast<int>(state_output::DOF);
r.effective_m = point_lio::RuntimeUpdateTrace::kNotApplicable;
r.branch = point_lio::RuntimeUpdateTrace::kNotApplicable;
}
g_update_trace.record(r);
return ok;
};
while (rclcpp::ok() && running_.load()) {
if (flg_exit)
break;
@@ -956,6 +1192,7 @@ void LaserMappingNode::processingLoop()
full_cloud_queue.clear();
has_full_cloud_anchor = false;
full_cloud_world->clear();
full_cloud_world_lidar.clear();
full_cloud_dropped_points_this_scan = 0;
{
@@ -1090,6 +1327,7 @@ void LaserMappingNode::processingLoop()
full_cloud_queue.clear();
has_full_cloud_anchor = false;
full_cloud_world->clear();
full_cloud_world_lidar.clear();
full_cloud_dropped_points_this_scan = 0;
continue;
}
@@ -1134,7 +1372,9 @@ void LaserMappingNode::processingLoop()
if (!time_seq.empty()) {
double pcl_beg_time = Measures.lidar_beg_time;
idx = -1;
++g_trace_scan_id;
for (k = 0; k < time_seq.size(); k++) {
g_trace_batch_id = static_cast<uint64_t>(k);
PointType & point_body = feats_down_body->points[idx + time_seq[k]];
time_current = point_body.curvature / 1000.0 + pcl_beg_time;
@@ -1184,7 +1424,7 @@ void LaserMappingNode::processingLoop()
/*** covariance update ***/
double dt = get_time_sec(imu_next.header.stamp) - time_predict_last_const;
kf_output.predict(dt, Q_output_, input_in, true, false);
trace_predict(dt, true, false, get_time_sec(imu_next.header.stamp));
time_predict_last_const = get_time_sec(imu_next.header.stamp); // big problem
{
@@ -1192,8 +1432,8 @@ void LaserMappingNode::processingLoop()
if (dt_cov > 0.0) {
time_update_last = get_time_sec(imu_next.header.stamp);
kf_output.predict(dt_cov, Q_output_, input_in, false, true);
kf_output.update_iterated_dyn_share_IMU();
trace_predict(dt_cov, false, true, get_time_sec(imu_next.header.stamp));
trace_imu_update(get_time_sec(imu_next.header.stamp));
}
}
imu_deque.pop_front();
@@ -1212,11 +1452,11 @@ void LaserMappingNode::processingLoop()
if (!prop_at_freq_of_imu) {
double dt_cov = time_current - time_update_last;
if (dt_cov > 0.0) {
kf_output.predict(dt_cov, Q_output_, input_in, false, true);
trace_predict(dt_cov, false, true, time_current);
time_update_last = time_current;
}
}
kf_output.predict(dt, Q_output_, input_in, true, false);
trace_predict(dt, true, false, time_current);
time_predict_last_const = time_current;
if (feats_down_size < 1) {
@@ -1226,7 +1466,7 @@ void LaserMappingNode::processingLoop()
}
const auto update_start = statistics_enabled ? std::chrono::steady_clock::now()
: std::chrono::steady_clock::time_point{};
const bool update_success = kf_output.update_iterated_dyn_share_modified();
const bool update_success = trace_lidar_update(time_current);
if (statistics_enabled) {
const double update_time_ms =
std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - update_start)
@@ -1300,10 +1540,10 @@ void LaserMappingNode::processingLoop()
{
double dt_cov = time_current - time_update_last;
if (dt_cov > 0.0) {
kf_output.predict(dt_cov, Q_output_, input_in, false, true);
trace_predict(dt_cov, false, true, time_current);
time_update_last = time_current;
}
kf_output.predict(dt, Q_output_, input_in, true, false);
trace_predict(dt, true, false, time_current);
}
time_predict_last_const = time_current;
@@ -1313,7 +1553,7 @@ void LaserMappingNode::processingLoop()
acc_avr << imu_next.linear_acceleration.x, imu_next.linear_acceleration.y,
imu_next.linear_acceleration.z;
// acc_avr_norm = acc_avr * G_m_s2 / acc_norm;
kf_output.update_iterated_dyn_share_IMU();
trace_imu_update(time_current);
imu_deque.pop_front();
if (imu_deque.empty())
break;
@@ -1650,6 +1890,8 @@ int main(int argc, char ** argv)
rclcpp::executors::MultiThreadedExecutor executor;
executor.add_node(node);
executor.spin();
// Trace teardown lives in ~LaserMappingNode(), so the composable path gets it
// too and it always runs after the processing worker has stopped.
rclcpp::shutdown();
return 0;
}