OSVR-Core
PoseConstantVelocity.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_PoseConstantVelocity_h_GUID_BC2C6525_D7E6_4BB2_0220_9D6065795E12
26 #define INCLUDED_PoseConstantVelocity_h_GUID_BC2C6525_D7E6_4BB2_0220_9D6065795E12
27 
28 // Internal Includes
29 #include "PoseState.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
47  PoseConstantVelocityProcessModel(double positionNoise = 0.01,
48  double orientationNoise = 0.1) {
49  setNoiseAutocorrelation(positionNoise, orientationNoise);
50  }
51  void setNoiseAutocorrelation(double positionNoise = 0.01,
52  double orientationNoise = 0.1) {
53  m_mu.head<3>() = types::Vector<3>::Constant(positionNoise);
54  m_mu.tail<3>() = types::Vector<3>::Constant(orientationNoise);
55  }
56  void setNoiseAutocorrelation(NoiseAutocorrelation const &noise) {
57  m_mu = noise;
58  }
59 
62  double dt) const {
63  return pose_externalized_rotation::stateTransitionMatrix(dt);
64  }
65 
66  void predictState(State &s, double dt) {
67  auto xHatMinus = computeEstimate(s, dt);
68  auto Pminus = predictErrorCovariance(s, *this, dt);
69  s.setStateVector(xHatMinus);
70  s.setErrorCovariance(Pminus);
71  }
72 
80  auto const dim = types::Dimension<State>::value;
81  StateSquareMatrix cov = StateSquareMatrix::Zero();
82  auto dt3 = (dt * dt * dt) / 3;
83  auto dt2 = (dt * dt) / 2;
84  for (std::size_t xIndex = 0; xIndex < dim / 2; ++xIndex) {
85  auto xDotIndex = xIndex + dim / 2;
86  // xIndex is 'i' and xDotIndex is 'j' in eq. 4.8
87  const auto mu = getMu(xIndex);
88  cov(xIndex, xIndex) = mu * dt3;
89  auto symmetric = mu * dt2;
90  cov(xIndex, xDotIndex) = symmetric;
91  cov(xDotIndex, xIndex) = symmetric;
92  cov(xDotIndex, xDotIndex) = mu * dt;
93  }
94  return cov;
95  }
96 
99  StateVector computeEstimate(State &state, double dt) const {
100  OSVR_KALMAN_DEBUG_OUTPUT("Time change", dt);
101  StateVector ret = pose_externalized_rotation::applyVelocity(
102  state.stateVector(), dt);
103 
104  return ret;
105  }
106 
107  private:
111  double getMu(std::size_t index) const {
112  assert(index < types::Dimension<State>::value / 2 &&
113  "Should only be passing "
114  "'i' - the main state, not "
115  "the derivative");
116  // This may not be totally correct but it's one of the parameters
117  // you can kind of fudge in kalman filters anyway.
118  // Should techincally be the diagonal of the correlation kernel of
119  // the noise sources. (p77, p197 in Welch 1996)
120  return m_mu(index);
121  }
122  };
123 
124 } // namespace kalman
125 } // namespace osvr
126 #endif // INCLUDED_PoseConstantVelocity_h_GUID_BC2C6525_D7E6_4BB2_0220_9D6065795E12
types::DimSquareMatrix< StateType > predictErrorCovariance(StateType const &state, ProcessModelType &processModel, double dt)
Computes P-.
Definition: FlexibleKalmanBase.h:135
StateVector computeEstimate(State &state, double dt) const
Returns a 12-element vector containing a predicted state based on a constant velocity process model...
Definition: PoseConstantVelocity.h:99
typename detail::Dimension_impl< T >::type Dimension
Given a state or measurement, get the dimension as a std::integral_constant.
Definition: FlexibleKalmanBase.h:87
A constant-velocity model for a 6DOF pose (with velocities)
Definition: PoseConstantVelocity.h:40
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
StateSquareMatrix getStateTransitionMatrix(State const &, double dt) const
Also known as the "process model jacobian" in TAG, this is A.
Definition: PoseConstantVelocity.h:61
StateVector const & stateVector() const
xhat
Definition: PoseState.h:177
Header.
StateSquareMatrix getSampledProcessNoiseCovariance(double dt) const
This is Q(deltaT) - the Sampled Process Noise Covariance.
Definition: PoseConstantVelocity.h:79
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
void setStateVector(StateVector const &state)
set xhat
Definition: PoseState.h:175