11#include <Eigen/Cholesky>
49template <
int States,
int Inputs,
int Outputs>
81 : m_f(
std::move(f)), m_h(
std::move(h)) {
94 m_f, m_xHat, InputVector::Zero());
96 m_h, m_xHat, InputVector::Zero());
111 fmt::format(
"{}\n\nQ =\n{}\n",
to_string(P.error()), discQ);
114 throw std::invalid_argument(msg);
118 fmt::format(
"{}\n\nR =\n{}\n",
to_string(P.error()), discR);
121 throw std::invalid_argument(msg);
123 std::string msg = fmt::format(
124 "The (A, C) pair is not detectable.\n\nA =\n{}\nC =\n{}\n",
128 throw std::invalid_argument(msg);
130 std::string msg = fmt::format(
"{}\n\nA =\n{}\nQ =\n{}\n",
134 throw std::invalid_argument(msg);
137 m_initP = StateMatrix::Zero();
171 m_residualFuncY(
std::move(residualFuncY)),
172 m_addFuncX(
std::move(addFuncX)) {
178 m_f, m_xHat, InputVector::Zero());
180 m_h, m_xHat, InputVector::Zero());
195 fmt::format(
"{}\n\nQ =\n{}\n",
to_string(P.error()), discQ);
198 throw std::invalid_argument(msg);
202 fmt::format(
"{}\n\nR =\n{}\n",
to_string(P.error()), discR);
205 throw std::invalid_argument(msg);
207 std::string msg = fmt::format(
208 "The (A, C) pair is not detectable.\n\nA =\n{}\nC =\n{}\n",
212 throw std::invalid_argument(msg);
214 std::string msg = fmt::format(
"{}\n\nA =\n{}\nQ =\n{}\n",
218 throw std::invalid_argument(msg);
221 m_initP = StateMatrix::Zero();
237 double P(
int i,
int j)
const {
return m_P(i, j); }
256 double Xhat(
int i)
const {
return m_xHat(i); }
271 void SetXhat(
int i,
double value) { m_xHat(i) = value; }
297 m_xHat =
RK4(m_f, m_xHat, u, dt);
300 m_P = discA * m_P * discA.transpose() + discQ;
353 Correct<Rows>(u, y, std::move(h), R, std::move(residualFuncY),
354 std::move(addFuncX));
401 S.transpose().ldlt().solve(C * m_P.transpose()).transpose();
404 m_xHat = addFuncX(m_xHat, K * residualFuncY(y, h(m_xHat, u)));
408 m_P = (StateMatrix::Identity() - K * C) * m_P *
409 (StateMatrix::Identity() - K * C).transpose() +
410 K * discR * K.transpose();
423 units::second_t m_dt;
A Kalman filter combines predictions from a model and measurements to give an estimate of the true sy...
Definition ExtendedKalmanFilter.h:50
void Correct(const InputVector &u, const Vectord< Rows > &y, std::function< Vectord< Rows >(const StateVector &, const InputVector &)> h, const Matrixd< Rows, Rows > &R, std::function< Vectord< Rows >(const Vectord< Rows > &, const Vectord< Rows > &)> residualFuncY, std::function< StateVector(const StateVector &, const StateVector &)> addFuncX)
Correct the state estimate x-hat using the measurements in y.
Definition ExtendedKalmanFilter.h:374
void Correct(const InputVector &u, const OutputVector &y)
Correct the state estimate x-hat using the measurements in y.
Definition ExtendedKalmanFilter.h:311
ExtendedKalmanFilter(std::function< StateVector(const StateVector &, const InputVector &)> f, std::function< OutputVector(const StateVector &, const InputVector &)> h, const StateArray &stateStdDevs, const OutputArray &measurementStdDevs, std::function< OutputVector(const OutputVector &, const OutputVector &)> residualFuncY, std::function< StateVector(const StateVector &, const StateVector &)> addFuncX, units::second_t dt)
Constructs an extended Kalman filter.
Definition ExtendedKalmanFilter.h:160
const StateMatrix & P() const
Returns the error covariance matrix P.
Definition ExtendedKalmanFilter.h:229
ExtendedKalmanFilter(std::function< StateVector(const StateVector &, const InputVector &)> f, std::function< OutputVector(const StateVector &, const InputVector &)> h, const StateArray &stateStdDevs, const OutputArray &measurementStdDevs, units::second_t dt)
Constructs an extended Kalman filter.
Definition ExtendedKalmanFilter.h:76
Vectord< Inputs > InputVector
Definition ExtendedKalmanFilter.h:53
void Reset()
Resets the observer.
Definition ExtendedKalmanFilter.h:276
void Predict(const InputVector &u, units::second_t dt)
Project the model into the future with a new control input u.
Definition ExtendedKalmanFilter.h:287
Matrixd< States, States > StateMatrix
Definition ExtendedKalmanFilter.h:59
Vectord< Outputs > OutputVector
Definition ExtendedKalmanFilter.h:54
void SetP(const StateMatrix &P)
Set the current error covariance matrix P.
Definition ExtendedKalmanFilter.h:244
void SetXhat(int i, double value)
Set an element of the initial state estimate x-hat.
Definition ExtendedKalmanFilter.h:271
const StateVector & Xhat() const
Returns the state estimate x-hat.
Definition ExtendedKalmanFilter.h:249
Vectord< States > StateVector
Definition ExtendedKalmanFilter.h:52
void SetXhat(const StateVector &xHat)
Set initial state estimate x-hat.
Definition ExtendedKalmanFilter.h:263
double P(int i, int j) const
Returns an element of the error covariance matrix P.
Definition ExtendedKalmanFilter.h:237
void Correct(const InputVector &u, const OutputVector &y, const Matrixd< Outputs, Outputs > &R)
Correct the state estimate x-hat using the measurements in y.
Definition ExtendedKalmanFilter.h:324
void Correct(const InputVector &u, const Vectord< Rows > &y, std::function< Vectord< Rows >(const StateVector &, const InputVector &)> h, const Matrixd< Rows, Rows > &R)
Correct the state estimate x-hat using the measurements in y.
Definition ExtendedKalmanFilter.h:343
double Xhat(int i) const
Returns an element of the state estimate x-hat.
Definition ExtendedKalmanFilter.h:256
This class is a wrapper around std::array that does compile time size checking.
Definition array.h:26
static void ReportError(const S &format, Args &&... args)
Definition MathShared.h:62
T RK4(F &&f, T x, units::second_t dt)
Performs 4th order Runge-Kutta integration of dx/dt = f(x) for dt.
Definition NumericalIntegration.h:23
void DiscretizeAQ(const Matrixd< States, States > &contA, const Matrixd< States, States > &contQ, units::second_t dt, Matrixd< States, States > *discA, Matrixd< States, States > *discQ)
Discretizes the given continuous A and Q matrices.
Definition Discretization.h:71
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21
Eigen::Vector< double, Size > Vectord
Definition EigenCore.h:12
Matrixd< Outputs, Outputs > DiscretizeR(const Matrixd< Outputs, Outputs > &R, units::second_t dt)
Returns a discretized version of the provided continuous measurement noise covariance matrix.
Definition Discretization.h:113
constexpr Matrixd< sizeof...(Ts), sizeof...(Ts)> MakeCovMatrix(Ts... stdDevs)
Creates a covariance matrix from the given vector for use with Kalman filters.
Definition StateSpaceUtil.h:75
wpi::expected< Eigen::Matrix< double, States, States >, DAREError > DARE(const Eigen::Matrix< double, States, States > &A, const Eigen::Matrix< double, States, Inputs > &B, const Eigen::Matrix< double, States, States > &Q, const Eigen::Matrix< double, Inputs, Inputs > &R, bool checkPreconditions=true)
Computes the unique stabilizing solution X to the discrete-time algebraic Riccati equation:
Definition DARE.h:165
constexpr std::string_view to_string(const DAREError &error)
Converts the given DAREError enum to a string.
Definition DARE.h:39
@ QNotPositiveSemidefinite
Q was not positive semidefinite.
@ RNotSymmetric
R was not symmetric.
@ QNotSymmetric
Q was not symmetric.
@ ACNotDetectable
(A, C) pair where Q = CᵀC was not detectable.
@ RNotPositiveDefinite
R was not positive definite.
@ ABNotStabilizable
(A, B) pair was not stabilizable.
bool IsDetectable(const Matrixd< States, States > &A, const Matrixd< Outputs, States > &C)
Returns true if (A, C) is a detectable pair.
Definition StateSpaceUtil.h:309
auto NumericalJacobianX(F &&f, const Vectord< States > &x, const Vectord< Inputs > &u, Args &&... args)
Returns numerical Jacobian with respect to x for f(x, u, ...).
Definition NumericalJacobian.h:51
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
#define S(label, offset, message)
Definition Errors.h:113