Java 机器人坠落检测问题
我正在尝试使用android的Java 机器人坠落检测问题,java,android,android-studio,timer,scheduled-tasks,Java,Android,Android Studio,Timer,Scheduled Tasks,我正在尝试使用android的加速计实现一个简单的坠落检测算法 手机的当前加速度存储在mAccel中,如果检测到突然震动计时器t开始计时。在定时器内有两个倒计时器 firstTimer用于在用户在过去5秒内未移动的情况下注册“坠落”,而secondTimer用于在用户未按下按钮(尚未实施)的情况下确认坠落 主要问题是,大多数时候访问firstTimer时,由于mAccel在减速时超过2.0阈值,它会被取消 我尝试使用Timer.schedule()将firstTimer的激活延迟到加速度值“re
加速计
实现一个简单的坠落检测算法
手机的当前加速度存储在mAccel
中,如果检测到突然震动计时器t
开始计时。在定时器
内有两个倒计时器
firstTimer
用于在用户在过去5秒内未移动的情况下注册“坠落”,而secondTimer
用于在用户未按下按钮(尚未实施)的情况下确认坠落
主要问题是,大多数时候访问firstTimer
时,由于mAccel
在减速时超过2.0阈值,它会被取消
我尝试使用
Timer.schedule()
将firstTimer
的激活延迟到加速度值“rest”之后,但它不起作用。我创建了一个新的计时器对象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);
}
}
}
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);
}
}
}