-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.cpp
51 lines (39 loc) · 1.62 KB
/
filter.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
#include "filter.h"
#include <math.h>
Filter::Filter(const Eigen::Vector3f &location) {
state << location[0], 0, location[1], 0, location[2], 0;
observation <<
1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0;
stateCovariance.setZero();
}
void Filter::Update(const float elapsed, const float processNoise, const float observationNoise, Eigen::Vector3f &location) {
Eigen::Matrix<float, 6, 6> transition;
transition <<
1, elapsed, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0,
0, 0, 1, elapsed, 0, 0,
0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 1, elapsed,
0, 0, 0, 0, 0, 1;
state = transition * state;
Eigen::Matrix<float, 6, 6> processCovariance;
processCovariance <<
pow(elapsed, 4) / 4, pow(elapsed, 3) / 2, 0, 0, 0, 0,
pow(elapsed, 3) / 2, pow(elapsed, 2), 0, 0, 0, 0,
0, 0, pow(elapsed, 4) / 4, pow(elapsed, 3) / 2, 0, 0,
0, 0, pow(elapsed, 3) / 2, pow(elapsed, 2), 0, 0,
0, 0, 0, 0, pow(elapsed, 4) / 4, pow(elapsed, 3) / 2,
0, 0, 0, 0, pow(elapsed, 3) / 2, pow(elapsed, 2);
stateCovariance = transition * stateCovariance * transition.transpose() + processCovariance * pow(processNoise, 2);
Eigen::Matrix<float, 3, 3> observationCovariance;
observationCovariance <<
pow(observationNoise, 2), 0, 0,
0, pow(observationNoise, 2), 0,
0, 0, pow(observationNoise, 2);
Eigen::Matrix<float, 6, 3> gain = stateCovariance * observation.transpose() * (observation * stateCovariance * observation.transpose() + observationCovariance).inverse();
state += gain * (location - observation * state);
stateCovariance = (Eigen::Matrix<float, 6, 6>().Identity() - gain * observation) * stateCovariance;
location = observation * state;
}