Understanding Sensor Fusion
Sensor fusion is the process of combining data from multiple physical sensors to create a new, virtual sensor that provides more accurate, reliable, or complete information than any single sensor could provide alone. In Android, this is crucial for determining device orientation, tracking motion, and enabling augmented reality features.
Composite Sensors
Android defines several composite sensors that rely on fusion algorithms. These are exposed to developers just like physical sensors, but their data is computed either in software (SensorService) or firmware (Sensor Hub).
Key Composite Types
- Rotation Vector: Represents the orientation of the device as a combination of an angle and an axis. It fuses data from the accelerometer, gyroscope, and magnetometer to provide an absolute orientation relative to the Earth's magnetic north.
- Gravity Sensor: Isolates the direction and magnitude of Earth's gravity. It uses the accelerometer and gyroscope to separate the gravity vector from the user's linear acceleration.
- Linear Acceleration Sensor: Measures the acceleration applied to the device, excluding gravity. This is derived by subtracting the gravity vector from the raw accelerometer readings.
// Registering a listener for the Rotation Vector sensor
SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor rotationVectorSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
sensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
// ... apply rotation matrix to 3D models ...
}
// ...
}, rotationVectorSensor, SensorManager.SENSOR_DELAY_GAME);
Sensor Fusion Algorithms
The mathematics behind sensor fusion typically involve advanced filtering techniques designed to mitigate the weaknesses of individual sensors.
The Kalman Filter Approach
While simple complementary filters can be used, high-quality sensor fusion often employs an Extended Kalman Filter (EKF).
- Accelerometer: Provides absolute reference for gravity but is noisy during movement.
- Magnetometer: Provides absolute reference for North but is highly susceptible to magnetic interference (hard and soft iron effects).
- Gyroscope: Provides extremely fast and smooth rotational data but suffers from drift over time.
The EKF uses the fast gyroscope data to update the orientation state rapidly, while periodically using the slower, noisier accelerometer and magnetometer data to correct the gyroscope's drift.
Calibration Procedures
For fusion to be accurate, the underlying sensors must be properly calibrated.
Magnetometer Calibration
Magnetometers are easily confused by the device's internal components (batteries, speakers) and external environmental factors.
- Hard Iron Calibration: Corrects for permanent magnetic fields originating from the device itself, which cause a constant offset in the readings.
- Soft Iron Calibration: Corrects for distortions caused by magnetically susceptible materials near the sensor, which stretch or skew the magnetic field readings.
Android devices often perform continuous background calibration. When the user moves the device in a figure-eight pattern, the system collects a sphere of data points to calculate calibration offsets.
Gyroscope Calibration
Gyroscopes exhibit a "zero-rate offset" (bias). Even when the device is perfectly still, the gyroscope might report a slight rotation. Calibration involves measuring this bias while the device is stationary and subtracting it from future readings.
// Example: Applying gyroscope bias correction in HAL
void applyGyroCalibration(float* uncalibratedData, const float* bias, float* calibratedData) {
calibratedData[0] = uncalibratedData[0] - bias[0];
calibratedData[1] = uncalibratedData[1] - bias[1];
calibratedData[2] = uncalibratedData[2] - bias[2];
}
Gaming vs. Geomagnetic Rotation Vector
Android provides variations of the rotation vector tailored for specific use cases.
Gaming Rotation Vector (TYPE_GAME_ROTATION_VECTOR)
This sensor fuses only the accelerometer and gyroscope. It explicitly ignores the magnetometer.
- Advantage: Completely immune to magnetic interference (e.g., playing a game near a strong speaker magnet).
- Disadvantage: The Z-axis (yaw) will slowly drift over time because there is no absolute reference to North. It provides relative orientation.
Geomagnetic Rotation Vector (TYPE_GEOMAGNETIC_ROTATION_VECTOR)
This sensor fuses only the accelerometer and magnetometer. It ignores the gyroscope.
- Advantage: Consumes significantly less power than the standard rotation vector because the gyroscope is the most power-hungry sensor.
- Disadvantage: Much noisier and less responsive to rapid movements. Best suited for low-power background tasks like a compass application or basic step counting orientation.
By understanding these nuances, developers can select the appropriate fusion model to balance power consumption, responsiveness, and accuracy.