slam,odometry,perception: di tren doc (G4, slam.yaml slope.enabled, mac dinh tat) -- z robot = tich -ds*sin(pitch IMU) kep phang < 1.5 do va neo vao ban do (HeightEstimator), odometry ngang, scan cho SLAM bu nghieng + bo tia cham san/doc theo ban do do cao tu depth (ScanLeveler, ElevationMap), z/roll/pitch luu vao node + do thi z 1-D + mat san theo node (height_map), voxel dat o z that va vat can tinh tu san cuc bo, xoa tuong gia LiDAR tren doc noi camera da thay mat san, web z uoc luong, --sim-imu-hz

This commit is contained in:
trung09122002
2026-10-04 00:03:02 +07:00
parent 6735b53423
commit f064dcf3ec
32 changed files with 1799 additions and 10 deletions

View File

@@ -18,10 +18,14 @@
#include "runtime.h"
#include "vslam/geometry.h"
#include "vslam/gravity_align.h"
#include "vslam/json_parse.h"
#include "vslam/log.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
namespace vslam {
@@ -36,8 +40,32 @@ StatusResponse Runtime::Wire() {
// rieng thi "cai nao chay truoc" la mot bay co that.
auto copy = std::make_shared<LaserScan>(*s);
sanitizer_->Apply(copy.get());
// Di tren doc: ban BU NGHIENG + bo tia cham san cho SLAM / dinh vi lai.
// An toan va web giu ban tho. Tat thi hai con tro la MOT.
LaserScanPtr slam_scan = copy;
if (slope_on_ && cfg_->slope.level_scan) {
Pose2D od;
double z = 0.0;
{
std::lock_guard<std::mutex> lk(odom_mu_);
od = ekf_->pose();
z = height_->z();
}
const double roll = tilt_roll_.load(std::memory_order_relaxed);
const double pitch = tilt_pitch_.load(std::memory_order_relaxed);
auto lv = std::make_shared<LaserScan>();
{
std::lock_guard<std::mutex> lk(elev_mu_);
LevelScan(*copy, laser_to_base_static_, roll, pitch,
cfg_->robot.geometry().wheel_radius, od, z, elev_.get(), level_cfg_,
lv.get(), &level_stats_);
}
slam_scan = lv;
}
std::lock_guard<std::mutex> lk(scan_mu_);
latest_scan_ = copy;
latest_slam_scan_ = slam_scan;
});
}
@@ -162,8 +190,23 @@ StatusResponse Runtime::Wire() {
gyro_ok ? std::max(2.4e-7 * dt, 1e-12) : 1e9;
const double var_tr = std::max(o.twist.covariance[0] * dt * dt, 1e-12);
ekf_->UpdateOdomDelta(e.stamp, d_trans, d_theta, d_theta_gyro,
// Di tren doc: z cong tu quang duong DOC mat doc; EKF (SE(2)) nhan
// quang duong NGANG -- doc 15 do ma khong chieu thi ban do gian 3.4%.
double d_trans_ekf = d_trans;
if (slope_on_) {
const double pitch = tilt_pitch_.load(std::memory_order_relaxed);
height_->OnWheelStep(d_trans, pitch);
if (cfg_->slope.horizontal_odom) d_trans_ekf = d_trans * std::cos(pitch);
}
ekf_->UpdateOdomDelta(e.stamp, d_trans_ekf, d_theta, d_theta_gyro,
var_tr, var_th_enc, var_th_gyro);
if (slope_on_) {
// Vet banh: san duoi robot o dung z robot. Thu tu khoa odom_mu_ ->
// elev_mu_ (xem runtime.h).
const Pose2D p = ekf_->pose();
std::lock_guard<std::mutex> lk2(elev_mu_);
elev_->AddFootprint(p.x, p.y, height_->z(), 0.15, e.stamp);
}
}
gyro_dtheta_ = 0.0;
gyro_dt_ = 0.0;
@@ -234,6 +277,9 @@ StatusResponse Runtime::Wire() {
have_depth_ = true;
}
// Di tren doc: depth -> ban do do cao san cuc bo (cho ScanLeveler).
if (slope_on_ && have_cam_to_base_) AddDepthToElevation(f);
// Ca chuoi perception chay INLINE o day: depth -> cloud -> loc ->
// can trong luc -> phat hien vuc. Mot loi goi ham, tren chinh luong
// camera.
@@ -397,4 +443,62 @@ StatusResponse Runtime::Wire() {
return StatusResponse::Ok();
}
void Runtime::AddDepthToElevation(const CameraFrame& f) {
const Image& d = *f.depth;
if (d.data == nullptr || d.width == 0 || d.height == 0) return;
const CameraInfo& ci = f.depth_info;
const double fx = ci.fx_binned(), fy = ci.fy_binned();
const double cx = ci.cx_binned(), cy = ci.cy_binned();
if (fx <= 0.0 || fy <= 0.0) return;
Pose2D od;
double z = 0.0;
{
std::lock_guard<std::mutex> lk(odom_mu_);
od = ekf_->pose();
z = height_->z();
}
const double roll = tilt_roll_.load(std::memory_order_relaxed);
const double pitch = tilt_pitch_.load(std::memory_order_relaxed);
// base_footprint DA CAN <- optical: CHUNG TiltLift voi GravityAlign va
// ScanLeveler -- ba ban hinh hoc lech nhau la lop loi da co trong repo cu.
const Transform fp_from_cam = TransformCompose(
TiltLift(roll, pitch, cfg_->robot.geometry().wheel_radius), cam_to_base_static_);
const double c = std::cos(od.theta), s = std::sin(od.theta);
const int stride = std::max(1, cfg_->slope.depth_stride);
const double dmax = cfg_->slope.elevation_max_depth_m;
std::vector<PointXYZ> pts;
pts.reserve((d.width / stride + 1) * (d.height / stride + 1));
for (uint32_t v = 0; v < d.height; v += static_cast<uint32_t>(stride)) {
const uint8_t* row = d.data + static_cast<size_t>(v) * d.step;
for (uint32_t u = 0; u < d.width; u += static_cast<uint32_t>(stride)) {
double depth = 0.0;
if (d.format == PixelFormat::kMono16) {
uint16_t raw;
std::memcpy(&raw, row + 2 * u, 2);
depth = raw * static_cast<double>(d.depth_scale);
} else if (d.format == PixelFormat::kFloat32) {
float raw;
std::memcpy(&raw, row + 4 * u, 4);
depth = raw;
} else {
return;
}
if (!(depth > 0.2) || depth > dmax) continue;
const Point p = TransformPoint(
fp_from_cam, Point{(u - cx) / fx * depth, (v - cy) / fy * depth, depth});
pts.push_back(PointXYZ{static_cast<float>(od.x + c * p.x - s * p.y),
static_cast<float>(od.y + s * p.x + c * p.y),
static_cast<float>(z + p.z)});
}
}
std::lock_guard<std::mutex> lk(elev_mu_);
elev_->AddPoints(pts, f.header.stamp);
if (SecondsBetween(f.header.stamp, elev_pruned_) > 1.0) {
elev_->Prune(od.x, od.y, f.header.stamp);
elev_pruned_ = f.header.stamp;
}
}
} // namespace vslam