KalmanFilter_2_1_1

class wpimath.estimator.KalmanFilter_2_1_1(plant: wpimath._controls._controls.system.LinearSystem_2_1_1, stateStdDevs: Tuple[float, float], measurementStdDevs: Tuple[float], dt: wpimath.units.seconds)

Bases: pybind11_object

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.

For more on the underlying math, read https://file.tavsys.net/control/controls-engineering-in-frc.pdf chapter 9 “Stochastic control theory”.

@tparam States Number of states. @tparam Inputs Number of inputs. @tparam Outputs Number of outputs.

Constructs a 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:
  • plant – The plant used for the prediction step.

  • stateStdDevs – Standard deviations of model states.

  • measurementStdDevs – Standard deviations of measurements.

  • dt – Nominal discretization timestep. @throws std::invalid_argument If the system is unobservable.

P(*args, **kwargs)

Overloaded function.

  1. P(self: wpimath._controls._controls.estimator.KalmanFilter_2_1_1) -> numpy.ndarray[numpy.float64[2, 2]]

Returns the error covariance matrix P.

  1. P(self: wpimath._controls._controls.estimator.KalmanFilter_2_1_1, i: int, j: int) -> float

Returns an element of the error covariance matrix P.

Parameters:
  • i – Row of P.

  • j – Column of P.

correct(*args, **kwargs)

Overloaded function.

  1. correct(self: wpimath._controls._controls.estimator.KalmanFilter_2_1_1, u: numpy.ndarray[numpy.float64[1, 1]], y: numpy.ndarray[numpy.float64[1, 1]]) -> None

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

Parameters:
  • u – Same control input used in the predict step.

  • y – Measurement vector.

  1. correct(self: wpimath._controls._controls.estimator.KalmanFilter_2_1_1, u: numpy.ndarray[numpy.float64[1, 1]], y: numpy.ndarray[numpy.float64[1, 1]], R: numpy.ndarray[numpy.float64[1, 1]]) -> None

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

This is useful for when the measurement noise covariances vary.

Parameters:
  • u – Same control input used in the predict step.

  • y – Measurement vector.

  • R – Continuous measurement noise covariance matrix.

predict(u: numpy.ndarray[numpy.float64[1, 1]], dt: wpimath.units.seconds) None

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

Parameters:
  • u – New control input from controller.

  • dt – Timestep for prediction.

reset() None

Resets the observer.

setP(P: numpy.ndarray[numpy.float64[2, 2]]) None

Set the current error covariance matrix P.

Parameters:

P – The error covariance matrix P.

setXhat(*args, **kwargs)

Overloaded function.

  1. setXhat(self: wpimath._controls._controls.estimator.KalmanFilter_2_1_1, xHat: numpy.ndarray[numpy.float64[2, 1]]) -> None

Set initial state estimate x-hat.

Parameters:

xHat – The state estimate x-hat.

  1. setXhat(self: wpimath._controls._controls.estimator.KalmanFilter_2_1_1, i: int, value: float) -> None

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

Parameters:
  • i – Row of x-hat.

  • value – Value for element of x-hat.

xhat(*args, **kwargs)

Overloaded function.

  1. xhat(self: wpimath._controls._controls.estimator.KalmanFilter_2_1_1) -> numpy.ndarray[numpy.float64[2, 1]]

Returns the state estimate x-hat.

  1. xhat(self: wpimath._controls._controls.estimator.KalmanFilter_2_1_1, i: int) -> float

Returns an element of the state estimate x-hat.

Parameters:

i – Row of x-hat.