168 lines
5.4 KiB
C++
168 lines
5.4 KiB
C++
#include "vslam/gravity_align.h"
|
|
|
|
#include <cmath>
|
|
|
|
#include "vslam/geometry.h"
|
|
#include "vslam/log.h"
|
|
#include "vslam/node_constants.h"
|
|
|
|
namespace vslam {
|
|
|
|
GravityAlign::GravityAlign(GravityAlignConfig cfg) : cfg_(std::move(cfg)) {}
|
|
|
|
void GravityAlign::Reset() {
|
|
initialized_ = false;
|
|
roll_ = pitch_ = 0.0;
|
|
lpf_init_ = false;
|
|
have_odom_ = false;
|
|
accel_ok_ = false;
|
|
stats_ = GravityAlignStats{};
|
|
}
|
|
|
|
void GravityAlign::FeedOdom(UnixTime stamp, double vx, double vyaw) {
|
|
if (have_odom_) {
|
|
const double dt = SecondsBetween(stamp, last_odom_);
|
|
if (dt > 0.0 && dt < 1.0) {
|
|
// Dao ham van toc, loc thong thap. Dao ham tho qua nhieu de tru truc
|
|
// tiep khoi gia toc ke.
|
|
const double a = (vx - v_prev_) / dt;
|
|
a_lin_ = cfg_.odom_accel_alpha * a + (1.0 - cfg_.odom_accel_alpha) * a_lin_;
|
|
}
|
|
}
|
|
v_prev_ = vx;
|
|
w_ = vyaw;
|
|
last_odom_ = stamp;
|
|
have_odom_ = true;
|
|
}
|
|
|
|
void GravityAlign::FeedImu(const Imu& imu) {
|
|
const UnixTime now = imu.header.stamp;
|
|
stats_.imu_samples++;
|
|
|
|
if (!initialized_) {
|
|
last_imu_ = now;
|
|
initialized_ = true;
|
|
return;
|
|
}
|
|
|
|
const double dt = SecondsBetween(now, last_imu_);
|
|
last_imu_ = now;
|
|
if (dt <= 0.0 || dt > 1.0) {
|
|
// Nhay thoi gian (reset mo phong / phat lai ban ghi) -> bo qua mau nay.
|
|
return;
|
|
}
|
|
|
|
// --- Du doan bang gyro ---
|
|
roll_ += imu.angular_velocity.x * dt;
|
|
pitch_ += imu.angular_velocity.y * dt;
|
|
|
|
// --- Loc thong thap accel TRUOC khi dung ---
|
|
// Accel tho dao dong manh do rung tiep xuc banh-san; ap cong |a| ~ g len
|
|
// mau tho se tu choi gan nhu moi mau, va bo loc chi con lai tich phan gyro
|
|
// -- tuc la troi tu do.
|
|
const double ax_raw = imu.linear_acceleration.x;
|
|
const double ay_raw = imu.linear_acceleration.y;
|
|
const double az_raw = imu.linear_acceleration.z;
|
|
|
|
if (!lpf_init_) {
|
|
ax_f_ = ax_raw;
|
|
ay_f_ = ay_raw;
|
|
az_f_ = az_raw;
|
|
lpf_init_ = true;
|
|
} else {
|
|
const double a = cfg_.accel_lpf_alpha;
|
|
ax_f_ = a * ax_raw + (1.0 - a) * ax_f_;
|
|
ay_f_ = a * ay_raw + (1.0 - a) * ay_f_;
|
|
az_f_ = a * az_raw + (1.0 - a) * az_f_;
|
|
}
|
|
|
|
// --- Hieu chinh bang accel, CHI khi dang gan trang thai tinh ---
|
|
const double ax = ax_f_, ay = ay_f_, az = az_f_;
|
|
|
|
// TRU gia toc cua CHINH ROBOT truoc khi coi phan con lai la trong luc.
|
|
// axc: gia toc tinh tien (dv/dt)
|
|
// ayc: gia toc huong tam khi quay (v * omega)
|
|
double axc = ax, ayc = ay;
|
|
const bool odom_fresh =
|
|
have_odom_ && SecondsBetween(now, last_odom_) < cfg_.odom_timeout;
|
|
if (cfg_.use_odom_accel && odom_fresh) {
|
|
axc = ax - a_lin_;
|
|
ayc = ay - v_prev_ * w_;
|
|
}
|
|
|
|
const double norm = std::sqrt(axc * axc + ayc * ayc + az * az);
|
|
stats_.last_accel_norm = norm;
|
|
|
|
if (std::fabs(norm - kGravity) < cfg_.accel_gate && norm > 1e-3) {
|
|
const double roll_acc = std::atan2(ayc, az);
|
|
const double pitch_acc = std::atan2(-axc, std::sqrt(ayc * ayc + az * az));
|
|
|
|
// Bo loc bu: gyro cho ngan han, accel keo ve dai han.
|
|
roll_ = cfg_.alpha * roll_ + (1.0 - cfg_.alpha) * roll_acc;
|
|
pitch_ = cfg_.alpha * pitch_ + (1.0 - cfg_.alpha) * pitch_acc;
|
|
accel_ok_ = true;
|
|
stats_.accel_accepted++;
|
|
} else {
|
|
// Dang tang toc / phanh / va cham -> khong biet dau la trong luc ->
|
|
// chi tich phan gyro. Troi cham con hon nghieng sai.
|
|
accel_ok_ = false;
|
|
stats_.accel_rejected++;
|
|
}
|
|
}
|
|
|
|
Transform TiltLift(double roll, double pitch, double wheel_radius) {
|
|
// Ghep hai phep bien doi:
|
|
// tilt : Ry(pitch) * Rx(roll) -- DUNG dau duong.
|
|
// Bo loc bu cho ra pitch = atan2(-ax, |a_yz|); voi robot tinh
|
|
// nghieng goc theta quanh +Y, gia toc ke doc ax = -g sin(theta),
|
|
// az = g cos(theta) => pitch = theta. Do la phep quay body->the
|
|
// gioi, tuc dung chieu de dua diem tu base_link ve mat phang ngang.
|
|
// Phu dinh o day se lam nghieng GAP DOI thay vi trie^t tieu.
|
|
// lift : base_link (tam truc) -> base_footprint (mat dat).
|
|
Transform tilt = TransformIdentity();
|
|
tilt.rotation = QuaternionFromRPY(roll, pitch, 0.0);
|
|
|
|
Transform lift = TransformIdentity();
|
|
lift.translation = Vector3{0.0, 0.0, wheel_radius};
|
|
|
|
return TransformCompose(lift, tilt);
|
|
}
|
|
|
|
bool GravityAlign::Align(const PointCloudXYZ& in, const Transform& cam_to_base,
|
|
UnixTime stamp, PointCloudXYZ* out) const {
|
|
if (!initialized_) return false;
|
|
|
|
stats_.clouds_in++;
|
|
stats_.points_in += in.points.size();
|
|
|
|
// Ghep ba phep bien doi:
|
|
// lift : base_link -> base_footprint (nang len chieu cao banh xe)
|
|
// tilt : bu nghieng DO duoc (nghich dao cua nghieng than)
|
|
// cam : camera -> base_link (tinh, tu robot.yaml)
|
|
const Transform total =
|
|
TransformCompose(TiltLift(roll_, pitch_, cfg_.wheel_radius), cam_to_base);
|
|
|
|
out->header = in.header;
|
|
out->header.stamp = stamp;
|
|
out->header.frame_id = kBaseFootprintFrame;
|
|
out->points.clear();
|
|
out->points.reserve(in.points.size());
|
|
out->is_dense = true;
|
|
|
|
for (const PointXYZ& p : in.points) {
|
|
if (!std::isfinite(p.x) || !std::isfinite(p.y) || !std::isfinite(p.z)) continue;
|
|
const Point q = TransformPoint(total, Point{p.x, p.y, p.z});
|
|
// Cat dai chieu cao SAU khi xoay: duoi min_height la san, tren max_height
|
|
// la tran va den.
|
|
if (q.z < cfg_.min_height || q.z > cfg_.max_height) continue;
|
|
out->points.push_back(PointXYZ{static_cast<float>(q.x), static_cast<float>(q.y),
|
|
static_cast<float>(q.z)});
|
|
}
|
|
|
|
stats_.clouds_out++;
|
|
stats_.points_out += out->points.size();
|
|
return true;
|
|
}
|
|
|
|
} // namespace vslam
|