|
| 1 | +#ifndef LIDAR_LOCALIZATION_EXPERIMENT_IMU_ROTATION_PREDICTION_HPP_ |
| 2 | +#define LIDAR_LOCALIZATION_EXPERIMENT_IMU_ROTATION_PREDICTION_HPP_ |
| 3 | + |
| 4 | +#include <Eigen/Core> |
| 5 | +#include <Eigen/Geometry> |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <cmath> |
| 9 | +#include <cstddef> |
| 10 | +#include <deque> |
| 11 | +#include <iterator> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +namespace lidar_localization |
| 15 | +{ |
| 16 | + |
| 17 | +enum class ImuRotationPredictionStatus |
| 18 | +{ |
| 19 | + kReady = 0, |
| 20 | + kDisabled, |
| 21 | + kInvalidInterval, |
| 22 | + kDurationTooLarge, |
| 23 | + kInsufficientCoverage, |
| 24 | + kSampleGapTooLarge, |
| 25 | + kNonFiniteSample, |
| 26 | +}; |
| 27 | + |
| 28 | +inline const char * imuRotationPredictionStatusName(ImuRotationPredictionStatus status) |
| 29 | +{ |
| 30 | + switch (status) { |
| 31 | + case ImuRotationPredictionStatus::kReady: return "imu_rotation_prediction_ready"; |
| 32 | + case ImuRotationPredictionStatus::kDisabled: return "imu_rotation_prediction_disabled"; |
| 33 | + case ImuRotationPredictionStatus::kInvalidInterval: |
| 34 | + return "imu_rotation_prediction_invalid_interval"; |
| 35 | + case ImuRotationPredictionStatus::kDurationTooLarge: |
| 36 | + return "imu_rotation_prediction_duration_too_large"; |
| 37 | + case ImuRotationPredictionStatus::kInsufficientCoverage: |
| 38 | + return "imu_rotation_prediction_insufficient_coverage"; |
| 39 | + case ImuRotationPredictionStatus::kSampleGapTooLarge: |
| 40 | + return "imu_rotation_prediction_sample_gap_too_large"; |
| 41 | + case ImuRotationPredictionStatus::kNonFiniteSample: |
| 42 | + return "imu_rotation_prediction_non_finite_sample"; |
| 43 | + } |
| 44 | + return "imu_rotation_prediction_unknown"; |
| 45 | +} |
| 46 | + |
| 47 | +struct ImuRotationSample |
| 48 | +{ |
| 49 | + double stamp_sec{0.0}; |
| 50 | + Eigen::Vector3d angular_velocity_base_rad_s{Eigen::Vector3d::Zero()}; |
| 51 | +}; |
| 52 | + |
| 53 | +struct ImuRotationPredictionResult |
| 54 | +{ |
| 55 | + ImuRotationPredictionStatus status{ImuRotationPredictionStatus::kInsufficientCoverage}; |
| 56 | + Eigen::Matrix3f relative_rotation{Eigen::Matrix3f::Identity()}; |
| 57 | + double duration_sec{0.0}; |
| 58 | + double max_sample_gap_sec{0.0}; |
| 59 | + std::size_t integrated_sample_count{0}; |
| 60 | + |
| 61 | + bool ready() const {return status == ImuRotationPredictionStatus::kReady;} |
| 62 | +}; |
| 63 | + |
| 64 | +class ImuRotationPredictionBuffer |
| 65 | +{ |
| 66 | +public: |
| 67 | + explicit ImuRotationPredictionBuffer(double history_duration_sec = 2.0) |
| 68 | + : history_duration_sec_(history_duration_sec) {} |
| 69 | + |
| 70 | + bool addSample(double stamp_sec, const Eigen::Vector3d & angular_velocity_base_rad_s) |
| 71 | + { |
| 72 | + if (!std::isfinite(stamp_sec) || !angular_velocity_base_rad_s.allFinite()) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + if (!samples_.empty() && stamp_sec <= samples_.back().stamp_sec) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + samples_.push_back({stamp_sec, angular_velocity_base_rad_s}); |
| 79 | + const double oldest_stamp = stamp_sec - history_duration_sec_; |
| 80 | + while (samples_.size() > 2 && samples_[1].stamp_sec < oldest_stamp) { |
| 81 | + samples_.pop_front(); |
| 82 | + } |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + void clear() {samples_.clear();} |
| 87 | + std::size_t size() const {return samples_.size();} |
| 88 | + |
| 89 | + ImuRotationPredictionResult predict( |
| 90 | + double start_sec, double end_sec, double max_duration_sec, |
| 91 | + double max_sample_gap_sec) const |
| 92 | + { |
| 93 | + ImuRotationPredictionResult result; |
| 94 | + result.duration_sec = end_sec - start_sec; |
| 95 | + if (!std::isfinite(start_sec) || !std::isfinite(end_sec) || end_sec <= start_sec) { |
| 96 | + result.status = ImuRotationPredictionStatus::kInvalidInterval; |
| 97 | + return result; |
| 98 | + } |
| 99 | + if (max_duration_sec <= 0.0 || result.duration_sec > max_duration_sec) { |
| 100 | + result.status = ImuRotationPredictionStatus::kDurationTooLarge; |
| 101 | + return result; |
| 102 | + } |
| 103 | + if ( |
| 104 | + samples_.size() < 2 || samples_.front().stamp_sec > start_sec || |
| 105 | + samples_.back().stamp_sec < end_sec) |
| 106 | + { |
| 107 | + result.status = ImuRotationPredictionStatus::kInsufficientCoverage; |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + std::vector<ImuRotationSample> window; |
| 112 | + window.reserve(samples_.size() + 2); |
| 113 | + window.push_back({start_sec, interpolate(start_sec)}); |
| 114 | + for (const auto & sample : samples_) { |
| 115 | + if (sample.stamp_sec > start_sec && sample.stamp_sec < end_sec) { |
| 116 | + window.push_back(sample); |
| 117 | + } |
| 118 | + } |
| 119 | + window.push_back({end_sec, interpolate(end_sec)}); |
| 120 | + |
| 121 | + Eigen::Quaterniond rotation = Eigen::Quaterniond::Identity(); |
| 122 | + for (std::size_t index = 1; index < window.size(); ++index) { |
| 123 | + const double dt = window[index].stamp_sec - window[index - 1].stamp_sec; |
| 124 | + result.max_sample_gap_sec = std::max(result.max_sample_gap_sec, dt); |
| 125 | + if (!std::isfinite(dt) || dt <= 0.0 || |
| 126 | + max_sample_gap_sec <= 0.0 || dt > max_sample_gap_sec) |
| 127 | + { |
| 128 | + result.status = ImuRotationPredictionStatus::kSampleGapTooLarge; |
| 129 | + return result; |
| 130 | + } |
| 131 | + const Eigen::Vector3d mean_rate = 0.5 * ( |
| 132 | + window[index - 1].angular_velocity_base_rad_s + |
| 133 | + window[index].angular_velocity_base_rad_s); |
| 134 | + if (!mean_rate.allFinite()) { |
| 135 | + result.status = ImuRotationPredictionStatus::kNonFiniteSample; |
| 136 | + return result; |
| 137 | + } |
| 138 | + const Eigen::Vector3d rotation_vector = mean_rate * dt; |
| 139 | + const double angle = rotation_vector.norm(); |
| 140 | + if (angle > 1e-12) { |
| 141 | + rotation = (rotation * Eigen::Quaterniond( |
| 142 | + Eigen::AngleAxisd(angle, rotation_vector / angle))).normalized(); |
| 143 | + } |
| 144 | + ++result.integrated_sample_count; |
| 145 | + } |
| 146 | + result.relative_rotation = rotation.toRotationMatrix().cast<float>(); |
| 147 | + if (!result.relative_rotation.allFinite()) { |
| 148 | + result.status = ImuRotationPredictionStatus::kNonFiniteSample; |
| 149 | + result.relative_rotation = Eigen::Matrix3f::Identity(); |
| 150 | + return result; |
| 151 | + } |
| 152 | + result.status = ImuRotationPredictionStatus::kReady; |
| 153 | + return result; |
| 154 | + } |
| 155 | + |
| 156 | +private: |
| 157 | + Eigen::Vector3d interpolate(double stamp_sec) const |
| 158 | + { |
| 159 | + auto right = std::lower_bound( |
| 160 | + samples_.begin(), samples_.end(), stamp_sec, |
| 161 | + [](const ImuRotationSample & sample, double stamp) { |
| 162 | + return sample.stamp_sec < stamp; |
| 163 | + }); |
| 164 | + if (right == samples_.begin()) { |
| 165 | + return right->angular_velocity_base_rad_s; |
| 166 | + } |
| 167 | + if (right == samples_.end()) { |
| 168 | + return samples_.back().angular_velocity_base_rad_s; |
| 169 | + } |
| 170 | + if (right->stamp_sec == stamp_sec) { |
| 171 | + return right->angular_velocity_base_rad_s; |
| 172 | + } |
| 173 | + const auto left = std::prev(right); |
| 174 | + const double fraction = |
| 175 | + (stamp_sec - left->stamp_sec) / (right->stamp_sec - left->stamp_sec); |
| 176 | + return left->angular_velocity_base_rad_s + fraction * ( |
| 177 | + right->angular_velocity_base_rad_s - left->angular_velocity_base_rad_s); |
| 178 | + } |
| 179 | + |
| 180 | + double history_duration_sec_{2.0}; |
| 181 | + std::deque<ImuRotationSample> samples_; |
| 182 | +}; |
| 183 | + |
| 184 | +inline Eigen::Matrix4f applyImuRotationPrediction( |
| 185 | + const Eigen::Matrix4f & accepted_pose, |
| 186 | + const Eigen::Matrix4f & translation_seed, |
| 187 | + const Eigen::Matrix3f & relative_rotation) |
| 188 | +{ |
| 189 | + Eigen::Matrix4f result = translation_seed; |
| 190 | + result.block<3, 3>(0, 0) = |
| 191 | + accepted_pose.block<3, 3>(0, 0) * relative_rotation; |
| 192 | + return result; |
| 193 | +} |
| 194 | + |
| 195 | +} // namespace lidar_localization |
| 196 | + |
| 197 | +#endif // LIDAR_LOCALIZATION_EXPERIMENT_IMU_ROTATION_PREDICTION_HPP_ |
0 commit comments