使用加速度計
加速度感測器測量施加到裝置的加速度,包括重力。以下程式碼展示如何獲取預設加速感測器的例項:
private SensorManager sensorManager;private Sensor sensor; ...sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
從概念上講,加速度感測器透過使用以下關係測量施加到感測器本身的力 (Fs) 來確定施加到裝置的加速度 (Ad): A_D=-(1/mass)∑F_S
但是,重力始終根據以下關係影響測量的加速度: A_D=-g-(1/mass)∑F_S
因此,當裝置位於桌子上(不加速)時,加速度計的讀數為 g = 9.81 m/s2。同樣,當裝置自由落體並因此以 9.81 m/s2 的速度快速向地面加速時,其加速度計的讀數為 g = 0 m/s2。因此,要測量裝置的實際加速度,必須從加速度計資料中移除重力的作用。這可以透過應用高通濾波器來實現。相反,您可以使用低通濾波器來隔離重力。以下示例展示如何執行此操作:
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)。該程式碼示例使用 0.8 的 alpha 值進行演示。如果您使用此過濾方法,則可能需要選擇其他 alpha 值。 加速度計使用標準的感測器座標系。實際上,這意味著當裝置以自然螢幕方向平放在桌子上時,以下條件適用:
>如果將裝置推向左側(因此向右移動),則 x 加速度值為正。>如果將裝置推到底部(因此它向遠離您的方向移動),則 y 加速度值為正。>如果您以 A m/s2 的加速度將裝置推向天空,則 z 加速度值等於 A + 9.81,該值對應裝置的加速度 (+A m/s2) 減去重力 (-9.81 m/s2)。>固定裝置的加速度值為 +9.81,該值對應裝置的加速度(0 m/s2 減去重力 -9.81 m/s2)。
通常,如果要監控裝置的運動,加速度計是一個很好的感測器。幾乎所有執行 Android 的手機和平板電腦都具有加速度計,其功耗比其他運動感測器低約 10 倍。一個缺點是您可能必須實現低通和高通濾波器,以消除重力並降低噪聲。
作為對比的英文版 Use the accelerometer
Demo
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];}
Note: You can use many different techniques to filter sensor data. The code sample above uses a simple filter constant (alpha) to create a low-pass filter. This filter constant is derived from a time constant (t), which is a rough representation of the latency that the filter adds to the sensor events, and the sensor's event delivery rate (dt). The code sample uses an alpha value of 0.8 for demonstration purposes. If you use this filtering method you may need to choose a different alpha value.Accelerometers use the standard sensor coordinate system. In practice, this means that the following conditions apply when a device is laying flat on a table in its natural orientation:If you push the device on the left side (so it moves to the right), the x acceleration value is positive. If you push the device on the bottom (so it moves away from you), the y acceleration value is positive. If you push the device toward the sky with an acceleration of A m/s2, the z acceleration value is equal to A + 9.81, which corresponds to the acceleration of the device (+A m/s2) minus the force of gravity (-9.81 m/s2). The stationary device will have an acceleration value of +9.81, which corresponds to the acceleration of the device (0 m/s2 minus the force of gravity, which is -9.81 m/s2).you might have to implement low-pass and high-pass filters to eliminate gravitational forces and reduce noise.Google提供了一個Demo來演示
* This is an example of using the accelerometer to integrate the device's * acceleration to a position using the Verlet method. This is illustrated with * a very simple particle system comprised of a few iron balls freely moving on * an inclined wooden table. The inclination of the virtual table is controlled * by the device's accelerometer. https://github.com/googlearchive/android-AccelerometerPlay
最新評論