pose_prediction.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2019 Google Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "pose_prediction.h"
  17. #include <chrono> // NOLINT
  18. #include "../util/logging.h"
  19. #include "../util/vectorutils.h"
  20. namespace cardboard {
  21. namespace {
  22. const double kEpsilon = 1.0e-15;
  23. } // namespace
  24. namespace pose_prediction {
  25. Rotation GetRotationFromGyroscope(const Vector3& gyroscope_value, double timestep_s)
  26. {
  27. const double velocity = Length(gyroscope_value);
  28. // When there is no rotation data return an identity rotation.
  29. if (velocity < kEpsilon) {
  30. CARDBOARD_LOGI("PosePrediction::GetRotationFromGyroscope: Velocity really small, "
  31. "returning identity rotation.");
  32. return Rotation::Identity();
  33. }
  34. // Since the gyroscope_value is a start from sensor transformation we need to
  35. // invert it to have a sensor from start transformation, hence the minus sign.
  36. // For more info:
  37. // http://developer.android.com/guide/topics/sensors/sensors_motion.html#sensors-motion-gyro
  38. return Rotation::FromAxisAndAngle(gyroscope_value / velocity, -timestep_s * velocity);
  39. }
  40. Rotation PredictPose(int64_t requested_pose_timestamp, const PoseState& current_state)
  41. {
  42. // Subtracting unsigned numbers is bad when the result is negative.
  43. const int64_t diff = requested_pose_timestamp - current_state.timestamp;
  44. const double timestep_s = diff * 1.0e-9;
  45. const Rotation update = GetRotationFromGyroscope(
  46. current_state.sensor_from_start_rotation_velocity, timestep_s);
  47. return update * current_state.sensor_from_start_rotation;
  48. }
  49. Rotation PredictPoseInv(int64_t requested_pose_timestamp, const PoseState& current_state)
  50. {
  51. // Subtracting unsigned numbers is bad when the result is negative.
  52. const int64_t diff = requested_pose_timestamp - current_state.timestamp;
  53. const double timestep_s = diff * 1.0e-9;
  54. const Rotation update = GetRotationFromGyroscope(
  55. current_state.sensor_from_start_rotation_velocity, timestep_s);
  56. return current_state.sensor_from_start_rotation * (-update);
  57. }
  58. } // namespace pose_prediction
  59. } // namespace cardboard