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:
66
libs/vslam_perception/src/elevation_map.cc
Normal file
66
libs/vslam_perception/src/elevation_map.cc
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "vslam/elevation_map.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <utility>
|
||||
|
||||
namespace vslam {
|
||||
|
||||
ElevationMap::ElevationMap(ElevationMapConfig cfg) : cfg_(std::move(cfg)) {}
|
||||
|
||||
uint64_t ElevationMap::Key(double x, double y) const {
|
||||
// Hai chi so 32 bit co dau gop vao 64 bit KHONG dau: dich trai so am la hanh
|
||||
// vi khong xac dinh truoc C++20.
|
||||
const int32_t ix = static_cast<int32_t>(std::floor(x / cfg_.cell_m));
|
||||
const int32_t iy = static_cast<int32_t>(std::floor(y / cfg_.cell_m));
|
||||
return (static_cast<uint64_t>(static_cast<uint32_t>(ix)) << 32) | static_cast<uint32_t>(iy);
|
||||
}
|
||||
|
||||
void ElevationMap::AddPoints(const std::vector<PointXYZ>& pts, UnixTime stamp) {
|
||||
const int64_t ns = stamp.toNSec();
|
||||
for (const PointXYZ& p : pts) {
|
||||
if (!std::isfinite(p.x) || !std::isfinite(p.y) || !std::isfinite(p.z)) continue;
|
||||
auto it = cells_.find(Key(p.x, p.y));
|
||||
if (it == cells_.end()) {
|
||||
cells_.emplace(Key(p.x, p.y), Cell{p.z, ns});
|
||||
} else {
|
||||
if (p.z < it->second.z) it->second.z = p.z;
|
||||
it->second.stamp_ns = ns;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ElevationMap::AddFootprint(double x, double y, double z, double r, UnixTime stamp) {
|
||||
const int64_t ns = stamp.toNSec();
|
||||
const int n = static_cast<int>(std::ceil(r / cfg_.cell_m));
|
||||
for (int dx = -n; dx <= n; ++dx) {
|
||||
for (int dy = -n; dy <= n; ++dy) {
|
||||
const double px = x + dx * cfg_.cell_m, py = y + dy * cfg_.cell_m;
|
||||
if (std::hypot(px - x, py - y) > r) continue;
|
||||
cells_[Key(px, py)] = Cell{static_cast<float>(z), ns};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ElevationMap::FloorAt(double x, double y, double* z) const {
|
||||
auto it = cells_.find(Key(x, y));
|
||||
if (it == cells_.end()) return false;
|
||||
*z = it->second.z;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ElevationMap::Prune(double cx, double cy, UnixTime now) {
|
||||
const int64_t now_ns = now.toNSec();
|
||||
const int64_t max_age = static_cast<int64_t>(cfg_.max_age_sec * 1e9);
|
||||
for (auto it = cells_.begin(); it != cells_.end();) {
|
||||
const int32_t ix = static_cast<int32_t>(static_cast<uint32_t>(it->first >> 32));
|
||||
const int32_t iy = static_cast<int32_t>(static_cast<uint32_t>(it->first & 0xffffffffu));
|
||||
const double x = (ix + 0.5) * cfg_.cell_m, y = (iy + 0.5) * cfg_.cell_m;
|
||||
if (std::hypot(x - cx, y - cy) > cfg_.radius_m || now_ns - it->second.stamp_ns > max_age) {
|
||||
it = cells_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
87
libs/vslam_perception/src/scan_leveler.cc
Normal file
87
libs/vslam_perception/src/scan_leveler.cc
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "vslam/scan_leveler.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
#include "vslam/geometry.h"
|
||||
#include "vslam/gravity_align.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
void LevelScan(const LaserScan& in, const Transform& laser_to_base, double roll,
|
||||
double pitch, double wheel_radius, const Pose2D& odom, double z_robot,
|
||||
const ElevationMap* em, const ScanLevelerConfig& cfg, LaserScan* out,
|
||||
ScanLevelStats* stats) {
|
||||
ScanLevelStats local;
|
||||
ScanLevelStats& st = stats != nullptr ? *stats : local;
|
||||
st.scans++;
|
||||
|
||||
*out = in;
|
||||
const size_t n = in.ranges.size();
|
||||
if (n == 0 || in.angle_increment == 0.0f) return;
|
||||
const float nan = std::numeric_limits<float>::quiet_NaN();
|
||||
out->ranges.assign(n, nan);
|
||||
if (!out->intensities.empty()) out->intensities.assign(n, 0.0f);
|
||||
|
||||
// base_footprint (can trong luc) <- laser_link.
|
||||
const Transform fp_from_laser =
|
||||
TransformCompose(TiltLift(roll, pitch, wheel_radius), laser_to_base);
|
||||
// Goc LiDAR TINH tren mat phang ngang (RTAB-Map dat diem bang ngoai lai
|
||||
// tinh, nen tinh lai khoang cach/goc tu dung goc do).
|
||||
const double lx0 = laser_to_base.translation.x, ly0 = laser_to_base.translation.y;
|
||||
const double laser_yaw = YawFromQuaternion(laser_to_base.rotation);
|
||||
const double c_th = std::cos(odom.theta), s_th = std::sin(odom.theta);
|
||||
|
||||
// Luot 1: tia KHONG huu han va sentinel cua sector mask giu nguyen o cu.
|
||||
// Sentinel (> range_max) = than robot bi cat -- khong tia nao duoc de len.
|
||||
std::vector<char> masked(n, 0);
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const float r = in.ranges[i];
|
||||
if (std::isnan(r)) continue;
|
||||
if (std::isinf(r)) {
|
||||
out->ranges[i] = r;
|
||||
} else if (r > in.range_max) {
|
||||
out->ranges[i] = r;
|
||||
masked[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Luot 2: tia huu han -> chieu, loc, xep lai.
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const float r = in.ranges[i];
|
||||
if (!std::isfinite(r) || r > in.range_max) continue;
|
||||
st.rays++;
|
||||
const double a = in.angle_min + static_cast<double>(i) * in.angle_increment;
|
||||
const Point p = TransformPoint(fp_from_laser, Point{r * std::cos(a), r * std::sin(a), 0.0});
|
||||
const double h = p.z;
|
||||
const double wx = odom.x + c_th * p.x - s_th * p.y;
|
||||
const double wy = odom.y + s_th * p.x + c_th * p.y;
|
||||
double floor = 0.0;
|
||||
if (em != nullptr && em->FloorAt(wx, wy, &floor)) {
|
||||
if (z_robot + h - floor < cfg.floor_margin) {
|
||||
st.dropped_floor++;
|
||||
continue;
|
||||
}
|
||||
} else if (h < -cfg.below_ground) {
|
||||
st.dropped_below++;
|
||||
continue;
|
||||
}
|
||||
const double dx = p.x - lx0, dy = p.y - ly0;
|
||||
const double rh = std::hypot(dx, dy);
|
||||
const double ah = NormalizeAngle(std::atan2(dy, dx) - laser_yaw);
|
||||
const long j = std::lround((ah - in.angle_min) / in.angle_increment);
|
||||
if (j < 0 || j >= static_cast<long>(n) || masked[static_cast<size_t>(j)]) continue;
|
||||
if (static_cast<size_t>(j) != i) st.rebinned++;
|
||||
float& slot = out->ranges[static_cast<size_t>(j)];
|
||||
if (!std::isfinite(slot) || rh < slot) {
|
||||
slot = static_cast<float>(rh);
|
||||
if (!out->intensities.empty() && i < in.intensities.size()) {
|
||||
out->intensities[static_cast<size_t>(j)] = in.intensities[i];
|
||||
}
|
||||
}
|
||||
st.kept++;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
Reference in New Issue
Block a user