Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion apps/lidar_odometry_step_1/lidar_odometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,27 @@ bool load_data(
return std::get<0>(a).first < std::get<0>(b).first;
});

if (params.imu_offset_qw != 1.0 || params.imu_offset_qx != 0.0 || params.imu_offset_qy != 0.0 || params.imu_offset_qz != 0.0)
{
const Eigen::Quaterniond imu_offset(params.imu_offset_qw, params.imu_offset_qx, params.imu_offset_qy, params.imu_offset_qz);
if (imu_offset.norm() < 1e-9)
{
std::cerr << "imu_offset quaternion (w x y z) is zero, it must describe a rotation\n";
return false;
}
rotate_imu(imu_data, imu_offset);
std::cout << "IMU readings rotated by offset quaternion (w x y z): " << params.imu_offset_qw << " " << params.imu_offset_qx
<< " " << params.imu_offset_qy << " " << params.imu_offset_qz << "\n";
}

if (params.imu_target_rate_hz > 0.0)
{
const size_t samples_before = imu_data.size();
imu_data = average_imu_to_rate(imu_data, params.imu_target_rate_hz);
std::cout << "IMU averaged to " << params.imu_target_rate_hz << " Hz: " << samples_before << " -> " << imu_data.size()
<< " samples\n";
}

// The IMU timestamps are already sorted; publishing their span needs no point-cloud reads or extra scan.
if (params.run_report && !imu_data.empty())
params.run_report->setRecordingSpan(std::get<0>(imu_data.front()).first, std::get<0>(imu_data.back()).first);
Expand Down Expand Up @@ -868,7 +889,7 @@ bool compute_step_1(
}

// correct points timestamps
if (wd.intermediate_trajectory_timestamps.size() > 2)
if (params.use_uniform_point_timestamps && wd.intermediate_trajectory_timestamps.size() > 2)
{
double ts_begin = wd.intermediate_trajectory_timestamps[0].first;
double ts_step = (wd.intermediate_trajectory_timestamps[wd.intermediate_trajectory_timestamps.size() - 1].first -
Expand Down
31 changes: 29 additions & 2 deletions apps/lidar_odometry_step_1/lidar_odometry_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,27 @@ void settings_gui()
params.threshold_nr_poses = 1;
}

ImGui::SetNextItemWidth(ImGuiNumberWidth);
ImGui::InputDouble("IMU target rate [Hz]", &params.imu_target_rate_hz, 0.0, 0.0, "%.1f");
if (ImGui::IsItemHovered())
ImGui::SetTooltip(
"> 0: average the IMU samples to this rate during load (one trajectory node per sample),\n"
"e.g. 207 for the 1451 Hz Murata IMU; -1: use every sample. Applies on Load data.");

double imu_offset[4] = { params.imu_offset_qw, params.imu_offset_qx, params.imu_offset_qy, params.imu_offset_qz };
ImGui::SetNextItemWidth(ImGuiNumberWidth * 2);
if (ImGui::InputScalarN("IMU offset quaternion (w x y z)", ImGuiDataType_Double, imu_offset, 4, nullptr, nullptr, "%.4f"))
{
params.imu_offset_qw = imu_offset[0];
params.imu_offset_qx = imu_offset[1];
params.imu_offset_qy = imu_offset[2];
params.imu_offset_qz = imu_offset[3];
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip(
"rotation from the IMU frame to the lidar frame applied to the IMU readings during load,\n"
"e.g. 0.7071 0 0 0.7071 for an IMU turned +90 deg about z; 1 0 0 0: unchanged. Applies on Load data.");

ImGui::NewLine();
}

Expand Down Expand Up @@ -1029,6 +1050,11 @@ void settings_gui()
ImGui::SetTooltip(
"Predict each window's rotation and translation from the previous one (constant velocity) instead of from "
"the IMU. For recordings without a usable IMU, e.g. a synthetic stationary one.");
ImGui::Checkbox("Uniform point timestamps", &params.use_uniform_point_timestamps);
if (ImGui::IsItemHovered())
ImGui::SetTooltip(
"On: overwrite each window's point timestamps with evenly spaced times between its first and last node.\n"
"Off: keep the lidar's own timestamps, better when its point rate is uneven (e.g. Hesai no-return sectors).");
ImGui::Checkbox("Use IMU preintegration", &params.use_imu_preintegration);
if (params.use_imu_preintegration)
{
Expand Down Expand Up @@ -2178,6 +2204,9 @@ void display()
ImGuiIO& io = ImGui::GetIO();
rlViewport(0, 0, GetRenderWidth(), GetRenderHeight());

// every frame, so the colour from the defaults, a preset or a loaded TOML shows from the start, not only after
// the View menu's picker has been opened
bg_color = params.clear_color;
ClearBackground(ColorFromNormalized(Vector4{ bg_color.x * bg_color.w, bg_color.y * bg_color.w, bg_color.z * bg_color.w, bg_color.w }));
rlEnableDepthTest();

Expand Down Expand Up @@ -2715,8 +2744,6 @@ void display()

ImGui::ColorEdit3("Background", (float*)&params.clear_color, ImGuiColorEditFlags_NoInputs);

bg_color = params.clear_color;

ImGui::Separator();

ImGui::MenuItem("Settings", nullptr, &is_settings_gui);
Expand Down
48 changes: 48 additions & 0 deletions apps/lidar_odometry_step_1/lidar_odometry_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,54 @@ std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector
return all_data;
}

std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector3f>> average_imu_to_rate(
const std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector3f>>& imu, double target_rate_hz)
{
if (target_rate_hz <= 0.0 || imu.empty())
return imu;

const double t0 = std::get<0>(imu.front()).first;
const auto bin_of = [&](size_t i)
{
return static_cast<int64_t>(std::floor((std::get<0>(imu[i]).first - t0) * target_rate_hz));
};

std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector3f>> averaged;
averaged.reserve(imu.size());
for (size_t begin = 0; begin < imu.size();)
{
const int64_t bin = bin_of(begin);
size_t end = begin + 1;
while (end < imu.size() && bin_of(end) == bin)
end++;

Eigen::Vector3d gyr_sum = Eigen::Vector3d::Zero();
Eigen::Vector3d acc_sum = Eigen::Vector3d::Zero();
for (size_t i = begin; i < end; i++)
{
gyr_sum += std::get<1>(imu[i]).cast<double>();
acc_sum += std::get<2>(imu[i]).cast<double>();
}
// stamped with the last sample: step 1 applies each reading over the interval that ends at its timestamp, so
// the bin's mean time would put the orientation half a bin ahead (2.4 ms at 207 Hz)
const double n = static_cast<double>(end - begin);
averaged.emplace_back(std::get<0>(imu[end - 1]), (gyr_sum / n).cast<float>(), (acc_sum / n).cast<float>());
begin = end;
}
return averaged;
}

void rotate_imu(
std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector3f>>& imu, const Eigen::Quaterniond& imu_to_lidar)
{
const Eigen::Matrix3d rotation = imu_to_lidar.normalized().toRotationMatrix();
for (auto& [timestamps, gyr, acc] : imu)
{
gyr = (rotation * gyr.cast<double>()).cast<float>();
acc = (rotation * acc.cast<double>()).cast<float>();
}
}

namespace
{
// Byte offset of the uint16 LAS extra-bytes attribute `name` within a point's
Expand Down
28 changes: 28 additions & 0 deletions apps/lidar_odometry_step_1/lidar_odometry_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ struct LidarOdometryParams
double threshould_output_filter = 0.5; // for export --> all points xyz.norm() < threshould_output_filter will be removed
int min_counter_concatenated_trajectory_nodes = 10; // for export

// IMU: > 0 averages the IMU samples to this rate [Hz] during load (one trajectory node per sample), e.g. 207 for
// the 1451 Hz Murata IMU; <= 0 uses every sample
double imu_target_rate_hz = -1.0;
// IMU: rotation from the IMU frame to the lidar frame applied to the IMU readings during load (v_lidar = q * v_imu),
// e.g. w = z = 0.7071 for an IMU turned +90 deg about z; identity (w = 1) leaves them unchanged
double imu_offset_qw = 1.0;
double imu_offset_qx = 0.0;
double imu_offset_qy = 0.0;
double imu_offset_qz = 0.0;

// AHRS type selection: false = Fusion (Madgwick, default), true = VQF
bool use_vqf = false;

Expand Down Expand Up @@ -135,6 +145,9 @@ struct LidarOdometryParams
// No usable IMU: predict each window's rotation and translation from the
// previous window (constant velocity) instead of from the IMU orientation.
bool lidar_only_odometry = false;
// true: overwrite each window's point timestamps with evenly spaced times between its first and last node, in point
// order; false: keep the lidar's own timestamps (better when its point rate is uneven, e.g. Hesai no-return sectors)
bool use_uniform_point_timestamps = true;
int nr_iter = 100;
NDT::GridParameters in_out_params_indoor;
NDT::GridParameters in_out_params_outdoor;
Expand Down Expand Up @@ -348,6 +361,21 @@ void update_rgd_spherical_coordinates(
//! @return vector of tuples (std::pair<timestamp, timestampUnix>, angular_velocity, linear_acceleration)
std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector3f>> load_imu(const std::string& imu_file, int imuToUse);

//! Averages IMU samples over consecutive time bins of 1 / target_rate_hz seconds, counted from the first sample.
//! Each non-empty bin becomes one sample with the mean angular velocity and acceleration, stamped with the timestamps
//! (both clocks) of the bin's last sample, as step 1 applies a reading over the interval that ends at its timestamp.
//! @param imu - samples sorted by timestamp, as returned by @ref load_imu
//! @param target_rate_hz - output rate [Hz]; <= 0 returns @p imu unchanged
//! @return averaged samples, sorted by timestamp
std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector3f>> average_imu_to_rate(
const std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector3f>>& imu, double target_rate_hz);

//! Rotates every IMU reading (angular velocity and acceleration) into another frame: v_lidar = imu_to_lidar * v_imu.
//! @param imu - samples, rotated in place; timestamps are unchanged
//! @param imu_to_lidar - rotation from the IMU frame to the lidar frame; normalized before use, must not be zero
void rotate_imu(
std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector3f>>& imu, const Eigen::Quaterniond& imu_to_lidar);

//! This function load point cloud from LAS/LAZ file.
//! Optionally it can apply extrinsic calibration to each point.
//! The calibration is stored in a map, where key is laser scanner id.
Expand Down
5 changes: 3 additions & 2 deletions apps/lidar_odometry_step_1/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 4.0.0)
project(lidar_odometry_step_1_tests)

# Unit tests for lidar_odometry_utils.h/.cpp: the MLvxCalib namespace, which
# parses Livox extrinsic/IMU calibration files (.json/.mjc and .sn), and
# find_lidar_gaps(). Compiles
# parses Livox extrinsic/IMU calibration files (.json/.mjc and .sn),
# find_lidar_gaps(), average_imu_to_rate() and rotate_imu(). Compiles
# the real lidar_odometry_utils.cpp (no GUI code path, WITH_GUI left
# undefined) instead of the whole lidar_odometry_step_1 app, so the include
# dirs/link libraries below are the subset that TU actually needs: laszip and
Expand All @@ -16,6 +16,7 @@ project(lidar_odometry_step_1_tests)
add_executable(lidar_odometry_step_1_tests
test_mlvx_calib.cpp
test_lidar_gaps.cpp
test_imu_load.cpp
../lidar_odometry_utils.cpp
)

Expand Down
91 changes: 91 additions & 0 deletions apps/lidar_odometry_step_1/tests/test_imu_load.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <doctest.h>

#include "../lidar_odometry_utils.h"

namespace
{
using ImuSamples = std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector3f>>;

// One sample at `t` [s]; the Unix timestamp is t + 1000, gyro and accelerometer readings are `value` on every axis
void appendSample(ImuSamples& imu, double t, float value)
{
imu.emplace_back(std::pair(t, t + 1000.0), Eigen::Vector3f::Constant(value), Eigen::Vector3f::Constant(-value));
}
} // namespace

TEST_CASE("average_imu_to_rate: a rate <= 0 returns the samples unchanged")
{
ImuSamples imu;
appendSample(imu, 10.000, 1.0f);
appendSample(imu, 10.001, 2.0f);

for (const double rate : { -1.0, 0.0 })
{
const auto averaged = average_imu_to_rate(imu, rate);
REQUIRE(averaged.size() == imu.size());
for (size_t i = 0; i < imu.size(); i++)
{
CHECK(std::get<0>(averaged[i]) == std::get<0>(imu[i]));
CHECK(std::get<1>(averaged[i]) == std::get<1>(imu[i]));
CHECK(std::get<2>(averaged[i]) == std::get<2>(imu[i]));
}
}
}

TEST_CASE("average_imu_to_rate: each bin of 1 / rate seconds becomes the mean of its readings at its last sample's time")
{
ImuSamples imu;
appendSample(imu, 10.000, 1.0f); // 200 Hz: bin [10.000, 10.005)
appendSample(imu, 10.001, 2.0f);
appendSample(imu, 10.002, 3.0f);
appendSample(imu, 10.003, 6.0f);
appendSample(imu, 10.006, 10.0f); // bin [10.005, 10.010)
appendSample(imu, 10.008, 20.0f);

const auto averaged = average_imu_to_rate(imu, 200.0);
REQUIRE(averaged.size() == 2);

CHECK(std::get<0>(averaged[0]).first == doctest::Approx(10.003));
CHECK(std::get<0>(averaged[0]).second == doctest::Approx(1010.003));
CHECK(std::get<1>(averaged[0]).x() == doctest::Approx(3.0));
CHECK(std::get<2>(averaged[0]).z() == doctest::Approx(-3.0));

CHECK(std::get<0>(averaged[1]).first == doctest::Approx(10.008));
CHECK(std::get<1>(averaged[1]).y() == doctest::Approx(15.0));
CHECK(std::get<2>(averaged[1]).x() == doctest::Approx(-15.0));
}

TEST_CASE("average_imu_to_rate: samples on either side of a gap stay apart")
{
ImuSamples imu;
appendSample(imu, 10.0000, 1.0f);
appendSample(imu, 10.0010, 3.0f);
appendSample(imu, 10.1002, 5.0f); // 20 bins later, the bins in between are empty
appendSample(imu, 10.1012, 7.0f);

const auto averaged = average_imu_to_rate(imu, 200.0);
REQUIRE(averaged.size() == 2);
CHECK(std::get<0>(averaged[0]).first == doctest::Approx(10.0010));
CHECK(std::get<1>(averaged[0]).x() == doctest::Approx(2.0));
CHECK(std::get<0>(averaged[1]).first == doctest::Approx(10.1012));
CHECK(std::get<1>(averaged[1]).x() == doctest::Approx(6.0));
}

TEST_CASE("rotate_imu: +90 deg about z maps the IMU x axis to lidar y and y to -x")
{
ImuSamples imu;
imu.emplace_back(std::pair(10.0, 1010.0), Eigen::Vector3f(1.0f, 0.0f, 0.5f), Eigen::Vector3f(0.0f, 1.0f, 0.0f));

// not normalized on purpose: the rotation is the same
rotate_imu(imu, Eigen::Quaterniond(2.0, 0.0, 0.0, 2.0));

const auto& [timestamps, gyr, acc] = imu[0];
CHECK(timestamps.first == 10.0);
CHECK(timestamps.second == 1010.0);
CHECK(gyr.x() == doctest::Approx(0.0));
CHECK(gyr.y() == doctest::Approx(1.0));
CHECK(gyr.z() == doctest::Approx(0.5));
CHECK(acc.x() == doctest::Approx(-1.0));
CHECK(acc.y() == doctest::Approx(0.0));
CHECK(acc.z() == doctest::Approx(0.0));
}
8 changes: 8 additions & 0 deletions apps/lidar_odometry_step_1/toml_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class TomlIO
{ "decimation", &LidarOdometryParams::decimation },
{ "threshould_output_filter", &LidarOdometryParams::threshould_output_filter },
{ "min_counter_concatenated_trajectory_nodes", &LidarOdometryParams::min_counter_concatenated_trajectory_nodes },
{ "imu_target_rate_hz", &LidarOdometryParams::imu_target_rate_hz },
{ "imu_offset_qw", &LidarOdometryParams::imu_offset_qw },
{ "imu_offset_qx", &LidarOdometryParams::imu_offset_qx },
{ "imu_offset_qy", &LidarOdometryParams::imu_offset_qy },
{ "imu_offset_qz", &LidarOdometryParams::imu_offset_qz },
{ "use_vqf", &LidarOdometryParams::use_vqf },
{ "fusion_gain", &LidarOdometryParams::fusion_gain },
{ "fusionConventionNwu", &LidarOdometryParams::fusionConventionNwu },
Expand Down Expand Up @@ -70,6 +75,7 @@ class TomlIO
{ "vqf_magRejectionFactor", &LidarOdometryParams::vqf_magRejectionFactor },
{ "use_motion_from_previous_step", &LidarOdometryParams::use_motion_from_previous_step },
{ "lidar_only_odometry", &LidarOdometryParams::lidar_only_odometry },
{ "use_uniform_point_timestamps", &LidarOdometryParams::use_uniform_point_timestamps },
{ "nr_iter", &LidarOdometryParams::nr_iter },
{ "sliding_window_trajectory_length_threshold", &LidarOdometryParams::sliding_window_trajectory_length_threshold },
{ "max_distance_lidar", &LidarOdometryParams::max_distance_lidar },
Expand Down Expand Up @@ -135,6 +141,7 @@ class TomlIO
"decimation",
"threshould_output_filter",
"min_counter_concatenated_trajectory_nodes" } },
{ "imu", { "imu_target_rate_hz", "imu_offset_qw", "imu_offset_qx", "imu_offset_qy", "imu_offset_qz" } },
{ "ahrs_vqf",
{ "use_vqf",
"fusion_gain",
Expand Down Expand Up @@ -170,6 +177,7 @@ class TomlIO
{ "lidar_odometry_control",
{ "use_motion_from_previous_step",
"lidar_only_odometry",
"use_uniform_point_timestamps",
"nr_iter",
"sliding_window_trajectory_length_threshold",
"max_distance_lidar",
Expand Down
6 changes: 6 additions & 0 deletions pybind/lidar_odometry_binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ PYBIND11_MODULE(lidar_odometry_py, m)
.def_readwrite("skip_first_file", &LidarOdometryParams::skip_first_file)
.def_readwrite("filter_threshold_xy_inner", &LidarOdometryParams::filter_threshold_xy_inner)
.def_readwrite("filter_threshold_xy_outer", &LidarOdometryParams::filter_threshold_xy_outer)
.def_readwrite("imu_target_rate_hz", &LidarOdometryParams::imu_target_rate_hz)
.def_readwrite("imu_offset_qw", &LidarOdometryParams::imu_offset_qw)
.def_readwrite("imu_offset_qx", &LidarOdometryParams::imu_offset_qx)
.def_readwrite("imu_offset_qy", &LidarOdometryParams::imu_offset_qy)
.def_readwrite("imu_offset_qz", &LidarOdometryParams::imu_offset_qz)
.def_readwrite("m_g", &LidarOdometryParams::m_g)
.def_readwrite("initial_points", &LidarOdometryParams::initial_points)
.def_readwrite("in_out_params_indoor", &LidarOdometryParams::in_out_params_indoor)
.def_readwrite("in_out_params_outdoor", &LidarOdometryParams::in_out_params_outdoor)
.def_readwrite("buckets_indoor", &LidarOdometryParams::buckets_indoor)
.def_readwrite("buckets_outdoor", &LidarOdometryParams::buckets_outdoor)
.def_readwrite("use_motion_from_previous_step", &LidarOdometryParams::use_motion_from_previous_step)
.def_readwrite("use_uniform_point_timestamps", &LidarOdometryParams::use_uniform_point_timestamps)
.def_readwrite("lidar_only_odometry", &LidarOdometryParams::lidar_only_odometry)
.def_readwrite("consecutive_distance", &LidarOdometryParams::consecutive_distance)
.def_readwrite("nr_iter", &LidarOdometryParams::nr_iter)
Expand Down
Loading