orientation_tracker.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "orientation_tracker.h"
  17. #include "sensors/pose_prediction.h"
  18. #include "util/logging.h"
  19. #include "util/vector.h"
  20. #include "util/vectorutils.h"
  21. namespace cardboard {
  22. OrientationTracker::OrientationTracker(const long sampling_period_ns)
  23. : sampling_period_ns_(sampling_period_ns)
  24. , calibration_(Vector3::Zero())
  25. , is_tracking_(false)
  26. , sensor_fusion_(new SensorFusionEkf())
  27. , latest_gyroscope_data_({ 0, 0, Vector3::Zero() })
  28. {
  29. sensor_fusion_->SetBiasEstimationEnabled(/*kGyroBiasEstimationEnabled*/ true);
  30. }
  31. void OrientationTracker::SetCalibration(const Vector3& calibration) {
  32. calibration_ = calibration;
  33. }
  34. void OrientationTracker::Pause()
  35. {
  36. if (!is_tracking_) {
  37. return;
  38. }
  39. // Create a gyro event with zero velocity. This effectively stops the prediction.
  40. GyroscopeData event = latest_gyroscope_data_;
  41. event.data = Vector3::Zero();
  42. OnGyroscopeData(event);
  43. is_tracking_ = false;
  44. }
  45. void OrientationTracker::Resume() { is_tracking_ = true; }
  46. Vector4 OrientationTracker::GetPose(int64_t timestamp_ns) const
  47. {
  48. Rotation predicted_rotation;
  49. const PoseState pose_state = sensor_fusion_->GetLatestPoseState();
  50. if (sensor_fusion_->IsFullyInitialized()) {
  51. predicted_rotation = pose_state.sensor_from_start_rotation;
  52. } else {
  53. CARDBOARD_LOGI("Tracker not fully initialized yet. Using pose prediction only.");
  54. predicted_rotation = pose_prediction::PredictPose(timestamp_ns, pose_state);
  55. }
  56. return (-predicted_rotation).GetQuaternion();
  57. }
  58. void OrientationTracker::OnAccelerometerData(const AccelerometerData& event)
  59. {
  60. if (!is_tracking_) {
  61. return;
  62. }
  63. sensor_fusion_->ProcessAccelerometerSample(event);
  64. }
  65. Vector4 OrientationTracker::OnGyroscopeData(const GyroscopeData& event)
  66. {
  67. if (!is_tracking_) {
  68. return Vector4();
  69. }
  70. const GyroscopeData data = { .system_timestamp = event.system_timestamp,
  71. .sensor_timestamp_ns = event.sensor_timestamp_ns,
  72. .data = event.data - calibration_ };
  73. latest_gyroscope_data_ = data;
  74. sensor_fusion_->ProcessGyroscopeSample(data);
  75. return GetPose(data.sensor_timestamp_ns + sampling_period_ns_);
  76. }
  77. } // namespace cardboard