File: bundle_sam.cpp

package info (click to toggle)
manif 0.0.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,576 kB
  • sloc: cpp: 11,789; ansic: 8,774; python: 2,158; sh: 24; makefile: 23; xml: 21
file content (386 lines) | stat: -rw-r--r-- 14,368 bytes parent folder | download
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
 * \file bundle_sam.cpp
 *
 *  Created on: Feb 28, 2021
 *      \author: pettni
 *
 * Adopted from se2_sam.cpp, see that file for explanations.
 */


// manif
#include "manif/Rn.h"
#include "manif/SE2.h"
#include "manif/Bundle.h"

// Std
#include <vector>
#include <map>
#include <list>
#include <cstdlib>

// Debug
#include <iostream>
#include <iomanip>

// std shortcuts and namespaces
using std::cout;
using std::endl;
using std::vector;
using std::map;
using std::list;
using std::pair;

// Eigen namespace
using namespace Eigen;

// manif namespace and shortcuts
using manif::SE2d;
using manif::SE2Tangentd;

static constexpr int DoF = SE2d::DoF;
static constexpr int Dim = SE2d::Dim;

// Define many data types (Tangent refers to the tangent of SE2)
typedef Array<double,  DoF, 1>      ArrayT;     // tangent-size array
typedef Matrix<double, DoF, 1>      VectorT;    // tangent-size vector
typedef Matrix<double, DoF, DoF>    MatrixT;    // tangent-size square matrix
typedef Matrix<double, Dim, 1>      VectorB;    // landmark-size vector
typedef Array<double,  Dim, 1>      ArrayY;     // measurement-size array
typedef Matrix<double, Dim, 1>      VectorY;    // measurement-size vector
typedef Matrix<double, Dim, Dim>    MatrixY;    // measurement-size square matrix
typedef Matrix<double, Dim, DoF>    MatrixYT;   // measurement x tangent size matrix
typedef Matrix<double, Dim, Dim>    MatrixYB;   // measurement x landmark size matrix

// some experiment constants
static const int NUM_POSES      = 3;
static const int NUM_LMKS       = 5;
static const int NUM_FACTORS    = 9;
static const int NUM_MEAS       = NUM_POSES * DoF + NUM_FACTORS * Dim;
static const int MAX_ITER       = 20;           // for the solver


// bundle state type to optimize over
using BundleT = manif::Bundle<double,
    manif::SE2,
    manif::SE2,
    manif::SE2,
    manif::R2,
    manif::R2,
    manif::R2,
    manif::R2,
    manif::R2
>;

// Insert a relative pose factor from pose XI to pose XJ
// into the residual-jacobian pair (r, J)
template<std::size_t XI, std::size_t XJ>
void add_pose_factor(
    const BundleT & X,
    const SE2Tangentd & control,
    const MatrixT & W,
    Eigen::Ref<Eigen::Matrix<double, 3, 1>> r,
    Eigen::Ref<Eigen::Matrix<double, 3, BundleT::DoF>> J)
{
    // index start position and length in the DoF of BundleT
    const int BegI = std::get<XI>(manif::internal::traits<BundleT>::DoFIdx);
    constexpr int LenI = BundleT::Element<XI>::DoF;
    const int BegJ = std::get<XJ>(manif::internal::traits<BundleT>::DoFIdx);
    constexpr int LenJ = BundleT::Element<XJ>::DoF;

    MatrixT         J_d_xi, J_d_xj; // Jacobian of motion wrt poses i and j

    auto d = X.element<XJ>().rminus(X.element<XI>(), J_d_xj, J_d_xi);
    r = W * (d - control).coeffs();
    J.setZero();
    J.block<3, LenI>(0, BegI) = W * J_d_xi;
    J.block<3, LenJ>(0, BegJ) = W * J_d_xj;
}


// Insert a landmark measurement factor of landmark LK
// from pose XI into the residual-jacobian pair (r, J)
template<std::size_t XI, std::size_t LK>
void add_beacon_factor(
    const BundleT & X,
    const VectorY & measurement,
    const MatrixY & S,
    Eigen::Ref<Eigen::Matrix<double, 2, 1>> r,
    Eigen::Ref<Eigen::Matrix<double, 2, BundleT::DoF>> J)
{
    // index start position and length in the DoF of BundleT
    const int BegX = std::get<XI>(manif::internal::traits<BundleT>::DoFIdx);
    constexpr int LenX = BundleT::Element<XI>::DoF;
    const int BegLMK = std::get<NUM_POSES + LK>(manif::internal::traits<BundleT>::DoFIdx);
    constexpr int LenLMK = BundleT::Element<NUM_POSES + LK>::DoF;

    MatrixT         J_ix_x;         // Jacobian of inverse pose wrt pose
    MatrixYT        J_e_ix;         // Jacobian of measurement expectation wrt inverse pose
    MatrixYT        J_e_x;          // Jacobian of measurement expectation wrt pose
    MatrixYB        J_e_b;          // Jacobian of measurement expectation wrt lmk

    auto e = X.element<XI>().inverse(J_ix_x).act(X.element<NUM_POSES + LK>().coeffs(), J_e_ix, J_e_b);
    J_e_x = J_e_ix * J_ix_x;
    r = S * (e - measurement);
    J.setZero();
    J.block<Dim, LenX>(0, BegX) = S * J_e_x;
    J.block<Dim, LenLMK>(0, BegLMK) = S * J_e_b;
}


int main()
{
    std::srand((unsigned int) time(0));

    // DEBUG INFO
    cout << endl;
    cout << "2D Smoothing and Mapping. 3 poses, 5 landmarks." << endl;
    cout << "-----------------------------------------------" << endl;
    cout << std::fixed   << std::setprecision(3) << std::showpos;

    // START CONFIGURATION
    //
    //

    // Define the robot pose elements
    SE2d         X_simu,    // pose of the simulated robot
                 Xi,        // robot pose at time i
                 Xj;        // robot pose at time j
    vector<SE2d> poses,     // estimator poses
                 poses_simu;// simulator poses
    Xi.setIdentity();
    X_simu.setIdentity();


    // Define a control vector and its noise and covariance in the tangent of SE2
    SE2Tangentd         u;          // control signal, generic
    SE2Tangentd         u_nom;      // nominal control signal
    ArrayT              u_sigmas;   // control noise std specification
    VectorT             u_noise;    // control noise
    // MatrixT             Q;          // Covariance
    MatrixT             W;          // sqrt Info
    vector<SE2Tangentd> controls;   // robot controls

    u_nom     << 0.1, 0.0, 0.05;
    u_sigmas  << 0.01, 0.01, 0.01;
    // Q         = (u_sigmas * u_sigmas).matrix().asDiagonal();
    W         =  u_sigmas.inverse()  .matrix().asDiagonal(); // this is Q^(-T/2)

    // Landmarks in R^2 and map
    VectorB b; // Landmark, generic
    vector<VectorB> landmarks(NUM_LMKS), landmarks_simu;
    {
        // Define five landmarks (beacons) in R^2
        VectorB b0, b1, b2, b3, b4;
        b0 << 3.0,  0.0;
        b1 << 2.0, -1.0;
        b2 << 2.0,  1.0;
        b3 << 3.0, -1.0;
        b4 << 3.0,  1.0;
        landmarks_simu.push_back(b0);
        landmarks_simu.push_back(b1);
        landmarks_simu.push_back(b2);
        landmarks_simu.push_back(b3);
        landmarks_simu.push_back(b4);
    } // destroy b0...b4

    // Define the beacon's measurements in R^2
    VectorY             y, y_noise;
    ArrayY              y_sigmas;
    // MatrixY             R; // Covariance
    MatrixY             S; // sqrt Info
    vector<map<int,VectorY>> measurements(NUM_POSES); // y = measurements[pose_id][lmk_id]

    y_sigmas << 0.001, 0.001;
    // R        = (y_sigmas * y_sigmas).matrix().asDiagonal();
    S        =  y_sigmas.inverse()  .matrix().asDiagonal(); // this is R^(-T/2)

    // Problem-size variables

    /*
     * The factor graph of the SAM problem looks like this:
     *
     *                  ------- b1
     *          b3    /         |
     *          |   /       b4  |
     *          | /       /    \|
     *          X0 ---- X1 ---- X2
     *          | \   /   \   /
     *          |   b0      b2
     *          *
     *
     * where:
     *   - Xi are poses
     *   - bk are landmarks or beacons
     *   - * is a pose prior to anchor the map and make the problem observable
     *
     * Define pairs of nodes for all the landmark measurements
     * There are 3 pose nodes [0..2] and 5 landmarks [0..4].
     * A pair pose -- lmk means that the lmk was measured from the pose
     * Each pair declares a factor in the factor graph
     * We declare 9 pairs, or 9 factors, as follows:
     */
    vector<list<int>> pairs(NUM_POSES);
    pairs[0].push_back(0);  // 0-0
    pairs[0].push_back(1);  // 0-1
    pairs[0].push_back(3);  // 0-3
    pairs[1].push_back(0);  // 1-0
    pairs[1].push_back(2);  // 1-2
    pairs[1].push_back(4);  // 1-4
    pairs[2].push_back(1);  // 2-1
    pairs[2].push_back(2);  // 2-2
    pairs[2].push_back(4);  // 2-4

    //
    //
    // END CONFIGURATION


    //// Simulator ###################################################################
    poses_simu. push_back(X_simu);
    poses.      push_back(Xi + SE2Tangentd::Random());  // use very noisy priors

    // temporal loop
    for (int i = 0; i < NUM_POSES; ++i)
    {
        // make measurements
        for (const auto& k : pairs[i])
        {
            // simulate measurement
            b       = landmarks_simu[k];              // lmk coordinates in world frame
            y_noise = y_sigmas * ArrayY::Random();      // measurement noise
            y       = X_simu.inverse().act(b);          // landmark measurement, before adding noise

            // add noise and compute prior lmk from prior pose
            measurements[i][k]  = y + y_noise;           // store noisy measurements
            b                   = Xi.act(y + y_noise);   // mapped landmark with noise
            landmarks[k]        = b + VectorB::Random(); // use very noisy priors
        }

        // make motions
        if (i < NUM_POSES - 1) // do not make the last motion since we're done after 3rd pose
        {
            // move simulator, without noise
            X_simu = X_simu + u_nom;

            // move prior, with noise
            u_noise = u_sigmas * ArrayT::Random();
            Xi = Xi + (u_nom + u_noise);

            // store
            poses_simu. push_back(X_simu);
            poses.      push_back(Xi + SE2Tangentd::Random()); // use very noisy priors
            controls.   push_back(u_nom + u_noise);
        }
    }

    //// Estimator #################################################################

    // Insert priors into bundle state
    BundleT X(poses[0], poses[1], poses[2],
        manif::R2d(landmarks[0]), manif::R2d(landmarks[1]),
        manif::R2d(landmarks[2]), manif::R2d(landmarks[3]),
        manif::R2d(landmarks[4]));

    cout << "prior" << std::showpos << endl;
    cout << "pose  :" << X.element<0>().translation().transpose() << " " << X.element<0>().angle() << endl;
    cout << "pose  :" << X.element<1>().translation().transpose() << " " << X.element<1>().angle() << endl;
    cout << "pose  :" << X.element<2>().translation().transpose() << " " << X.element<2>().angle() << endl;

    cout << "lmk   :" << X.element<3>() << endl;
    cout << "lmk   :" << X.element<4>() << endl;
    cout << "lmk   :" << X.element<5>() << endl;
    cout << "lmk   :" << X.element<6>() << endl;
    cout << "lmk   :" << X.element<7>() << endl;
    cout << "-----------------------------------------------" << endl;

    // iterate
    // DEBUG INFO
    cout << "iterations" << std::noshowpos << endl;
    for (int iteration = 0; iteration < MAX_ITER; ++iteration)
    {
        Matrix<double, NUM_MEAS, BundleT::DoF>    J;  // full Jacobian
        Matrix<double, NUM_MEAS, 1>               r;  // full residual

        int row = 0;  // keep track of row in J and r

        // first residual: prior
        r.segment<DoF>(row) = X.element<0>().lminus(SE2d::Identity(), J.block<DoF, DoF>(row, 0)).coeffs();
        row += DoF;

        // motion residuals
        add_pose_factor<0, 1>(X, controls[0], W, r.segment<DoF>(row), J.block<DoF, BundleT::DoF>(row, 0));
        row += DoF;

        add_pose_factor<1, 2>(X, controls[1], W, r.segment<DoF>(row), J.block<DoF, BundleT::DoF>(row, 0));
        row += DoF;

        // measurement residuals
        add_beacon_factor<0, 0>(X, measurements[0][0], S, r.segment<Dim>(row), J.block<Dim, BundleT::DoF>(row, 0));
        row += Dim;
        add_beacon_factor<0, 1>(X, measurements[0][1], S, r.segment<Dim>(row), J.block<Dim, BundleT::DoF>(row, 0));
        row += Dim;
        add_beacon_factor<0, 3>(X, measurements[0][3], S, r.segment<Dim>(row), J.block<Dim, BundleT::DoF>(row, 0));
        row += Dim;

        add_beacon_factor<1, 0>(X, measurements[1][0], S, r.segment<Dim>(row), J.block<Dim, BundleT::DoF>(row, 0));
        row += Dim;
        add_beacon_factor<1, 2>(X, measurements[1][2], S, r.segment<Dim>(row), J.block<Dim, BundleT::DoF>(row, 0));
        row += Dim;
        add_beacon_factor<1, 4>(X, measurements[1][4], S, r.segment<Dim>(row), J.block<Dim, BundleT::DoF>(row, 0));
        row += Dim;

        add_beacon_factor<2, 1>(X, measurements[2][1], S, r.segment<Dim>(row), J.block<Dim, BundleT::DoF>(row, 0));
        row += Dim;
        add_beacon_factor<2, 2>(X, measurements[2][2], S, r.segment<Dim>(row), J.block<Dim, BundleT::DoF>(row, 0));
        row += Dim;
        add_beacon_factor<2, 4>(X, measurements[2][4], S, r.segment<Dim>(row), J.block<Dim, BundleT::DoF>(row, 0));
        row += Dim;

        // 4. Solve -----------------------------------------------------------------

        // compute optimal step
        // ATTENTION: This is an expensive step!!
        // ATTENTION: Use QR factorization and column reordering for larger problems!!
        const auto dX = (-(J.transpose() * J).inverse() * J.transpose() * r).eval();

        // update estimate
        X += BundleT::Tangent(dX);

        // DEBUG INFO
        cout << "residual norm: " << std::scientific << r.norm() << ", step norm: " << dX.norm() << endl;

        // conditional exit
        if (dX.norm() < 1e-6) break;
    }
    cout << "-----------------------------------------------" << endl;


    //// Print results ####################################################################

    cout << std::fixed;

    // solved problem
    cout << "posterior" << std::showpos << endl;
    cout << "pose  :" << X.element<0>().translation().transpose() << " " << X.element<0>().angle() << endl;
    cout << "pose  :" << X.element<1>().translation().transpose() << " " << X.element<1>().angle() << endl;
    cout << "pose  :" << X.element<2>().translation().transpose() << " " << X.element<2>().angle() << endl;

    cout << "lmk   :" << X.element<3>() << endl;
    cout << "lmk   :" << X.element<4>() << endl;
    cout << "lmk   :" << X.element<5>() << endl;
    cout << "lmk   :" << X.element<6>() << endl;
    cout << "lmk   :" << X.element<7>() << endl;

    cout << "-----------------------------------------------" << endl;

    // ground truth
    cout << "ground truth" << std::showpos << endl;
    for (const auto& ps : poses_simu)
        cout << "pose  : " << ps.translation().transpose() << " " << ps.angle() << endl;
    for (const auto& ls : landmarks_simu)
        cout << "lmk : " << ls.transpose() << endl;
    cout << "-----------------------------------------------" << endl;

    return 0;
}