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 = - ∑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 - ∑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 * 115 * <pre class="prettyprint"> 116 * 117 * public void onSensorChanged(SensorEvent event) 118 * { 119 * // alpha is calculated as t / (t + dT) 120 * // with t, the low-pass filter's time-constant 121 * // and dT, the event delivery rate 122 * 123 * final float alpha = 0.8; 124 * 125 * gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0]; 126 * gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1]; 127 * gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2]; 128 * 129 * linear_acceleration[0] = event.values[0] - gravity[0]; 130 * linear_acceleration[1] = event.values[1] - gravity[1]; 131 * linear_acceleration[2] = event.values[2] - gravity[2]; 132 * } 133 * </pre> 134 * 135 * <p> 136 * <u>Examples</u>: 137 * <ul> 138 * <li>When the device lies flat on a table and is pushed on its left side 139 * toward the right, the x acceleration value is positive.</li> 140 * 141 * <li>When the device lies flat on a table, the acceleration value is 142 * +9.81, which correspond to the acceleration of the device (0 m/s^2) minus 143 * the force of gravity (-9.81 m/s^2).</li> 144 * 145 * <li>When the device lies flat on a table and is pushed toward the sky 146 * with an acceleration of A m/s^2, the acceleration value is equal to 147 * A+9.81 which correspond to the acceleration of the device (+A m/s^2) 148 * minus the force of gravity (-9.81 m/s^2).</li> 149 * </ul> 150 * 151 * 152 * <h4>{@link android.hardware.Sensor#TYPE_MAGNETIC_FIELD 153 * Sensor.TYPE_MAGNETIC_FIELD}:</h4> 154 * All values are in micro-Tesla (uT) and measure the ambient magnetic field 155 * in the X, Y and Z axis. 156 * 157 * <h4>{@link android.hardware.Sensor#TYPE_GYROSCOPE Sensor.TYPE_GYROSCOPE}: 158 * </h4> All values are in radians/second and measure the rate of rotation 159 * around the device's local X, Y and Z axis. The coordinate system is the 160 * same as is used for the acceleration sensor. Rotation is positive in the 161 * counter-clockwise direction. That is, an observer looking from some 162 * positive location on the x, y or z axis at a device positioned on the 163 * origin would report positive rotation if the device appeared to be 164 * rotating counter clockwise. Note that this is the standard mathematical 165 * definition of positive rotation and does not agree with the definition of 166 * roll given earlier. 167 * <ul> 168 * <p> 169 * values[0]: Angular speed around the x-axis 170 * </p> 171 * <p> 172 * values[1]: Angular speed around the y-axis 173 * </p> 174 * <p> 175 * values[2]: Angular speed around the z-axis 176 * </p> 177 * </ul> 178 * <p> 179 * Typically the output of the gyroscope is integrated over time to 180 * calculate a rotation describing the change of angles over the timestep, 181 * for example: 182 * </p> 183 * 184 * <pre class="prettyprint"> 185 * private static final float NS2S = 1.0f / 1000000000.0f; 186 * private final float[] deltaRotationVector = new float[4](); 187 * private float timestamp; 188 * 189 * public void onSensorChanged(SensorEvent event) { 190 * // This timestep's delta rotation to be multiplied by the current rotation 191 * // after computing it from the gyro sample data. 192 * if (timestamp != 0) { 193 * final float dT = (event.timestamp - timestamp) * NS2S; 194 * // Axis of the rotation sample, not normalized yet. 195 * float axisX = event.values[0]; 196 * float axisY = event.values[1]; 197 * float axisZ = event.values[2]; 198 * 199 * // Calculate the angular speed of the sample 200 * float omegaMagnitude = sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ); 201 * 202 * // Normalize the rotation vector if it's big enough to get the axis 203 * if (omegaMagnitude > EPSILON) { 204 * axisX /= omegaMagnitude; 205 * axisY /= omegaMagnitude; 206 * axisZ /= omegaMagnitude; 207 * } 208 * 209 * // Integrate around this axis with the angular speed by the timestep 210 * // in order to get a delta rotation from this sample over the timestep 211 * // We will convert this axis-angle representation of the delta rotation 212 * // into a quaternion before turning it into the rotation matrix. 213 * float thetaOverTwo = omegaMagnitude * dT / 2.0f; 214 * float sinThetaOverTwo = sin(thetaOverTwo); 215 * float cosThetaOverTwo = cos(thetaOverTwo); 216 * deltaRotationVector[0] = sinThetaOverTwo * axisX; 217 * deltaRotationVector[1] = sinThetaOverTwo * axisY; 218 * deltaRotationVector[2] = sinThetaOverTwo * axisZ; 219 * deltaRotationVector[3] = cosThetaOverTwo; 220 * } 221 * timestamp = event.timestamp; 222 * float[] deltaRotationMatrix = new float[9]; 223 * SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector); 224 * // User code should concatenate the delta rotation we computed with the current rotation 225 * // in order to get the updated rotation. 226 * // rotationCurrent = rotationCurrent * deltaRotationMatrix; 227 * } 228 * </pre> 229 * <p> 230 * In practice, the gyroscope noise and offset will introduce some errors 231 * which need to be compensated for. This is usually done using the 232 * information from other sensors, but is beyond the scope of this document. 233 * </p> 234 * <h4>{@link android.hardware.Sensor#TYPE_LIGHT Sensor.TYPE_LIGHT}:</h4> 235 * <ul> 236 * <p> 237 * values[0]: Ambient light level in SI lux units 238 * </ul> 239 * 240 * <h4>{@link android.hardware.Sensor#TYPE_PRESSURE Sensor.TYPE_PRESSURE}:</h4> 241 * <ul> 242 * <p> 243 * values[0]: Atmospheric pressure in hPa (millibar) 244 * </ul> 245 * 246 * <h4>{@link android.hardware.Sensor#TYPE_PROXIMITY Sensor.TYPE_PROXIMITY}: 247 * </h4> 248 * 249 * <ul> 250 * <p> 251 * values[0]: Proximity sensor distance measured in centimeters 252 * </ul> 253 * 254 * <p> 255 * <b>Note:</b> Some proximity sensors only support a binary <i>near</i> or 256 * <i>far</i> measurement. In this case, the sensor should report its 257 * {@link android.hardware.Sensor#getMaximumRange() maximum range} value in 258 * the <i>far</i> state and a lesser value in the <i>near</i> state. 259 * </p> 260 * 261 * <h4>{@link android.hardware.Sensor#TYPE_GRAVITY Sensor.TYPE_GRAVITY}:</h4> 262 * <p>A three dimensional vector indicating the direction and magnitude of gravity. Units 263 * are m/s^2. The coordinate system is the same as is used by the acceleration sensor.</p> 264 * <p><b>Note:</b> When the device is at rest, the output of the gravity sensor should be identical 265 * to that of the accelerometer.</p> 266 * 267 * <h4>{@link android.hardware.Sensor#TYPE_LINEAR_ACCELERATION Sensor.TYPE_LINEAR_ACCELERATION}:</h4> 268 * A three dimensional vector indicating acceleration along each device axis, not including 269 * gravity. All values have units of m/s^2. The coordinate system is the same as is used by the 270 * acceleration sensor. 271 * <p>The output of the accelerometer, gravity and linear-acceleration sensors must obey the 272 * following relation:</p> 273 * <p><ul>acceleration = gravity + linear-acceleration</ul></p> 274 * 275 * <h4>{@link android.hardware.Sensor#TYPE_ROTATION_VECTOR Sensor.TYPE_ROTATION_VECTOR}:</h4> 276 * <p>The rotation vector represents the orientation of the device as a combination of an <i>angle</i> 277 * and an <i>axis</i>, in which the device has rotated through an angle θ around an axis 278 * <x, y, z>.</p> 279 * <p>The three elements of the rotation vector are 280 * <x*sin(θ/2), y*sin(θ/2), z*sin(θ/2)>, such that the magnitude of the rotation 281 * vector is equal to sin(θ/2), and the direction of the rotation vector is equal to the 282 * direction of the axis of rotation.</p> 283 * </p>The three elements of the rotation vector are equal to 284 * the last three components of a <b>unit</b> quaternion 285 * <cos(θ/2), x*sin(θ/2), y*sin(θ/2), z*sin(θ/2)>.</p> 286 * <p>Elements of the rotation vector are unitless. 287 * The x,y, and z axis are defined in the same way as the acceleration 288 * sensor.</p> 289 * The reference coordinate system is defined as a direct orthonormal basis, 290 * where: 291 * </p> 292 * 293 * <ul> 294 * <li>X is defined as the vector product <b>Y.Z</b> (It is tangential to 295 * the ground at the device's current location and roughly points East).</li> 296 * <li>Y is tangential to the ground at the device's current location and 297 * points towards magnetic north.</li> 298 * <li>Z points towards the sky and is perpendicular to the ground.</li> 299 * </ul> 300 * 301 * <p> 302 * <center><img src="../../../images/axis_globe.png" 303 * alt="World coordinate-system diagram." border="0" /></center> 304 * </p> 305 * 306 * <ul> 307 * <p> 308 * values[0]: x*sin(θ/2) 309 * </p> 310 * <p> 311 * values[1]: y*sin(θ/2) 312 * </p> 313 * <p> 314 * values[2]: z*sin(θ/2) 315 * </p> 316 * <p> 317 * values[3]: cos(θ/2) <i>(optional: only if value.length = 4)</i> 318 * </p> 319 * </ul> 320 * 321 * <h4>{@link android.hardware.Sensor#TYPE_ORIENTATION 322 * Sensor.TYPE_ORIENTATION}:</h4> All values are angles in degrees. 323 * 324 * <ul> 325 * <p> 326 * values[0]: Azimuth, angle between the magnetic north direction and the 327 * y-axis, around the z-axis (0 to 359). 0=North, 90=East, 180=South, 328 * 270=West 329 * </p> 330 * 331 * <p> 332 * values[1]: Pitch, rotation around x-axis (-180 to 180), with positive 333 * values when the z-axis moves <b>toward</b> the y-axis. 334 * </p> 335 * 336 * <p> 337 * values[2]: Roll, rotation around y-axis (-90 to 90), with positive values 338 * when the x-axis moves <b>toward</b> the z-axis. 339 * </p> 340 * </ul> 341 * 342 * <p> 343 * <b>Note:</b> This definition is different from <b>yaw, pitch and roll</b> 344 * used in aviation where the X axis is along the long side of the plane 345 * (tail to nose). 346 * </p> 347 * 348 * <p> 349 * <b>Note:</b> This sensor type exists for legacy reasons, please use 350 * {@link android.hardware.SensorManager#getRotationMatrix 351 * getRotationMatrix()} in conjunction with 352 * {@link android.hardware.SensorManager#remapCoordinateSystem 353 * remapCoordinateSystem()} and 354 * {@link android.hardware.SensorManager#getOrientation getOrientation()} to 355 * compute these values instead. 356 * </p> 357 * 358 * <p> 359 * <b>Important note:</b> For historical reasons the roll angle is positive 360 * in the clockwise direction (mathematically speaking, it should be 361 * positive in the counter-clockwise direction). 362 * </p> 363 * 364 * <h4>{@link android.hardware.Sensor#TYPE_RELATIVE_HUMIDITY 365 * Sensor.TYPE_RELATIVE_HUMIDITY}:</h4> 366 * <ul> 367 * <p> 368 * values[0]: Relative ambient air humidity in percent 369 * </p> 370 * </ul> 371 * <p> 372 * When relative ambient air humidity and ambient temperature are 373 * measured, the dew point and absolute humidity can be calculated. 374 * </p> 375 * <u>Dew Point</u> 376 * <p> 377 * The dew point is the temperature to which a given parcel of air must be 378 * cooled, at constant barometric pressure, for water vapor to condense 379 * into water. 380 * </p> 381 * <center><pre> 382 * ln(RH/100%) + m·t/(T<sub>n</sub>+t) 383 * t<sub>d</sub>(t,RH) = T<sub>n</sub> · ------------------------------ 384 * m - [ln(RH/100%) + m·t/(T<sub>n</sub>+t)] 385 * </pre></center> 386 * <dl> 387 * <dt>t<sub>d</sub></dt> <dd>dew point temperature in °C</dd> 388 * <dt>t</dt> <dd>actual temperature in °C</dd> 389 * <dt>RH</dt> <dd>actual relative humidity in %</dd> 390 * <dt>m</dt> <dd>17.62</dd> 391 * <dt>T<sub>n</sub></dt> <dd>243.12 °C</dd> 392 * </dl> 393 * <p>for example:</p> 394 * <pre class="prettyprint"> 395 * h = Math.log(rh / 100.0) + (17.62 * t) / (243.12 + t); 396 * td = 243.12 * h / (17.62 - h); 397 * </pre> 398 * <u>Absolute Humidity</u> 399 * <p> 400 * The absolute humidity is the mass of water vapor in a particular volume 401 * of dry air. The unit is g/m<sup>3</sup>. 402 * </p> 403 * <center><pre> 404 * RH/100%·A·exp(m·t/(T<sub>n</sub>+t)) 405 * d<sub>v</sub>(t,RH) = 216.7 · ------------------------- 406 * 273.15 + t 407 * </pre></center> 408 * <dl> 409 * <dt>d<sub>v</sub></dt> <dd>absolute humidity in g/m<sup>3</sup></dd> 410 * <dt>t</dt> <dd>actual temperature in °C</dd> 411 * <dt>RH</dt> <dd>actual relative humidity in %</dd> 412 * <dt>m</dt> <dd>17.62</dd> 413 * <dt>T<sub>n</sub></dt> <dd>243.12 °C</dd> 414 * <dt>A</dt> <dd>6.112 hPa</dd> 415 * </dl> 416 * <p>for example:</p> 417 * <pre class="prettyprint"> 418 * dv = 216.7 * 419 * (rh / 100.0 * 6.112 * Math.exp(17.62 * t / (243.12 + t)) / (273.15 + t)); 420 * </pre> 421 * 422 * <h4>{@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE Sensor.TYPE_AMBIENT_TEMPERATURE}: 423 * </h4> 424 * 425 * <ul> 426 * <p> 427 * values[0]: ambient (room) temperature in degree Celsius. 428 * </ul> 429 * 430 * @see SensorEvent 431 * @see GeomagneticField 432 */ 433 434 public final float[] values; 435 436 /** 437 * The sensor that generated this event. See 438 * {@link android.hardware.SensorManager SensorManager} for details. 439 */ 440 public Sensor sensor; 441 442 /** 443 * The accuracy of this event. See {@link android.hardware.SensorManager 444 * SensorManager} for details. 445 */ 446 public int accuracy; 447 448 449 /** 450 * The time in nanosecond at which the event happened 451 */ 452 public long timestamp; 453 454 455 SensorEvent(int size) { 456 values = new float[size]; 457 } 458} 459