The complementary filter idea is to combine slow-moving signals from an accelerometer with fast-moving signals from a gyroscope. The accelerometer provides a good indicator of orientation in static conditions, while the gyroscope provides a good indicator of tilt in dynamic conditions. Thus, the idea is to pass the accelerometer signals through a low-pass filter and the gyroscope signals through a high-pass filter, and then combine them to give the final rate.

See Github here.

References

  1. https://www.w3.org/TR/motion-sensors/#complementary-filters
  2. Android's getRotationMatrix function: https://developer.android.com/reference/android/hardware/SensorManager#getRotationMatrixhttps://developer.android.com/reference/android/hardware/SensorManager#getRotationMatrix(float[], float[], float[], float[])
  3. Android's getOrientation function: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/hardware/SensorManager.java#1478