OSVR-Core
FlexibleKalmanCorrect.h
Go to the documentation of this file.
1 
11 // Copyright 2016 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_FlexibleKalmanCorrect_h_GUID_354B7B5B_CF4F_49AF_7F71_A4279BD8DA8C
26 #define INCLUDED_FlexibleKalmanCorrect_h_GUID_354B7B5B_CF4F_49AF_7F71_A4279BD8DA8C
27 
28 // Internal Includes
29 #include "FlexibleKalmanBase.h"
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 // - none
36 
37 namespace osvr {
38 namespace kalman {
39  template <typename StateType, typename MeasurementType>
42  static const types::DimensionType m =
45  static const types::DimensionType n =
47 
48  CorrectionInProgress(StateType &state, MeasurementType &meas,
49  types::SquareMatrix<n> const &P_,
50  types::Matrix<n, m> const &PHt_,
51  types::SquareMatrix<m> const &S)
52  : P(P_), PHt(PHt_), denom(S), deltaz(meas.getResidual(state)),
53  stateCorrection(PHt * denom.solve(deltaz)), state_(state),
54  stateCorrectionFinite(stateCorrection.array().allFinite()) {}
55 
58 
61 
69  // TooN/TAG use this one, and others online seem to suggest it.
71 
74 
77 
80 
82 
88  bool finishCorrection(bool cancelIfNotFinite = true) {
89  // Compute the new error covariance
90  // differs from the (I-KH)P form by not factoring out the P (since
91  // we already have PHt computed).
93  P - (PHt * denom.solve(PHt.transpose()));
94 
95 #if 0
96  // Test fails with this one:
97  // VariedProcessModelStability/1.AbsolutePoseMeasurementXlate111,
98  // where TypeParam =
99  // osvr::kalman::PoseDampedConstantVelocityProcessModel
100  OSVR_KALMAN_DEBUG_OUTPUT(
101  "error covariance scale",
102  (types::SquareMatrix<n>::Identity() - PHt * denom.solve(H)));
104  (types::SquareMatrix<n>::Identity() - PHt * denom.solve(H)) * P;
105 #endif
106 
107  if (!newP.array().allFinite()) {
108  return false;
109  }
110 
111  // Correct the state estimate
112  state_.setStateVector(state_.stateVector() + stateCorrection);
113 
114  // Correct the error covariance
115  state_.setErrorCovariance(newP);
116 
117 #if 0
118  // Doesn't seem necessary to re-symmetrize the covariance matrix.
119  state_.setErrorCovariance((newP + newP.transpose()) / 2.);
120 #endif
121 
122  // Let the state do any cleanup it has to (like fixing externalized
123  // quaternions)
124  state_.postCorrect();
125  return true;
126  }
127 
128  private:
129  StateType &state_;
130  };
131 
132  template <typename StateType, typename ProcessModelType,
133  typename MeasurementType>
135  beginCorrection(StateType &state, ProcessModelType &processModel,
136  MeasurementType &meas) {
137 
139  static const auto m = types::Dimension<MeasurementType>::value;
141  static const auto n = types::Dimension<StateType>::value;
142 
144  types::Matrix<m, n> H = meas.getJacobian(state);
145 
147  types::SquareMatrix<m> R = meas.getCovariance(state);
148 
150  types::SquareMatrix<n> P = state.errorCovariance();
151 
153  types::Matrix<n, m> PHt = P * H.transpose();
154 
157  types::SquareMatrix<m> S = H * PHt + R;
158 
161  PHt, S);
162  }
163 
164 } // namespace kalman
165 } // namespace osvr
166 
167 #endif // INCLUDED_FlexibleKalmanCorrect_h_GUID_354B7B5B_CF4F_49AF_7F71_A4279BD8DA8C
bool finishCorrection(bool cancelIfNotFinite=true)
That&#39;s as far as we go here before you choose to continue.
Definition: FlexibleKalmanCorrect.h:88
Robust Cholesky decomposition of a matrix with pivoting.
Definition: LDLT.h:48
typename FilterType::State StateType
Given a filter type, get the state type.
Definition: FlexibleKalmanBase.h:91
typename detail::Dimension_impl< T >::type Dimension
Given a state or measurement, get the dimension as a std::integral_constant.
Definition: FlexibleKalmanBase.h:87
Eigen::LDLT< types::SquareMatrix< m > > denom
Decomposition of S.
Definition: FlexibleKalmanCorrect.h:70
const internal::solve_retval< LDLT, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: LDLT.h:186
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
types::SquareMatrix< n > P
State error covariance.
Definition: FlexibleKalmanCorrect.h:57
Definition: FlexibleKalmanCorrect.h:40
typename FilterType::ProcessModel ProcessModelType
Given a filter type, get the process model type.
Definition: FlexibleKalmanBase.h:95
static const types::DimensionType n
Dimension of state.
Definition: FlexibleKalmanCorrect.h:45
CorrectionInProgress< StateType, MeasurementType > beginCorrection(StateType &state, ProcessModelType &processModel, MeasurementType &meas)
Definition: FlexibleKalmanCorrect.h:135
types::Vector< m > deltaz
Measurement residual/delta z/innovation.
Definition: FlexibleKalmanCorrect.h:73
bool stateCorrectionFinite
Is the state correction free of NaNs and +- infs?
Definition: FlexibleKalmanCorrect.h:79
static const types::DimensionType m
Dimension of measurement.
Definition: FlexibleKalmanCorrect.h:42
types::Vector< n > stateCorrection
Corresponding state change to apply.
Definition: FlexibleKalmanCorrect.h:76
types::Matrix< n, m > PHt
The kalman gain stuff to not invert (called P12 in TAG)
Definition: FlexibleKalmanCorrect.h:60
std::size_t DimensionType
Type for dimensions.
Definition: FlexibleKalmanBase.h:51
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127