Most visited

Recently visited

Added in API level 3

SensorEvent

public class SensorEvent
extends Object

java.lang.Object
   ↳ android.hardware.SensorEvent


此课程代表 Sensor事件,并保存诸如传感器类型,时间戳,准确性等信息,当然还包括传感器 data

SensorEvent API使用的坐标系的定义。

坐标系统是相对于默认方向的手机屏幕定义的。 当设备的屏幕方向改变时,轴不交换。

X轴是水平的并指向右侧,Y轴是垂直的并指向上方,Z轴指向屏幕正面的外侧。 在这个系统中,屏幕后面的坐标具有负Z值。

Sensors coordinate-system diagram.

注意:此坐标系与原始位于左上角的Android 2D API中使用的坐标系不同。

也可以看看:

Summary

Fields

public int accuracy

此事件的准确性。

public Sensor sensor

生成此事件的传感器。

public long timestamp

事件发生的时间为纳秒

public final float[] values

values数组的长度和内容取决于正在监视哪个 sensor类型(另请参阅 SensorEvent了解所用坐标系的定义)。

Inherited methods

From class java.lang.Object

Fields

accuracy

Added in API level 3
int accuracy

此事件的准确性。 有关详细信息,请参阅SensorManager

sensor

Added in API level 3
Sensor sensor

生成此事件的传感器。 有关详细信息,请参阅SensorManager

timestamp

Added in API level 3
long timestamp

事件发生的时间为纳秒

values

Added in API level 3
float[] values

values数组的长度和内容取决于正在监视哪个 sensor类型(另请参阅 SensorEvent以了解所用坐标系的定义)。

Sensor.TYPE_ACCELEROMETER:

All values are in SI units (m/s^2)
  • values[0]: Acceleration minus Gx on the x-axis
  • values[1]: Acceleration minus Gy on the y-axis
  • values[2]: Acceleration minus Gz on the z-axis

这种类型的传感器测量施加到设备的加速度( Ad )。 从概念上讲,它通过使用以下关系测量施加到传感器本身( Fs )的力:

Ad = - ∑Fs / mass

特别是,重力总是影响测量的加速度:

Ad = -g - ∑F / mass

出于这个原因,当设备坐在桌子上(并且显然不加速)时,加速度计读取的幅度为 g = 9.81m / s ^ 2

类似地,当设备处于自由落体状态并因此危险地以9.81 m / s ^ 2的速度加速到地面时,其加速度计读数为0 m / s ^ 2。

应该明显的是,为了测量装置的真实加速度,必须消除重力的贡献。 这可以通过应用高通滤波器来实现。 相反,可以使用低通滤波器来隔离重力。


     public void onSensorChanged(SensorEvent event)
     {
          // alpha is calculated as t / (t + dT)
          // with t, the low-pass filter's time-constant
          // and dT, the event delivery rate

          final float alpha = 0.8;

          gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
          gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
          gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

          linear_acceleration[0] = event.values[0] - gravity[0];
          linear_acceleration[1] = event.values[1] - gravity[1];
          linear_acceleration[2] = event.values[2] - gravity[2];
     }
 

例子

  • When the device lies flat on a table and is pushed on its left side toward the right, the x acceleration value is positive.
  • When the device lies flat on a table, the acceleration value is +9.81, which correspond to the acceleration of the device (0 m/s^2) minus the force of gravity (-9.81 m/s^2).
  • When the device lies flat on a table and is pushed toward the sky with an acceleration of A m/s^2, the acceleration value is equal to A+9.81 which correspond to the acceleration of the device (+A m/s^2) minus the force of gravity (-9.81 m/s^2).

Sensor.TYPE_MAGNETIC_FIELD:

All values are in micro-Tesla (uT) and measure the ambient magnetic field in the X, Y and Z axis.

Sensor.TYPE_GYROSCOPE:

All values are in radians/second and measure the rate of rotation around the device's local X, Y and Z axis. The coordinate system is the same as is used for the acceleration sensor. Rotation is positive in the counter-clockwise direction. That is, an observer looking from some positive location on the x, y or z axis at a device positioned on the origin would report positive rotation if the device appeared to be rotating counter clockwise. Note that this is the standard mathematical definition of positive rotation and does not agree with the definition of roll given earlier.
  • values[0]: Angular speed around the x-axis
  • values[1]: Angular speed around the y-axis
  • values[2]: Angular speed around the z-axis

典型地,陀螺仪的输出随时间积分以计算描述角度在时间步长上的变化的旋转,例如:

     private static final float NS2S = 1.0f / 1000000000.0f;
     private final float[] deltaRotationVector = new float[4]();
     private float timestamp;

     public void onSensorChanged(SensorEvent event) {
          // This time step's delta rotation to be multiplied by the current rotation
          // after computing it from the gyro sample data.
          if (timestamp != 0) {
              final float dT = (event.timestamp - timestamp) * NS2S;
              // Axis of the rotation sample, not normalized yet.
              float axisX = event.values[0];
              float axisY = event.values[1];
              float axisZ = event.values[2];

              // Calculate the angular speed of the sample
              float omegaMagnitude = sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);

              // Normalize the rotation vector if it's big enough to get the axis
              if (omegaMagnitude > EPSILON) {
                  axisX /= omegaMagnitude;
                  axisY /= omegaMagnitude;
                  axisZ /= omegaMagnitude;
              }

              // Integrate around this axis with the angular speed by the time step
              // in order to get a delta rotation from this sample over the time step
              // We will convert this axis-angle representation of the delta rotation
              // into a quaternion before turning it into the rotation matrix.
              float thetaOverTwo = omegaMagnitude * dT / 2.0f;
              float sinThetaOverTwo = sin(thetaOverTwo);
              float cosThetaOverTwo = cos(thetaOverTwo);
              deltaRotationVector[0] = sinThetaOverTwo * axisX;
              deltaRotationVector[1] = sinThetaOverTwo * axisY;
              deltaRotationVector[2] = sinThetaOverTwo * axisZ;
              deltaRotationVector[3] = cosThetaOverTwo;
          }
          timestamp = event.timestamp;
          float[] deltaRotationMatrix = new float[9];
          SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);
          // User code should concatenate the delta rotation we computed with the current rotation
          // in order to get the updated rotation.
          // rotationCurrent = rotationCurrent * deltaRotationMatrix;
     }
 

实际上,陀螺仪噪声和偏移会引入一些需要补偿的误差。 这通常是使用来自其他传感器的信息完成的,但超出了本文的范围。

Sensor.TYPE_LIGHT:

  • values[0]: Ambient light level in SI lux units

Sensor.TYPE_PRESSURE:

  • values[0]: Atmospheric pressure in hPa (millibar)

Sensor.TYPE_PROXIMITY:

  • values[0]: Proximity sensor distance measured in centimeters

注意:一些接近传感器仅支持二进制测量。 在这种情况下,传感器应报告远端状态下的值为maximum range ,并且在近端状态下报告较小的值。

Sensor.TYPE_GRAVITY:

指示重力方向和大小的三维矢量。 单位是m / s ^ 2。 坐标系与加速度传感器使用的坐标系相同。

注意:当设备静止时,重力传感器的输出应与加速度计的输出相同。

Sensor.TYPE_LINEAR_ACCELERATION:

A three dimensional vector indicating acceleration along each device axis, not including gravity. All values have units of m/s^2. The coordinate system is the same as is used by the acceleration sensor.

加速度计,重力和线性加速度传感器的输出必须遵守以下关系:

    acceleration = gravity + linear-acceleration

Sensor.TYPE_ROTATION_VECTOR:

旋转矢量表示设备作为 角度的组合的 方向 ,其中设备围绕轴<x,y,z>旋转角度θ。

The three elements of the rotation vector are <x*sin(θ/2), y*sin(θ/2), z*sin(θ/2)>, such that the magnitude of the rotation vector is equal to sin(θ/2), and the direction of the rotation vector is equal to the direction of the axis of rotation.

The three elements of the rotation vector are equal to the last three components of a unit quaternion <cos(θ/2), x*sin(θ/2), y*sin(θ/2), z*sin(θ/2)>.

旋转矢量的元素是无单位的。 x,y和z轴的定义与加速度传感器相同。

The reference coordinate system is defined as a direct orthonormal basis, where:

  • X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points East).
  • Y is tangential to the ground at the device's current location and points towards magnetic north.
  • Z points towards the sky and is perpendicular to the ground.

World coordinate-system diagram.

  • values[0]: x*sin(θ/2)
  • values[1]: y*sin(θ/2)
  • values[2]: z*sin(θ/2)
  • values[3]: cos(θ/2)
  • values[4]: estimated heading Accuracy (in radians) (-1 if unavailable)

最初可选的值[3]将始终从SDK级别18开始提供。 值[4]是在SDK Level 18中添加的新值。

Sensor.TYPE_ORIENTATION:

All values are angles in degrees.
  • values[0]: Azimuth, angle between the magnetic north direction and the y-axis, around the z-axis (0 to 359). 0=North, 90=East, 180=South, 270=West

    值[1]:围绕x轴的螺距(-180至180)旋转,当z轴 朝向 y轴移动时为正值。

    值[2]:绕着y轴旋转(-90到90°),随着设备顺时针移动而增加。

注意:这个定义不同于航空中使用的 偏航,俯仰和滚转 ,其中X轴沿着飞机的长边(尾到鼻)。

注意:由于传统原因,此传感器类型存在,请使用 rotation vector sensor typegetRotationMatrix()结合 remapCoordinateSystem()getOrientation()来计算这些值。

重要提示:由于历史原因,顺时针方向的滚动角度为正值(从数学角度而言,逆时针方向应为正值)。

Sensor.TYPE_RELATIVE_HUMIDITY:

  • values[0]: Relative ambient air humidity in percent

当测量相对环境空气湿度和环境温度时,可计算露点和绝对湿度。

Dew Point

露点是在恒定的气压下,给定的空气块必须冷却的温度,以使水蒸气冷凝成水。

                    ln(RH/100%) + m·t/(Tn+t)
 td(t,RH) = Tn · ------------------------------
                 m - [ln(RH/100%) + m·t/(Tn+t)]
 
t d
dew point temperature in °C
t
actual temperature in °C
RH
actual relative humidity in %
m
17.62
T n
243.12 °C

例如:

 h = Math.log(rh / 100.0) + (17.62 * t) / (243.12 + t);
 td = 243.12 * h / (17.62 - h);
 
Absolute Humidity

绝对湿度是特定体积干空气中水蒸气的质量。 单位为克/米3。

                    RH/100%·A·exp(m·t/(Tn+t))
 dv(t,RH) = 216.7 · -------------------------
                           273.15 + t
 
d v
absolute humidity in g/m 3
t
actual temperature in °C
RH
actual relative humidity in %
m
17.62
T n
243.12 °C
A
6.112 hPa

例如:

 dv = 216.7 *
 (rh / 100.0 * 6.112 * Math.exp(17.62 * t / (243.12 + t)) / (273.15 + t));
 

Sensor.TYPE_AMBIENT_TEMPERATURE:

  • values[0]: ambient (room) temperature in degree Celsius.

Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED:

Similar to TYPE_MAGNETIC_FIELD, but the hard iron calibration is reported separately instead of being included in the measurement. Factory calibration and temperature compensation will still be applied to the "uncalibrated" measurement. Assumptions that the magnetic field is due to the Earth's poles is avoided.

values数组如下所示:

  • values[0] = x_uncalib
  • values[1] = y_uncalib
  • values[2] = z_uncalib
  • values[3] = x_bias
  • values[4] = y_bias
  • values[5] = z_bias

x_uncalib,y_uncalib,z_uncalib是X,Y,Z轴上的测量磁场。 应用软铁和温度校准。 但是硬铁校准不适用。 数值以微特斯拉(uT)表示。

x_bias,y_bias,z_bias给出在X,Y,Z轴上估计的铁偏置。 每个领域是估计的硬铁校准的一个组成部分。 数值以微特斯拉(uT)表示。

硬铁 - 由于设备上的磁铁,钢或永磁体而产生这些扭曲。 软铁 - 这些扭曲是由于与地球磁场的相互作用而产生的。

TYPE_GAME_ROTATION_VECTOR:

Identical to TYPE_ROTATION_VECTOR except that it doesn't use the geomagnetic field. Therefore the Y axis doesn't point north, but instead to some other reference, that reference is allowed to drift by the same order of magnitude as the gyroscope drift around the Z axis.

在理想的情况下,旋转并回到相同的真实世界方向的电话将报告相同的游戏旋转矢量(不使用地球的地磁场)。 然而,随着时间的推移,方向可能会有所变化 有关这些值的详细说明,请参阅TYPE_ROTATION_VECTOR 该传感器将不具有估计航向精度值。

Sensor.TYPE_GYROSCOPE_UNCALIBRATED:

All values are in radians/second and measure the rate of rotation around the X, Y and Z axis. An estimation of the drift on each axis is reported as well.

没有执行陀螺漂移补偿。 工厂校准和温度补偿仍然适用于旋转速度(角速度)。

坐标系与用于TYPE_ACCELEROMETER的坐标系相同,逆时针旋转方向为正(右手定则)。 也就是说,如果设备似乎逆时针旋转,则位于原点处的设备上的从x,y或z轴上的某个正位置看的观察者将报告正转。 范围至少为17.45 rad / s(即:〜1000 deg / s)。

  • values[0] : angular speed (w/o drift compensation) around the X axis in rad/s
  • values[1] : angular speed (w/o drift compensation) around the Y axis in rad/s
  • values[2] : angular speed (w/o drift compensation) around the Z axis in rad/s
  • values[3] : estimated drift around X axis in rad/s
  • values[4] : estimated drift around Y axis in rad/s
  • values[5] : estimated drift around Z axis in rad/s

专业提示:在对其执行操作时,始终使用values数组的长度。 在以前的版本中,这个版本总是3,现在已经改变了。

也可以看看:

Hooray!