gpt4 book ai didi

java - 如何使用加速度计检测1Gs以上的振动?

转载 作者:太空狗 更新时间:2023-10-29 14:31:49 24 4
gpt4 key购买 nike

我正在做一个类似于汽车黑匣子的 android 应用程序,它记录了汽车的行驶过程。

但现在我面临的问题是如何集成一个能够检测轻微运动(可能> 1Gs)的加速度计,当发生事故时它可能会触发视频录制停止并将其保存到存档文件,因此不会因事故而丢失文件。任何人都知道如何执行上述任务来监控任何形式的振动?

我是 android/java 的新手,有人可以帮助指导我吗?提前致谢...

这是视频录制部分的一部分,但现在我要如何将加速度计用于“自动存档”目的?

最佳答案

需要使用接口(interface)SensorEventListeneronSensorChanged(SensorEvent event)回调

在此处获取SensorEvent 类的详细信息:http://developer.android.com/reference/android/hardware/SensorEvent.html

IBM 的 developerWorks 页面上有一个示例:http://www.ibm.com/developerworks/opensource/library/os-android-sensor/index.html


来自 android 引用页面:

public final float[]

values Since: API Level 3

The length and contents of the values array depends on which sensor type is being monitored (see also SensorEvent for a definition of the coordinate system used). Sensor.TYPE_ACCELEROMETER: All values are in SI units (m/s^2)

values[0]: Acceleration minus Gx on the x-axis

values[1]: Acceleration minus Gy on the y-axis

values[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];
}

关于java - 如何使用加速度计检测1Gs以上的振动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5891550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com