OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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>
40  struct CorrectionInProgress {
42  static const types::DimensionType m =
43  types::Dimension<MeasurementType>::value;
45  static const types::DimensionType n =
46  types::Dimension<StateType>::value;
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 
57  types::SquareMatrix<n> P;
58 
60  types::Matrix<n, m> PHt;
61 
69  // TooN/TAG use this one, and others online seem to suggest it.
70  Eigen::LDLT<types::SquareMatrix<m>> denom;
71 
73  types::Vector<m> deltaz;
74 
76  types::Vector<n> stateCorrection;
77 
79  bool stateCorrectionFinite;
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).
92  types::SquareMatrix<n> newP =
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)));
103  types::SquareMatrix<n> newP =
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>
134  inline CorrectionInProgress<StateType, 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 
160  return CorrectionInProgress<StateType, MeasurementType>(state, meas, P,
161  PHt, S);
162  }
163 
164 } // namespace kalman
165 } // namespace osvr
166 
167 #endif // INCLUDED_FlexibleKalmanCorrect_h_GUID_354B7B5B_CF4F_49AF_7F71_A4279BD8DA8C
typename FilterType::State StateType
Given a filter type, get the state type.
typename detail::Dimension_impl< T >::type Dimension
Eigen::Matrix< Scalar, n, n > SquareMatrix
A square matrix, n x n.
The main namespace for all C++ elements of the framework, internal and external.
Definition: ClientKit.h:31
typename FilterType::ProcessModel ProcessModelType
Given a filter type, get the process model type.
CorrectionInProgress< StateType, MeasurementType > beginCorrection(StateType &state, ProcessModelType &processModel, MeasurementType &meas)
std::size_t DimensionType
Type for dimensions.
Eigen::Matrix< Scalar, m, n > Matrix
A matrix with rows = m, cols = n.