V. SENSOR FUSION
The application’s sensor fusion relies on quaternion linear interpolation to combine the gyroscope, accelerometer and magnetometer data. Gyroscopes suffer from drift but produce noise-free data at high frequencies. Accelerometers and mag- netometers can be combined to produce a rough approximation of rotation. The resulting signal contains no noticeable drift but oscillates in excess of one degree per frame of motion. The three sensors can be fused so that the accelerometer and magnetometer are used by the gyroscope as a reference point to cancel drift. The entire motion processing can be divided in three separate stages: data preparation, drift cancellation and post-processing.
A. Data Preparation
The first step of the preparation stage is to combine the accelerometer and magnetometer readings using the Android native API. The result is a quaternion representing a rough approximation of the device’s orientation. Its rotation contains noise that can be attenuated by applying a low-pass filter. A set of transformations is applied so that the data matches the
3D engine’s orthogonal configuration.
The second step is to gather the gyroscope data and compute its world space rotation. The gyroscope outputs three angular speeds corresponding to azimuth, pitch and roll. Angular speeds are converted into rotation increments when multiplied by the time step. The time step is the time interval between two consecutive samplings. A summation of the rotation increments produces a world space rotation. The two rotations are converted from Euler to quaternions to avoid gimbal lock and for performance reasons (quaternion multiplications are much faster to compute than trigonometry).
B. Drift Cancellation
In this context, drift is the difference between the gyroscope’s rotation and the combined accelerome- ter/magnetometer rotation. The two streams of data are compared to determine a drift coefficient that is represented by a percentage ratio. Controlled drift is induced so that the gyroscope rotation gradually matches the accelerometer/magnetometer rotation. This process is achieved using quaternion linear interpolation and the drift coefficient. The fused rotation will replace the gyroscope’s accumulated rotation before the next frame of motion is computed. The result contains some unwanted noise that can be masked using filters. A popular alternative to this solution is the Kalman filter [14].
C. Post-Processing
Once drift cancellation is complete, the data is subject to cleaning and smoothing. Occasionally, some of the quaternions are miscalculated due to sensor or software inaccuracies. The cleaning filter traverses the recording to compare every frame of motion against its predecessors. A frame is deemed inaccurate if it exceeds the filter’s threshold. All inaccuracies are discarded and substituted for new interpolated values. The cleaning is followed by smoothing where a Gaussian mask is applied to every frame of motion to filter out any remaining noise. The user can adjust or disable the amount of smoothing depending on the task in hand. For example, a fluent motion may be desired for an animation while raw data may be preferred for scientific studies.