I'm writing Fall Detection application as a project for studies. I'm using accelerometer to detect impacts such as falls, jumps, and shakes, then I'm using barometer to check the height. If the height is bigger then 0.5 m between standing posture and lying posture after impact, it should detect it as fall. I have a problem with saving barometer's pressure value before Impact from accelerometer and compare it with current pressure to see the difference. Now I'm saving pressure value when Impact is detected and then later when currentstate is active or inactive. I need this to check the height and to see if the fall has really happend. Here is the code:
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
double ax = event.values[0];
double ay = event.values[1];
double az = event.values[2];
double acceleration = getMagnitude(ax, ay, az);
pushAcceleration(acceleration);
setKinematicState(accelerationFrame, ay);
/*
Checking Barometers Height in case of Falling
*/
if (currentState != previousState) {
if (currentState == KinematicState.FALL) {
PressuredataObject barometer;
barometer = pressuredataObjects.get(pressuredataObjects.size() - 1);
beforeFallAltitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, barometer.getAirPressure());
impactDetected = true;
}
if ((currentState == KinematicState.INACTIVE || currentState == KinematicState.ACTIVE) && impactDetected) {
PressuredataObject barometer;
barometer = pressuredataObjects.get(pressuredataObjects.size() - 1);
afterFallAltitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, barometer.getAirPressure());
float diffAltitude = beforeFallAltitude - afterFallAltitude;
float abs_diffAltitude = (diffAltitude < 0) ? -diffAltitude : diffAltitude;
if (abs_diffAltitude > 0.5) {
Log.d("abs_diffAltitude", "" + abs_diffAltitude);
state.setText("FALL DETECTED");
state.setTextColor(Color.parseColor("#FF0000"));
fallPlayer.start();
showDialog();
}
impactDetected = false;
}
systemState(currentState);
previousState = currentState;
}
}
if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
float[] values = event.values;
pressuredataObjects.add(new PressuredataObject(values[0], 0f, System.currentTimeMillis()));
if (pressuredataObjects.size() > BUFF_SIZE)
pressuredataObjects.remove(0);
PressuredataObject lastMeasure = pressuredataObjects.get(pressuredataObjects.size() - 1);
PressuredataObject medianValue = PressureUtilities.selectMedianValue(pressuredataObjects);
// Calculate speed and altitude
float speed = 0f;
float altitude = 0f;
if (pdoPrevious == null) {
medianValue.setSpeed(0);
} else {
speed = lastMeasure.getSpeed();
altitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, lastMeasure.getAirPressure());
}
pdoPrevious = medianValue;
}
}
And here you can see the functions for accelerometer
private void setKinematicState(List<Double> buffer, double accelerationY) {
int zrc = getMagnitudeValues(buffer);
if (zrc <= 3) {
currentState = KinematicState.INACTIVE;
} else if (zrc > 3 && zrc < 6) {
currentState = KinematicState.ACTIVE;
}
else if (zrc > 6) {
currentState = KinematicState.FALL;
}
}
private int getMagnitudeValues(List<Double> accelerationFrame) {
int count = 0;
for (int i = 1; i < accelerationFrame.size(); i++) {
if ((accelerationFrame.get(i - 1) - GRAVITY_ACC) < APPROXIMATION_ERROR
&& (accelerationFrame.get(i) - GRAVITY_ACC) > APPROXIMATION_ERROR) {
count++;
}
}
return count;
}
private double getMagnitude(double accelerationX, double accelerationY, double accelerationZ) {
return Math.sqrt(accelerationX * accelerationX
+ accelerationY * accelerationY + accelerationZ * accelerationZ);
}
private void pushAcceleration(double acceleration) {
accelerationFrame.add(acceleration);
if (accelerationFrame.size() > BUFF_SIZE)
accelerationFrame.remove(0);
}
Does anyone has any idea how to save previous pressure value just before the impact from accelerometer?