• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.device
16import its.objects
17import its.caps
18import time
19
20def main():
21    """Test if image and motion sensor events are in the same time domain.
22    """
23
24    with its.device.ItsSession() as cam:
25        props = cam.get_camera_properties()
26
27        # Only run test if the appropriate caps are claimed.
28        if not its.caps.sensor_fusion(props):
29            print "Test skipped"
30            return
31
32        # Get the timestamp of a captured image.
33        req, fmt = its.objects.get_fastest_manual_capture_settings(props)
34        cap = cam.do_capture(req, fmt)
35        ts_image0 = cap['metadata']['android.sensor.timestamp']
36
37        # Get the timestamps of motion events.
38        print "Reading sensor measurements"
39        cam.start_sensor_events()
40        time.sleep(0.5)
41        events = cam.get_sensor_events()
42        assert(len(events["gyro"]) > 0)
43        assert(len(events["accel"]) > 0)
44        assert(len(events["mag"]) > 0)
45        ts_gyro0 = events["gyro"][0]["time"]
46        ts_gyro1 = events["gyro"][-1]["time"]
47        ts_accel0 = events["accel"][0]["time"]
48        ts_accel1 = events["accel"][-1]["time"]
49        ts_mag0 = events["mag"][0]["time"]
50        ts_mag1 = events["mag"][-1]["time"]
51
52        # Get the timestamp of another image.
53        cap = cam.do_capture(req, fmt)
54        ts_image1 = cap['metadata']['android.sensor.timestamp']
55
56        print "Image timestamps:", ts_image0, ts_image1
57        print "Gyro timestamps:", ts_gyro0, ts_gyro1
58        print "Accel timestamps:", ts_accel0, ts_accel1
59        print "Mag timestamps:", ts_mag0, ts_mag1
60
61        # The motion timestamps must be between the two image timestamps.
62        assert ts_image0 < min(ts_gyro0, ts_accel0, ts_mag0) < ts_image1
63        assert ts_image0 < max(ts_gyro1, ts_accel1, ts_mag1) < ts_image1
64
65if __name__ == '__main__':
66    main()
67
68