1 /* 2 * Copyright (C) 2013 The Android Open Source Project 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 17 package com.android.cts.verifier.sensors; 18 19 import com.android.cts.verifier.R; 20 import com.android.cts.verifier.sensors.base.SensorCtsVerifierTestActivity; 21 22 import android.hardware.Sensor; 23 import android.hardware.SensorEvent; 24 import android.hardware.SensorManager; 25 import android.hardware.cts.helpers.SensorCalibratedUncalibratedVerifier; 26 import android.hardware.cts.helpers.SensorCtsHelper; 27 import android.hardware.cts.helpers.TestSensorEnvironment; 28 import android.hardware.cts.helpers.TestSensorEventListener; 29 import android.hardware.cts.helpers.TestSensorManager; 30 import android.hardware.cts.helpers.sensoroperations.TestSensorOperation; 31 import android.hardware.cts.helpers.sensorverification.MagnitudeVerification; 32 import android.hardware.cts.helpers.sensorverification.StandardDeviationVerification; 33 34 /** 35 * Semi-automated test that focuses characteristics associated with Accelerometer measurements. 36 * These test cases require calibration of the sensor before performing the verifications. 37 * Also, it is recommended to execute these tests outdoors, or at least far from magnetic 38 * disturbances. 39 */ 40 public class MagneticFieldMeasurementTestActivity extends SensorCtsVerifierTestActivity { 41 private static final float THRESHOLD_CALIBRATED_UNCALIBRATED_UT = 3f; 42 43 public MagneticFieldMeasurementTestActivity() { 44 super(MagneticFieldMeasurementTestActivity.class); 45 } 46 47 @Override 48 public void activitySetUp() throws InterruptedException { 49 calibrateMagnetometer(); 50 } 51 52 /** 53 * This test verifies that the Norm of the sensor data is close to the expected reference value. 54 * The units of the reference value are dependent on the type of sensor. 55 * This test is used to verify that the data reported by the sensor is close to the expected 56 * range and scale. 57 * 58 * The test takes a sample from the sensor under test and calculates the Euclidean Norm of the 59 * vector represented by the sampled data. It then compares it against the test expectations 60 * that are represented by a reference value and a threshold. 61 * 62 * The test is susceptible to errors when the Sensor under test is uncalibrated, or the units in 63 * which the data are reported and the expectations are set are different. 64 * 65 * The assertion associated with the test provides the required data needed to identify any 66 * possible issue. It provides: 67 * - the thread id on which the failure occurred 68 * - the sensor type and sensor handle that caused the failure 69 * - the values representing the expectation of the test 70 * - the values sampled from the sensor 71 */ 72 @SuppressWarnings("unused") 73 public String testNorm() throws Throwable { 74 getTestLogger().logMessage(R.string.snsr_mag_verify_norm); 75 76 TestSensorEnvironment environment = new TestSensorEnvironment( 77 getApplicationContext(), 78 Sensor.TYPE_MAGNETIC_FIELD, 79 SensorManager.SENSOR_DELAY_FASTEST); 80 TestSensorOperation verifyNorm = 81 TestSensorOperation.createOperation(environment, 100 /* event count */); 82 83 float expectedMagneticFieldEarth = 84 (SensorManager.MAGNETIC_FIELD_EARTH_MAX + SensorManager.MAGNETIC_FIELD_EARTH_MIN) / 2; 85 float magneticFieldEarthThreshold = 86 expectedMagneticFieldEarth - SensorManager.MAGNETIC_FIELD_EARTH_MIN; 87 verifyNorm.addVerification(new MagnitudeVerification( 88 expectedMagneticFieldEarth, 89 magneticFieldEarthThreshold)); 90 verifyNorm.execute(getCurrentTestNode()); 91 return null; 92 } 93 94 /** 95 * This test verifies that the standard deviation of a set of sampled data from a particular 96 * sensor falls into the expectations defined in the CDD. The verification applies to each axis 97 * of the sampled data reported by the Sensor under test. 98 * This test is used to validate the requirement imposed by the CDD to Sensors in Android. And 99 * characterizes how the Sensor behaves while static. 100 * 101 * The test takes a set of samples from the sensor under test, and calculates the Standard 102 * Deviation for each of the axes the Sensor reports data for. The StdDev is compared against 103 * the expected value documented in the CDD. 104 * 105 * The test is susceptible to errors if the device is moving while the test is running, or if 106 * the Sensor's sampled data indeed falls into a large StdDev. 107 * 108 * The assertion associated with the test provides the required data to identify any possible 109 * issue. It provides: 110 * - the thread id on which the failure occurred 111 * - the sensor type and sensor handle that caused the failure 112 * - the expectation of the test 113 * - the std dev calculated and the axis it applies to 114 * Additionally, the device's debug output (adb logcat) dumps the set of values associated with 115 * the failure to help track down the issue. 116 */ 117 @SuppressWarnings("unused") 118 public String testStandardDeviation() throws Throwable { 119 getTestLogger().logMessage(R.string.snsr_mag_verify_std_dev); 120 121 TestSensorEnvironment environment = new TestSensorEnvironment( 122 getApplicationContext(), 123 Sensor.TYPE_MAGNETIC_FIELD, 124 SensorManager.SENSOR_DELAY_FASTEST); 125 TestSensorOperation verifyStdDev = 126 TestSensorOperation.createOperation(environment, 100 /* event count */); 127 128 verifyStdDev.addVerification(new StandardDeviationVerification( 129 new float[]{2f, 2f, 2f} /* uT */)); 130 verifyStdDev.execute(getCurrentTestNode()); 131 return null; 132 } 133 134 /** 135 * Verifies that the relationship between readings from calibrated and their corresponding 136 * uncalibrated sensors comply to the following equation: 137 * calibrated = uncalibrated - bias 138 */ 139 @SuppressWarnings("unused") 140 public String testCalibratedAndUncalibrated() throws Throwable { 141 getTestLogger().logMessage(R.string.snsr_mag_verify_calibrated_uncalibrated); 142 143 TestSensorEnvironment calibratedEnvironment = new TestSensorEnvironment( 144 getApplicationContext(), 145 Sensor.TYPE_MAGNETIC_FIELD, 146 SensorManager.SENSOR_DELAY_FASTEST); 147 TestSensorEnvironment uncalibratedEnvironment = new TestSensorEnvironment( 148 getApplicationContext(), 149 Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED, 150 SensorManager.SENSOR_DELAY_FASTEST); 151 SensorCalibratedUncalibratedVerifier verifier = new SensorCalibratedUncalibratedVerifier( 152 calibratedEnvironment, 153 uncalibratedEnvironment, 154 THRESHOLD_CALIBRATED_UNCALIBRATED_UT); 155 156 try { 157 verifier.execute(); 158 } finally { 159 playSound(); 160 } 161 return null; 162 } 163 164 /** 165 * A routine to help operators calibrate the magnetometer. 166 */ 167 private void calibrateMagnetometer() throws InterruptedException { 168 TestSensorEnvironment environment = new TestSensorEnvironment( 169 getApplicationContext(), 170 Sensor.TYPE_MAGNETIC_FIELD, 171 SensorManager.SENSOR_DELAY_NORMAL); 172 173 SensorTestLogger logger = getTestLogger(); 174 logger.logInstructions(R.string.snsr_mag_calibration_description); 175 logger.logInstructions(R.string.snsr_mag_calibration_complete); 176 waitForUserToContinue(); 177 178 TestSensorEventListener listener = new TestSensorEventListener(environment) { 179 @Override 180 public void onSensorChanged(SensorEvent event) { 181 clearText(); 182 183 float values[] = event.values; 184 logger.logMessage( 185 R.string.snsr_mag_measurement, 186 values[0], 187 values[1], 188 values[2], 189 SensorCtsHelper.getMagnitude(values)); 190 } 191 }; 192 193 TestSensorManager magnetometer = new TestSensorManager(environment); 194 try { 195 magnetometer.registerListener(listener); 196 waitForUserToContinue(); 197 } finally { 198 magnetometer.unregisterListener(); 199 } 200 } 201 } 202