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