[使用Kotlin的Android跌倒检测:问题

问题描述 投票:0回答:1

我正在尝试使用android的加速度计实现一种简单的跌倒检测算法。

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
    }
}

我正在使用简单的阈值系统来检测跌倒并在一个时间系统上相加,这意味着我跌倒空闲时间到地面的时间差为> 900毫秒,<9000毫秒然后是跌倒警报

但是我没有得到所需的结果,您能否建议这个代码有什么问题?

android kotlin accelerometer sensor
1个回答
0
投票

您可以尝试使用此库:

https://github.com/tompadree/freeFall/tree/master/frefalllib

用法:

    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
            }
        })
© www.soinside.com 2019 - 2024. All rights reserved.