Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
kalman_filter.hpp
Go to the documentation of this file.
1/*****************************************************************************/
2/********** !!! WARNING: CODE GENERATED BY TAPROOT. DO NOT EDIT !!! **********/
3/*****************************************************************************/
4
5/*
6 * Copyright (c) 2020-2021 Advanced Robotics at the University of Washington <robomstr@uw.edu>
7 *
8 * This file is part of Taproot.
9 *
10 * Taproot is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Taproot is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Taproot. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef TAPROOT_KALMAN_FILTER_HPP_
25#define TAPROOT_KALMAN_FILTER_HPP_
26
27#include <cinttypes>
28
29#include "modm/architecture/interface/assert.h"
30#include "modm/math/matrix.hpp"
31
32#include "cmsis_mat.hpp"
33
34namespace tap::algorithms
35{
43template <uint16_t STATES, uint16_t INPUTS>
45{
46public:
55 const float (&A)[STATES * STATES],
56 const float (&C)[INPUTS * STATES],
57 const float (&Q)[STATES * STATES],
58 const float (&R)[INPUTS * INPUTS],
59 const float (&P0)[STATES * STATES])
60 : A(A),
61 At(),
62 C(C),
63 Ct(),
64 Q(Q),
65 R(R),
66 xHat(),
67 P(P0),
68 P0(P0),
69 K(),
70 I()
71 {
72 arm_mat_trans_f32(&this->A.matrix, &At.matrix);
73 arm_mat_trans_f32(&this->C.matrix, &Ct.matrix);
75 }
76
77 void init(const float (&initialX)[STATES * 1])
78 {
79 xHat.copyData(initialX);
80 P.data = P0.data;
81 initialized = true;
82 }
83
85 {
86 if (!initialized)
87 {
88 return;
89 }
90
91 // Predict state
92 // TODO add control vector if necessary in the future
93 xHat = A * xHat;
94 P = A * P * At + Q;
95
96 // Update step
97 K = P * Ct * (C * P * Ct + R).inverse();
98 xHat = xHat + K * (y - C * xHat);
99 P = (I - K * C) * P;
100 }
101
102 const std::array<float, STATES> &getStateVectorAsMatrix() const { return xHat.data; }
103
108 inline std::array<float, INPUTS * INPUTS> &getMeasurementCovariance() { return R.data; }
109
110private:
117
123
130
135
140
147
154
159
164
170
171 bool initialized = false;
172};
173
174} // namespace tap::algorithms
175
176#endif // TAPROOT_KALMAN_FILTER_HPP_
Definition kalman_filter.hpp:45
const std::array< float, STATES > & getStateVectorAsMatrix() const
Definition kalman_filter.hpp:102
void init(const float(&initialX)[STATES *1])
Definition kalman_filter.hpp:77
std::array< float, INPUTS *INPUTS > & getMeasurementCovariance()
Definition kalman_filter.hpp:108
KalmanFilter(const float(&A)[STATES *STATES], const float(&C)[INPUTS *STATES], const float(&Q)[STATES *STATES], const float(&R)[INPUTS *INPUTS], const float(&P0)[STATES *STATES])
Definition kalman_filter.hpp:54
void performUpdate(const CMSISMat< INPUTS, 1 > &y)
Definition kalman_filter.hpp:84
Definition ballistics.cpp:29
Definition cmsis_mat.hpp:45
arm_matrix_instance_f32 matrix
Definition cmsis_mat.hpp:47
bool constructIdentityMatrix()
Definition cmsis_mat.hpp:104
void copyData(const float(&other)[ROWS *COLS])
Definition cmsis_mat.hpp:93
std::array< float, ROWS *COLS > data
Definition cmsis_mat.hpp:46