OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
FlexibleUnscentedCorrect.h
Go to the documentation of this file.
1 
19 // Copyright 2016 Sensics, Inc.
20 //
21 // Licensed under the Apache License, Version 2.0 (the "License");
22 // you may not use this file except in compliance with the License.
23 // You may obtain a copy of the License at
24 //
25 // http://www.apache.org/licenses/LICENSE-2.0
26 //
27 // Unless required by applicable law or agreed to in writing, software
28 // distributed under the License is distributed on an "AS IS" BASIS,
29 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 // See the License for the specific language governing permissions and
31 // limitations under the License.
32 
33 #ifndef INCLUDED_FlexibleUnscentedCorrect_h_GUID_21E01E3B_5BD0_4F85_3B75_BBF6C657DBB4
34 #define INCLUDED_FlexibleUnscentedCorrect_h_GUID_21E01E3B_5BD0_4F85_3B75_BBF6C657DBB4
35 
36 // Internal Includes
37 #include "SigmaPointGenerator.h"
38 
39 // Library/third-party includes
40 // - none
41 
42 // Standard includes
43 // - none
44 
45 namespace osvr {
46 namespace kalman {
47 
48  template <typename State, typename Measurement>
50  public:
51 #if 0
52  static const types::DimensionType StateDim =
54  static const types::DimensionType MeasurementDim =
56 #endif
58  static const types::DimensionType m =
60 
61  using StateVec = types::DimVector<State>;
62  using StateSquareMatrix = types::DimSquareMatrix<State>;
63  using MeasurementVec = types::DimVector<Measurement>;
64  using MeasurementSquareMatrix = types::DimSquareMatrix<Measurement>;
65 
68  using AugmentedStateVec = types::Vector<AugmentedStateDim>;
69  using AugmentedStateCovMatrix = types::SquareMatrix<AugmentedStateDim>;
70  using SigmaPointsGen =
72 
73  static const types::DimensionType NumSigmaPoints =
74  SigmaPointsGen::NumSigmaPoints;
75 
76  using Reconstruction =
78  using TransformedSigmaPointsMat =
79  typename Reconstruction::TransformedSigmaPointsMat;
80 
81  using GainMatrix = types::Matrix<n, m>;
82 
84  State &s, Measurement &m,
86  : state(s), measurement(m),
87  sigmaPoints(getAugmentedStateVec(s, m),
88  getAugmentedStateCov(s, m), params),
89  transformedPoints(
90  transformSigmaPoints(state, measurement, sigmaPoints)),
91  reconstruction(sigmaPoints, transformedPoints),
92  innovationCovariance(computeInnovationCovariance(
93  state, measurement, reconstruction)),
94  PvvDecomp(innovationCovariance.ldlt()),
95 #if 0
96  K(computeKalmanGain(innovationCovariance, reconstruction)),
97 #endif
98  deltaz(measurement.getResidual(reconstruction.getMean(), state)),
99 #if 0
100  stateCorrection(K * deltaz),
101 #else
102  stateCorrection(
103  computeStateCorrection(reconstruction, deltaz, PvvDecomp)),
104 #endif
105  stateCorrectionFinite(stateCorrection.array().allFinite()) {
106  }
107 
108  static AugmentedStateVec getAugmentedStateVec(State const &s,
109  Measurement const &m) {
110  AugmentedStateVec ret;
112  ret << s.stateVector(), MeasurementVec::Zero();
113  return ret;
114  }
115 
116  static AugmentedStateCovMatrix getAugmentedStateCov(State const &s,
117  Measurement &meas) {
118  AugmentedStateCovMatrix ret;
119  ret << s.errorCovariance(), types::Matrix<n, m>::Zero(),
120  types::Matrix<m, n>::Zero(), meas.getCovariance(s);
121  return ret;
122  }
123 
127  static TransformedSigmaPointsMat
128  transformSigmaPoints(State const &s, Measurement &meas,
129  SigmaPointsGen const &sigmaPoints) {
130  TransformedSigmaPointsMat ret;
131  State tempS = s;
132  for (std::size_t i = 0; i < NumSigmaPoints; ++i) {
133  tempS.setStateVector(sigmaPoints.getSigmaPoint(i));
134  ret.col(i) = meas.predictMeasurement(tempS);
135  }
136  return ret;
137  }
138 
139  static MeasurementSquareMatrix
140  computeInnovationCovariance(State const &s, Measurement &meas,
141  Reconstruction const &recon) {
142  return recon.getCov() + meas.getCovariance(s);
143  }
144 
145 #if 0
146  // Solve for K in K=Pxy Pvv^-1
147  // where the cross-covariance matrix from the reconstruction is
148  // transpose(Pxy) and Pvv is the reconstructed covariance plus the
149  // measurement covariance
150  static GainMatrix computeKalmanGain(MeasurementSquareMatrix const &Pvv,
151  Reconstruction const &recon) {
152  // (Actually solves with transpose(Pvv) * transpose(K) =
153  // transpose(Pxy) )
154  GainMatrix ret = Pvv.transpose().ldlt().solve(recon.getCrossCov());
155  return ret;
156  }
157 #endif
158  static StateVec computeStateCorrection(
159  Reconstruction const &recon, MeasurementVec const &deltaz,
160  Eigen::LDLT<MeasurementSquareMatrix> const &pvvDecomp) {
161  StateVec ret = recon.getCrossCov() * pvvDecomp.solve(deltaz);
162  return ret;
163  }
164 
170  bool finishCorrection(bool cancelIfNotFinite = true) {
171 #if 0
172  StateSquareMatrix newP = state.errorCovariance() -
173  K * innovationCovariance * K.transpose();
174 #else
175  StateSquareMatrix newP =
190  state.errorCovariance() -
191  reconstruction.getCrossCov() *
192  PvvDecomp.solve(MeasurementSquareMatrix::Identity()) *
193  reconstruction.getCrossCov().transpose();
194 #endif
195  bool finite = newP.array().allFinite();
196  if (cancelIfNotFinite && !finite) {
197  return false;
198  }
199 
200  state.setStateVector(state.stateVector() + stateCorrection);
201 
202  state.setErrorCovariance(newP);
203  // Let the state do any cleanup it has to (like fixing externalized
204  // quaternions)
205  state.postCorrect();
206  return finite;
207  }
208 
209  State &state;
210  Measurement &measurement;
211  SigmaPointsGen sigmaPoints;
212  TransformedSigmaPointsMat transformedPoints;
213  Reconstruction reconstruction;
215  MeasurementSquareMatrix innovationCovariance;
216  Eigen::LDLT<MeasurementSquareMatrix> PvvDecomp;
217 #if 0
218  GainMatrix K;
219 #endif
222  StateVec stateCorrection;
223  bool stateCorrectionFinite;
224  };
225  template <typename State, typename Measurement>
227  beginUnscentedCorrection(
228  State &s, Measurement &m,
229  SigmaPointParameters const &params = SigmaPointParameters()) {
231  params);
232  }
233 } // namespace kalman
234 } // namespace osvr
235 #endif // INCLUDED_FlexibleUnscentedCorrect_h_GUID_21E01E3B_5BD0_4F85_3B75_BBF6C657DBB4
Eigen::Matrix< Scalar, n, 1 > Vector
A vector of length n.
static AugmentedStateVec getAugmentedStateVec(State const &s, Measurement const &m)
static TransformedSigmaPointsMat transformSigmaPoints(State const &s, Measurement &meas, SigmaPointsGen const &sigmaPoints)
typename detail::Dimension_impl< T >::type Dimension
Eigen::Matrix< Scalar, n, n > SquareMatrix
A square matrix, n x n.
Vector< Dimension< T >::value > DimVector
A vector of length = dimension of T.
static const types::DimensionType AugmentedStateDim
state augmented with measurement noise mean
SquareMatrix< Dimension< T >::value > DimSquareMatrix
A square matrix, n x n, where n is the dimension of T.
types::Vector< m > deltaz
reconstructed mean measurement residual/delta z/innovation
bool finishCorrection(bool cancelIfNotFinite=true)
std::size_t DimensionType
Type for dimensions.
Eigen::Matrix< Scalar, m, n > Matrix
A matrix with rows = m, cols = n.