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 import com.android.cts.verifier.sensors.renderers.GLRotationGuideRenderer; 22 23 import android.hardware.Sensor; 24 import android.hardware.SensorManager; 25 import android.hardware.cts.helpers.SensorCalibratedUncalibratedVerifier; 26 import android.hardware.cts.helpers.TestSensorEnvironment; 27 import android.hardware.cts.helpers.sensoroperations.TestSensorOperation; 28 import android.hardware.cts.helpers.sensorverification.GyroscopeIntegrationVerification; 29 30 import java.util.concurrent.TimeUnit; 31 32 /** 33 * Semi-automated test that focuses on characteristics associated with Gyroscope measurements. 34 */ 35 public class GyroscopeMeasurementTestActivity extends SensorCtsVerifierTestActivity { 36 private static final float THRESHOLD_CALIBRATED_UNCALIBRATED_RAD_SEC = 0.01f; 37 private static final float THRESHOLD_AXIS_UNDER_ROTATION_DEG = 10.0f; 38 private static final float THRESHOLD_AXIS_UNDER_NO_ROTATION_DEG = 50.0f; 39 40 private static final int ROTATE_360_DEG = 360; 41 private static final int ROTATION_COLLECTION_SEC = 10; 42 43 private static final int X_AXIS = 0; 44 private static final int Y_AXIS = 1; 45 private static final int Z_AXIS = 2; 46 47 private final GLRotationGuideRenderer mRenderer = new GLRotationGuideRenderer(); 48 49 public GyroscopeMeasurementTestActivity() { 50 super(GyroscopeMeasurementTestActivity.class); 51 mEnableRetry = true; 52 } 53 54 @Override 55 protected void activitySetUp() throws InterruptedException { 56 getTestLogger().logInstructions(R.string.snsr_gyro_device_placement); 57 waitForUserToContinue(); 58 initializeGlSurfaceView(mRenderer); 59 } 60 61 @Override 62 protected void activityCleanUp() { 63 closeGlSurfaceView(); 64 } 65 66 @SuppressWarnings("unused") 67 public String testDeviceStatic() throws Throwable { 68 return verifyMeasurements( 69 R.string.snsr_gyro_device_static, 70 -1 /* rotationAxis */, 71 0 /* expectationDeg */); 72 } 73 74 @SuppressWarnings("unused") 75 public String testRotateClockwise() throws Throwable { 76 return verifyMeasurements(R.string.snsr_gyro_rotate_device, Z_AXIS, -ROTATE_360_DEG); 77 } 78 79 @SuppressWarnings("unused") 80 public String testRotateCounterClockwise() throws Throwable { 81 return verifyMeasurements(R.string.snsr_gyro_rotate_device, Z_AXIS, ROTATE_360_DEG); 82 } 83 84 @SuppressWarnings("unused") 85 public String testRotateRightSide() throws Throwable { 86 return verifyMeasurements(R.string.snsr_gyro_rotate_device, Y_AXIS, ROTATE_360_DEG); 87 } 88 89 @SuppressWarnings("unused") 90 public String testRotateLeftSide() throws Throwable { 91 return verifyMeasurements(R.string.snsr_gyro_rotate_device, Y_AXIS, -ROTATE_360_DEG); 92 } 93 94 @SuppressWarnings("unused") 95 public String testRotateTopSide() throws Throwable { 96 return verifyMeasurements(R.string.snsr_gyro_rotate_device, X_AXIS, -ROTATE_360_DEG); 97 } 98 99 @SuppressWarnings("unused") 100 public String testRotateBottomSide() throws Throwable { 101 return verifyMeasurements(R.string.snsr_gyro_rotate_device, X_AXIS, ROTATE_360_DEG); 102 } 103 104 /** 105 * Verifies that the relationship between readings from calibrated and their corresponding 106 * uncalibrated sensors comply to the following equation: 107 * calibrated = uncalibrated - bias 108 */ 109 @SuppressWarnings("unused") 110 public String testCalibratedAndUncalibrated() throws Throwable { 111 setRendererRotation(Z_AXIS, false); 112 113 SensorTestLogger logger = getTestLogger(); 114 if (!mShouldRetry) { 115 logger.logInstructions(R.string.snsr_keep_device_rotating_clockwise); 116 waitForUserToBegin(); 117 } 118 logger.logWaitForSound(); 119 120 TestSensorEnvironment calibratedEnvironment = new TestSensorEnvironment( 121 getApplicationContext(), 122 Sensor.TYPE_GYROSCOPE, 123 SensorManager.SENSOR_DELAY_FASTEST); 124 TestSensorEnvironment uncalibratedEnvironment = new TestSensorEnvironment( 125 getApplicationContext(), 126 Sensor.TYPE_GYROSCOPE_UNCALIBRATED, 127 SensorManager.SENSOR_DELAY_FASTEST); 128 SensorCalibratedUncalibratedVerifier verifier = new SensorCalibratedUncalibratedVerifier( 129 calibratedEnvironment, 130 uncalibratedEnvironment, 131 THRESHOLD_CALIBRATED_UNCALIBRATED_RAD_SEC); 132 133 try { 134 verifier.execute(); 135 } finally { 136 playSound(); 137 } 138 return null; 139 } 140 141 /** 142 * This test verifies that the Gyroscope measures the appropriate angular position. 143 * 144 * The test takes a set of samples from the sensor under test and calculates the angular 145 * position for each axis that the sensor data collects. It then compares it against the test 146 * expectations that are represented by signed values. It verifies that the readings have the 147 * right magnitude. 148 */ 149 private String verifyMeasurements(int instructionsResId, int rotationAxis, int expectationDeg) 150 throws Throwable { 151 setRendererRotation(rotationAxis, expectationDeg >= 0); 152 153 SensorTestLogger logger = getTestLogger(); 154 if (!mShouldRetry) { 155 logger.logInstructions(instructionsResId); 156 waitForUserToBegin(); 157 } 158 logger.logWaitForSound(); 159 160 TestSensorEnvironment environment = new TestSensorEnvironment( 161 getApplicationContext(), 162 Sensor.TYPE_GYROSCOPE, 163 SensorManager.SENSOR_DELAY_FASTEST); 164 TestSensorOperation sensorOperation = TestSensorOperation 165 .createOperation(environment, ROTATION_COLLECTION_SEC, TimeUnit.SECONDS); 166 167 int gyroscopeAxes = environment.getSensorAxesCount(); 168 int[] expectationsDeg = getExpectationsDeg(gyroscopeAxes, rotationAxis, expectationDeg); 169 float[] thresholdsDeg = getThresholdsDeg(gyroscopeAxes, rotationAxis); 170 GyroscopeIntegrationVerification integrationVerification = 171 new GyroscopeIntegrationVerification(expectationsDeg, thresholdsDeg); 172 sensorOperation.addVerification(integrationVerification); 173 174 try { 175 sensorOperation.execute(getCurrentTestNode()); 176 } finally { 177 playSound(); 178 } 179 return null; 180 } 181 182 private int[] getExpectationsDeg(int axes, int rotationAxis, int expectationDeg) { 183 int[] expectationsDeg = new int[axes]; 184 for (int i = 0; i < axes; ++i) { 185 // tests assume that rotation is expected on one axis at a time 186 expectationsDeg[i] = (i == rotationAxis) ? expectationDeg : 0; 187 } 188 return expectationsDeg; 189 } 190 191 private float[] getThresholdsDeg(int axes, int rotationAxis) { 192 float[] thresholdsDeg = new float[axes]; 193 for (int i = 0; i < axes; ++i) { 194 // tests set a high threshold on the axes where rotation is not expected, to account 195 // for movement from the operator 196 // the rotation axis has a lower threshold to ensure the gyroscope's accuracy 197 thresholdsDeg[i] = (i == rotationAxis) 198 ? THRESHOLD_AXIS_UNDER_ROTATION_DEG 199 : THRESHOLD_AXIS_UNDER_NO_ROTATION_DEG; 200 } 201 return thresholdsDeg; 202 } 203 204 private void setRendererRotation(int rotationAxis, boolean positiveRotation) { 205 int axis1 = 0; 206 int axis2 = 0; 207 int axis3 = 0; 208 switch (rotationAxis) { 209 case X_AXIS: 210 axis1 = positiveRotation ? 1 : -1; 211 break; 212 case Y_AXIS: 213 axis2 = positiveRotation ? 1 : -1; 214 break; 215 case Z_AXIS: 216 axis3 = positiveRotation ? 1 : -1; 217 break; 218 } 219 mRenderer.setRotation(axis1, axis2, axis3); 220 } 221 } 222