android传感器介绍

因为墙的原因翻译备用,先贴上原文链接: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);
单位与加速度传感器使用的单位相同。
Note:当设备处于静止状态时,重力传感器的输出应与加速度计的输出相同。


线性加速度传感器

线性加速度传感器为你提供一个三维矢量,表示沿每个设备轴的加速度,不包括重力。您可以使用此值执行手势检测。该值还可

以作为惯性导航系统的输入,该系统使用了“死计算”。下面的代码向您展示了如何获得默认的线性加速传感器的实例。

private SensorManager mSensorManager;
private Sensor mSensor;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

从概念上讲,该传感器根据以下关系为您提供加速度数据。
线性加速度=加速度-重力加速度。

当你想获得加速度数据而不受重力影响时,你通常会使用这个传感器。例如,你可以用这个传感器来看看你的车开得有多快。线性加速度传感器总是有一个偏移量,你需要移除它。最简单的方法是在应用程序中构建一个校准步骤。在校准过程中,您可以要求用户将设备设置在一个表上,然后读取所有三个轴的偏移量。然后你可以从加速度传感器的直接读数中减去偏移量来得到实际的线性加速度。

传感器坐标系统与加速度传感器所使用的系统是一样的,测量单位也是一样。


旋转矢量传感器

旋转矢量代表设备的结合的方向角和轴的设备通过旋转一个角度θ绕轴(x,y,或z)。下面的代码显示了如何获得一个默认的旋转矢量传感器的实例

private SensorManager mSensorManager;
private Sensor mSensor;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
旋转向量的三个元素表示如下。

旋转向量的大小等于sin(θ/2),旋转向量的方向等于旋转轴的方向。

旋转向量的三个元素等于一个单位四元数的最后三个分量(cos(θ/2), x*sin(θ/2), y*sin(θ/2), z*sin(θ/2))。
旋转向量的元素是无单位的。x、y和z轴的定义与加速度传感器相同。参考坐标系统被定义为一个直接的标准正交基(见图1)。该坐标系具有以下特征。

X被定义为矢量积Y x Z,它与设备当前位置上的地面相切,并指向近东。
Y与设备当前位置的地面相切,指向地磁北极。
Z指向天空,垂直于地面。
Android SDK提供了一个示例应用程序,演示了如何使用旋转矢量传感器。示例应用程序位于API Demos代码(OS - RotationVectorDemo)中。

使用重要的运动传感器

重要的运动传感器触发一个事件,每一次重要的运动被检测到,然后它将自己禁用。一个重要的运动是一个可能导致用户位置改变的运动;例如散步,骑自行车,或坐在移动的汽车里。下面的代码向您展示了如何获得默认重要运动传感器的实例,以及如何注册一个事件监听器。

有关更多信息,请参见 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);

step counter传感器提供了用户自上一次重新启动时所采取的步骤,而传感器被激活。step counter传感器有更多的延迟(最多10秒),但是比step detector传感器的精度更高。下面的代码向您展示了如何获得默认step counter传感器的实例
private SensorManager mSensorManager;
private Sensor mSensor;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
为了在运行你的应用程序的设备上保存电池,你应该使用JobScheduler类在特定的时间间隔内从步骤计数器传感器获取当前值。虽然不同类型的应用程序需要不同的传感器读取时间间隔,但你应该尽可能长时间间隔,除非你的应用需要来自传感器的实时数据。
JobScheduler这是一个API,用于调度各种类型的作业,而不是在应用程序自己的过程中执行的框架。
请参见JobInfo,以了解可以运行的作业类型以及如何构造它们。您将构建这些JobInfo对象,并将其传递给调度程序(JobInfo)。当所声明的标准被满足时,系统将在您的应用程序的JobService上执行该作业。当使用JobInfo构造JobInfo时,您可以识别实现工作逻辑的服务组件。android.content.ComponentName Builder(int)。
该框架在执行作业时将是明智的,并尝试批处理并尽可能地推迟它们。通常,如果您没有指定作业的最后期限,则可以根据JobScheduler的内部队列的当前状态随时运行。

您不能直接实例化该类;相反,通过检索它 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);
从概念上讲,加速度传感器通过测量作用于传感器本身的力决定应用于设备的加速度。
然而,重力总是影响测量的加速度。
由于这个原因,当设备放在桌子上(而不是加速)时,加速度计读数为g = 9.81 。类似地,当设备处于自由落体状态时,因此在9.81处快速地向地面加速。它的加速度计读数为g = 0。 因此,为了测量设备的真实加速度,必须从加速度计数据中移除重力的贡献。这可以通过应用高通滤波器来实现。相反,低通滤波器可以用来隔离重力。下面的示例展示了如何实现这一点。
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;
   }
}

标准陀螺仪提供原始的旋转数据,没有任何过滤或校正的噪音和漂移(偏差)。在实践中,陀螺仪的噪声和漂移将引入需要补偿的误

差。你通常通过监测其他传感器(如重力传感器或加速计)来确定漂移(偏差)和噪音。

Uncalibrated Gyroscope
未校准的陀螺仪与陀螺仪相似,只是没有将陀螺漂移补偿应用于旋转速度。工厂校准和温度补偿仍然适用于旋转速度。未校准的

陀螺仪对后处理和融合方向数据很有用。一般来说,陀螺仪的事件。值[0]将接近未校准的陀螺仪事件。值[0]-未校准的陀螺仪事件。

值[3]。

校准 ~=未校准的 - 偏差估计。

Android加速器Android BatchStepSensor示例进一步演示了在这一页上覆盖的api的使用情况

下面是传感器参数介绍

Table 1. Motion sensors that are supported on the Android platform.

SensorSensor event dataDescriptionUnits of measure
TYPE_ACCELEROMETERSensorEvent.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_UNCALIBRATEDSensorEvent.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_GRAVITYSensorEvent.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_GYROSCOPESensorEvent.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_UNCALIBRATEDSensorEvent.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_ACCELERATIONSensorEvent.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_VECTORSensorEvent.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_MOTIONN/AN/AN/A
TYPE_STEP_COUNTERSensorEvent.values[0]Number of steps taken by the user since the last reboot while the sensor was activated.Steps
TYPE_STEP_DETECTORN/AN/AN/A

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值