-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathse3_localization.cpp
304 lines (243 loc) · 10.1 KB
/
se3_localization.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* \file se3_localization.cpp
*
* Created on: Dec 12, 2018
* \author: jsola
*
* ---------------------------------------------------------
* This file is:
* (c) 2018 Joan Sola @ IRI-CSIC, Barcelona, Catalonia
*
* This file is part of `manif`, a C++ template-only library
* for Lie theory targeted at estimation for robotics.
* Manif is:
* (c) 2018 Jeremie Deray @ IRI-UPC, Barcelona
* ---------------------------------------------------------
*
* ---------------------------------------------------------
* Demonstration example:
*
* 3D Robot localization based on fixed beacons.
*
* See se2_localization.cpp for the 2D equivalent.
* See se3_sam.cpp for a more advanced example performing smoothing and mapping.
* ---------------------------------------------------------
*
* This demo corresponds to the 3D version of the application
* in chapter V, section A, in the paper Sola-18,
* [https://arxiv.org/abs/1812.01537].
*
* The following is an abstract of the content of the paper.
* Please consult the paper for better reference.
*
*
* We consider a robot in 3D space surrounded by a small
* number of punctual landmarks or _beacons_.
* The robot receives control actions in the form of axial
* and angular velocities, and is able to measure the location
* of the beacons w.r.t its own reference frame.
*
* The robot pose X is in SE(3) and the beacon positions b_k in R^3,
*
* X = | R t | // position and orientation
* | 0 1 |
*
* b_k = (bx_k, by_k, bz_k) // lmk coordinates in world frame
*
* The control signal u is a twist in se(3) comprising longitudinal
* velocity vx and angular velocity wz, with no other velocity
* components, integrated over the sampling time dt.
*
* u = (vx*dt, 0, 0, 0, 0, w*dt)
*
* The control is corrupted by additive Gaussian noise u_noise,
* with covariance
*
* Q = diagonal(sigma_x^2, sigma_y^2, sigma_z^2, sigma_roll^2, sigma_pitch^2, sigma_yaw^2).
*
* This noise accounts for possible lateral and rotational slippage
* through a non-zero values of sigma_y, sigma_z, sigma_roll and sigma_pitch.
*
* At the arrival of a control u, the robot pose is updated
* with X <-- X * Exp(u) = X + u.
*
* Landmark measurements are of the range and bearing type,
* though they are put in Cartesian form for simplicity.
* Their noise n is zero mean Gaussian, and is specified
* with a covariances matrix R.
* We notice the rigid motion action y = h(X,b) = X^-1 * b
* (see appendix D),
*
* y_k = (brx_k, bry_k, brz_k) // lmk coordinates in robot frame
*
* We consider the beacons b_k situated at known positions.
* We define the pose to estimate as X in SE(3).
* The estimation error dx and its covariance P are expressed
* in the tangent space at X.
*
* All these variables are summarized again as follows
*
* X : robot pose, SE(3)
* u : robot control, (v*dt; 0; 0; 0; 0; w*dt) in se(3)
* Q : control perturbation covariance
* b_k : k-th landmark position, R^3
* y : Cartesian landmark measurement in robot frame, R^3
* R : covariance of the measurement noise
*
* The motion and measurement models are
*
* X_(t+1) = f(X_t, u) = X_t * Exp ( w ) // motion equation
* y_k = h(X, b_k) = X^-1 * b_k // measurement equation
*
* The algorithm below comprises first a simulator to
* produce measurements, then uses these measurements
* to estimate the state, using a Lie-based error-state Kalman filter.
*
* This file has plain code with only one main() function.
* There are no function calls other than those involving `manif`.
*
* Printing simulated state and estimated state together
* with an unfiltered state (i.e. without Kalman corrections)
* allows for evaluating the quality of the estimates.
*/
#include "manif/SE3.h"
#include <Eigen/Dense>
#include <vector>
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using namespace Eigen;
typedef Array<double, 3, 1> Array3d;
typedef Array<double, 6, 1> Array6d;
typedef Matrix<double, 6, 1> Vector6d;
typedef Matrix<double, 6, 6> Matrix6d;
int main()
{
// START CONFIGURATION
//
//
const int NUMBER_OF_LMKS_TO_MEASURE = 5;
// Define the robot pose element and its covariance
manif::SE3d X, X_simulation, X_unfiltered;
Matrix6d P;
X_simulation.setIdentity();
X.setIdentity();
X_unfiltered.setIdentity();
P.setZero();
// Define a control vector and its noise and covariance
manif::SE3Tangentd u_simu, u_est, u_unfilt;
Vector6d u_nom, u_noisy, u_noise;
Array6d u_sigmas;
Matrix6d U;
u_nom << 0.1, 0.0, 0.0, 0.0, 0.0, 0.05;
u_sigmas << 0.1, 0.1, 0.1, 0.1, 0.1, 0.1;
U = (u_sigmas * u_sigmas).matrix().asDiagonal();
// Declare the Jacobians of the motion wrt robot and control
manif::SE3d::Jacobian J_x, J_u;
// Define five landmarks in R^3
Vector3d b0, b1, b2, b3, b4, b;
b0 << 2.0, 0.0, 0.0;
b1 << 3.0, -1.0, -1.0;
b2 << 2.0, -1.0, 1.0;
b3 << 2.0, 1.0, 1.0;
b4 << 2.0, 1.0, -1.0;
std::vector<Vector3d> landmarks;
landmarks.push_back(b0);
landmarks.push_back(b1);
landmarks.push_back(b2);
landmarks.push_back(b3);
landmarks.push_back(b4);
// Define the beacon's measurements
Vector3d y, y_noise;
Array3d y_sigmas;
Matrix3d R;
std::vector<Vector3d> measurements(landmarks.size());
y_sigmas << 0.01, 0.01, 0.01;
R = (y_sigmas * y_sigmas).matrix().asDiagonal();
// Declare the Jacobian of the measurements wrt the robot pose
Matrix<double, 3, 6> H; // H = J_e_x
// Declare some temporaries
Vector3d e, z; // expectation, innovation
Matrix3d E, Z; // covariances of the above
Matrix<double, 6, 3> K; // Kalman gain
manif::SE3Tangentd dx; // optimal update step, or error-state
manif::SE3d::Jacobian J_xi_x; // Jacobian is typedef Matrix
Matrix<double, 3, 6> J_e_xi; // Jacobian
//
//
// CONFIGURATION DONE
// DEBUG
cout << std::fixed << std::setprecision(3) << std::showpos << endl;
cout << "X STATE : X Y Z TH_x TH_y TH_z " << endl;
cout << "-------------------------------------------------------" << endl;
cout << "X initial : " << X_simulation.log().coeffs().transpose() << endl;
cout << "-------------------------------------------------------" << endl;
// END DEBUG
// START TEMPORAL LOOP
//
//
// Make 10 steps. Measure up to three landmarks each time.
for (int t = 0; t < 10; t++)
{
//// I. Simulation ###############################################################################
/// simulate noise
u_noise = u_sigmas * Array6d::Random(); // control noise
u_noisy = u_nom + u_noise; // noisy control
u_simu = u_nom;
u_est = u_noisy;
u_unfilt = u_noisy;
/// first we move - - - - - - - - - - - - - - - - - - - - - - - - - - - -
X_simulation = X_simulation + u_simu; // overloaded X.rplus(u) = X * exp(u)
/// then we measure all landmarks - - - - - - - - - - - - - - - - - - - -
for (int i = 0; i < landmarks.size(); i++)
{
b = landmarks[i]; // lmk coordinates in world frame
/// simulate noise
y_noise = y_sigmas * Array3d::Random(); // measurement noise
y = X_simulation.inverse().act(b); // landmark measurement, before adding noise
y = y + y_noise; // landmark measurement, noisy
measurements[i] = y; // store for the estimator just below
}
//// II. Estimation ###############################################################################
/// First we move - - - - - - - - - - - - - - - - - - - - - - - - - - - -
X = X.plus(u_est, J_x, J_u); // X * exp(u), with Jacobians
P = J_x * P * J_x.transpose() + J_u * U * J_u.transpose();
/// Then we correct using the measurements of each lmk - - - - - - - - -
for (int i = 0; i < NUMBER_OF_LMKS_TO_MEASURE; i++)
{
// landmark
b = landmarks[i]; // lmk coordinates in world frame
// measurement
y = measurements[i]; // lmk measurement, noisy
// expectation
e = X.inverse(J_xi_x).act(b, J_e_xi); // note: e = R.tr * ( b - t ), for X = (R,t).
H = J_e_xi * J_xi_x; // note: H = J_e_x = J_e_xi * J_xi_x
E = H * P * H.transpose();
// innovation
z = y - e;
Z = E + R;
// Kalman gain
K = P * H.transpose() * Z.inverse(); // K = P * H.tr * ( H * P * H.tr + R).inv
// Correction step
dx = K * z; // dx is in the tangent space at X
// Update
X = X + dx; // overloaded X.rplus(dx) = X * exp(dx)
P = P - K * Z * K.transpose();
}
//// III. Unfiltered ##############################################################################
// move also an unfiltered version for comparison purposes
X_unfiltered = X_unfiltered + u_unfilt;
//// IV. Results ##############################################################################
// DEBUG
cout << "X simulated : " << X_simulation.log().coeffs().transpose() << endl;
cout << "X estimated : " << X.log().coeffs().transpose() << endl;
cout << "X unfilterd : " << X_unfiltered.log().coeffs().transpose() << endl;
cout << "-------------------------------------------------------" << endl;
// END DEBUG
}
//
//
// END OF TEMPORAL LOOP. DONE.
return 0;
}