-
Notifications
You must be signed in to change notification settings - Fork 7
/
lidar_undistortion.hpp
205 lines (179 loc) · 8.58 KB
/
lidar_undistortion.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef LIDAR_UNDISTORTION_HPP_
#define LIDAR_UNDISTORTION_HPP_
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/common/common.h>
#include <pcl/common/eigen.h>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <iostream>
class LidarUndistortion
{
public:
LidarUndistortion(){}
// Ref:LeGO-LOAM(BSD-3 LICENSE)
// https://github.com/RobustFieldAutonomyLab/LeGO-LOAM/blob/master/LeGO-LOAM/src/featureAssociation.cpp#L431-L459
void getImu(Eigen::Vector3f angular_velo, Eigen::Vector3f acc, const Eigen::Quaternionf quat, const double imu_time /*[sec]*/)
{
float roll, pitch, yaw;
Eigen::Affine3f affine(quat);
pcl::getEulerAngles(affine, roll, pitch, yaw);
imu_ptr_last_ = (imu_ptr_last_ + 1) % imu_que_length_;
if ((imu_ptr_last_ + 1) % imu_que_length_ == imu_ptr_front_) {
imu_ptr_front_ = (imu_ptr_front_ + 1) % imu_que_length_;
}
imu_time_[imu_ptr_last_] = imu_time;
imu_roll_[imu_ptr_last_] = roll;
imu_pitch_[imu_ptr_last_] = pitch;
imu_yaw_[imu_ptr_last_] = yaw;
imu_acc_x_[imu_ptr_last_] = acc.x();
imu_acc_y_[imu_ptr_last_] = acc.y();
imu_acc_z_[imu_ptr_last_] = acc.z();
imu_angular_velo_x_[imu_ptr_last_] = angular_velo.x();
imu_angular_velo_y_[imu_ptr_last_] = angular_velo.y();
imu_angular_velo_z_[imu_ptr_last_] = angular_velo.z();
Eigen::Matrix3f rot = quat.toRotationMatrix();
acc = rot * acc;
angular_velo = rot * angular_velo;
int imu_ptr_back = (imu_ptr_last_ - 1 + imu_que_length_) % imu_que_length_;
double time_diff = imu_time_[imu_ptr_last_] - imu_time_[imu_ptr_back];
if (time_diff < scan_period_) {
imu_shift_x_[imu_ptr_last_] =
imu_shift_x_[imu_ptr_back] +imu_velo_x_[imu_ptr_back] * time_diff + acc(0) * time_diff * time_diff * 0.5;
imu_shift_y_[imu_ptr_last_] =
imu_shift_y_[imu_ptr_back] + imu_velo_y_[imu_ptr_back] * time_diff + acc(1) * time_diff * time_diff * 0.5;
imu_shift_z_[imu_ptr_last_] =
imu_shift_z_[imu_ptr_back] + imu_velo_z_[imu_ptr_back] * time_diff + acc(2) * time_diff * time_diff * 0.5;
imu_velo_x_[imu_ptr_last_] = imu_velo_x_[imu_ptr_back] + acc(0) * time_diff;
imu_velo_y_[imu_ptr_last_] = imu_velo_y_[imu_ptr_back] + acc(1) * time_diff;
imu_velo_z_[imu_ptr_last_] = imu_velo_z_[imu_ptr_back] + acc(2) * time_diff;
imu_angular_rot_x_[imu_ptr_last_] = imu_angular_rot_x_[imu_ptr_back] + angular_velo(0) * time_diff;
imu_angular_rot_y_[imu_ptr_last_] = imu_angular_rot_y_[imu_ptr_back] + angular_velo(1) * time_diff;
imu_angular_rot_z_[imu_ptr_last_] = imu_angular_rot_z_[imu_ptr_back] + angular_velo(2) * time_diff;
}
}
// Ref:LeGO-LOAM(BSD-3 LICENSE)
// https://github.com/RobustFieldAutonomyLab/LeGO-LOAM/blob/master/LeGO-LOAM/src/featureAssociation.cpp#L491-L619
void adjustDistortion(pcl::PointCloud<pcl::PointXYZI>::Ptr & cloud, const double scan_time /*[sec]*/)
{
bool half_passed = false;
int cloud_size = cloud->points.size();
float start_ori = -std::atan2(cloud->points[0].y, cloud->points[0].x);
float end_ori = -std::atan2(cloud->points[cloud_size - 1].y, cloud->points[cloud_size - 1].x);
if (end_ori - start_ori > 3 * M_PI) {
end_ori -= 2 * M_PI;
}
else if (end_ori - start_ori < M_PI) {
end_ori += 2 * M_PI;
}
float ori_diff = end_ori - start_ori;
Eigen::Vector3f rpy_start, shift_start, velo_start, rpy_cur, shift_cur, velo_cur;
Eigen::Vector3f shift_from_start;
Eigen::Matrix3f r_s_i, r_c;
Eigen::Vector3f adjusted_p;
float ori_h;
for (int i = 0; i < cloud_size; ++i) {
pcl::PointXYZI &p = cloud->points[i];
ori_h = -std::atan2(p.y, p.x);
if (!half_passed) {
if (ori_h < start_ori - M_PI * 0.5) {
ori_h += 2 * M_PI;
} else if (ori_h > start_ori + M_PI * 1.5) {
ori_h -= 2 * M_PI;
}
if (ori_h - start_ori > M_PI) {
half_passed = true;
}
} else {
ori_h += 2 * M_PI;
if (ori_h < end_ori - 1.5 * M_PI) {
ori_h += 2 * M_PI;
}
else if (ori_h > end_ori + 0.5 * M_PI) {
ori_h -= 2 * M_PI;
}
}
float rel_time = (ori_h - start_ori) / ori_diff * scan_period_;
if (imu_ptr_last_ > 0) {
imu_ptr_front_ = imu_ptr_last_iter_;
while (imu_ptr_front_ != imu_ptr_last_) {
if (scan_time + rel_time > imu_time_[imu_ptr_front_]) {
break;
}
imu_ptr_front_ = (imu_ptr_front_ + 1) % imu_que_length_;
}
if (scan_time + rel_time > imu_time_[imu_ptr_front_]) {
rpy_cur(0) = imu_roll_[imu_ptr_front_];
rpy_cur(1) = imu_pitch_[imu_ptr_front_];
rpy_cur(2) = imu_yaw_[imu_ptr_front_];
shift_cur(0) = imu_shift_x_[imu_ptr_front_];
shift_cur(1) = imu_shift_y_[imu_ptr_front_];
shift_cur(2) = imu_shift_z_[imu_ptr_front_];
velo_cur(0) = imu_velo_x_[imu_ptr_front_];
velo_cur(1) = imu_velo_y_[imu_ptr_front_];
velo_cur(2) = imu_velo_z_[imu_ptr_front_];
} else {
int imu_ptr_back = (imu_ptr_front_ - 1 + imu_que_length_) % imu_que_length_;
float ratio_front = (scan_time + rel_time - imu_time_[imu_ptr_back]) /
(imu_time_[imu_ptr_front_] - imu_time_[imu_ptr_back]);
float ratio_back = 1.0 - ratio_front;
rpy_cur(0) = imu_roll_[imu_ptr_front_] * ratio_front + imu_roll_[imu_ptr_back] * ratio_back;
rpy_cur(1) = imu_pitch_[imu_ptr_front_] * ratio_front + imu_pitch_[imu_ptr_back] * ratio_back;
rpy_cur(2) = imu_yaw_[imu_ptr_front_] * ratio_front + imu_yaw_[imu_ptr_back] * ratio_back;
shift_cur(0) = imu_shift_x_[imu_ptr_front_] * ratio_front + imu_shift_x_[imu_ptr_back] * ratio_back;
shift_cur(1) = imu_shift_y_[imu_ptr_front_] * ratio_front + imu_shift_y_[imu_ptr_back] * ratio_back;
shift_cur(2) = imu_shift_z_[imu_ptr_front_] * ratio_front + imu_shift_z_[imu_ptr_back] * ratio_back;
velo_cur(0) = imu_velo_x_[imu_ptr_front_] * ratio_front + imu_velo_x_[imu_ptr_back] * ratio_back;
velo_cur(1) = imu_velo_y_[imu_ptr_front_] * ratio_front + imu_velo_y_[imu_ptr_back] * ratio_back;
velo_cur(2) = imu_velo_z_[imu_ptr_front_] * ratio_front + imu_velo_z_[imu_ptr_back] * ratio_back;
}
r_c = (
Eigen::AngleAxisf(rpy_cur(2), Eigen::Vector3f::UnitZ()) *
Eigen::AngleAxisf(rpy_cur(1), Eigen::Vector3f::UnitY()) *
Eigen::AngleAxisf(rpy_cur(0), Eigen::Vector3f::UnitX())
).toRotationMatrix();
if (i == 0) {
rpy_start = rpy_cur;
shift_start = shift_cur;
velo_start = velo_cur;
r_s_i = r_c.inverse();
} else {
shift_from_start = shift_cur - shift_start - velo_start * rel_time;
adjusted_p = r_s_i * (r_c * Eigen::Vector3f(p.x, p.y, p.z) + shift_from_start);
p.x = adjusted_p.x();
p.y = adjusted_p.y();
p.z = adjusted_p.z();
}
}
imu_ptr_last_iter_ = imu_ptr_front_;
}
}
void setScanPeriod(const double scan_period /*[sec]*/)
{
scan_period_ = scan_period;
}
private:
double scan_period_{0.1};
static const int imu_que_length_{200};
int imu_ptr_front_{0}, imu_ptr_last_{-1}, imu_ptr_last_iter_{0};
std::array<double, imu_que_length_> imu_time_;
std::array<float, imu_que_length_> imu_roll_;
std::array<float, imu_que_length_> imu_pitch_;
std::array<float, imu_que_length_> imu_yaw_;
std::array<float, imu_que_length_> imu_acc_x_;
std::array<float, imu_que_length_> imu_acc_y_;
std::array<float, imu_que_length_> imu_acc_z_;
std::array<float, imu_que_length_> imu_velo_x_;
std::array<float, imu_que_length_> imu_velo_y_;
std::array<float, imu_que_length_> imu_velo_z_;
std::array<float, imu_que_length_> imu_shift_x_;
std::array<float, imu_que_length_> imu_shift_y_;
std::array<float, imu_que_length_> imu_shift_z_;
std::array<float, imu_que_length_> imu_angular_velo_x_;
std::array<float, imu_que_length_> imu_angular_velo_y_;
std::array<float, imu_que_length_> imu_angular_velo_z_;
std::array<float, imu_que_length_> imu_angular_rot_x_;
std::array<float, imu_que_length_> imu_angular_rot_y_;
std::array<float, imu_que_length_> imu_angular_rot_z_;
};
#endif // LIDAR_UNDISTORTION_HPP_