OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
OrientationConstantVelocity.h
Go to the documentation of this file.
1 
11 // Copyright 2015 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_OrientationConstantVelocity_h_GUID_72B09543_A2CC_458F_2973_7DFD0593F8CC
26 #define INCLUDED_OrientationConstantVelocity_h_GUID_72B09543_A2CC_458F_2973_7DFD0593F8CC
27 
28 // Internal Includes
29 #include "OrientationState.h"
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <cassert>
36 
37 namespace osvr {
38 namespace kalman {
41  public:
42  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
43  using State = orient_externalized_rotation::State;
44  using StateVector = orient_externalized_rotation::StateVector;
45  using StateSquareMatrix =
46  orient_externalized_rotation::StateSquareMatrix;
47  using NoiseAutocorrelation = types::Vector<3>;
48  OrientationConstantVelocityProcessModel(double orientationNoise = 0.1) {
49  setNoiseAutocorrelation(orientationNoise);
50  }
51  void setNoiseAutocorrelation(double orientationNoise = 0.1) {
52  m_mu.head<3>() = types::Vector<3>::Constant(orientationNoise);
53  }
54  void setNoiseAutocorrelation(NoiseAutocorrelation const &noise) {
55  m_mu = noise;
56  }
57 
59  StateSquareMatrix getStateTransitionMatrix(State const &,
60  double dt) const {
61  return orient_externalized_rotation::stateTransitionMatrix(dt);
62  }
63 
64  void predictState(State &s, double dt) {
65  auto xHatMinus = computeEstimate(s, dt);
66  auto Pminus = predictErrorCovariance(s, *this, dt);
67  s.setStateVector(xHatMinus);
68  s.setErrorCovariance(Pminus);
69  }
70 
77  StateSquareMatrix getSampledProcessNoiseCovariance(double dt) const {
78  auto const dim = types::Dimension<State>::value;
79  StateSquareMatrix cov = StateSquareMatrix::Zero();
80  auto dt3 = (dt * dt * dt) / 3;
81  auto dt2 = (dt * dt) / 2;
82  for (std::size_t xIndex = 0; xIndex < dim / 2; ++xIndex) {
83  auto xDotIndex = xIndex + dim / 2;
84  // xIndex is 'i' and xDotIndex is 'j' in eq. 4.8
85  const auto mu = getMu(xDotIndex);
86  cov(xIndex, xIndex) = mu * dt3;
87  auto symmetric = mu * dt2;
88  cov(xIndex, xDotIndex) = symmetric;
89  cov(xDotIndex, xIndex) = symmetric;
90  cov(xDotIndex, xDotIndex) = mu * dt;
91  }
92  return cov;
93  }
94 
97  StateVector computeEstimate(State &state, double dt) const {
98  OSVR_KALMAN_DEBUG_OUTPUT("Time change", dt);
99  StateVector ret = orient_externalized_rotation::applyVelocity(
100  state.stateVector(), dt);
101  return ret;
102  }
103 
104  private:
107  NoiseAutocorrelation m_mu;
108  double getMu(std::size_t index) const {
109  assert(index < types::Dimension<State>::value / 2 &&
110  "Should only be passing "
111  "'i' - the main state, not "
112  "the derivative");
113  // This may not be totally correct but it's one of the parameters
114  // you can kind of fudge in kalman filters anyway.
115  // Should techincally be the diagonal of the correlation kernel of
116  // the noise sources. (p77, p197 in Welch 1996)
117  return m_mu(index);
118  }
119  };
120 
121 } // namespace kalman
122 } // namespace osvr
123 #endif // INCLUDED_OrientationConstantVelocity_h_GUID_72B09543_A2CC_458F_2973_7DFD0593F8CC
Eigen::Matrix< Scalar, n, 1 > Vector
A vector of length n.
types::DimSquareMatrix< StateType > predictErrorCovariance(StateType const &state, ProcessModelType &processModel, double dt)
StateVector computeEstimate(State &state, double dt) const
typename detail::Dimension_impl< T >::type Dimension
The main namespace for all C++ elements of the framework, internal and external.
Definition: ClientKit.h:31
StateSquareMatrix getSampledProcessNoiseCovariance(double dt) const
StateSquareMatrix getStateTransitionMatrix(State const &, double dt) const
Also known as the "process model jacobian" in TAG, this is A.
A model for a 3DOF pose (with angular velocity)