OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ApplyIMUToState.cpp
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 // Internal Includes
26 #include "ApplyIMUToState.h"
27 #include "AngVelTools.h"
28 #include "CrossProductMatrix.h"
30 #include "IMUStateMeasurements.h"
31 #include "SpaceTransformations.h"
32 
33 // Library/third-party includes
37 #include <osvr/Util/EigenExtras.h>
39 #include <util/Stride.h>
40 
41 // Standard includes
42 #include <iostream>
43 
44 namespace osvr {
45 namespace vbtracker {
46  inline void applyOriToState(TrackingSystem const &sys, BodyState &state,
47  BodyProcessModel &processModel,
48  CannedIMUMeasurement const &meas) {
49  Eigen::Quaterniond quat;
50  meas.restoreQuat(quat);
51  Eigen::Vector3d var;
52  meas.restoreQuatVariance(var);
53  util::Angle yawCorrection = meas.getYawCorrection();
54 
55  Eigen::Quaterniond quatInCamSpace = getTransformedOrientation(
56  quat, Eigen::Quaterniond(sys.getRoomToCamera().rotation()),
57  yawCorrection);
58  OrientationMeasurement kalmanMeas{quatInCamSpace, var};
59 #if 0
60  auto correctionInProgress =
61  kalman::beginCorrection(state, processModel, kalmanMeas);
62 #else
63  auto correctionInProgress =
64  kalman::beginUnscentedCorrection(state, kalmanMeas);
65 #endif
66  auto outputMeas = [&] {
67  std::cout << "state: " << state.getQuaternion().coeffs().transpose()
68  << " and measurement: "
69 #if 0
70  << quat.coeffs().transpose();
71 #else
72  << quatInCamSpace.coeffs().transpose();
73 #endif
74  };
75  if (!correctionInProgress.stateCorrectionFinite) {
76  std::cout
77  << "Non-finite state correction in applying orientation: ";
78  outputMeas();
79  std::cout << "\n";
80  return;
81  }
82 #if 0
83  static ::util::Stride s(401);
84  if (++s) {
85 
86  std::cout << "delta z\t "
87  << correctionInProgress.deltaz.transpose();
88  std::cout << "\t state correction "
89  << correctionInProgress.stateCorrection.transpose()
90  << "\n";
91  }
92 #endif
93  if (!correctionInProgress.finishCorrection(true)) {
94  std::cout
95  << "Non-finite error covariance after applying orientation: ";
96  outputMeas();
97  std::cout << "\n";
98  }
99  }
100 
101  inline void applyAngVelToState(TrackingSystem const &sys, BodyState &state,
102  BodyProcessModel &processModel,
103  CannedIMUMeasurement const &meas) {
104 
105  Eigen::Vector3d angVel;
106  meas.restoreAngVel(angVel);
107  Eigen::Vector3d var;
108  meas.restoreAngVelVariance(var);
109 #if 0
110  static const double dt = 0.02;
113  Eigen::Quaterniond cTb = state.getQuaternion();
114  // Eigen::Matrix3d bTc(state.getQuaternion().conjugate());
116  Eigen::Quaterniond incrementalQuat =
117  cTb * angVelVecToIncRot(angVel, dt) * cTb.conjugate();
119  angVel = incRotToAngVelVec(incrementalQuat, dt);
120  // angVel = (getRotationMatrixToCameraSpace(sys) * angVel).eval();
122 
123  kalman::AngularVelocityMeasurement<BodyState> kalmanMeas{angVel, var};
124  kalman::correct(state, processModel, kalmanMeas);
125 #else
126  kalman::IMUAngVelMeasurement kalmanMeas{angVel, var};
127  auto correctionInProgress =
128  kalman::beginUnscentedCorrection(state, kalmanMeas);
129  auto outputMeas = [&] {
130  std::cout << "state: " << state.angularVelocity().transpose()
131  << " and measurement: " << angVel.transpose();
132  };
133  if (!correctionInProgress.stateCorrectionFinite) {
134  std::cout
135  << "Non-finite state correction in applying angular velocity: ";
136  outputMeas();
137  std::cout << "\n";
138  return;
139  }
140  if (!correctionInProgress.finishCorrection(true)) {
141  std::cout << "Non-finite error covariance after applying angular "
142  "velocity: ";
143  outputMeas();
144  std::cout << "\n";
145  }
146 #endif
147  }
148 
149  void applyIMUToState(TrackingSystem const &sys,
150  util::time::TimeValue const &initialTime,
151  BodyState &state, BodyProcessModel &processModel,
152  util::time::TimeValue const &newTime,
153  CannedIMUMeasurement const &meas) {
154  if (newTime != initialTime) {
155  auto dt = osvrTimeValueDurationSeconds(&newTime, &initialTime);
156  kalman::predict(state, processModel, dt);
157 #if 0
158  state.externalizeRotation();
159 #endif
160  }
161  if (meas.orientationValid()) {
162  applyOriToState(sys, state, processModel, meas);
163  } else if (meas.angVelValid()) {
164  applyAngVelToState(sys, state, processModel, meas);
165  } else {
166  // unusually, the measurement is totally invalid. Just normalize and
167  // go on.
168  state.postCorrect();
169  }
170  }
171 } // namespace vbtracker
172 } // namespace osvr
AngleRadiansd Angle
Default angle type.
Definition: Angles.h:63
Header for a flexible Unscented-style Kalman filter correction from a measurement.
OSVR_EXTERN_C_END double osvrTimeValueDurationSeconds(const OSVR_TimeValue *tvA, const OSVR_TimeValue *tvB)
Compute the difference between the two time values, returning the duration as a double-precision floa...
Definition: TimeValueC.h:185
::OSVR_TimeValue TimeValue
C++-friendly typedef for the OSVR_TimeValue structure.
Definition: TimeValue.h:48
CorrectionInProgress< StateType, MeasurementType > beginCorrection(StateType &state, ProcessModelType &processModel, MeasurementType &meas)
bool correct(StateType &state, ProcessModelType &processModel, MeasurementType &meas, bool cancelIfNotFinite=true)
Header for measurements of absolute orientation.