-
Notifications
You must be signed in to change notification settings - Fork 0
/
ukf.cpp
238 lines (178 loc) · 4.93 KB
/
ukf.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
#include "ukf.hpp"
#include "eigen_csv.hpp"
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
using namespace Eigen;
// Prediction step
void UKF::predict(double tp) {
MatrixXd Pxxi(nx,nx), Pxxp(nx,nx), Xi(nx,nsp), Xp(nx,nsp), W(nw,nsp);
VectorXd xesti, xestp;
xesti = xest.back();
Pxxi = Pxx.back();
double ti = t.back();
if (tp > ti) {
Xi = xesti.rowwise().replicate(nsp) + Pxxi.llt().matrixL() * Sp.topRows(nx);
if (addw)
W.setZero();
else
W = Cww * Sp.bottomRows(nw);
for (int i = 0; i < nsp; i++)
Xp.col(i) = f(ti, tp, Xi.col(i), W.col(i));
xestp = Xp * wp;
Pxxp = Xp * wp.asDiagonal() * Xp.transpose() - xestp * xestp.transpose();
if (addw)
Pxxp += Pww;
t.push_back(tp);
xest.push_back(xestp);
Pxx.push_back(Pxxp);
}
}
// Update step with one measurement
void UKF::update(const Eigen::VectorXd& z) {
MatrixXd Pxxp(nx,nx), Pxxu(nx,nx), X(nx,nsu), Z(nz,nsu), Pzz(nz,nz),
Pzx(nz,nx), K(nx,nz);
VectorXd xestp(nx), xestu(nx), zm(nz);
xestp = xest.back();
Pxxp = Pxx.back();
double tz = t.back();
X = xestp.rowwise().replicate(nsu) + Pxxp.llt().matrixL() * Su;
for (int i = 0; i < nsu; i++)
Z.col(i) = h(tz, X.col(i));
zm = Z * wu;
Pzz = Z * wu.asDiagonal() * Z.transpose() - zm * zm.transpose() + Pnn;
Pzx = Z * wu.asDiagonal() * X.transpose() - zm * xestp.transpose();
K = Pzz.llt().solve(Pzx).transpose();
xestu = xestp + K * (z - zm);
Pxxu = Pxxp - K * Pzx;
xest.back() = xestu;
Pxx.back() = Pxxu;
}
// Run filter for sequence of measurements
void UKF::run(const Eigen::VectorXd& tz, const Eigen::MatrixXd& Z) {
for (int i = 0; i < tz.size(); i++) {
predict(tz(i));
update(Z.col(i));
}
}
// Constructor
UKF::UKF (
const dyn_model& f_,
const meas_model& h_,
bool addw_,
double t0,
const Eigen::VectorXd& xm0,
const Eigen::MatrixXd& Pxx0,
const Eigen::MatrixXd& Pww_,
const Eigen::MatrixXd& Pnn_,
sig_type stype,
double k
) :
nx(xm0.size()),
nz(Pnn_.rows()),
nw(Pww_.rows()),
addw(addw_),
f(f_),
h(h_),
Pww(Pww_),
Pnn(Pnn_),
Cww(Pww.llt().matrixL()),
Sp(sigmaSt(stype, addw ? nx : nx+nw, k)),
Su(sigmaSt(stype, nx, k)),
wp(sigmaWt(stype, addw ? nx : nx+nw, k)),
wu(sigmaWt(stype, nx, k)),
nsp(Sp.cols()),
nsu(Su.cols())
{
t.push_back(t0);
xest.push_back(xm0);
Pxx.push_back(Pxx0);
}
// Generate standardized sigma points
Eigen::MatrixXd UKF::sigmaSt(UKF::sig_type stype, int n, double k) {
MatrixXd S;
if (stype == JU) {
S.resize(n, 2*n+1);
S.leftCols(n).setIdentity();
S.rightCols(n).setIdentity();
S.rightCols(n) *= -1;
S.col(n).setZero();
S *= sqrt(n + k);
} else {
int order;
if (stype == CUT8)
order = 8;
else if (stype == CUT6)
order = 6;
else
order = 4;
string file = cut_dir;
file += "pts_";
file += to_string(order);
file += "_";
file += to_string(n);
file += ".csv";
EigenCSV::read(file, false, true, S);
}
return S;
}
// Generate sigma point weights
Eigen::VectorXd UKF::sigmaWt(UKF::sig_type stype, int n, double k) {
VectorXd w;
if (stype == JU) {
w.resize(2*n+1);
w.setConstant(0.5 / (n + k));
w(n) = k / (n + k);
} else {
int order;
if (stype == CUT8)
order = 8;
else if (stype == CUT6)
order = 6;
else
order = 4;
string file = cut_dir;
file += "wts_";
file += to_string(order);
file += "_";
file += to_string(n);
file += ".csv";
EigenCSV::read(file, false, true, w);
}
return w;
}
// Reset filter
void UKF::reset(
double t0,
const Eigen::VectorXd& xm0,
const Eigen::MatrixXd& Pxx0
) {
t.clear();
xest.clear();
Pxx.clear();
t.push_back(t0);
xest.push_back(xm0);
Pxx.push_back(Pxx0);
}
// Save results
void UKF::save(const string& filename) {
int steps = xest.size();
MatrixXd table(steps, 2*nx+1);
for (int k = 0; k < steps; k++) {
table(k,0) = t[k];
table.row(k).segment(1, nx) = xest[k];
table.row(k).tail(nx) = Pxx[k].diagonal().cwiseSqrt();
}
vector<string> header(2*nx+1);
header[0] = "TIME";
for (int i = 1; i <= nx; i++) {
header[i] = "EST X";
header[i+nx] = "STD X";
header[i] += to_string(i);
header[i+nx] += to_string(i);
}
EigenCSV::write(table, header, filename);
}
// Default location for CUT files
string UKF::cut_dir = "CUT/";