• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 android.hardware.cts.helpers;
18 
19 import junit.framework.Assert;
20 
21 import java.util.List;
22 import java.util.concurrent.TimeUnit;
23 
24 /**
25  * A bundled sensor test operation/verification.
26  *
27  * It verifies the relationship between measurements from calibrated sensors and their corresponding
28  * uncalibrated sensors comply to the following equation:
29  *  calibrated = uncalibrated - bias
30  */
31 // TODO: refactor into proper test operation/verification classes:
32 // currently this is the only verification that requires input from multiple sensors, and the class
33 // is factored into: operation, verification, and listener as other sensor test operations
34 public class SensorCalibratedUncalibratedVerifier {
35 
36     private final TestSensorManager mCalibratedSensorManager;
37     private final TestSensorManager mUncalibratedSensorManager;
38     private final float mThreshold;
39 
SensorCalibratedUncalibratedVerifier( TestSensorEnvironment calibratedEnvironment, TestSensorEnvironment uncalibratedEnvironment, float threshold)40     public SensorCalibratedUncalibratedVerifier(
41             TestSensorEnvironment calibratedEnvironment,
42             TestSensorEnvironment uncalibratedEnvironment,
43             float threshold) {
44         mCalibratedSensorManager = new TestSensorManager(calibratedEnvironment);
45         mUncalibratedSensorManager = new TestSensorManager(uncalibratedEnvironment);
46         mThreshold = threshold;
47     }
48 
49     /**
50      * Executes the operation: it collects the data and run verifications on it.
51      */
execute()52     public void execute() throws Throwable {
53         CollectingSensorEventListener calibratedTestListener = new CollectingSensorEventListener();
54         CollectingSensorEventListener uncalibratedTestListener = new CollectingSensorEventListener();
55         mCalibratedSensorManager.registerListener(calibratedTestListener);
56         mUncalibratedSensorManager.registerListener(uncalibratedTestListener);
57 
58         Thread.sleep(TimeUnit.SECONDS.toMillis(10));
59 
60         mCalibratedSensorManager.unregisterListener();
61         mUncalibratedSensorManager.unregisterListener();
62 
63         verifyMeasurements(
64                 calibratedTestListener.getEvents(),
65                 uncalibratedTestListener.getEvents(),
66                 mThreshold);
67     }
68 
verifyMeasurements( List<TestSensorEvent> calibratedEvents, List<TestSensorEvent> uncalibratedEvents, float threshold)69     private void verifyMeasurements(
70             List<TestSensorEvent> calibratedEvents,
71             List<TestSensorEvent> uncalibratedEvents,
72             float threshold) {
73         long measuredSamplingPeriodNs = SensorCtsHelper.getSamplingPeriodNs(calibratedEvents);
74         long synchronizationPeriodNs = measuredSamplingPeriodNs / 2;
75         int eventsValidated = 0;
76 
77         // TODO: this makes the algorithm O(n^2) when we could have it O(n), but it has little
78         // impact on the overall test duration because the data collection is what takes the most
79         // time
80         for (TestSensorEvent calibratedEvent : calibratedEvents) {
81             long calibratedTimestampNs = calibratedEvent.timestamp;
82             long lowerTimestampThresholdNs = calibratedTimestampNs - synchronizationPeriodNs;
83             long upperTimestampThresholdNs = calibratedTimestampNs + synchronizationPeriodNs;
84 
85             for (TestSensorEvent uncalibratedEvent : uncalibratedEvents) {
86                 long uncalibratedTimestampNs = uncalibratedEvent.timestamp;
87                 if (uncalibratedTimestampNs > lowerTimestampThresholdNs
88                         && uncalibratedTimestampNs < upperTimestampThresholdNs) {
89                     // perform validation
90                     verifyCalibratedUncalibratedPair(
91                             calibratedEvent,
92                             uncalibratedEvent,
93                             threshold);
94                     ++eventsValidated;
95                 }
96             }
97         }
98 
99         String eventsValidatedMessage = String.format(
100                 "Expected to find at least one Calibrated/Uncalibrated event pair for validation."
101                         + " Found=%d",
102                 eventsValidated);
103         Assert.assertTrue(eventsValidatedMessage, eventsValidated > 0);
104     }
105 
verifyCalibratedUncalibratedPair( TestSensorEvent calibratedEvent, TestSensorEvent uncalibratedEvent, float threshold)106     private void verifyCalibratedUncalibratedPair(
107             TestSensorEvent calibratedEvent,
108             TestSensorEvent uncalibratedEvent,
109             float threshold) {
110         for (int i = 0; i < 3; ++i) {
111             float calibrated = calibratedEvent.values[i];
112             float uncalibrated = uncalibratedEvent.values[i];
113             float bias = uncalibratedEvent.values[i + 3];
114             String message = String.format(
115                     "Calibrated (%s) and Uncalibrated (%s) sensor readings are expected to satisfy:"
116                             + " calibrated = uncalibrated - bias. Axis=%d, Calibrated=%s, "
117                             + "Uncalibrated=%s, Bias=%s, Threshold=%s",
118                     calibratedEvent.sensor.getName(),
119                     uncalibratedEvent.sensor.getName(),
120                     i,
121                     calibrated,
122                     uncalibrated,
123                     bias,
124                     threshold);
125             Assert.assertEquals(message, calibrated, uncalibrated - bias, threshold);
126         }
127     }
128 }
129