Home | History | Annotate | Download | only in scene0
      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 
     15 import os
     16 import time
     17 
     18 import its.caps
     19 import its.device
     20 import its.image
     21 import its.objects
     22 import its.target
     23 import matplotlib
     24 from matplotlib import pylab
     25 import numpy
     26 
     27 NAME = os.path.basename(__file__).split('.')[0]
     28 N = 20  # Number of samples averaged together, in the plot.
     29 MEAN_THRESH = 0.01  # PASS/FAIL threshold for gyro mean drift
     30 VAR_THRESH = 0.001  # PASS/FAIL threshold for gyro variance drift
     31 
     32 
     33 def main():
     34     """Test if the gyro has stable output when device is stationary.
     35     """
     36     with its.device.ItsSession() as cam:
     37         props = cam.get_camera_properties()
     38         # Only run test if the appropriate caps are claimed.
     39         its.caps.skip_unless(its.caps.sensor_fusion(props) and
     40             cam.get_sensors().get("gyro"))
     41 
     42         print 'Collecting gyro events'
     43         cam.start_sensor_events()
     44         time.sleep(5)
     45         gyro_events = cam.get_sensor_events()['gyro']
     46 
     47     nevents = (len(gyro_events) / N) * N
     48     gyro_events = gyro_events[:nevents]
     49     times = numpy.array([(e['time'] - gyro_events[0]['time'])/1000000000.0
     50                          for e in gyro_events])
     51     xs = numpy.array([e['x'] for e in gyro_events])
     52     ys = numpy.array([e['y'] for e in gyro_events])
     53     zs = numpy.array([e['z'] for e in gyro_events])
     54 
     55     # Group samples into size-N groups and average each together, to get rid
     56     # of individual random spikes in the data.
     57     times = times[N/2::N]
     58     xs = xs.reshape(nevents/N, N).mean(1)
     59     ys = ys.reshape(nevents/N, N).mean(1)
     60     zs = zs.reshape(nevents/N, N).mean(1)
     61 
     62     pylab.plot(times, xs, 'r', label='x')
     63     pylab.plot(times, ys, 'g', label='y')
     64     pylab.plot(times, zs, 'b', label='z')
     65     pylab.xlabel('Time (seconds)')
     66     pylab.ylabel('Gyro readings (mean of %d samples)'%(N))
     67     pylab.legend()
     68     matplotlib.pyplot.savefig('%s_plot.png' % (NAME))
     69 
     70     for samples in [xs, ys, zs]:
     71         mean = samples.mean()
     72         var = numpy.var(samples)
     73         assert mean < MEAN_THRESH, 'mean: %.3f, TOL=%.2f' % (mean, MEAN_THRESH)
     74         assert var < VAR_THRESH, 'var: %.4f, TOL=%.3f' % (var, VAR_THRESH)
     75 
     76 if __name__ == '__main__':
     77     main()
     78 
     79