-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKalmanFilter.h
48 lines (34 loc) · 1.48 KB
/
KalmanFilter.h
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
//
// Created by Misha on 12/5/2020.
//
#ifndef AUTOCYCLE_STABILITY_FIRMWARE_KALMANFILTER_H
#define AUTOCYCLE_STABILITY_FIRMWARE_KALMANFILTER_H
#include <BasicLinearAlgebra.h>
using namespace BLA;
template<int xN, int yN, int uN, typename dtype = float>
class KalmanFilter {
public:
void predict(BLA::Matrix<uN, 1, Array<uN, 1, dtype>> u);
void update(BLA::Matrix<yN, 1, Array<yN, 1, dtype>> y);
BLA::Matrix<xN, 1, Array<xN, 1, dtype>> x; // State estimate
BLA::Matrix<xN, xN, Array<xN, xN, dtype>> P; // State estimate covariance matrix
BLA::Matrix<xN, xN, Array<xN, xN, dtype>> A; // State transition matrix
BLA::Matrix<xN, uN, Array<xN, uN, dtype>> B; // Control matrix
BLA::Matrix<yN, xN, Array<yN, xN, dtype>> C; // Sensor matrix
BLA::Matrix<xN, xN, Array<xN, xN, dtype>> Q; // Process covariance matrix
BLA::Matrix<yN, yN, Array<yN, yN, dtype>> R; // Measurement covariance matrix
};
template<int xN, int yN, int uN, typename dtype>
void KalmanFilter<xN, yN, uN, dtype>::predict(BLA::Matrix<uN, 1, Array<uN, 1, dtype>> u) {
x = A * x + B * u;
P = A * P * (~A) + Q;
}
template<int xN, int yN, int uN, typename dtype>
void KalmanFilter<xN, yN, uN, dtype>::update(BLA::Matrix<yN, 1, Array<yN, 1, dtype>> y) {
auto residual = y - C * x;
auto s = C * P * (~C) + R;
auto K = P * (~C) * (BLA::Inverse(s));
x = x + K * residual;
P = (BLA::Identity<xN, xN>() - K * C) * P;
}
#endif //AUTOCYCLE_STABILITY_FIRMWARE_KALMANFILTER_H