因为墙的原因翻译备用,先贴上原文链接: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() {@Overridepublic 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 samplefloat 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.

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

android传感器介绍相关推荐

  1. Android传感器介绍及指南针的实现

    Android平台支持的丰富的传感器是其亮点之一,虽然相比iPhone来说稍有逊色,但相对于原来占据智能市场的Synbian等手机平台有一个明显的飞跃.我们现在看到的旅游出行必备的指南针,甩一甩就显示 ...

  2. Android 传感器介绍

    在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. #define SENSOR_TYPE_ACCELEROMETER 1 //加速度 #define ...

  3. Android传感器系列介绍-刘宇

    Android官方给我们提供一套Android sensor framework,俗称Android传感器框架,简称ASF.传感器分为动作传感器.位置传感器.环境传感器.由于传感器较多,使用过程差不多 ...

  4. [转帖]Android操作系统11种传感器介绍

    Android操作系统11种传感器介绍 在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. #define SENSOR_TYPE_ACCELEROME ...

  5. MTK Android操作系统11种传感器介绍

    Android操作系统11种传感器介绍 1 加速度传感器 2 磁力传感器 3 方向传感器 4 陀螺仪传感器 5 光线感应传感器 6 压力传感器 7 温度传感器 8 接近传感器 9 重力传感器 10 线 ...

  6. 【转】深入探讨 Android 传感器

    Android 是一个面向应用程序开发的富平台,它拥有许多具有吸引力的用户界面元素和数据管理功能.Android 还提供了一组丰富的接口选项.在本文中,学习如何配合使用 Android 的各种传感器选 ...

  7. Android传感器-开发指南

    大部分Android平台的设备都带有多个传感器,使你能监视其方位和运动状态的变化.很多设备还有其它类型的传感器,让你能感知周围的环境条件,比如温度.压力.湿度和光线.你可以利用Android的传感器框 ...

  8. 关于Android 传感器坐标与读数的进一步讨论

    关于Android 传感器坐标与读数的进一步讨论 之前一些相关的讨论见: Android Sensors (3) 传感器坐标系统 Android Sensors (5) 运动传感器 Motion Se ...

  9. android 传感器 balance filter,Android 传感器 API.doc

    Android 传感器 API 1.1 序言 ? ?[Android蛋蛋网]在这一章我们主要针对Android的传感器(Sensor)进行说明.如果说Java的程序和硬件有些远的感觉,那么可以说传感器 ...

最新文章

  1. Resource punkt not found.nltk.download()下载失败
  2. Gradle学习之基础篇
  3. java接口与类相同不同_浅谈java的接口和C++虚类的相同和不同之处
  4. Docker容器的迁移
  5. 矩阵分析理论在实际工程中的应用_【顶管技术在市政给排水工程中的应用分析】...
  6. Android WebView 调起H5支付,提示商家参数格式有误
  7. SAP BC470 课程中文自学笔记
  8. 一个声明被new多次
  9. 求出一个整型数组的最大子集和
  10. Solr搜索引擎——中文分词器
  11. SonarLint 默认扫描规则
  12. 使用phpQuery 抓取HTML 页面内容
  13. ubuntu版网易云音乐下载
  14. 【强连通分量】 Kosaraju和Tarjan算法 (标准模板+详细注释)
  15. QT入门学习(一):什么是QT
  16. 狭义相对论推导过程中的数学悖论--有没有人能解释下?
  17. 2021年口碑超好旗舰:蓝牙耳机排行榜10强推荐
  18. html5新年网页做给父母的,给父母的感谢信5篇模板
  19. c实现 图像dither算法_Atitit (Sketch Filter)素描滤镜的实现  图像处理  attilax总结...
  20. 计算机视觉论文-2021-06-04

热门文章

  1. TCP之长连接与短连接
  2. 解决Ubuntu系统下启动root账户后Linux版本edge浏览器无法启动
  3. GPS周和周内秒转公历时间 以及 公历时间转GPS周和周内秒 (python版)
  4. 两种方式实现矩阵键盘扫描(含程序)
  5. 意外发现一款很好用的开源BI工具——DataEase
  6. 单片机中常用的串口通信协议帧
  7. java 获取office文件页数_jacob如何获取word文档的页码
  8. windows下Ardupilot编译环境搭建
  9. 一个refine/refactor的例子
  10. canvas-实现放大镜效果