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 os.path 16 17import its.caps 18import its.device 19import its.image 20import its.objects 21 22import matplotlib 23from matplotlib import pylab 24 25# PASS/FAIL thresholds 26MIN_AVG_FRAME_DELTA = 30 # at least 30ms delta between frames 27MAX_VAR_FRAME_DELTA = 0.01 # variance of frame deltas 28MAX_FRAME_DELTA_JITTER = 0.3 # max ms gap from the average frame delta 29 30NAME = os.path.basename(__file__).split('.')[0] 31 32 33def main(): 34 """Measure jitter in camera timestamps.""" 35 36 with its.device.ItsSession() as cam: 37 props = cam.get_camera_properties() 38 its.caps.skip_unless(its.caps.manual_sensor(props) and 39 its.caps.sensor_fusion(props)) 40 41 req, fmt = its.objects.get_fastest_manual_capture_settings(props) 42 caps = cam.do_capture([req]*50, [fmt]) 43 44 # Print out the millisecond delta between the start of each exposure 45 tstamps = [c['metadata']['android.sensor.timestamp'] for c in caps] 46 deltas = [tstamps[i]-tstamps[i-1] for i in range(1, len(tstamps))] 47 deltas_ms = [d/1000000.0 for d in deltas] 48 avg = sum(deltas_ms) / len(deltas_ms) 49 var = sum([d*d for d in deltas_ms]) / len(deltas_ms) - avg * avg 50 range0 = min(deltas_ms) - avg 51 range1 = max(deltas_ms) - avg 52 print 'Average:', avg 53 print 'Variance:', var 54 print 'Jitter range:', range0, 'to', range1 55 56 # Draw a plot. 57 pylab.plot(range(len(deltas_ms)), deltas_ms) 58 pylab.title(NAME) 59 pylab.xlabel('frame number') 60 pylab.ylabel('jitter (ms)') 61 matplotlib.pyplot.savefig('%s_deltas.png' % (NAME)) 62 63 # Test for pass/fail. 64 emsg = 'avg: %.4fms, TOL: %.fms' % (avg, MIN_AVG_FRAME_DELTA) 65 assert avg > MIN_AVG_FRAME_DELTA, emsg 66 emsg = 'var: %.4fms, TOL: %.2fms' % (var, MAX_VAR_FRAME_DELTA) 67 assert var < MAX_VAR_FRAME_DELTA, emsg 68 emsg = 'range0: %.4fms, range1: %.4fms, TOL: %.2fms' % ( 69 range0, range1, MAX_FRAME_DELTA_JITTER) 70 assert abs(range0) < MAX_FRAME_DELTA_JITTER, emsg 71 assert abs(range1) < MAX_FRAME_DELTA_JITTER, emsg 72 73if __name__ == '__main__': 74 main() 75 76