gyroscope_bias_estimator.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #ifndef CARDBOARD_SDK_SENSORS_GYROSCOPE_BIAS_ESTIMATOR_H_
  17. #define CARDBOARD_SDK_SENSORS_GYROSCOPE_BIAS_ESTIMATOR_H_
  18. #include <chrono> // NOLINT
  19. #include <cstdint>
  20. #include <list>
  21. #include <memory>
  22. #include <vector>
  23. #include "lowpass_filter.h"
  24. #include "mean_filter.h"
  25. #include "median_filter.h"
  26. #include "../util/vector.h"
  27. namespace cardboard {
  28. // Class that attempts to estimate the gyroscope's bias.
  29. // Its main idea is that it averages the gyroscope values when the phone is
  30. // considered stationary.
  31. // Usage: A client should call the ProcessGyroscope and ProcessAccelerometer
  32. // methods for every accelerometer and gyroscope sensor sample. This class
  33. // expects these calls to be frequent, i.e., at least at 10 Hz. The client can
  34. // then call GetGyroBias to retrieve the current estimate of the gyroscope bias.
  35. // For best results, the fastest available delay option should be used when
  36. // registering to sensors. Note that this class is not thread-safe.
  37. //
  38. // The filtering applied to the accelerometer to estimate a rotation
  39. // from it follows :
  40. // Baptiste Delporte, Laurent Perroton, Thierry Grandpierre, Jacques Trichet.
  41. // Accelerometer and Magnetometer Based Gyroscope Emulation on Smart Sensor
  42. // for a Virtual Reality Application. Sensor and Transducers Journal, 2012.
  43. //
  44. // which is a combination of a IIR filter, a median and a mean filter.
  45. class GyroscopeBiasEstimator {
  46. public:
  47. GyroscopeBiasEstimator();
  48. virtual ~GyroscopeBiasEstimator();
  49. // Updates the estimator with a gyroscope event.
  50. //
  51. // @param gyroscope_sample the angular speed around the x, y, z axis in
  52. // radians/sec.
  53. // @param timestamp_ns the nanosecond at which the event occurred. Only
  54. // guaranteed to be comparable with timestamps from other PocessGyroscope
  55. // invocations.
  56. virtual void ProcessGyroscope(const Vector3& gyroscope_sample, uint64_t timestamp_ns);
  57. // Processes accelerometer samples to estimate if device is
  58. // stable or not.
  59. //
  60. // First we filter the accelerometer. This is done with 3 filters.
  61. // - A IIR low-pass filter
  62. // - A median filter
  63. // - A mean filter.
  64. // Then a rotation is computed between consecutive filtered accelerometer
  65. // samples.
  66. // Finally this is converted to a velocity to emulate a gyroscope.
  67. //
  68. // @param accelerometer_sample the acceleration (including gravity) on the x,
  69. // y, z axis in meters/s^2.
  70. // @param timestamp_ns the nanosecond at which the event occurred. Only
  71. // guaranteed to be comparable with timestamps from other
  72. // ProcessAccelerometer invocations.
  73. virtual void ProcessAccelerometer(const Vector3& accelerometer_sample, uint64_t timestamp_ns);
  74. // Returns the estimated gyroscope bias.
  75. //
  76. // @return Estimated gyroscope bias. A vector with zeros is returned if no
  77. // estimate has been computed.
  78. virtual Vector3 GetGyroscopeBias() const;
  79. // Resets the estimator state.
  80. void Reset();
  81. // Returns true if the current estimate returned by GetGyroscopeBias is
  82. // correct. The device (measured using the sensors) has to be static for this
  83. // function to return true.
  84. virtual bool IsCurrentEstimateValid() const;
  85. private:
  86. // A helper class to keep track of whether some signal can be considered
  87. // static over specified number of frames.
  88. class IsStaticCounter;
  89. // Updates gyroscope bias estimation.
  90. //
  91. // @return false if the current sample is too large.
  92. bool UpdateGyroscopeBias(const Vector3& gyroscope_sample, uint64_t timestamp_ns);
  93. // Returns device angular velocity (rad/s) from the latest accelerometer data.
  94. //
  95. // @param timestep in seconds between the last two samples.
  96. // @return rotation velocity from latest accelerometer. This can be
  97. // interpreted as an gyroscope.
  98. Vector3 ComputeAngularVelocityFromLatestAccelerometer(double timestep) const;
  99. LowpassFilter accelerometer_lowpass_filter_;
  100. LowpassFilter simulated_gyroscope_from_accelerometer_lowpass_filter_;
  101. LowpassFilter gyroscope_lowpass_filter_;
  102. LowpassFilter gyroscope_bias_lowpass_filter_;
  103. std::unique_ptr<IsStaticCounter> accelerometer_static_counter_;
  104. std::unique_ptr<IsStaticCounter> gyroscope_static_counter_;
  105. // Sum of the weight of sample used for gyroscope filtering.
  106. float current_accumulated_weights_gyroscope_bias_;
  107. // Set of filters for accelerometer data to estimate a rotation
  108. // based only on accelerometer.
  109. MeanFilter mean_filter_;
  110. MedianFilter median_filter_;
  111. // Last computed filter accelerometer value used for finite differences.
  112. Vector3 last_mean_filtered_accelerometer_value_;
  113. };
  114. } // namespace cardboard
  115. #endif // CARDBOARD_SDK_SENSORS_GYROSCOPE_BIAS_ESTIMATOR_H_