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),旋转向量的方向等于旋转轴的方向。
旋转向量的元素是无单位的。x、y和z轴的定义与加速度传感器相同。参考坐标系统被定义为一个直接的标准正交基(见图1)。该坐标系具有以下特征。
重要的运动传感器触发一个事件,每一次重要的运动被检测到,然后它将自己禁用。一个重要的运动是一个可能导致用户位置改变的运动;例如散步,骑自行车,或坐在移动的汽车里。下面的代码向您展示了如何获得默认重要运动传感器的实例,以及如何注册一个事件监听器。
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);
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 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]。
校准 ~=未校准的 - 偏差估计。
下面是传感器参数介绍
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传感器介绍相关推荐
- Android传感器介绍及指南针的实现
Android平台支持的丰富的传感器是其亮点之一,虽然相比iPhone来说稍有逊色,但相对于原来占据智能市场的Synbian等手机平台有一个明显的飞跃.我们现在看到的旅游出行必备的指南针,甩一甩就显示 ...
- Android 传感器介绍
在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. #define SENSOR_TYPE_ACCELEROMETER 1 //加速度 #define ...
- Android传感器系列介绍-刘宇
Android官方给我们提供一套Android sensor framework,俗称Android传感器框架,简称ASF.传感器分为动作传感器.位置传感器.环境传感器.由于传感器较多,使用过程差不多 ...
- [转帖]Android操作系统11种传感器介绍
Android操作系统11种传感器介绍 在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. #define SENSOR_TYPE_ACCELEROME ...
- MTK Android操作系统11种传感器介绍
Android操作系统11种传感器介绍 1 加速度传感器 2 磁力传感器 3 方向传感器 4 陀螺仪传感器 5 光线感应传感器 6 压力传感器 7 温度传感器 8 接近传感器 9 重力传感器 10 线 ...
- 【转】深入探讨 Android 传感器
Android 是一个面向应用程序开发的富平台,它拥有许多具有吸引力的用户界面元素和数据管理功能.Android 还提供了一组丰富的接口选项.在本文中,学习如何配合使用 Android 的各种传感器选 ...
- Android传感器-开发指南
大部分Android平台的设备都带有多个传感器,使你能监视其方位和运动状态的变化.很多设备还有其它类型的传感器,让你能感知周围的环境条件,比如温度.压力.湿度和光线.你可以利用Android的传感器框 ...
- 关于Android 传感器坐标与读数的进一步讨论
关于Android 传感器坐标与读数的进一步讨论 之前一些相关的讨论见: Android Sensors (3) 传感器坐标系统 Android Sensors (5) 运动传感器 Motion Se ...
- android 传感器 balance filter,Android 传感器 API.doc
Android 传感器 API 1.1 序言 ? ?[Android蛋蛋网]在这一章我们主要针对Android的传感器(Sensor)进行说明.如果说Java的程序和硬件有些远的感觉,那么可以说传感器 ...
最新文章
- Resource punkt not found.nltk.download()下载失败
- Gradle学习之基础篇
- java接口与类相同不同_浅谈java的接口和C++虚类的相同和不同之处
- Docker容器的迁移
- 矩阵分析理论在实际工程中的应用_【顶管技术在市政给排水工程中的应用分析】...
- Android WebView 调起H5支付,提示商家参数格式有误
- SAP BC470 课程中文自学笔记
- 一个声明被new多次
- 求出一个整型数组的最大子集和
- Solr搜索引擎——中文分词器
- SonarLint 默认扫描规则
- 使用phpQuery 抓取HTML 页面内容
- ubuntu版网易云音乐下载
- 【强连通分量】 Kosaraju和Tarjan算法 (标准模板+详细注释)
- QT入门学习(一):什么是QT
- 狭义相对论推导过程中的数学悖论--有没有人能解释下?
- 2021年口碑超好旗舰:蓝牙耳机排行榜10强推荐
- html5新年网页做给父母的,给父母的感谢信5篇模板
- c实现 图像dither算法_Atitit (Sketch Filter)素描滤镜的实现 图像处理 attilax总结...
- 计算机视觉论文-2021-06-04