I'm trying to implement a simple fall detection algorithm using android's accelerometer.
override fun onSensorChanged(event: SensorEvent?)
{
x=event!!.values[0].toDouble()
y=event!!.values[1].toDouble()
z=event!!.values[2].toDouble()
var sqroot= sqrt(x.pow(2) + y.pow(2)+z.pow(2))
textView.setText("x= ${event!!.values[0]}\n\n"+"y= ${event!!.values[1]}\n\n\n"+
"z= ${event!!.values[2]}"+"\n\n\n acceleration=$sqroot")
if (sqroot<3 && minv==false && status==false)
{
minv=true
lasttime=System.currentTimeMillis()
Log.i("min thresholds","free fall to ground $sqroot" )
Toast.makeText(this,"free fall to ground $sqroot",Toast.LENGTH_SHORT).show()
}
if (minv)
{
counter++
if (sqroot>=30 && maxv==false && status==false) {
newtime = System.currentTimeMillis()
diff = newtime - lasttime
Toast.makeText(this,"last:$lasttime && new:$newtime \n diff:$diff\n\n $sqroot",Toast.LENGTH_LONG).show()
if (diff > 150 && diff<9000)
{
maxv = true
Log.i("hit the ground", "hiting to ground $sqroot")
Toast.makeText(this, "hit to ground $sqroot", Toast.LENGTH_SHORT).show()
status = true
}
}
}
if(maxv==true && minv==true && status==true)
{
Log.i("fall detected ","fall detected $sqroot")
Toast.makeText(this,"fall detected $sqroot",Toast.LENGTH_SHORT).show()
textView2.text="x=$x\ny=$y\nz=$z \n acc=$sqroot \n\n\n last:$lasttime && new:$newtime \n" +
" diff:$diff"
x=0.0
y=0.0
z=0.0
minv=false
maxv=false
status=false
}
if (counter>10)
{
counter=0
minv=true
maxv=true
}
}
I am using simple threshold value system to detect the fall and addition on a time system mean I fall free time to hit the ground time difference is >900 millisecond and <9000 millisecond then fall alert
but I am not getting the required result can you please give the suggestion whats is wrong with this code?
You may try this library:
https://github.com/tompadree/freeFall/tree/master/frefalllib
Usage:
FreeFallService.startService(context,
object : OnSensorChanged {
override fun onFall(fallObject: FallObject) {
// code when free fall is detected
}
},
object : OnFallsListFetch {
override fun onFallsFetch(fallsList: ArrayList<FallObject>) {
// handling call for fetching all events from the db
}
})