-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoKalman.cpp
67 lines (52 loc) · 1.49 KB
/
AutoKalman.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
#include "AutoKalman.h"
AutoKalman::AutoKalman(float processNoise, float measurementNoise, float estimatedError, float initialValue) {
_processNoise = processNoise;
_measurementNoise = measurementNoise;
_estimatedError = estimatedError;
_value = initialValue;
_isInitialized = true;
}
void AutoKalman::setProcessNoise(float q) {
_processNoise = q;
}
void AutoKalman::setMeasurementNoise(float r) {
_measurementNoise = r;
}
void AutoKalman::setEstimatedError(float p) {
_estimatedError = p;
}
void AutoKalman::setInitialValue(float value) {
_value = value;
_isInitialized = true;
}
float AutoKalman::filter(float measurement) {
if (!_isInitialized) {
_value = measurement;
_isInitialized = true;
return _value;
}
// Kalman Gain
float kalmanGain = _estimatedError / (_estimatedError + _measurementNoise);
// Update state estimate
_value = _value + kalmanGain * (measurement - _value);
// Update error covariance
_estimatedError = (1.0f - kalmanGain) * _estimatedError + fabs(_value) * _processNoise;
return _value;
}
void AutoKalman::reset() {
_isInitialized = false;
_value = 0.0;
_estimatedError = 1.0;
}
float AutoKalman::getProcessNoise() const {
return _processNoise;
}
float AutoKalman::getMeasurementNoise() const {
return _measurementNoise;
}
float AutoKalman::getEstimatedError() const {
return _estimatedError;
}
float AutoKalman::getStateEstimate() const {
return _value;
}