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
|
/*
* =====================================================================================
*
* 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(6,6,3,3) = 1e-6*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::enu ( const UTM &utm )
{
utm_c = utm.zone_c;
utm_i = utm.zone_i;
body->enu(utm);
return ;
} /* ----- end of method State::enu ----- */
void
State::update ( const Matrix<double,1,1> &z )
{
Matrix<double,1,9> H;
H=body->H();
Matrix<double,1,1> S;
S=body->S(P);
Matrix<double,9,1> K;
K = P*H.transpose()*S.inverse();
P = (Matrix<double,9,9>::Identity()-K*H)*P;
P = 0.5*(P+P.transpose());
Matrix<double,9,1> dx;
Matrix<double,1,1> h;
h = body->h();
dx = K*(z-h);
body->update(dx);
}
void
State::feature_update ( const std::vector<measurement_t> &z, const Quaterniond &q)
{
std::vector<measurement_t> featuresToAdd;
for (auto i=z.begin(); i!=z.end(); ++i) {
if (exists(i->id)) {
;
} else {
featuresToAdd.push_back(*i);
}
}
addFeatures( featuresToAdd, q);
return ;
} /* ----- end of method State::feature_update ----- */
void
State::addFeatures ( std::vector<measurement_t> &F, const Quaterniond &q)
{
// Expand P matrix all at once
expandP(F.size());
//
// Add new features
for (auto i=F.begin(); i!=F.end(); ++i) {
// Estimate initial depth
Vector3d xib;
// Create feature
Feature *f = new Feature(i->id, i->source, i->reflection, body->ned(), q, -0.44);
features.push_back(f);
// Initialize P values
int j=features.size();
Matrix<double,3,3> Pi;
Pi = f->P0();
initializePi(j,Pi);
}
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);
std::cout << pt << std::endl;
std::cout << P.cols() << std::endl;
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 ----- */
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)
{
Matrix<double,9,9> Q;
Q = body->Q(dt);
MatrixXd f;
f = F ( q, ang, dt );
//cout << f << endl;
P = f*P*f.transpose()+Q;
// 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);
return ;
} /* ----- end of method State::motionModel ----- */
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();
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 :: expandP
* Description: Grows P so that Nnew new features can be added. This could mean
* explicitly resizing the P matrix or P being a block of a preallocated matrix.
*--------------------------------------------------------------------------------------
*/
void
State::expandP ( int Nnew )
{
int curRows, newRows;
curRows = P.rows();
newRows = curRows + 3*Nnew;
P.conservativeResize(newRows,newRows);
return ;
} /* ----- end of method State::expandP ----- */
|