How to use the accelerometer to detect virbration above or equal to 2Gs?
I'm doing android application which is something like a car "blackbox" which records the traveling process of the car.
But I'm face with the problem of how am i going to integrate an accelerometer which is capable of detecting movement (Probably >= 2Gs) when an accident occur then it should trigger the video recording to stop and saving it to the Archive file, thus not losing th开发者_Python百科e file as a result of the accident.. Anyone knows how to do the above mention task, i'm rather needing urgent help here please! I've read android developer on accelerometer and its not helping in my situation here first i'm rather bad in physics second i'm new to android/java and my first attempt working with the accelerometer? Any simple solution? 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?
A couple of points: The Bosch BMA150 is used in many smartphones with 2g set as the maximum acceleration value (so you might never see >2g). With SENSOR_DELAY_FASTEST you can take readings about every 20 milliseconds on an HTC Desire. However, since you have a multi-tasking operating system on the phone, you cannot guarantee this timing (delays of a couple of seconds might occur when the operating system is busy).
Hence a smartphone is currently not really suitable for this application. If Android allows smarter use of accelerometers in future this could change. If onSensorChanged was allowed a threshold parameter then accelerations exceeding this threshold could be buffered in the accelerometer chip's memory and read out when appropriate.
Put your startRecording() method in the Activity below, it's called when acceleration exceeds 2G, you can change this by changing the value of CRASH_G_MULTIPLIER
public class YourActivity extends Activity {
private double G = 9.81;
private double CRASH_G_MULTIPLIER = 2;
@Override public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startDropListening();
}
private void startRecording(){
// your code that does the recording here
}
private void startDropListening(){
SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
sm.registerListener(
new SensorEventListener(){
@Override public void onAccuracyChanged(Sensor arg0, int arg1) {}
@Override public void onSensorChanged(SensorEvent arg0) {
double accel = Math.sqrt(
Math.pow(arg0.values[0], 2) +
Math.pow(arg0.values[1], 2) +
Math.pow(arg0.values[2], 2));
if (accel > G * CRASH_G_MULTIPLIER){
startRecording();
}
}
},
sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
}
There are some things you should be aware of:
As mentioned by others, the value of 2 needs to be increased quite substantially, but you can fine tune this yourself by experimentation.
You will want to acquire a wake lock. To create a wake lock, do this:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ATag"); wl.acquire();
And the following when you are finished:
wl.release();
(You may want to change FULL_WAKE_LOCK to a lower priority wake lock). (You will also need to add the appropriate permission to your manifest file for the wake lock)
You may wish to increase the sample rate. This will drain battery significantly though. There are different values you can assign to it, in the code above, replace:
SensorManager.SENSOR_DELAY_NORMAL
with any of these:
SensorManager.SENSOR_DELAY_UI
SensorManager.SENSOR_DELAY_GAME
SensorManager.SENSOR_DELAY_FASTEST
精彩评论