feat(localization): add keyframe mapping and immutable map persistence
This commit is contained in:
1 parent
9b72f580c3
commit
55e9456e4a
6 files changed
+177
-4
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "field_localization/core/registration.hpp"
|
#include "field_localization/core/map_io.hpp"
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@@ -38,7 +38,10 @@ struct LocalizationStatus {
|
|||||||
std::size_t last_point_count{0};
|
std::size_t last_point_count{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class MapMode { MAPPING, LOCALIZATION };
|
||||||
struct CoreOptions {
|
struct CoreOptions {
|
||||||
|
MapMode map_mode{MapMode::LOCALIZATION};
|
||||||
|
double map_resolution{0.1};
|
||||||
RegistrationOptions registration;
|
RegistrationOptions registration;
|
||||||
NoiseOptions noise;
|
NoiseOptions noise;
|
||||||
Pose3D initial_pose;
|
Pose3D initial_pose;
|
||||||
@@ -56,6 +59,8 @@ public:
|
|||||||
explicit LocalizationCore(const CoreOptions &options);
|
explicit LocalizationCore(const CoreOptions &options);
|
||||||
[[nodiscard]] LocalizationSnapshot get_snapshot() const;
|
[[nodiscard]] LocalizationSnapshot get_snapshot() const;
|
||||||
void set_map_points(std::span<const Eigen::Vector3d> points);
|
void set_map_points(std::span<const Eigen::Vector3d> points);
|
||||||
|
bool load_map(const std::filesystem::path &path,std::string &error);
|
||||||
|
bool export_map(const std::filesystem::path &path,std::string &error) const;
|
||||||
|
|
||||||
// 接收 IMU 数据
|
// 接收 IMU 数据
|
||||||
bool process_imu(const ImuData &imu);
|
bool process_imu(const ImuData &imu);
|
||||||
@@ -81,6 +86,7 @@ private:
|
|||||||
CoreOptions options_;
|
CoreOptions options_;
|
||||||
VoxelMap map_;
|
VoxelMap map_;
|
||||||
double last_correction_time_{-1.0};
|
double last_correction_time_{-1.0};
|
||||||
|
std::optional<Pose3D> last_keyframe_;
|
||||||
ErrorStateFilter filter_;
|
ErrorStateFilter filter_;
|
||||||
std::optional<ImuData> previous_imu_;
|
std::optional<ImuData> previous_imu_;
|
||||||
std::vector<ImuData> imu_history_;
|
std::vector<ImuData> imu_history_;
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "field_localization/core/registration.hpp"
|
||||||
|
#include <filesystem>
|
||||||
|
#include <string>
|
||||||
|
namespace field_localization::core {
|
||||||
|
bool save_map(const VoxelMap &map,const std::filesystem::path &path,std::string &error);
|
||||||
|
std::optional<VoxelMap> read_map(const std::filesystem::path &path,std::string &error);
|
||||||
|
bool new_keyframe(const Pose3D &previous,const Pose3D ¤t);
|
||||||
|
} // namespace field_localization::core
|
||||||
@@ -5,11 +5,22 @@
|
|||||||
|
|
||||||
namespace field_localization::core {
|
namespace field_localization::core {
|
||||||
LocalizationCore::LocalizationCore():LocalizationCore(CoreOptions{}) {}
|
LocalizationCore::LocalizationCore():LocalizationCore(CoreOptions{}) {}
|
||||||
LocalizationCore::LocalizationCore(const CoreOptions &options):options_(options),filter_(options.noise) {
|
LocalizationCore::LocalizationCore(const CoreOptions &options):options_(options),map_(options.map_resolution),filter_(options.noise) {
|
||||||
MotionState initial;
|
MotionState initial;
|
||||||
initial.pose=compose(options.initial_pose,options.base_from_imu);
|
initial.pose=compose(options.initial_pose,options.base_from_imu);
|
||||||
filter_.initialize(initial); pose_=options.initial_pose;
|
filter_.initialize(initial); pose_=options.initial_pose;
|
||||||
}
|
}
|
||||||
|
bool LocalizationCore::load_map(const std::filesystem::path &path,std::string &error) {
|
||||||
|
auto map=read_map(path,error);
|
||||||
|
if (!map) return false;
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
map_=std::move(*map); status_.map_point_count=map_.size(); return true;
|
||||||
|
}
|
||||||
|
bool LocalizationCore::export_map(const std::filesystem::path &path,std::string &error) const {
|
||||||
|
VoxelMap copy;
|
||||||
|
{ std::lock_guard<std::mutex> lock(mutex_); copy=map_; }
|
||||||
|
return save_map(copy,path,error);
|
||||||
|
}
|
||||||
void LocalizationCore::set_map_points(std::span<const Eigen::Vector3d> points) {
|
void LocalizationCore::set_map_points(std::span<const Eigen::Vector3d> points) {
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
map_.clear(); for (const auto &point:points) map_.insert(point);
|
map_.clear(); for (const auto &point:points) map_.insert(point);
|
||||||
@@ -110,7 +121,6 @@ bool LocalizationCore::process_point_cloud(const PointCloud &cloud) {
|
|||||||
|
|
||||||
++status_.point_cloud_count;
|
++status_.point_cloud_count;
|
||||||
status_.mode=TrackingMode::IMU_ONLY;
|
status_.mode=TrackingMode::IMU_ONLY;
|
||||||
if (map_.size()<5) { status_.reason=FailureReason::MAP_UNAVAILABLE; return true; }
|
|
||||||
const auto [start,end]=scan_window(cloud,options_.deskew);
|
const auto [start,end]=scan_window(cloud,options_.deskew);
|
||||||
if (imu_history_.empty() || start<imu_history_.front().timestamp_sec ||
|
if (imu_history_.empty() || start<imu_history_.front().timestamp_sec ||
|
||||||
end>imu_history_.back().timestamp_sec || end<=last_correction_time_) {
|
end>imu_history_.back().timestamp_sec || end<=last_correction_time_) {
|
||||||
@@ -135,6 +145,16 @@ bool LocalizationCore::process_point_cloud(const PointCloud &cloud) {
|
|||||||
if (end>a->timestamp_sec && !at_scan.propagate(*a,*finish)) {
|
if (end>a->timestamp_sec && !at_scan.propagate(*a,*finish)) {
|
||||||
status_.reason=FailureReason::IMU_GAP; return true;
|
status_.reason=FailureReason::IMU_GAP; return true;
|
||||||
}
|
}
|
||||||
|
if (map_.size()<5) {
|
||||||
|
if (options_.map_mode==MapMode::MAPPING && corrected->points.size()>=options_.registration.minimum_inliers) {
|
||||||
|
map_.insert(voxel_downsample(*corrected,options_.map_resolution),
|
||||||
|
compose(at_scan.snapshot().state.pose,options_.deskew.imu_from_sensor));
|
||||||
|
last_keyframe_=compose(at_scan.snapshot().state.pose,inverse(options_.base_from_imu));
|
||||||
|
status_.keyframe_count=1; status_.map_point_count=map_.size();
|
||||||
|
status_.reason=FailureReason::INITIALIZING;
|
||||||
|
} else status_.reason=FailureReason::MAP_UNAVAILABLE;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
const auto registration=scan_to_map(*corrected,options_.deskew.imu_from_sensor,map_,
|
const auto registration=scan_to_map(*corrected,options_.deskew.imu_from_sensor,map_,
|
||||||
at_scan.snapshot(),options_.noise,options_.registration);
|
at_scan.snapshot(),options_.noise,options_.registration);
|
||||||
status_.registration_rmse=registration.rmse; status_.inlier_count=registration.inliers;
|
status_.registration_rmse=registration.rmse; status_.inlier_count=registration.inliers;
|
||||||
@@ -160,6 +180,13 @@ bool LocalizationCore::process_point_cloud(const PointCloud &cloud) {
|
|||||||
}
|
}
|
||||||
imu_history_.swap(corrected_imus); state_history_.swap(corrected_states);
|
imu_history_.swap(corrected_imus); state_history_.swap(corrected_states);
|
||||||
filter_.restore(replay.snapshot());
|
filter_.restore(replay.snapshot());
|
||||||
|
const Pose3D scan_base=compose(registration.estimate.state.pose,inverse(options_.base_from_imu));
|
||||||
|
if (options_.map_mode==MapMode::MAPPING && registration.degeneracy.dimensions==0 &&
|
||||||
|
(!last_keyframe_ || new_keyframe(*last_keyframe_,scan_base))) {
|
||||||
|
map_.insert(voxel_downsample(*corrected,options_.map_resolution),
|
||||||
|
compose(registration.estimate.state.pose,options_.deskew.imu_from_sensor));
|
||||||
|
last_keyframe_=scan_base; ++status_.keyframe_count; status_.map_point_count=map_.size();
|
||||||
|
}
|
||||||
last_correction_time_=end; status_.mode=TrackingMode::FULL; status_.reason=FailureReason::NONE;
|
last_correction_time_=end; status_.mode=TrackingMode::FULL; status_.reason=FailureReason::NONE;
|
||||||
pose_=compose(filter_.snapshot().state.pose,inverse(options_.base_from_imu));
|
pose_=compose(filter_.snapshot().state.pose,inverse(options_.base_from_imu));
|
||||||
return true;
|
return true;
|
||||||
@@ -197,7 +224,9 @@ void LocalizationCore::reset() {
|
|||||||
pose_ = options_.initial_pose;
|
pose_ = options_.initial_pose;
|
||||||
MotionState initial; initial.pose=compose(pose_,options_.base_from_imu);
|
MotionState initial; initial.pose=compose(pose_,options_.base_from_imu);
|
||||||
filter_.initialize(initial); last_correction_time_=-1; previous_imu_.reset(); imu_history_.clear(); state_history_.clear();
|
filter_.initialize(initial); last_correction_time_=-1; previous_imu_.reset(); imu_history_.clear(); state_history_.clear();
|
||||||
status_ = LocalizationStatus{};
|
if (options_.map_mode==MapMode::MAPPING) map_.clear();
|
||||||
|
last_keyframe_.reset();
|
||||||
|
status_ = LocalizationStatus{}; status_.map_point_count=map_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace field_localization::core
|
} // namespace field_localization::core
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
#include "field_localization/core/map_io.hpp"
|
||||||
|
#include <array>
|
||||||
|
#include <bit>
|
||||||
|
#include <fstream>
|
||||||
|
#include <numbers>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace field_localization::core {
|
||||||
|
namespace {
|
||||||
|
constexpr std::array<char,8> magic{'F','L','M','A','P','0','0','1'};
|
||||||
|
template<class T> void write(std::ostream &out,T value) {
|
||||||
|
auto bytes=std::bit_cast<std::array<char,sizeof(T)>>(value);
|
||||||
|
if constexpr(std::endian::native==std::endian::big) std::reverse(bytes.begin(),bytes.end());
|
||||||
|
out.write(bytes.data(),bytes.size());
|
||||||
|
}
|
||||||
|
template<class T> T read(std::istream &in) {
|
||||||
|
std::array<char,sizeof(T)> bytes{}; in.read(bytes.data(),bytes.size());
|
||||||
|
if (!in) throw std::runtime_error("truncated map");
|
||||||
|
if constexpr(std::endian::native==std::endian::big) std::reverse(bytes.begin(),bytes.end());
|
||||||
|
return std::bit_cast<T>(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bool new_keyframe(const Pose3D &previous,const Pose3D ¤t) {
|
||||||
|
return (current.translation-previous.translation).norm()>0.5 ||
|
||||||
|
log_so3(previous.rotation.conjugate()*current.rotation).norm()>10*std::numbers::pi/180;
|
||||||
|
}
|
||||||
|
bool save_map(const VoxelMap &map,const std::filesystem::path &path,std::string &error) {
|
||||||
|
error.clear(); const auto temporary=std::filesystem::path(path.string()+".tmp");
|
||||||
|
try {
|
||||||
|
if (map.size()==0) throw std::runtime_error("cannot save an empty map");
|
||||||
|
std::ofstream out(temporary,std::ios::binary|std::ios::trunc);
|
||||||
|
out.exceptions(std::ios::badbit|std::ios::failbit);
|
||||||
|
out.write(magic.data(),magic.size()); write<std::uint32_t>(out,1);
|
||||||
|
write<double>(out,map.resolution()); write<std::uint64_t>(out,map.size());
|
||||||
|
for (const auto &p:map.points()) for (int i=0;i<3;++i) write<double>(out,p[i]);
|
||||||
|
out.close(); std::filesystem::rename(temporary,path); return true;
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
error=e.what(); std::error_code ignored; std::filesystem::remove(temporary,ignored); return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::optional<VoxelMap> read_map(const std::filesystem::path &path,std::string &error) {
|
||||||
|
error.clear();
|
||||||
|
try {
|
||||||
|
std::ifstream in(path,std::ios::binary);
|
||||||
|
if (!in) throw std::runtime_error("cannot open map: "+path.string());
|
||||||
|
std::array<char,8> signature{}; in.read(signature.data(),signature.size());
|
||||||
|
if (signature!=magic || read<std::uint32_t>(in)!=1) throw std::runtime_error("unsupported map format");
|
||||||
|
const double resolution=read<double>(in); const auto count=read<std::uint64_t>(in);
|
||||||
|
if (count==0 || count>5000000 || std::filesystem::file_size(path)!=28+count*24)
|
||||||
|
throw std::runtime_error("invalid map point count or length");
|
||||||
|
VoxelMap map(resolution);
|
||||||
|
for (std::uint64_t i=0;i<count;++i) {
|
||||||
|
Eigen::Vector3d p; for (int j=0;j<3;++j) p[j]=read<double>(in);
|
||||||
|
if (!p.allFinite() || p.cwiseAbs().maxCoeff()/resolution>1e12)
|
||||||
|
throw std::runtime_error("invalid map coordinate");
|
||||||
|
map.insert(p);
|
||||||
|
}
|
||||||
|
if (map.size()==0) throw std::runtime_error("empty voxel map");
|
||||||
|
return map;
|
||||||
|
} catch (const std::exception &e) { error=e.what(); return std::nullopt; }
|
||||||
|
}
|
||||||
|
} // namespace field_localization::core
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#include "field_localization/core/localization_core.hpp"
|
||||||
|
#include "test_util.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <iterator>
|
||||||
|
#include <numbers>
|
||||||
|
using namespace field_localization::core;
|
||||||
|
namespace {
|
||||||
|
std::string bytes(const std::filesystem::path &p) {
|
||||||
|
std::ifstream in(p,std::ios::binary); return {std::istreambuf_iterator<char>(in),{}};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int main() {
|
||||||
|
const auto folder=std::filesystem::temp_directory_path()/"field_lio_map_tests";
|
||||||
|
std::filesystem::create_directories(folder);
|
||||||
|
Pose3D previous,current;
|
||||||
|
current.translation.x()=0.5; CHECK(!new_keyframe(previous,current));
|
||||||
|
current.translation.x()=0.50001; CHECK(new_keyframe(previous,current));
|
||||||
|
current={}; current.rotation=exp_so3({0,0,9.99*std::numbers::pi/180}); CHECK(!new_keyframe(previous,current));
|
||||||
|
current.rotation=exp_so3({0,0,10.01*std::numbers::pi/180}); CHECK(new_keyframe(previous,current));
|
||||||
|
CoreOptions options; options.map_mode=MapMode::MAPPING; options.base_from_imu={};
|
||||||
|
LocalizationCore mapping(options);
|
||||||
|
ImuData imu; imu.linear_acceleration={0,0,9.81};
|
||||||
|
PointCloud scan;
|
||||||
|
for (int i=-15;i<=15;++i) for (int j=-15;j<=15;++j) {
|
||||||
|
for (const Eigen::Vector3f &p:{Eigen::Vector3f{0.12F*i,0.12F*j,0},Eigen::Vector3f{3,0.12F*i,0.12F*j},Eigen::Vector3f{0.12F*i,3,0.12F*j}}) {
|
||||||
|
PointXYZI point; point.position=p; scan.points.push_back(point);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i=0;i<=30;++i) { imu.timestamp_sec=0.01*i; CHECK(mapping.process_imu(imu)); }
|
||||||
|
scan.timestamp_sec=0.1; CHECK(mapping.process_point_cloud(scan)); CHECK(mapping.get_status().keyframe_count==1);
|
||||||
|
scan.timestamp_sec=0.2; CHECK(mapping.process_point_cloud(scan)); CHECK(mapping.get_status().mode==TrackingMode::FULL);
|
||||||
|
CHECK(mapping.get_status().keyframe_count==1);
|
||||||
|
Pose3D shifted; shifted.translation.x()=0.6; mapping.set_pose(shifted);
|
||||||
|
for (int i=40;i<=70;++i) { imu.timestamp_sec=0.01*i; CHECK(mapping.process_imu(imu)); }
|
||||||
|
scan.timestamp_sec=0.6;
|
||||||
|
for (auto &p:scan.points) p.position.x()-=0.6F;
|
||||||
|
CHECK(mapping.process_point_cloud(scan)); CHECK(mapping.get_status().keyframe_count==2);
|
||||||
|
std::string error;
|
||||||
|
CHECK(mapping.export_map(folder/"map.flmap",error)); CHECK(error.empty());
|
||||||
|
const auto map=read_map(folder/"map.flmap",error); CHECK(map.has_value());
|
||||||
|
LocalizationCore localization;
|
||||||
|
CHECK(localization.load_map(folder/"map.flmap",error));
|
||||||
|
const auto count=localization.get_status().map_point_count;
|
||||||
|
for (int i=0;i<=30;++i) { imu.timestamp_sec=0.01*i; CHECK(localization.process_imu(imu)); }
|
||||||
|
// Map must remain byte-identical even if localization measurements are rejected.
|
||||||
|
scan.timestamp_sec=0.2; CHECK(localization.process_point_cloud(scan));
|
||||||
|
CHECK(localization.get_status().map_point_count==count);
|
||||||
|
CHECK(localization.export_map(folder/"readonly.flmap",error));
|
||||||
|
CHECK(bytes(folder/"map.flmap")==bytes(folder/"readonly.flmap"));
|
||||||
|
{ std::ofstream out(folder/"broken.flmap",std::ios::binary); out<<"FLMAP001"; }
|
||||||
|
CHECK(!localization.load_map(folder/"broken.flmap",error)); CHECK(!error.empty());
|
||||||
|
CHECK(localization.get_status().map_point_count==count);
|
||||||
|
CHECK(!localization.load_map(folder/"missing.flmap",error));
|
||||||
|
mapping.reset(); CHECK(mapping.get_status().map_point_count==0);
|
||||||
|
localization.reset(); CHECK(localization.get_status().map_point_count==count);
|
||||||
|
std::filesystem::remove_all(folder);
|
||||||
|
return sentry_nav_test::Summary("MapManagement");
|
||||||
|
}
|
||||||
@@ -327,3 +327,12 @@ target("test_lio_synthetic")
|
|||||||
add_files("tests/test_lio_synthetic.cpp")
|
add_files("tests/test_lio_synthetic.cpp")
|
||||||
add_deps("field_localization_core")
|
add_deps("field_localization_core")
|
||||||
add_tests("default", {group = "unit"})
|
add_tests("default", {group = "unit"})
|
||||||
|
|
||||||
|
|
||||||
|
target("test_map_management")
|
||||||
|
set_targetdir("$(builddir)/bin")
|
||||||
|
set_kind("binary")
|
||||||
|
set_languages("c++23")
|
||||||
|
add_files("tests/test_map_management.cpp")
|
||||||
|
add_deps("field_localization_core")
|
||||||
|
add_tests("default", {group = "unit"})
|
||||||
Reference in new issue
Block a user