Android 下陀螺仪、加速度和磁场传感器数据定义
来源:互联网 发布:c语言中使用thumb指令 编辑:程序博客网 时间:2024/05/17 09:44
摘自Android SensorEvent.java代码注释
Sensor.TYPE_ACCELEROMETER(加速度传感器类型)
All values are in SI units (m/s^2)
values[0]: Acceleration minus Gx on the x-axisvalues[1]: Acceleration minus Gy on the y-axisvalues[2]: Acceleration minus Gz on the z-axis
A sensor of this type measures the acceleration applied to the device (Ad). Conceptually, it does so by measuring forces applied to the sensor itself (Fs) using the relation:
Ad = - ∑Fs / mass
In particular, the force of gravity is always influencing the measured acceleration:
Ad = -g - ∑F / mass
For this reason, when the device is sitting on a table (and obviously not accelerating), the accelerometer reads a magnitude of g = 9.81 m/s^2
Similarly, when the device is in free-fall and therefore dangerously accelerating towards to ground at 9.81 m/s^2, its accelerometer reads a magnitude of 0 m/s^2.
It should be apparent that in order to measure the real acceleration of the device, the contribution of the force of gravity must be eliminated. This can be achieved by applying a high-pass filter. Conversely, a low-pass filter can be used to isolate the force of gravity.
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]; }
Examples:
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).
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-axisvalues[1]: Angular speed around the y-axisvalues[2]: Angular speed around the z-axis
Typically the output of the gyroscope is integrated over time to calculate a rotation describing the change of angles over the timestep, for example:
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 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; }
In practice, the gyroscope noise and offset will introduce some errors which need to be compensated for. This is usually done using the information from other sensors, but is beyond the scope of this document.
- Android 下陀螺仪、加速度和磁场传感器数据定义
- 【Android】传感器(线性加速度、陀螺仪、磁场)信号实时获取,实时监测指定wifi、蓝牙信号强度
- 采集手机加速度、陀螺仪、磁场
- Android_开发 Android传感器(加速度传感器,磁场传感器,光线传感器,方向传感器)
- android使用磁场传感器和加速度传感器确定当前朝向(即:方位角),以及常见问题的解决办法
- 加速度及陀螺仪传感器BMI160
- 谈谈对陀螺仪和加速度传感器的感性认识
- 谈谈对陀螺仪和加速度传感器的感性认识
- Pro Android学习笔记(一五六):传感器(6): 磁场传感器和方位(下)
- Pro Android学习笔记(一五六):传感器(6): 磁场传感器和方位(下)
- CMMotionManager加速度和陀螺仪
- android 磁场传感器应用
- Android传感器之磁场传感器
- Android基础入门教程——10.12 传感器专题(3)——加速度-陀螺仪传感器
- 手机中的传感器:重力感应器、加速度传感器、陀螺仪、电子罗盘和光线距离感应器
- 手机中的传感器:重力感应器、加速度传感器、陀螺仪、电子罗盘和光线距离感应器
- 加速度、陀螺仪、电源、距离传感器的使用方法
- Android传感器学习之加速度传感器数据获取
- Delphi对Excel的所有操作(一)
- session
- eclipse中文变乱码原因及解决办法
- swift单例的创建与使用
- linux kernel的中断子系统之(九):tasklet
- Android 下陀螺仪、加速度和磁场传感器数据定义
- 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- ViewPager
- 回调方法介绍之中国好室友篇(Java示例)
- 14.3.2.3 Consistent Nonlocking Reads 一致性非锁定读
- 如何修改NSNotificationCenter addObsever方法的实现
- PyCharm中使用virtualenv进行django开发
- 数据结构与算法:排序
- appium自动测试化相关