Reputation: 1061
I'm doing android application which is something like a car blackbox which records the traveling process of the car.
But now I'm face with the problem of how am i going to integrate an accelerometer which is capable of detecting slight movement (Probably > 1Gs) when an accident occur it could trigger the video recording to stop and saving it to the Archive file, thus not losing the file as a result to the accident.. Anyone knows how to do the above mention task to monitor any forms of vibration?
I'm new to android/java could someone help guide me along? Thanks in advance...
This is part of the section of the video recording but now how am i going to incorporate accelerometer for "Auto-Archiving" purposes?
Upvotes: 3
Views: 2811
Reputation: 40391
You need to use onSensorChanged(SensorEvent event)
callback of the interface SensorEventListener
Get the details of the SensorEvent
class here: http://developer.android.com/reference/android/hardware/SensorEvent.html
There's an example on the IBM's developerWorks page: http://www.ibm.com/developerworks/opensource/library/os-android-sensor/index.html
From the android reference pages:
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-axisA 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];
}
Upvotes: 1