1# Copyright 2014 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Verify if the gyro has stable output when device is stationary.""" 15 16import logging 17import os 18import time 19 20import matplotlib 21from matplotlib import pylab 22from mobly import test_runner 23import numpy 24 25import its_base_test 26import camera_properties_utils 27import its_session_utils 28 29NAME = os.path.basename(__file__).split('.')[0] 30N = 20 # Number of samples averaged together, in the plot. 31NSEC_TO_SEC = 1E-9 32MEAN_THRESH = 0.01 # PASS/FAIL threshold for gyro mean drift 33VAR_THRESH = 0.001 # PASS/FAIL threshold for gyro variance drift 34 35 36class GyroBiasTest(its_base_test.ItsBaseTest): 37 """Test if the gyro has stable output when device is stationary. 38 """ 39 40 def test_gyro_bias(self): 41 with its_session_utils.ItsSession( 42 device_id=self.dut.serial, 43 camera_id=self.camera_id, 44 hidden_physical_id=self.hidden_physical_id) as cam: 45 props = cam.get_camera_properties() 46 props = cam.override_with_hidden_physical_camera_props(props) 47 # Only run test if the appropriate caps are claimed. 48 camera_properties_utils.skip_unless( 49 camera_properties_utils.sensor_fusion(props) and 50 cam.get_sensors().get('gyro')) 51 52 logging.debug('Collecting gyro events') 53 cam.start_sensor_events() 54 time.sleep(5) 55 gyro_events = cam.get_sensor_events()['gyro'] 56 57 nevents = (len(gyro_events) // N) * N 58 gyro_events = gyro_events[:nevents] 59 times = numpy.array([(e['time'] - gyro_events[0]['time'])*NSEC_TO_SEC 60 for e in gyro_events]) 61 xs = numpy.array([e['x'] for e in gyro_events]) 62 ys = numpy.array([e['y'] for e in gyro_events]) 63 zs = numpy.array([e['z'] for e in gyro_events]) 64 65 # Group samples into size-N groups and average each together, to get rid 66 # of individual random spikes in the data. 67 times = times[N // 2::N] 68 xs = xs.reshape(nevents // N, N).mean(1) 69 ys = ys.reshape(nevents // N, N).mean(1) 70 zs = zs.reshape(nevents // N, N).mean(1) 71 72 # add y limits so plot doesn't look like amplified noise 73 y_min = min([numpy.amin(xs), numpy.amin(ys), numpy.amin(zs), -MEAN_THRESH]) 74 y_max = max([numpy.amax(xs), numpy.amax(ys), numpy.amax(xs), MEAN_THRESH]) 75 76 pylab.plot(times, xs, 'r', label='x') 77 pylab.plot(times, ys, 'g', label='y') 78 pylab.plot(times, zs, 'b', label='z') 79 pylab.title(NAME) 80 pylab.xlabel('Time (seconds)') 81 pylab.ylabel('Gyro readings (mean of %d samples)'%(N)) 82 pylab.ylim([y_min, y_max]) 83 pylab.ticklabel_format(axis='y', style='sci', scilimits=(-3, -3)) 84 pylab.legend() 85 logging.debug('Saving plot') 86 matplotlib.pyplot.savefig('%s_plot.png' % os.path.join(self.log_path, NAME)) 87 88 for samples in [xs, ys, zs]: 89 mean = samples.mean() 90 var = numpy.var(samples) 91 logging.debug('mean: %.3e', mean) 92 logging.debug('var: %.3e', var) 93 if mean >= MEAN_THRESH: 94 raise AssertionError(f'mean: {mean}.3e, TOL={MEAN_THRESH}') 95 if var >= VAR_THRESH: 96 raise AssertionError(f'var: {var}.3e, TOL={VAR_THRESH}') 97 98 99if __name__ == '__main__': 100 test_runner.main() 101