Class SteadyStateKalmanFilter<States extends Num,Inputs extends Num,Outputs extends Num>
- Type Parameters:
States
- Number of states.Inputs
- Number of inputs.Outputs
- Number of outputs.
Kalman filters use a K gain matrix to determine whether to trust the model or measurements more. Kalman filter theory uses statistics to compute an optimal K gain which minimizes the sum of squares error in the state estimate. This K gain is used to correct the state estimate by some amount of the difference between the actual measurements and the measurements predicted by the model.
This class assumes predict() and correct() are called in pairs, so the Kalman gain converges
to a steady-state value. If they aren't, use KalmanFilter
instead.
For more on the underlying math, read https://file.tavsys.net/control/controls-engineering-in-frc.pdf chapter 9 "Stochastic control theory".
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Correct the state estimate x-hat using the measurements in y.getK()
Returns the steady-state Kalman gain matrix K.double
getK
(int row, int col) Returns an element of the steady-state Kalman gain matrix K.getXhat()
Returns the state estimate x-hat.double
getXhat
(int row) Returns an element of the state estimate x-hat.void
Project the model into the future with a new control input u.final void
reset()
Resets the observer.void
setXhat
(int row, double value) Set an element of the initial state estimate x-hat.void
Set initial state estimate x-hat.
-
Constructor Details
-
SteadyStateKalmanFilter
public SteadyStateKalmanFilter(Nat<States> states, Nat<Outputs> outputs, LinearSystem<States, Inputs, Outputs> plant, Matrix<States, N1> stateStdDevs, Matrix<Outputs, N1> measurementStdDevs, double dtSeconds) Constructs a steady-state Kalman filter with the given plant.See https://docs.wpilib.org/en/stable/docs/software/advanced-controls/state-space/state-space-observers.html#process-and-measurement-noise-covariance-matrices for how to select the standard deviations.
- Parameters:
states
- A Nat representing the states of the system.outputs
- A Nat representing the outputs of the system.plant
- The plant used for the prediction step.stateStdDevs
- Standard deviations of model states.measurementStdDevs
- Standard deviations of measurements.dtSeconds
- Nominal discretization timestep.- Throws:
IllegalArgumentException
- If the system is undetectable.
-
-
Method Details
-
reset
Resets the observer. -
getK
Returns the steady-state Kalman gain matrix K.- Returns:
- The steady-state Kalman gain matrix K.
-
getK
Returns an element of the steady-state Kalman gain matrix K.- Parameters:
row
- Row of K.col
- Column of K.- Returns:
- the element (i, j) of the steady-state Kalman gain matrix K.
-
setXhat
Set initial state estimate x-hat.- Parameters:
xhat
- The state estimate x-hat.
-
setXhat
Set an element of the initial state estimate x-hat.- Parameters:
row
- Row of x-hat.value
- Value for element of x-hat.
-
getXhat
Returns the state estimate x-hat.- Returns:
- The state estimate x-hat.
-
getXhat
Returns an element of the state estimate x-hat.- Parameters:
row
- Row of x-hat.- Returns:
- the state estimate x-hat at that row.
-
predict
Project the model into the future with a new control input u.- Parameters:
u
- New control input from controller.dtSeconds
- Timestep for prediction.
-
correct
Correct the state estimate x-hat using the measurements in y.- Parameters:
u
- Same control input used in the last predict step.y
- Measurement vector.
-