WPILibC++ 2024.3.2
SteadyStateKalmanFilter.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <wpi/SymbolExports.h>
8#include <wpi/array.h>
9
10#include "frc/EigenCore.h"
12#include "units/time.h"
13
14namespace frc {
15
16/**
17 * A Kalman filter combines predictions from a model and measurements to give an
18 * estimate of the true system state. This is useful because many states cannot
19 * be measured directly as a result of sensor noise, or because the state is
20 * "hidden".
21 *
22 * Kalman filters use a K gain matrix to determine whether to trust the model or
23 * measurements more. Kalman filter theory uses statistics to compute an optimal
24 * K gain which minimizes the sum of squares error in the state estimate. This K
25 * gain is used to correct the state estimate by some amount of the difference
26 * between the actual measurements and the measurements predicted by the model.
27 *
28 * This class assumes predict() and correct() are called in pairs, so the Kalman
29 * gain converges to a steady-state value. If they aren't, use KalmanFilter
30 * instead.
31 *
32 * For more on the underlying math, read
33 * https://file.tavsys.net/control/controls-engineering-in-frc.pdf chapter 9
34 * "Stochastic control theory".
35 *
36 * @tparam States Number of states.
37 * @tparam Inputs Number of inputs.
38 * @tparam Outputs Number of outputs.
39 */
40template <int States, int Inputs, int Outputs>
42 public:
46
49
50 /**
51 * Constructs a staeady-state Kalman filter with the given plant.
52 *
53 * See
54 * https://docs.wpilib.org/en/stable/docs/software/advanced-controls/state-space/state-space-observers.html#process-and-measurement-noise-covariance-matrices
55 * for how to select the standard deviations.
56 *
57 * @param plant The plant used for the prediction step.
58 * @param stateStdDevs Standard deviations of model states.
59 * @param measurementStdDevs Standard deviations of measurements.
60 * @param dt Nominal discretization timestep.
61 * @throws std::invalid_argument If the system is unobservable.
62 */
64 const StateArray& stateStdDevs,
65 const OutputArray& measurementStdDevs,
66 units::second_t dt);
67
70
71 /**
72 * Returns the steady-state Kalman gain matrix K.
73 */
74 const Matrixd<States, Outputs>& K() const { return m_K; }
75
76 /**
77 * Returns an element of the steady-state Kalman gain matrix K.
78 *
79 * @param i Row of K.
80 * @param j Column of K.
81 */
82 double K(int i, int j) const { return m_K(i, j); }
83
84 /**
85 * Returns the state estimate x-hat.
86 */
87 const StateVector& Xhat() const { return m_xHat; }
88
89 /**
90 * Returns an element of the state estimate x-hat.
91 *
92 * @param i Row of x-hat.
93 */
94 double Xhat(int i) const { return m_xHat(i); }
95
96 /**
97 * Set initial state estimate x-hat.
98 *
99 * @param xHat The state estimate x-hat.
100 */
101 void SetXhat(const StateVector& xHat) { m_xHat = xHat; }
102
103 /**
104 * Set an element of the initial state estimate x-hat.
105 *
106 * @param i Row of x-hat.
107 * @param value Value for element of x-hat.
108 */
109 void SetXhat(int i, double value) { m_xHat(i) = value; }
110
111 /**
112 * Resets the observer.
113 */
114 void Reset() { m_xHat.setZero(); }
115
116 /**
117 * Project the model into the future with a new control input u.
118 *
119 * @param u New control input from controller.
120 * @param dt Timestep for prediction.
121 */
122 void Predict(const InputVector& u, units::second_t dt);
123
124 /**
125 * Correct the state estimate x-hat using the measurements in y.
126 *
127 * @param u Same control input used in the last predict step.
128 * @param y Measurement vector.
129 */
130 void Correct(const InputVector& u, const OutputVector& y);
131
132 private:
134
135 /**
136 * The steady-state Kalman gain matrix.
137 */
139
140 /**
141 * The state estimate.
142 */
143 StateVector m_xHat;
144};
145
146extern template class EXPORT_TEMPLATE_DECLARE(WPILIB_DLLEXPORT)
147 SteadyStateKalmanFilter<1, 1, 1>;
148extern template class EXPORT_TEMPLATE_DECLARE(WPILIB_DLLEXPORT)
149 SteadyStateKalmanFilter<2, 1, 1>;
150
151} // namespace frc
152
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
A plant defined using state-space notation.
Definition: LinearSystem.h:31
A Kalman filter combines predictions from a model and measurements to give an estimate of the true sy...
Definition: SteadyStateKalmanFilter.h:41
void SetXhat(const StateVector &xHat)
Set initial state estimate x-hat.
Definition: SteadyStateKalmanFilter.h:101
SteadyStateKalmanFilter & operator=(SteadyStateKalmanFilter &&)=default
SteadyStateKalmanFilter(LinearSystem< States, Inputs, Outputs > &plant, const StateArray &stateStdDevs, const OutputArray &measurementStdDevs, units::second_t dt)
Constructs a staeady-state Kalman filter with the given plant.
Definition: SteadyStateKalmanFilter.inc:23
void Correct(const InputVector &u, const OutputVector &y)
Correct the state estimate x-hat using the measurements in y.
Definition: SteadyStateKalmanFilter.inc:80
void Predict(const InputVector &u, units::second_t dt)
Project the model into the future with a new control input u.
Definition: SteadyStateKalmanFilter.inc:74
void Reset()
Resets the observer.
Definition: SteadyStateKalmanFilter.h:114
void SetXhat(int i, double value)
Set an element of the initial state estimate x-hat.
Definition: SteadyStateKalmanFilter.h:109
double K(int i, int j) const
Returns an element of the steady-state Kalman gain matrix K.
Definition: SteadyStateKalmanFilter.h:82
double Xhat(int i) const
Returns an element of the state estimate x-hat.
Definition: SteadyStateKalmanFilter.h:94
const Matrixd< States, Outputs > & K() const
Returns the steady-state Kalman gain matrix K.
Definition: SteadyStateKalmanFilter.h:74
Vectord< Outputs > OutputVector
Definition: SteadyStateKalmanFilter.h:45
SteadyStateKalmanFilter(SteadyStateKalmanFilter &&)=default
Vectord< Inputs > InputVector
Definition: SteadyStateKalmanFilter.h:44
const StateVector & Xhat() const
Returns the state estimate x-hat.
Definition: SteadyStateKalmanFilter.h:87
Vectord< States > StateVector
Definition: SteadyStateKalmanFilter.h:43
This class is a wrapper around std::array that does compile time size checking.
Definition: array.h:26
Definition: AprilTagPoseEstimator.h:15
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition: EigenCore.h:21
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12
template class EXPORT_TEMPLATE_DECLARE(WPILIB_DLLEXPORT) LinearQuadraticRegulator< 1