OSVR-Core
PoseState.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_PoseState_h_GUID_57A246BA_940D_4386_ECA4_4C4172D97F5A
26 #define INCLUDED_PoseState_h_GUID_57A246BA_940D_4386_ECA4_4C4172D97F5A
27 
28 // Internal Includes
29 #include "ExternalQuaternion.h"
30 #include "FlexibleKalmanBase.h"
31 
32 // Library/third-party includes
33 #include <Eigen/Core>
34 #include <Eigen/Geometry>
35 
36 // Standard includes
37 // - none
38 
39 namespace osvr {
40 namespace kalman {
41  namespace pose_externalized_rotation {
42  using Dimension = types::DimensionConstant<12>;
43  using StateVector = types::DimVector<Dimension>;
44  using StateVectorBlock3 = StateVector::FixedSegmentReturnType<3>::Type;
45  using ConstStateVectorBlock3 =
46  StateVector::ConstFixedSegmentReturnType<3>::Type;
47 
48  using StateVectorBlock6 = StateVector::FixedSegmentReturnType<6>::Type;
49  using StateSquareMatrix = types::DimSquareMatrix<Dimension>;
50 
53  inline StateVectorBlock3 position(StateVector &vec) {
54  return vec.head<3>();
55  }
56  inline ConstStateVectorBlock3 position(StateVector const &vec) {
57  return vec.head<3>();
58  }
59 
60  inline StateVectorBlock3 incrementalOrientation(StateVector &vec) {
61  return vec.segment<3>(3);
62  }
63  inline ConstStateVectorBlock3
64  incrementalOrientation(StateVector const &vec) {
65  return vec.segment<3>(3);
66  }
67 
68  inline StateVectorBlock3 velocity(StateVector &vec) {
69  return vec.segment<3>(6);
70  }
71  inline ConstStateVectorBlock3 velocity(StateVector const &vec) {
72  return vec.segment<3>(6);
73  }
74 
75  inline StateVectorBlock3 angularVelocity(StateVector &vec) {
76  return vec.segment<3>(9);
77  }
78  inline ConstStateVectorBlock3 angularVelocity(StateVector const &vec) {
79  return vec.segment<3>(9);
80  }
81 
83  inline StateVectorBlock6 velocities(StateVector &vec) {
84  return vec.segment<6>(6);
85  }
87 
90  inline StateSquareMatrix stateTransitionMatrix(double dt) {
91  // eq. 4.5 in Welch 1996 - except we have all the velocities at the
92  // end
93  StateSquareMatrix A = StateSquareMatrix::Identity();
94  A.topRightCorner<6, 6>() = types::SquareMatrix<6>::Identity() * dt;
95 
96  return A;
97  }
100  inline double computeAttenuation(double damping, double dt) {
101  return std::pow(damping, dt);
102  }
103 
108  inline StateSquareMatrix
109  stateTransitionMatrixWithVelocityDamping(double dt, double damping) {
110  // eq. 4.5 in Welch 1996
111  auto A = stateTransitionMatrix(dt);
112  A.bottomRightCorner<6, 6>() *= computeAttenuation(damping, dt);
113  return A;
114  }
115 
120  inline StateSquareMatrix
122  double posDamping,
123  double oriDamping) {
124  // eq. 4.5 in Welch 1996
125  auto A = stateTransitionMatrix(dt);
126  A.block<3, 3>(6, 6) *= computeAttenuation(posDamping, dt);
127  A.bottomRightCorner<3, 3>() *= computeAttenuation(oriDamping, dt);
128  return A;
129  }
130 
132  inline StateVector applyVelocity(StateVector const &state, double dt) {
133  // eq. 4.5 in Welch 1996
134 
137 
138  StateVector ret = state;
139  position(ret) += velocity(state) * dt;
140  incrementalOrientation(ret) += angularVelocity(state) * dt;
141  return ret;
142  }
143 
145  inline void dampenVelocities(StateVector &state, double damping,
146  double dt) {
147  auto attenuation = computeAttenuation(damping, dt);
148  velocities(state) *= attenuation;
149  }
150 
153  double posDamping,
154  double oriDamping, double dt) {
155  velocity(state) *= computeAttenuation(posDamping, dt);
156  angularVelocity(state) *= computeAttenuation(oriDamping, dt);
157  }
158 
159  inline Eigen::Quaterniond
160  incrementalOrientationToQuat(StateVector const &state) {
161  return external_quat::vecToQuat(incrementalOrientation(state));
162  }
163 
164  class State : public HasDimension<12> {
165  public:
166  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
167 
170  : m_state(StateVector::Zero()),
171  m_errorCovariance(StateSquareMatrix::Identity() *
172  10 ),
173  m_orientation(Eigen::Quaterniond::Identity()) {}
175  void setStateVector(StateVector const &state) { m_state = state; }
177  StateVector const &stateVector() const { return m_state; }
178  // set P
179  void setErrorCovariance(StateSquareMatrix const &errorCovariance) {
180  m_errorCovariance = errorCovariance;
181  }
184  return m_errorCovariance;
185  }
186  StateSquareMatrix &errorCovariance() { return m_errorCovariance; }
187 
189  void setQuaternion(Eigen::Quaterniond const &quaternion) {
190  m_orientation = quaternion.normalized();
191  }
192 
193  void postCorrect() { externalizeRotation(); }
194 
195  void externalizeRotation() {
197  incrementalOrientation() = Eigen::Vector3d::Zero();
198  }
199 
200  StateVectorBlock3 position() {
201  return pose_externalized_rotation::position(m_state);
202  }
203 
204  ConstStateVectorBlock3 position() const {
205  return pose_externalized_rotation::position(m_state);
206  }
207 
208  StateVectorBlock3 incrementalOrientation() {
209  return pose_externalized_rotation::incrementalOrientation(
210  m_state);
211  }
212 
213  ConstStateVectorBlock3 incrementalOrientation() const {
214  return pose_externalized_rotation::incrementalOrientation(
215  m_state);
216  }
217 
218  StateVectorBlock3 velocity() {
219  return pose_externalized_rotation::velocity(m_state);
220  }
221 
222  ConstStateVectorBlock3 velocity() const {
223  return pose_externalized_rotation::velocity(m_state);
224  }
225 
226  StateVectorBlock3 angularVelocity() {
227  return pose_externalized_rotation::angularVelocity(m_state);
228  }
229 
230  ConstStateVectorBlock3 angularVelocity() const {
231  return pose_externalized_rotation::angularVelocity(m_state);
232  }
233 
234  Eigen::Quaterniond const &getQuaternion() const {
235  return m_orientation;
236  }
237 
240  return incrementalOrientationToQuat(m_state).normalized() *
241  m_orientation;
242  }
243 
247  Eigen::Isometry3d ret;
248  ret.fromPositionOrientationScale(position(), getQuaternion(),
249  Eigen::Vector3d::Constant(1));
250  return ret;
251  }
252 
253  private:
257  StateVector m_state;
259  StateSquareMatrix m_errorCovariance;
261  Eigen::Quaterniond m_orientation;
262  };
263 
266  template <typename OutputStream>
267  inline OutputStream &operator<<(OutputStream &os, State const &state) {
268  os << "State:" << state.stateVector().transpose() << "\n";
269  os << "quat:" << state.getCombinedQuaternion().coeffs().transpose()
270  << "\n";
271  os << "error:\n" << state.errorCovariance() << "\n";
272  return os;
273  }
274  } // namespace pose_externalized_rotation
275 } // namespace kalman
276 } // namespace osvr
277 
278 #endif // INCLUDED_PoseState_h_GUID_57A246BA_940D_4386_ECA4_4C4172D97F5A
typename detail::Dimension_impl< T >::type Dimension
Given a state or measurement, get the dimension as a std::integral_constant.
Definition: FlexibleKalmanBase.h:87
const Coefficients & coeffs() const
Definition: Quaternion.h:93
Convenience base class for things (like states and measurements) that have a dimension.
Definition: FlexibleKalmanBase.h:63
void setQuaternion(Eigen::Quaterniond const &quaternion)
Intended for startup use.
Definition: PoseState.h:189
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: TestIMU_Common.h:87
Eigen::Isometry3d getIsometry() const
Get the position and quaternion combined into a single isometry (transformation)
Definition: PoseState.h:246
void separatelyDampenVelocities(StateVector &state, double posDamping, double oriDamping, double dt)
Separately dampen the linear and angular velocities.
Definition: PoseState.h:152
StateVector const & stateVector() const
xhat
Definition: PoseState.h:177
EIGEN_MAKE_ALIGNED_OPERATOR_NEW State()
Default constructor.
Definition: PoseState.h:169
StateVectorBlock6 velocities(StateVector &vec)
both translational and angular velocities
Definition: PoseState.h:83
StateSquareMatrix const & errorCovariance() const
P.
Definition: PoseState.h:183
double computeAttenuation(double damping, double dt)
Function used to compute the coefficient m in v_new = m * v_old.
Definition: PoseState.h:100
StateSquareMatrix stateTransitionMatrixWithSeparateVelocityDamping(double dt, double posDamping, double oriDamping)
Returns the state transition matrix for a constant velocity with separate damping paramters for linea...
Definition: PoseState.h:121
Definition: Quaternion.h:47
Quaternion< double > Quaterniond
double precision quaternion type
Definition: Quaternion.h:211
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Quaternion normalized() const
Definition: Quaternion.h:154
Eigen::Quaterniond getCombinedQuaternion() const
Definition: PoseState.h:238
void setStateVector(StateVector const &state)
set xhat
Definition: PoseState.h:175
Definition: Transform.h:43