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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
|
/*
* =====================================================================================
*
* Filename: state.cpp
*
* Description: A Class for managing body and features.
*
* Version: 1.0
* Created: 03/17/2017 07:55:56 PM
* Revision: none
* Compiler: gcc
*
* Author: Martin Miller (MHM), miller7@illinois.edu
* Organization: Aerospace Robotics and Controls Lab (ARC)
*
* =====================================================================================
*/
#include "state.h"
State::State ( )
{
body = new Body;
P = Matrix<double,Dynamic,Dynamic>::Zero(9,9);
P.block<3,3>(6,6) = COVBIAS*Matrix3d::Identity();
return ;
} /* ----- end of method State::State ----- */
void
State::vel ( const Matrix<double,3,1> &v )
{
body->vel(v);
return ;
} /* ----- end of method State::vel ----- */
void
State::pos ( const UTM &utm )
{
utm_c = utm.zone_c;
utm_i = utm.zone_i;
body->pos(utm);
return ;
} /* ----- end of method State::pos ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: H
* Description: Returns the matrix H for the measurements z
*--------------------------------------------------------------------------------------
*/
MatrixXd
State::H ( const Quaterniond &q, const std::vector<measurement_t> &z )
{
Vector3d pos;
pos = body->ned();
MatrixXd h;
// Determine the size of H
int cols = 9 + 3*features.size();
int rows = Hrows(z);
h = MatrixXd::Zero(rows,cols);
for (int i=0,row=0; i<z.size(); ++i) {
switch ( z[i].z_type ) {
int col;
Feature *fi;
case REFLECTION:
col = rowById(z[i].id);
if (col==-1) {
fprintf(stderr, "Feature %d not found, quitting.\n", z[i].id);
exit(1);
}
fi = featureById(z[i].id);
h.block<6,9>(row,0) = fi->Hx(pos,q);
h.block<6,3>(row,col) = fi->Hy(pos,q);
row += 6;
break;
case MONO:
col = rowById(z[i].id);
if (col==-1) {
fprintf(stderr, "Feature %d not found, quitting.\n", z[i].id);
exit(1);
}
fi = featureById( z[i].id );
h.block<4,9>(row,0) = fi->Hx(pos,q).block<4,9>(0,0);
break;
case HEIGHT:
h.block<1,9>(row,0) = body->H();
row += 1;
break;
default:
break;
} /* ----- end switch ----- */
}
return h;
} /* ----- end of method State::H ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: Hrows
* Description: Returns the number of rows in the measurement vector based on
* the number and types of measurements.
*--------------------------------------------------------------------------------------
*/
int
State::Hrows ( const std::vector<measurement_t> &z)
{
int rows = 0;
for (auto i=z.begin(); i!=z.end(); ++i) {
switch ( i->z_type ) {
case REFLECTION:
rows += 6;
break;
case MONO:
rows += 4;
break;
case HEIGHT:
rows += 1;
break;
default:
break;
} /* ----- end switch ----- */
}
return rows ;
} /* ----- end of method State::Hrows ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: R
* Description: Returns the measurement noise matrix.
*--------------------------------------------------------------------------------------
*/
MatrixXd
State::R ( const std::vector<measurement_t> &z )
{
int rows = Hrows(z);
MatrixXd r;
r = MatrixXd::Zero(rows,rows);
for (int i=0,row=0; i<z.size(); ++i) {
Feature *fi;
switch ( z[i].z_type ) {
case REFLECTION:
fi = featureById(z[i].id);
r.block<6,6>(row,row) = fi->R();
row += 6;
break;
case MONO:
fi = featureById(z[i].id);
r.block<4,4>(row,row) = fi->R().block<4,4>(0,0);
row += 4;
break;
case HEIGHT:
r.block<1,1>(row,row) = body->R();
row += 1;
break;
default:
break;
} /* ----- end switch ----- */
}
return r ;
} /* ----- end of method State::R ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: kalmanUpdate
* Description: Performs the kalman update.
*--------------------------------------------------------------------------------------
*/
void
State::kalmanUpdate( const MatrixXd &h, const MatrixXd &S,
const std::vector<measurement_t> &z, const Quaterniond &q)
{
MatrixXd K;
// P^T is implied here since P is symmetric
#ifdef BLOCKSI // Here S is S^-1
K = P*h.transpose()*S;
#else
K = S.fullPivLu().solve(h*P).transpose();
#endif
// Compute P_k|k
P = (MatrixXd::Identity(P.rows(),P.rows())-K*h)*P;
P = 0.5*(P+P.transpose());
// Compute the innovation or error
Matrix<double,Dynamic,1> y;
y = innovation(z,q);
// Get the update
Matrix<double,Dynamic,1> dx;
dx = K*y;
body->dx(dx.segment<9>(0));
{
int row=9;
for (auto i=features.begin(); i!=features.end(); ++i, row+=3) {
(*i)->dx(dx.segment<3>(row));
}
}
return ;
} /* ----- end of method State::kalmanUpdate ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: innovation
* Description: Returns the innovation vector z-hhat
*--------------------------------------------------------------------------------------
*/
Matrix<double,Dynamic,1>
State::innovation( const std::vector<measurement_t> &z, const Quaterniond &q)
{
Matrix<double,Dynamic,1> y;
int rows = Hrows(z);
y = Matrix<double,Dynamic,1>::Zero(rows,1);
for (int i=0,row=0; i<z.size(); ++i) {
Feature *fi;
measurement_type mt=z[i].z_type;
if (mt==REFLECTION) {
fi = featureById(z[i].id);
Matrix<double,6,1> hi, zi;
hi = fi->h(body->ned(), q);
zi = fi->measurement_vector(z[i].source, z[i].reflection);
y.segment<6>(row) = zi-hi;
row += 6;
} else if (mt==MONO) {
fi = featureById(z[i].id);
Matrix<double,4,1> hi, zi;
hi = fi->h(body->ned(), q).segment<4>(0);
zi = fi->measurement_vector(z[i].source);
y.segment<4>(row) = zi-hi;
row += 4;
} else if (mt==HEIGHT) {
Matrix<double,1,1> zi, hi;
zi[0] = z[i].height;
Vector3d xbw;
xbw = body->ned();
hi[0] = xbw[2];
y.segment<1>(row) = zi-hi;
row += 1;
} else {
fprintf(stderr, "Unknown measurement type\n");
}
}
return y;
}
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: handle_measurements
* Description: Update EKF, and adds new features to state.
*--------------------------------------------------------------------------------------
*/
void
State::handle_measurements ( const std::vector<measurement_t> &z, const Quaterniond &q)
{
std::vector<measurement_t> featuresToAdd;
std::vector<measurement_t> featuresToUpdate;
double zmeas = body->ned()[2];
for (auto i=z.begin(); i!=z.end(); ++i) {
if (i->z_type==HEIGHT) {
featuresToUpdate.push_back(*i);
zmeas = i->height;
} else if (exists(i->id)) {
featuresToUpdate.push_back(*i);
} else {
featuresToAdd.push_back(*i);
}
}
// Remove features that don't have measurements in this timestep.
auto i=features.begin();
int feati=0;
while (i!=features.end()) {
bool measured=false;
for (auto j=z.begin(); j!=z.end(); ++j) {
if (j->z_type!=REFLECTION) continue;
if (j->id==(*i)->id()) {
measured=true;
break;
}
}
if (measured==true) {
++i;
++feati;
} else {
i=removeFeature(i,feati);
}
}
if (featuresToUpdate.size()>1) {
#ifdef FULLS
MatrixXd h;
h = H(q,featuresToUpdate);
MatrixXd S;
S = h*P*h.transpose() + R(featuresToUpdate);
kalmanUpdate(h,S,featuresToUpdate,q);
#endif
#ifdef BLOCKSI
MatrixXd h;
h = H(q,featuresToUpdate);
MatrixXd S;
S = blockSI(featuresToUpdate,q);
cout << S << endl;
kalmanUpdate(h,S,featuresToUpdate,q);
#endif
#ifdef MEAS1
for (auto i=featuresToUpdate.begin();
i!=featuresToUpdate.end(); ++i) {
Matrix<double,Dynamic,Dynamic,0,6,6> S;
MatrixXd h;
std::vector<measurement_t> onez;
onez.push_back(*i);
h = H(q,onez);
if (i->z_type==HEIGHT) {
S = body->S(Pxx());
} else if (i->z_type==REFLECTION) {
Feature *f = featureById(i->id);
S = f->S(Pxx(), Pxy(i->id), Pyy(i->id), body->ned() ,q);
} else {
Feature *f = featureById(i->id);
S = f->S(Pxx(), Pxy(i->id), Pyy(i->id), body->ned() ,q);
S = S.block<4,4>(0,0);
}
kalmanUpdate(h,S,onez,q);
}
#endif /* ----- not FULLS ----- */
}
addFeatures( featuresToAdd, q, zmeas);
return ;
} /* ----- end of method State::feature_update ----- */
MatrixXd
State::blockSI ( const std::vector<measurement_t> &z, const Quaterniond &q )
{
int rows = Hrows(z);
MatrixXd SI;
SI = MatrixXd::Zero(rows,rows);
{
int row = 0;
for (auto i=z.begin(); i!=z.end(); ++i) {
if (i->z_type==HEIGHT) {
Matrix<double,1,1> s;
s = body->S(Pxx());
SI.block<1,1>(row,row) = s.inverse();
row += 1;
} else if (i->z_type==REFLECTION) {
Feature *f;
Matrix<double,6,6> s;
f = featureById(i->id);
s = f->S(Pxx(), Pxy(i->id), Pyy(i->id), body->ned() ,q);
SI.block<6,6>(row,row) = s.inverse();
row += 6;
} else if (i->z_type==MONO) {
/*
Feature *f;
Matrix<double,6,6> s;
f = featureById(i->id);
s = f->S(Pxx(), Pxy(i->id), Pyy(i->id), body->ned() ,q);
s = s.block<4,4>(0,0);
SI.block<4,4>(row,row) = s.inverse();
row += 4;
*/
} else {
fprintf(stderr, "Unrecognized feature type, quitting\n");
exit(1);
}
}
}
return SI ;
} /* ----- end of method State::blockSI ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: Pxy
* Description: Return the covariance block for feature with the given id.
*--------------------------------------------------------------------------------------
*/
Matrix<double,9,3>
State::Pxy ( int id )
{
int col = rowById(id);
return P.block<9,3>(0,col) ;
} /* ----- end of method State::Pxy ----- */
Matrix<double,3,3>
State::Pyy ( int id )
{
int row = rowById(id);
return P.block<3,3>(row,row) ;
} /* ----- end of method State::Pyy ----- */
void
State::addFeatures ( std::vector<measurement_t> &F, const Quaterniond &q, double z)
{
// Add new features
Vector3d pos = body->ned();
for (auto i=F.begin(); i!=F.end(); ++i) {
if (features.size()>MAXFEATURES) break;
// Create feature
Feature *f;
if (i->z_type==REFLECTION) {
f = new Feature(i->id, i->source, i->reflection, pos, q, z);
//f = new Feature(i->id, i->source, pos, q);
} else {
f = new Feature(i->id, i->source, pos, q);
}
if (!f->sane()) {
delete f;
continue;
}
features.push_back(f);
// Expand P
expandP(f->P0(i->z_type));
}
return ;
} /* ----- end of method State::addFeatures ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: initializePi
* Description: Zeros out the off-diagonal blocks and sets the Pii block to Pi.
*--------------------------------------------------------------------------------------
*/
void
State::initializePi ( int i, const Matrix<double,3,3> &Pi )
{
int pt=9+3*(i-1);
P.block(pt,0,3,P.cols()) = MatrixXd::Zero(3,P.cols());
P.block(0,pt,P.rows(),3) = MatrixXd::Zero(P.rows(),3);
P.block<3,3>(pt,pt) = Pi;
return ;
} /* ----- end of method State::initializePi ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: Q
* Description: Compose full Q from body and features.
*--------------------------------------------------------------------------------------
*/
MatrixXd
State::Q ( double dt )
{
MatrixXd q;
int rows = 9 + 3*features.size();
q = MatrixXd::Zero(rows,rows);
q.topLeftCorner<9,9>() = body->Q(dt);
{ // limit i's scope
auto i = features.begin();
for (int row=9; row<rows; row+=3, ++i) {
q.block<3,3>(row,row) = (*i)->Q(dt);
}
}
return q;
} /* ----- end of method State::q ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: F
* Description: Compose full F from body and features
*--------------------------------------------------------------------------------------
*/
MatrixXd
State::F ( const Quaterniond &q, const Vector3d &w, double dt )
{
Vector3d v;
v = body->vel();
// Allocate matrix F
MatrixXd f;
int rows = 9 + 3*features.size();
f = MatrixXd::Zero(rows,rows);
// Set body F
f.topLeftCorner<9,9>() = body->F(w,q,dt);
// Set Fxi Fyi
{ // limit i's scope
auto i = features.begin();
for (int row=9; row<rows; row+=3,++i) {
f.block<3,9>(row,0) = (*i)->Fx(dt);
f.block<3,3>(row,row) = (*i)->Fy(v,w,dt);
}
}
return f ;
} /* ----- end of method State::F ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: Pkk1
* Description: Updates P_k|k-1
*--------------------------------------------------------------------------------------
*/
void
State::Pkk1 ( const Vector3d &ang, const Quaterniond &q, const double dt)
{
MatrixXd f,fullQ;
fullQ = Q(dt);
f = F ( q, ang, dt );
P = f*P*f.transpose()+fullQ;
// Enforce symmetry
P = 0.5*(P+P.transpose());
return ;
} /* ----- end of method State::Pkk1 ----- */
void
State::motionModel ( const Vector3d &acc, const Vector3d &ang,
const Quaterniond &q, const double dt)
{
Pkk1(ang,q,dt);
body->motionModel(acc,ang,q,dt);
Vector3d vel;
vel = body->vel();
#ifdef FASTMOTIONMODEL
Matrix<double,Dynamic,6> fullL;
fullL = L();
Matrix<double,6,1> V;
V << vel[1], vel[2], vel[0], ang[1], ang[2], ang[0];
Matrix<double,Dynamic,1> f;
f = fullL*V*dt;
// Update features' states
{
int rows = 3*features.size();
auto i = features.begin();
for (int row=0; row<rows; row+=3, ++i) {
Vector3d dx;
dx = f.segment<3>(row);
(*i)->dx(dx);
}
}
#else /* ----- not FASTMOTIONMODEL ----- */
for (auto i=features.begin(); i!=features.end(); ++i) {
(*i)->motionModel(ang,vel,dt);
}
#endif /* ----- not FASTMOTIONMODEL ----- */
// Remove features that leave FOV
{
auto i = features.begin();
int j=0;
while (i!=features.end()) {
if ((*i)->inFOV()) {
++i;
++j;
} else {
i=removeFeature(i,j);
}
}
}
return ;
} /* ----- end of method State::motionModel ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: removeFeature
* Description: Remove the Feature i and Pj in a clean fashion.
*--------------------------------------------------------------------------------------
*/
std::list<Feature *>::iterator
State::removeFeature ( std::list<Feature *>::iterator &i, int j )
{
// It is important to shrink P first, because it depends on knowing the
// current size of the feature vector.
shrinkP(j);
delete *i;
i = features.erase(i);
return i;
} /* ----- end of method State::removeFeature ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: L
* Description: Return the composition of all Li's of the features
*--------------------------------------------------------------------------------------
*/
Matrix<double,Dynamic,6>
State::L ( )
{
Matrix<double,Dynamic,6> l;
int rows = 3*features.size();
l = Matrix<double,Dynamic,6>::Zero(rows,6);
{
auto i=features.begin();
for (int row=0; row<rows; row+=3, ++i) {
l.block<3,6>(row,0) = (*i)->L();
}
}
return l;
} /* ----- end of method State::L ----- */
void
State::position_covariance ( const Matrix<double,3,3> &p )
{
P.block(0,0,3,3) = p;
return ;
} /* ----- end of method State::position_covariance ----- */
void
State::velocity_covariance ( const Matrix<double,3,3> &p )
{
P.block(3,3,3,3) = p;
return ;
} /* ----- end of method State::velocity_covariance ----- */
void
State::unicsv ( )
{
body->unicsv();
printf(",%d\n", features.size());
return ;
} /* ----- end of method State::unicsv ----- */
void
State::accelerometer_bias ( const Vector3d &b)
{
body->accelerometer_bias(b);
return ;
} /* ----- end of method State::accelerometer_bias ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: exists
* Description: Tests if the id is a current feature.
*--------------------------------------------------------------------------------------
*/
bool
State::exists ( int id )
{
for (auto i=features.begin(); i!=features.end(); ++i) {
if ((*i)->id()==id) return true;
}
return false;
} /* ----- end of method State::exists ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: rowById
* Description: Returns the first row of the feature with the given ID. Returns
* -1 if not found.
*--------------------------------------------------------------------------------------
*/
int
State::rowById ( int id )
{
int j=9;
for (auto i=features.begin(); i!=features.end(); ++i, j+=3) {
if ((*i)->id()==id) return j;
}
return -1;
} /* ----- end of method State::rowById ----- */
Feature *
State::featureById ( int id )
{
for (auto i=features.begin(); i!=features.end(); ++i) {
if ((*i)->id()==id) return *i;
}
return NULL;
} /* ----- end of method State::featureById ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: expandP
* Description: Expand the P matrix to fit one more feature.
*--------------------------------------------------------------------------------------
*/
void
State::expandP ( const Matrix3d &Pi)
{
P.conservativeResizeLike(MatrixXd::Zero(3+P.cols(),3+P.cols()));
P.bottomRightCorner<3,3>() = Pi;
return ;
} /* ----- end of method State::expandP ----- */
Matrix<double,9,9>
State::Pxx ( )
{
return P.topLeftCorner<9,9>() ;
} /* ----- end of method State::Pxx ----- */
/*
*--------------------------------------------------------------------------------------
* Class: State
* Method: State :: shrinkP
* Description: Delete the ith feature from P
*--------------------------------------------------------------------------------------
*/
void
State::shrinkP ( int i )
{
int N = 9 + 3*features.size();
int I = 9 + 3*i;
P.block(I,0,N-I-3,I) = P.bottomLeftCorner(N-I-3,I);
P.block(0,I,I,N-I-3) = P.topRightCorner(I,N-I-3);
P.block(I,I,N-I-3,N-I-3) = P.bottomRightCorner(N-I-3,N-I-3);
P.conservativeResize(N-3,N-3);
return ;
} /* ----- end of method State::shrinkP ----- */
|