SensorEvent.java revision dbd77cd444f89d94ec5333223c1bc17dbe0c90cd
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.hardware;
18
19/**
20 * <p>
21 * This class represents a {@link android.hardware.Sensor Sensor} event and
22 * holds informations such as the sensor's type, the time-stamp, accuracy and of
23 * course the sensor's {@link SensorEvent#values data}.
24 * </p>
25 *
26 * <p>
27 * <u>Definition of the coordinate system used by the SensorEvent API.</u>
28 * </p>
29 *
30 * <p>
31 * The coordinate-system is defined relative to the screen of the phone in its
32 * default orientation. The axes are not swapped when the device's screen
33 * orientation changes.
34 * </p>
35 *
36 * <p>
37 * The X axis is horizontal and points to the right, the Y axis is vertical and
38 * points up and the Z axis points towards the outside of the front face of the
39 * screen. In this system, coordinates behind the screen have negative Z values.
40 * </p>
41 *
42 * <p>
43 * <center><img src="../../../images/axis_device.png"
44 * alt="Sensors coordinate-system diagram." border="0" /></center>
45 * </p>
46 *
47 * <p>
48 * <b>Note:</b> This coordinate system is different from the one used in the
49 * Android 2D APIs where the origin is in the top-left corner.
50 * </p>
51 *
52 * @see SensorManager
53 * @see SensorEvent
54 * @see Sensor
55 *
56 */
57
58public class SensorEvent {
59    /**
60     * <p>
61     * The length and contents of the {@link #values values} array depends on
62     * which {@link android.hardware.Sensor sensor} type is being monitored (see
63     * also {@link SensorEvent} for a definition of the coordinate system used).
64     * </p>
65     *
66     * <h4>{@link android.hardware.Sensor#TYPE_ACCELEROMETER
67     * Sensor.TYPE_ACCELEROMETER}:</h4> All values are in SI units (m/s^2)
68     *
69     * <ul>
70     * <p>
71     * values[0]: Acceleration minus Gx on the x-axis
72     * </p>
73     * <p>
74     * values[1]: Acceleration minus Gy on the y-axis
75     * </p>
76     * <p>
77     * values[2]: Acceleration minus Gz on the z-axis
78     * </p>
79     * </ul>
80     *
81     * <p>
82     * A sensor of this type measures the acceleration applied to the device
83     * (<b>Ad</b>). Conceptually, it does so by measuring forces applied to the
84     * sensor itself (<b>Fs</b>) using the relation:
85     * </p>
86     *
87     * <b><center>Ad = - &#8721;Fs / mass</center></b>
88     *
89     * <p>
90     * In particular, the force of gravity is always influencing the measured
91     * acceleration:
92     * </p>
93     *
94     * <b><center>Ad = -g - &#8721;F / mass</center></b>
95     *
96     * <p>
97     * For this reason, when the device is sitting on a table (and obviously not
98     * accelerating), the accelerometer reads a magnitude of <b>g</b> = 9.81
99     * m/s^2
100     * </p>
101     *
102     * <p>
103     * Similarly, when the device is in free-fall and therefore dangerously
104     * accelerating towards to ground at 9.81 m/s^2, its accelerometer reads a
105     * magnitude of 0 m/s^2.
106     * </p>
107     *
108     * <p>
109     * It should be apparent that in order to measure the real acceleration of
110     * the device, the contribution of the force of gravity must be eliminated.
111     * This can be achieved by applying a <i>high-pass</i> filter. Conversely, a
112     * <i>low-pass</i> filter can be used to isolate the force of gravity.
113     * </p>
114     * <p>
115     * <u>Examples</u>:
116     * <ul>
117     * <li>When the device lies flat on a table and is pushed on its left side
118     * toward the right, the x acceleration value is positive.</li>
119     *
120     * <li>When the device lies flat on a table, the acceleration value is
121     * +9.81, which correspond to the acceleration of the device (0 m/s^2) minus
122     * the force of gravity (-9.81 m/s^2).</li>
123     *
124     * <li>When the device lies flat on a table and is pushed toward the sky
125     * with an acceleration of A m/s^2, the acceleration value is equal to
126     * A+9.81 which correspond to the acceleration of the device (+A m/s^2)
127     * minus the force of gravity (-9.81 m/s^2).</li>
128     * </ul>
129     *
130     *
131     * <h4>{@link android.hardware.Sensor#TYPE_MAGNETIC_FIELD
132     * Sensor.TYPE_MAGNETIC_FIELD}:</h4>
133     * All values are in micro-Tesla (uT) and measure the ambient magnetic field
134     * in the X, Y and Z axis.
135     *
136     * <h4>{@link android.hardware.Sensor#TYPE_LIGHT Sensor.TYPE_LIGHT}:</h4>
137     *
138     * <ul>
139     * <p>
140     * values[0]: Ambient light level in SI lux units
141     * </ul>
142     *
143     * <h4>{@link android.hardware.Sensor#TYPE_PROXIMITY Sensor.TYPE_PROXIMITY}:
144     * </h4>
145     *
146     * <ul>
147     * <p>
148     * values[0]: Proximity sensor distance measured in centimeters
149     * </ul>
150     *
151     * <p>
152     * <b>Note:</b> Some proximity sensors only support a binary <i>near</i> or
153     * <i>far</i> measurement. In this case, the sensor should report its
154     * {@link android.hardware.Sensor#getMaximumRange() maximum range} value in
155     * the <i>far</i> state and a lesser value in the <i>near</i> state.
156     * </p>
157     *
158     * <h4>{@link android.hardware.Sensor#TYPE_ORIENTATION
159     * Sensor.TYPE_ORIENTATION}:</h4> All values are angles in degrees.
160     *
161     * <ul>
162     * <p>
163     * values[0]: Azimuth, angle between the magnetic north direction and the
164     * y-axis, around the z-axis (0 to 359). 0=North, 90=East, 180=South,
165     * 270=West
166     * </p>
167     *
168     * <p>
169     * values[1]: Pitch, rotation around x-axis (-180 to 180), with positive
170     * values when the z-axis moves <b>toward</b> the y-axis.
171     * </p>
172     *
173     * <p>
174     * values[2]: Roll, rotation around y-axis (-90 to 90), with positive values
175     * when the x-axis moves <b>toward</b> the z-axis.
176     * </p>
177     * </ul>
178     *
179     * <p>
180     * <b>Note:</b> This definition is different from <b>yaw, pitch and roll</b>
181     * used in aviation where the X axis is along the long side of the plane
182     * (tail to nose).
183     * </p>
184     *
185     * <p>
186     * <b>Note:</b> This sensor type exists for legacy reasons, please use
187     * {@link android.hardware.SensorManager#getRotationMatrix
188     * getRotationMatrix()} in conjunction with
189     * {@link android.hardware.SensorManager#remapCoordinateSystem
190     * remapCoordinateSystem()} and
191     * {@link android.hardware.SensorManager#getOrientation getOrientation()} to
192     * compute these values instead.
193     * </p>
194     *
195     * <p>
196     * <b>Important note:</b> For historical reasons the roll angle is positive
197     * in the clockwise direction (mathematically speaking, it should be
198     * positive in the counter-clockwise direction).
199     * </p>
200     *
201     * @see SensorEvent
202     * @see GeomagneticField
203     */
204    public final float[] values;
205
206    /**
207     * The sensor that generated this event. See
208     * {@link android.hardware.SensorManager SensorManager} for details.
209     */
210   public Sensor sensor;
211
212    /**
213     * The accuracy of this event. See {@link android.hardware.SensorManager
214     * SensorManager} for details.
215     */
216    public int accuracy;
217
218
219    /**
220     * The time in nanosecond at which the event happened
221     */
222    public long timestamp;
223
224
225    SensorEvent(int size) {
226        values = new float[size];
227    }
228}
229