-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptiveextendedkalmanfilter.cpp
325 lines (285 loc) · 12.1 KB
/
adaptiveextendedkalmanfilter.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// $Id: adaptiveextendedkalmanfilter.cpp 30022 2015-09-06 16:10:30Z kunaltyagi $
// Copyright (C) 2003 Kunal Tyagi <last dot first at live dot com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "adaptiveextendedkalmanfilter.h"
#include <cmath>
namespace BFL
{
using namespace MatrixWrapper;
#define AnalyticSys AnalyticSystemModelGaussianUncertainty
#define AnalyticMeas AnalyticMeasurementModelGaussianUncertainty
AdaptiveExtendedKalmanFilter::AdaptiveExtendedKalmanFilter(Gaussian* prior, double Nr, double Nq)
: // KalmanFilter(prior)
Filter<ColumnVector,ColumnVector>(prior)
, _Mu_new(prior->DimensionGet())
, _Sigma_new(prior->DimensionGet())
, _Sigma_temp(prior->DimensionGet(),prior->DimensionGet())
, _Sigma_temp_par(prior->DimensionGet(),prior->DimensionGet()) // kalmanfilter end, ekf start
, _x(prior->DimensionGet())
, _J(prior->DimensionGet())
, _F(prior->DimensionGet(),prior->DimensionGet())
, _Q(prior->DimensionGet())
// changes for adaptive
, _Nr(Nr)
, _Nq(Nq)
{
// create posterior dencity
_post = new Gaussian(*prior);
_alpha1 = (_Nr - 1)/_Nr; // reduce loss in signigicant digits
_alpha1 = (_Nq - 1)/_Nq;
_firstData = true;
}
AdaptiveExtendedKalmanFilter::~AdaptiveExtendedKalmanFilter()
{
delete _post;
}
// kf start
void
AdaptiveExtendedKalmanFilter::AllocateMeasModel(const vector<unsigned int>& meas_dimensions)
{
unsigned int meas_dimension;
for(int i = 0 ; i< meas_dimensions.size(); i++)
{
// find if variables with size meas_sizes[i] are already allocated
meas_dimension = meas_dimensions[i];
_mapMeasUpdateVariables_it = _mapMeasUpdateVariables.find(meas_dimension);
if( _mapMeasUpdateVariables_it == _mapMeasUpdateVariables.end())
{
//variables with size z.rows() not allocated yet
_mapMeasUpdateVariables_it = (_mapMeasUpdateVariables.insert
(std::pair<unsigned int, MeasUpdateVariables>( meas_dimension,MeasUpdateVariables(meas_dimension,_Mu_new.rows()) ))).first;
}
}
}
void
AdaptiveExtendedKalmanFilter::AllocateMeasModel(const unsigned int& meas_dimension)
{
// find if variables with size meas_sizes[i] are already allocated
_mapMeasUpdateVariables_it = _mapMeasUpdateVariables.find(meas_dimension);
if( _mapMeasUpdateVariables_it == _mapMeasUpdateVariables.end())
{
//variables with size z.rows() not allocated yet
_mapMeasUpdateVariables_it = (_mapMeasUpdateVariables.insert
(std::pair<unsigned int, MeasUpdateVariables>( meas_dimension,MeasUpdateVariables(meas_dimension,_Mu_new.rows()) ))).first;
}
}
void
AdaptiveExtendedKalmanFilter::CalculateSysUpdate(const ColumnVector& J, const Matrix& F, const SymmetricMatrix& Q)
{
_Sigma_temp = F * ( (Matrix)_post->CovarianceGet() * F.transpose());
_Sigma_temp += (Matrix)Q;
_Sigma_temp.convertToSymmetricMatrix(_Sigma_new);
// set new state gaussian
PostMuSet ( J );
PostSigmaSet( _Sigma_new );
}
void
AdaptiveExtendedKalmanFilter::CalculateMeasUpdate(const ColumnVector& z, const ColumnVector& Z, const Matrix& H, const SymmetricMatrix& R)
{
// allocate measurement for z.rows() if needed
AllocateMeasModel(z.rows());
(_mapMeasUpdateVariables_it->second)._postHT = (Matrix)(_post->CovarianceGet()) * H.transpose() ;
(_mapMeasUpdateVariables_it->second)._S_Matrix = H * (_mapMeasUpdateVariables_it->second)._postHT;
// calcutate new state gaussian
// Mu = expectedValue + K*(z-Z)
(_mapMeasUpdateVariables_it->second)._innov = z-Z;
// changes for adaptive begin (part 1)
(_mapMeasUpdateVaraiblesAdapt_it->second)._OptError *= _alpha1;
(_mapMeasUpdateVaraiblesAdapt_it->second)._OptError += (_mapMeasUpdateVariables_it->second)._innov/_Nr;
(_mapMeasUpdateVaraiblesAdapt_it->second)._DeltaR = ((_mapMeasUpdateVariables_it->second)._innov - (_mapMeasUpdateVaraiblesAdapt_it->second)._OptError);
(_mapMeasUpdateVaraiblesAdapt_it->second)._DeltaR *= (_mapMeasUpdateVaraiblesAdapt_it->second)._DeltaR.transpose()/(Nr - 1);
(_mapMeasUpdateVaraiblesAdapt_it->second)._DeltaR -= (_mapMeasUpdateVariables_it->second)._S_Matrix/_Nr;
((Matrix)(R*_alpha1 + (_mapMeasUpdateVaraiblesAdapt_it->second)._DeltaR)).convertToSymmetricMatrix(R);
for (int i = 0; i < R.rows(); ++i)
{
for (int j = 0; j < R.columns(); ++j)
{
if (R(i,j) < 0)
{
R(i,j) = -R(i,j);
}
}
}
// changes for adaptive end (part 1)
(_mapMeasUpdateVariables_it->second)._S_Matrix += (Matrix)R;
// _K = covariance * H' * S(-1)
(_mapMeasUpdateVariables_it->second)._K = (_mapMeasUpdateVariables_it->second)._postHT * ( (_mapMeasUpdateVariables_it->second)._S_Matrix.inverse());
_Mu_new = (_mapMeasUpdateVariables_it->second)._K * (_mapMeasUpdateVariables_it->second)._innov ;
// changes for adaptive begin (part 2)
_alpha2 = (_Nq - 1)/_Nq;
(_mapMeasUpdateVaraiblesAdapt_it->second).OptOmega *= _alpha2;
(_mapMeasUpdateVaraiblesAdapt_it->second).OptOmega += _Mu_new/_Nq;
(_mapMeasUpdateVaraiblesAdapt_it->second)._DeltaQ = (_Mu_new._innov - (_mapMeasUpdateVaraiblesAdapt_it->second)._OptOmega);
(_mapMeasUpdateVaraiblesAdapt_it->second)._DeltaQ *= (_mapMeasUpdateVaraiblesAdapt_it->second)._DeltaQ.transpose()/(Nq - 1);
// changes for adaptive end (part 2)
_Mu_new += _post->ExpectedValueGet() ;
// Sigma = post - K*H*post
_Sigma_temp = (_post->CovarianceGet());
_Sigma_temp_par = (_mapMeasUpdateVariables_it->second)._K * H ;
_Sigma_temp -= _Sigma_temp_par * (Matrix)(_post->CovarianceGet());
// convert to symmetric matrix
_Sigma_temp.convertToSymmetricMatrix(_Sigma_new);
// changes for adaptive begin (part 3)
((Matrix)(_mapMeasUpdateVaraiblesAdapt_it->second)._Q*_alpha2 + (_mapMeasUpdateVaraiblesAdapt_it->second)._DeltaQ)).convertToSymmetricMatrix((_mapMeasUpdateVaraiblesAdapt_it->second)._Q); // if required, take the diagonal elements only
for (int i = 0; i < _mapMeasUpdateVaraiblesAdapt_it->second)._Q.rows(); ++i)
{
for (int j = 0; j < _mapMeasUpdateVaraiblesAdapt_it->second)._Q.columns(); ++j)
{
if (_mapMeasUpdateVaraiblesAdapt_it->second)._Q(i,j) < 0)
{
_mapMeasUpdateVaraiblesAdapt_it->second)._Q(i,j) = -_mapMeasUpdateVaraiblesAdapt_it->second)._Q(i,j);
}
}
}
// changes for adaptive end (part 3)
// set new state gaussian
PostMuSet ( _Mu_new );
PostSigmaSet( _Sigma_new );
/*
cout << "H:\n" << H << endl;
cout << "R:\n" << R << endl;
cout << "Z:\n" << Z << endl;
cout << "inov:\n" << z-Z << endl;
cout << "S:\n" << S << endl;
cout << "S.inverse:\n" << S.inverse() << endl;
cout << "K:\n" << K << endl;
cout << "Mu_new:\n" << Mu_new << endl;
cout << "sigma_new\n" << Sigma_new << endl;
*/
}
bool
AdaptiveExtendedKalmanFilter::UpdateInternal(SystemModel<ColumnVector>* const sysmodel,
const ColumnVector& u,
MeasurementModel<ColumnVector,ColumnVector>* const measmodel,
const ColumnVector& z, const ColumnVector& s)
{
if (sysmodel != NULL)
{
SysUpdate(sysmodel,u);
}
if (measmodel != NULL)
{
MeasUpdate(measmodel,z,s);
}
return true;
}
void
AdaptiveExtendedKalmanFilter::PostSigmaSet( const SymmetricMatrix& s)
{
dynamic_cast<Gaussian *>(_post)->CovarianceSet(s);
}
void
AdaptiveExtendedKalmanFilter::PostMuSet( const ColumnVector& c)
{
dynamic_cast<Gaussian *>(_post)->ExpectedValueSet(c);
}
Gaussian*
AdaptiveExtendedKalmanFilter::PostGet()
{
return (Gaussian*)Filter<ColumnVector,ColumnVector>::PostGet();
}
// kf ends
// ekf starts
void
AdaptiveExtendedKalmanFilter::AllocateMeasModelAdapt(const vector<unsigned int>& meas_dimensions)
{
unsigned int meas_dimension;
for(int i = 0 ; i< meas_dimensions.size(); i++)
{
// find if variables with size meas_sizes[i] are already allocated
meas_dimension = meas_dimensions[i];
_mapMeasUpdateVariablesAdapt_it = _mapMeasUpdateVariablesAdapt.find(meas_dimension);
if( _mapMeasUpdateVariablesAdapt_it == _mapMeasUpdateVariablesAdapt.end())
{
//variables with size z.rows() not allocated yet
_mapMeasUpdateVariablesAdapt_it = (_mapMeasUpdateVariablesAdapt.insert
(std::pair<unsigned int, MeasUpdateVariablesAdapt>( meas_dimension,MeasUpdateVariablesAdapt(meas_dimension,_x.rows()) ))).first;
}
}
}
void
AdaptiveExtendedKalmanFilter::AllocateMeasModelAdapt(const unsigned int& meas_dimension)
{
// find if variables with size meas_sizes[i] are already allocated
_mapMeasUpdateVariablesAdapt_it = _mapMeasUpdateVariablesAdapt.find(meas_dimension);
if( _mapMeasUpdateVariablesAdapt_it == _mapMeasUpdateVariablesAdapt.end())
{
//variables with size z.rows() not allocated yet
_mapMeasUpdateVariablesAdapt_it = (_mapMeasUpdateVariablesAdapt.insert
(std::pair<unsigned int, MeasUpdateVariablesAdapt>( meas_dimension,MeasUpdateVariablesAdapt(meas_dimension,_x.rows()) ))).first;
}
}
void
AdaptiveExtendedKalmanFilter::AllocateMeasModelExt(const vector<unsigned int>& meas_dimensions)
{
unsigned int meas_dimension;
for(int i = 0 ; i< meas_dimensions.size(); i++)
{
// find if variables with size meas_sizes[i] are already allocated
meas_dimension = meas_dimensions[i];
_mapMeasUpdateVariablesExt_it = _mapMeasUpdateVariablesExt.find(meas_dimension);
if( _mapMeasUpdateVariablesExt_it == _mapMeasUpdateVariablesExt.end())
{
//variables with size z.rows() not allocated yet
_mapMeasUpdateVariablesExt_it = (_mapMeasUpdateVariablesExt.insert
(std::pair<unsigned int, MeasUpdateVariablesExt>( meas_dimension,MeasUpdateVariablesExt(meas_dimension,_x.rows()) ))).first;
}
}
}
void
AdaptiveExtendedKalmanFilter::AllocateMeasModelExt(const unsigned int& meas_dimension)
{
// find if variables with size meas_sizes[i] are already allocated
_mapMeasUpdateVariablesExt_it = _mapMeasUpdateVariablesExt.find(meas_dimension);
if( _mapMeasUpdateVariablesExt_it == _mapMeasUpdateVariablesExt.end())
{
//variables with size z.rows() not allocated yet
_mapMeasUpdateVariablesExt_it = (_mapMeasUpdateVariablesExt.insert
(std::pair<unsigned int, MeasUpdateVariablesExt>( meas_dimension,MeasUpdateVariablesExt(meas_dimension,_x.rows()) ))).first;
}
}
void
AdaptiveExtendedKalmanFilter::SysUpdate(SystemModel<ColumnVector>* const sysmodel,
const ColumnVector& u)
{
_x = _post->ExpectedValueGet();
_J = ((AnalyticSys*)sysmodel)->PredictionGet(u,_x);
_F = ((AnalyticSys*)sysmodel)->df_dxGet(u,_x);
_Q = ((AnalyticSys*)sysmodel)->CovarianceGet(u,_x);
// @TODO: confirm this
CalculateSysUpdate(_J, _F, (_mapMeasUpdateVaraiblesAdapt_it->second)._Q);
}
void
AdaptiveExtendedKalmanFilter::MeasUpdate(MeasurementModel<ColumnVector,ColumnVector>* const measmodel,
const ColumnVector& z,
const ColumnVector& s)
{
// allocate measurement for z.rows() if needed
AllocateMeasModelExt(z.rows());
_x = _post->ExpectedValueGet();
(_mapMeasUpdateVariablesExt_it->second)._Z = ((AnalyticMeas*)measmodel)->PredictionGet(s,_x);
(_mapMeasUpdateVariablesExt_it->second)._H = ((AnalyticMeas*)measmodel)->df_dxGet(s,_x);
// remove this
if (true == _firstData)
{
(_mapMeasUpdateVariablesExt_it->second)._R = ((AnalyticMeas*)measmodel)->CovarianceGet(s,_x);
(_mapMeasUpdateVariablesAdapt_it->second)._Q = _Q;
_firstData = false;
}
CalculateMeasUpdate(z, (_mapMeasUpdateVariablesExt_it->second)._Z, (_mapMeasUpdateVariablesExt_it->second)._H, (_mapMeasUpdateVariablesExt_it->second)._R);
}
// ekf ends
} // end namespace BFL