因为墙的原因翻译备用,先贴上原文链接:https://developer.android.google.cn/guide/topics/sensors/sensors_motion.html
传感器简单介绍
Android平台提供了几个传感器,可以让你监控设备的运动。传感器的可能结构因传感器类型而异:重力、线性加速度、旋转矢量、重要的运动、阶跃计数器和步进探测器传感器都是基于硬件的或基于软件的。加速度计和陀螺仪传感器都是基于硬件的。大多数采用安卓系统的设备都有一个加速计,其中许多现在包括一个陀螺仪。基于软件的传感器的可用性更大,因为它们通常依赖于一个或多个硬件传感器来获得它们的数据。
旋转矢量传感器和重力传感器是最常用的运动检测和监测传感器。旋转矢量传感器的用途特别广泛,可用于各种与运动相关的任务,如检测手势、监测角变化、监测相对方位变化等。例如,如果你正在开发一个游戏,一个增强现实应用,一个二维或三维罗盘,或者一个相机稳定应用程序,旋转矢量传感器是最理想的。在大多数情况下,使用这些传感器是比使用accele更好的选择。
Android开源项目(AOSP)提供了三个基于软件的运动传感器:重力传感器、线性加速度传感器和旋转矢量传感器。这些传感器是在Android 4.0中更新的,现在使用一个设备的陀螺仪(除了其他传感器)来提高稳定性和性能。如果您想尝试这些传感器,您可以通过使用getVendor()方法和getVersion()方法来识别它们(供应商是谷歌LLC;版本号是3)。根据厂商和版本号识别这些传感器是必要的,因为Android系统考虑这三个传感器。
重力传感器
重力传感器提供了一个三维矢量,表示重力的方向和大小。通常,该传感器用于确定设备在空间中的相对方位。下面的代码向您展示了如何获得默认重力传感器的实例。
private SensorManager mSensorManager; private Sensor mSensor; ... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
线性加速度传感器
private SensorManager mSensorManager; private Sensor mSensor; ... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
当你想获得加速度数据而不受重力影响时,你通常会使用这个传感器。例如,你可以用这个传感器来看看你的车开得有多快。线性加速度传感器总是有一个偏移量,你需要移除它。最简单的方法是在应用程序中构建一个校准步骤。在校准过程中,您可以要求用户将设备设置在一个表上,然后读取所有三个轴的偏移量。然后你可以从加速度传感器的直接读数中减去偏移量来得到实际的线性加速度。
private SensorManager mSensorManager; private Sensor mSensor; ... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
旋转向量的大小等于sin(θ/2),旋转向量的方向等于旋转轴的方向。
重要的运动传感器触发一个事件,每一次重要的运动被检测到,然后它将自己禁用。一个重要的运动是一个可能导致用户位置改变的运动;例如散步,骑自行车,或坐在移动的汽车里。下面的代码向您展示了如何获得默认重要运动传感器的实例,以及如何注册一个事件监听器。
TriggerEventListener
.
private SensorManager mSensorManager; private Sensor mSensor; private TriggerEventListener mTriggerEventListener; ... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION); mTriggerEventListener = new TriggerEventListener() { @Override public void onTrigger(TriggerEvent event) { // Do work } }; mSensorManager.requestTriggerSensor(mTriggerEventListener, mSensor);
private SensorManager mSensorManager; private Sensor mSensor; ... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
Context.getSystemService(Context.JOB_SCHEDULER_SERVICE)
.
Context.getSystemService(Class)
通过参数
JobScheduler.class
或者
Context.getSystemService(String)
通过参数
Context.JOB_SCHEDULER_SERVICE
.
private SensorManager mSensorManager; private Sensor mSensor; ... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
public void onSensorChanged(SensorEvent event){ // In this example, alpha is calculated as t / (t + dT), // where t is the low-pass filter's time-constant and // dT is the event delivery rate. final float alpha = 0.8; // Isolate the force of gravity with the low-pass filter. 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]; // Remove the gravity contribution with the high-pass filter. linear_acceleration[0] = event.values[0] - gravity[0]; linear_acceleration[1] = event.values[1] - gravity[1]; linear_acceleration[2] = event.values[2] - gravity[2]; }
注意:您可以使用许多不同的技术来过滤传感器数据。上面的代码示例使用一个简单的过滤器常量(alpha)来创建一个低通滤波器。这个过滤器常数来自于一个时间常数(t),它是过滤器增加传感器事件的延迟的粗略表示,以及传感器的事件传递率(dt)。代码示例使用的alpha值为0.8,用于演示目的。如果您使用这个过滤方法,您可能需要选择一个不同的alpha值。
加速度计使用标准的传感器坐标系统。在实际操作中,这意味着当一个设备在其自然方向上平躺在一个桌子上时,应用以下条件。如果你设备推左边(所以它向右移动),x的加速度值是正的。如果你将设备推底部(它会远离你),y加速值是正的。如果你用加速度A把设备推向天空。z加速度值等于A + 9。81。
一般来说,如果你在监测设备的运动,加速度计是一个很好的传感器。几乎所有搭载安卓系统的手机和平板电脑都有一个加速计,它的功率比其他运动传感器低10倍。一个缺点是你可能必须实现低通和高通滤波器来消除重力和减少噪音。
陀螺仪测量旋转的速度
传感器的坐标系统与用于加速度传感器的坐标系相同。逆时针方向旋转是正的;也就是说,观察者从某一位置上的x、y或z轴上的某一位置观察到,如果设备出现逆时针方向旋转,就会报告正旋转。这是正旋转的标准数学定义,与方向传感器所使用的滚动定义不一样。
// Create a constant to convert nanoseconds to seconds. 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 timestep'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 // (that is, EPSILON should represent your maximum allowable margin of error) if (omegaMagnitude > EPSILON) { axisX /= omegaMagnitude; axisY /= omegaMagnitude; axisZ /= omegaMagnitude; } // Integrate around this axis with the angular speed by the timestep // in order to get a delta rotation from this sample over the timestep // 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 | Sensor event data | Description | Units of measure |
---|---|---|---|
TYPE_ACCELEROMETER |
SensorEvent.values[0] |
Acceleration force along the x axis (including gravity). | m/s2 |
SensorEvent.values[1] |
Acceleration force along the y axis (including gravity). | ||
SensorEvent.values[2] |
Acceleration force along the z axis (including gravity). | ||
TYPE_ACCELEROMETER_UNCALIBRATED |
SensorEvent.values[0] |
Measured acceleration along the X axis without any bias compensation. | m/s2 |
SensorEvent.values[1] |
Measured acceleration along the Y axis without any bias compensation. | ||
SensorEvent.values[2] |
Measured acceleration along the Z axis without any bias compensation. | ||
SensorEvent.values[3] |
Measured acceleration along the X axis with estimated bias compensation. | ||
SensorEvent.values[4] |
Measured acceleration along the Y axis with estimated bias compensation. | ||
SensorEvent.values[5] |
Measured acceleration along the Z axis with estimated bias compensation. | ||
TYPE_GRAVITY |
SensorEvent.values[0] |
Force of gravity along the x axis. | m/s2 |
SensorEvent.values[1] |
Force of gravity along the y axis. | ||
SensorEvent.values[2] |
Force of gravity along the z axis. | ||
TYPE_GYROSCOPE |
SensorEvent.values[0] |
Rate of rotation around the x axis. | rad/s |
SensorEvent.values[1] |
Rate of rotation around the y axis. | ||
SensorEvent.values[2] |
Rate of rotation around the z axis. | ||
TYPE_GYROSCOPE_UNCALIBRATED |
SensorEvent.values[0] |
Rate of rotation (without drift compensation) around the x axis. | rad/s |
SensorEvent.values[1] |
Rate of rotation (without drift compensation) around the y axis. | ||
SensorEvent.values[2] |
Rate of rotation (without drift compensation) around the z axis. | ||
SensorEvent.values[3] |
Estimated drift around the x axis. | ||
SensorEvent.values[4] |
Estimated drift around the y axis. | ||
SensorEvent.values[5] |
Estimated drift around the z axis. | ||
TYPE_LINEAR_ACCELERATION |
SensorEvent.values[0] |
Acceleration force along the x axis (excluding gravity). | m/s2 |
SensorEvent.values[1] |
Acceleration force along the y axis (excluding gravity). | ||
SensorEvent.values[2] |
Acceleration force along the z axis (excluding gravity). | ||
TYPE_ROTATION_VECTOR |
SensorEvent.values[0] |
Rotation vector component along the x axis (x * sin(θ/2)). | Unitless |
SensorEvent.values[1] |
Rotation vector component along the y axis (y * sin(θ/2)). | ||
SensorEvent.values[2] |
Rotation vector component along the z axis (z * sin(θ/2)). | ||
SensorEvent.values[3] |
Scalar component of the rotation vector ((cos(θ/2)).1 | ||
TYPE_SIGNIFICANT_MOTION |
N/A | N/A | N/A |
TYPE_STEP_COUNTER |
SensorEvent.values[0] |
Number of steps taken by the user since the last reboot while the sensor was activated. | Steps |
TYPE_STEP_DETECTOR |
N/A | N/A | N/A |