Android 跌倒检测问题
Posted
技术标签:
【中文标题】Android 跌倒检测问题【英文标题】:Android Fall Detection Issue 【发布时间】:2018-06-29 22:27:36 【问题描述】:我正在尝试使用 android 的accelerometer
实现一个简单的跌倒检测算法。
手机当前的加速度存储在mAccel
中,如果检测到突然抖动Timer t
开始。 Timer
内部有两个 CountDownTimers
。
firstTimer
用于在用户在过去 5 秒内没有移动时注册“跌倒”,secondTimer
用于在用户未按下按钮时确认跌倒(尚未实现) .
public void onSensorChanged(SensorEvent event)
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values.clone();
float x = mGravity[0];
float y = mGravity[1];
float z = mGravity[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = (float) Math.sqrt(x * x + y * y + z * z);
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;
mAccel = abs(mAccel);
textView.setText("a:"+mAccel);
if (abs(mAccel) > 5.0f) // Shake detection
t();
public void t ()
firstTimer = new CountDownTimer(5000, 1000) //fall registration timer
@Override
public void onTick(long millisUntilFinished)
//if there is movement before 5 seconds pass cancel the timer
if (abs(mAccel) > 2.0f)
firstTimer.cancel();
firstTimer = null;
@Override
public void onFinish()
toast.show();
secondTimer = new CountDownTimer(30*1000, 1000) //fall confirmation timer
@Override
public void onTick(long millisUntilFinished)
textView2.setText("" + millisUntilFinished / 1000);
@Override
public void onFinish()
Toast toast = Toast.makeText(getApplicationContext(),
"Fall Registered!", Toast.LENGTH_LONG);
toast.show();
.start();
.start();
主要问题是大多数时候firstTimer
被访问,由于mAccel
超过2.0阈值而被取消,同时它正在减速。
我尝试使用Timer.schedule()
将firstTimer
的激活延迟到加速度值“休息”之后,但它无法使用它。
【问题讨论】:
【参考方案1】:我创建了一个新的 Timer 对象 mTimer,这似乎适用于所有情况。
CountDownTimer firstTimer = new CountDownTimer(5000, 1000) //fall registration timer
@Override
public void onTick(long millisUntilFinished)
//if there is movement before 5 seconds pass cancel the timer
if (abs(mAccel) > 2.0f)
Toast toast = Toast.makeText(getApplicationContext(),
"Fall not Detected", Toast.LENGTH_SHORT);
toast.show();
firstTimer.cancel();
@Override
public void onFinish()
Toast toast = Toast.makeText(getApplicationContext(),
"Fall Detected!", Toast.LENGTH_SHORT);
toast.show();
secondTimer.start();
;
CountDownTimer secondTimer = new CountDownTimer(30*1000, 1000)
//fall confirmation timer
@Override
public void onTick(long millisUntilFinished)
textView2.setText("" + millisUntilFinished / 1000);
@Override
public void onFinish()
Toast toast = Toast.makeText(getApplicationContext(),
"Fall Registered!", Toast.LENGTH_LONG);
toast.show();
;
@Override
public void onSensorChanged(SensorEvent event)
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values.clone();
float x = mGravity[0];
float y = mGravity[1];
float z = mGravity[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = (float) Math.sqrt(x * x + y * y + z * z);
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;
mAccel = abs(mAccel);
textView.setText("a:"+mAccel);
if (abs(mAccel) > 5.0f) // Shake detection
mTimer.schedule(new TimerTask()
//start after 2 second delay to make acceleration values "rest"
@Override
public void run()
firstTimer.start();
, 2000);
【讨论】:
以上是关于Android 跌倒检测问题的主要内容,如果未能解决你的问题,请参考以下文章
跌倒检测基于matlab GUI人体跌倒检测系统含Matlab源码 2428期
毕设参考跌倒检测 ESP32+HaaS Python Motion API 快速打造上云的跌倒检测系统