WPILibC++ 2024.3.2
frc::SteadyStateKalmanFilter< States, Inputs, Outputs > Class Template Reference

A Kalman filter combines predictions from a model and measurements to give an estimate of the true system state. More...

#include <frc/estimator/SteadyStateKalmanFilter.h>

Public Types

using StateVector = Vectord< States >
 
using InputVector = Vectord< Inputs >
 
using OutputVector = Vectord< Outputs >
 
using StateArray = wpi::array< double, States >
 
using OutputArray = wpi::array< double, Outputs >
 

Public Member Functions

 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. More...
 
 SteadyStateKalmanFilter (SteadyStateKalmanFilter &&)=default
 
SteadyStateKalmanFilteroperator= (SteadyStateKalmanFilter &&)=default
 
const Matrixd< States, Outputs > & K () const
 Returns the steady-state Kalman gain matrix K. More...
 
double K (int i, int j) const
 Returns an element of the steady-state Kalman gain matrix K. More...
 
const StateVectorXhat () const
 Returns the state estimate x-hat. More...
 
double Xhat (int i) const
 Returns an element of the state estimate x-hat. More...
 
void SetXhat (const StateVector &xHat)
 Set initial state estimate x-hat. More...
 
void SetXhat (int i, double value)
 Set an element of the initial state estimate x-hat. More...
 
void Reset ()
 Resets the observer. More...
 
void Predict (const InputVector &u, units::second_t dt)
 Project the model into the future with a new control input u. More...
 
void Correct (const InputVector &u, const OutputVector &y)
 Correct the state estimate x-hat using the measurements in y. More...
 

Detailed Description

template<int States, int Inputs, int Outputs>
class frc::SteadyStateKalmanFilter< States, Inputs, Outputs >

A Kalman filter combines predictions from a model and measurements to give an estimate of the true system state.

This is useful because many states cannot be measured directly as a result of sensor noise, or because the state is "hidden".

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".

Template Parameters
StatesNumber of states.
InputsNumber of inputs.
OutputsNumber of outputs.

Member Typedef Documentation

◆ InputVector

template<int States, int Inputs, int Outputs>
using frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::InputVector = Vectord<Inputs>

◆ OutputArray

template<int States, int Inputs, int Outputs>
using frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::OutputArray = wpi::array<double, Outputs>

◆ OutputVector

template<int States, int Inputs, int Outputs>
using frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::OutputVector = Vectord<Outputs>

◆ StateArray

template<int States, int Inputs, int Outputs>
using frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::StateArray = wpi::array<double, States>

◆ StateVector

template<int States, int Inputs, int Outputs>
using frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::StateVector = Vectord<States>

Constructor & Destructor Documentation

◆ SteadyStateKalmanFilter() [1/2]

template<int States, int Inputs, int Outputs>
frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::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.

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
plantThe plant used for the prediction step.
stateStdDevsStandard deviations of model states.
measurementStdDevsStandard deviations of measurements.
dtNominal discretization timestep.
Exceptions
std::invalid_argumentIf the system is unobservable.

◆ SteadyStateKalmanFilter() [2/2]

template<int States, int Inputs, int Outputs>
frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::SteadyStateKalmanFilter ( SteadyStateKalmanFilter< States, Inputs, Outputs > &&  )
default

Member Function Documentation

◆ Correct()

template<int States, int Inputs, int Outputs>
void frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::Correct ( const InputVector u,
const OutputVector y 
)

Correct the state estimate x-hat using the measurements in y.

Parameters
uSame control input used in the last predict step.
yMeasurement vector.

◆ K() [1/2]

template<int States, int Inputs, int Outputs>
const Matrixd< States, Outputs > & frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::K ( ) const
inline

Returns the steady-state Kalman gain matrix K.

◆ K() [2/2]

template<int States, int Inputs, int Outputs>
double frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::K ( int  i,
int  j 
) const
inline

Returns an element of the steady-state Kalman gain matrix K.

Parameters
iRow of K.
jColumn of K.

◆ operator=()

template<int States, int Inputs, int Outputs>
SteadyStateKalmanFilter & frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::operator= ( SteadyStateKalmanFilter< States, Inputs, Outputs > &&  )
default

◆ Predict()

template<int States, int Inputs, int Outputs>
void frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::Predict ( const InputVector u,
units::second_t  dt 
)

Project the model into the future with a new control input u.

Parameters
uNew control input from controller.
dtTimestep for prediction.

◆ Reset()

template<int States, int Inputs, int Outputs>
void frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::Reset ( )
inline

Resets the observer.

◆ SetXhat() [1/2]

template<int States, int Inputs, int Outputs>
void frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::SetXhat ( const StateVector xHat)
inline

Set initial state estimate x-hat.

Parameters
xHatThe state estimate x-hat.

◆ SetXhat() [2/2]

template<int States, int Inputs, int Outputs>
void frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::SetXhat ( int  i,
double  value 
)
inline

Set an element of the initial state estimate x-hat.

Parameters
iRow of x-hat.
valueValue for element of x-hat.

◆ Xhat() [1/2]

template<int States, int Inputs, int Outputs>
const StateVector & frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::Xhat ( ) const
inline

Returns the state estimate x-hat.

◆ Xhat() [2/2]

template<int States, int Inputs, int Outputs>
double frc::SteadyStateKalmanFilter< States, Inputs, Outputs >::Xhat ( int  i) const
inline

Returns an element of the state estimate x-hat.

Parameters
iRow of x-hat.

The documentation for this class was generated from the following files: