橘子味的心
标题:9.8 Android运动传感器开发

目前,Android 平台支持的运动传感器包括以下 5 种:
  • TYPE_ACCELEROMETER。
  • TYPE_GRAVITY。
  • TYPE_GYROSCOPE。
  • TYPE_LINEAR_ACCELERATION。
  • TYPE_ROTATION_VECTOR。

本节教程将对这几种传感器的用法做简单介绍。

加速度传感器

获取加速度传感器实例的代码如下:
  1. private SensorManager mSensorManager;
  2. private Sensor mSensor;
  3. ...
  4. mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
  5. mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
从传感器获取数据并计算三个方向的加速度的代码如下:
  1. public void onSensorChanged(SensorEvent event) {
  2. // In this example, alpha is calculated as t / (t+dT) ,
  3. // where t is the low-pass filter's time-constant and
  4. // dT is the event delivery rate.
  5.  
  6. final float alpha = 0.8;
  7.  
  8. // Isolate the force of gravity with the low-pass filter.
  9. gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
  10. gravity[1] = alpha * gravity[l] + (1 - alpha) * event.values[1];
  11. gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
  12.  
  13. // Remove the gravity contribution with the high-pass filter.
  14. linear_acceleration[0] = event.values[0] - gravity[0];
  15. linear_acceleration[1] = event.values[1] - gravity[l];
  16. linear_acceleration[2] = event.values[2] - gravity[2];
  17. }
该计算方法仅是举例使用,实际计算方法要针对应用而确定。

重力传感器

重力传感器是加速度传感器的一种,其数据处理方式也相似。此处不再重复重力传感器的数据计算方法。获取重力传感器的代码如下:
  1. private SensorManager mSensorManager;
  2. private Sensor mSensor;
  3. ...
  4. mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
  5. mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

陀螺仪

陀螺仪可以在三个纬度上测量设备的旋转情况。获取陀螺仪传感器的代码如下:
  1. private SensorManager mSensorManager;
  2. private Sensor mSensor;
  3. ...
  4. mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
  5. mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
从陀螺仪数据计算三个纬度旋转情况的代码如下:
  1. // Create a constant to convert nanoseconds to seconds.
  2. private static final float NS2S = 1.0f / 1000000000.0f;
  3. private final float[] deltaRotationVector = new float[4]();
  4. private float timestamp;
  5.  
  6. public void onSensorChanged(SensorEvent event) {
  7. // This timestep's delta rotation to be multiplied by the current rotation
  8. // after computing it from the gyro sample data.
  9. if (timestamp != 0) {
  10. final float dT = (event.timestamp - timestamp) * NS2S;
  11. // Axis of the rotation sample, not normalized yet.
  12. float axisX = event.values[0];
  13. float axisY = event.values[1];
  14. float axisZ = event.values[2];
  15.  
  16. // Calculate the angular speed of the sample
  17. float omegaMagnitude = sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ);
  18.  
  19. // Normalize the rotation vector if it's big enough to get the axis
  20. // (that is, EPSILON should represent your maximum allowable margin of error)
  21. if (omegaMagnitude > EPSILON) {
  22. axisX /= omegaMagnitude;
  23. axisY /= omegaMagnitude;
  24. axisZ /= omegaMagnitude;
  25. }
  26. // Integrate around this axis with the angular speed by the timestep
  27. // in order to get a delta rotation from this sample over the timestep
  28. // We will convert this axis-angle representation of the delta rotation
  29. // into a quaternion before turning it into the rotation matrix.
  30. float thetaOverTwo = omegaMagnitude * dT / 2. Of;
  31. float sinThetaOverTwo = sin(thetaOverTwo);
  32. float cosThetaOverTwo = cos(thetaOverTwo);
  33. deltaRotationVector[0] = sinThetaOverTwo * axisX;
  34. deltaRotationVector[1] = sinThetaOverTwo * axisY;
  35. deltaRotationVector[2] = sinThetaOverTwo * axisZ;
  36. deltaRotationVector[3] = cosThetaOverTwo;
  37. }
  38. timestamp = event.timestamp;
  39. float[] deltaRotationMatrix = new float[9];
  40. SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);
  41. // User code should concatenate the delta rotation we computed with the current rotation
  42. // in order to get the updated rotation.
  43. // rotationCurrent=rotationCurrent * deltaRotationMatrix;
  44. }

线性加速度传感器

线性加速度传感器是传感器的一种。其获取实例的代码如下:
  1. private SensorManager mSensorManager;
  2. private Sensor mSensor;
  3. ...
  4. mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
  5. mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

旋转向量传感器

旋转向量传感器能反映出当前设备的状态,其返回值是旋转角度与旋转轴的集合。获取旋转向量传感器实例的相关代码如下:
  1. private SensorManager mSensorManager;
  2. private Sensor mSensor;
  3. ....
  4. mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
  5. mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);