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