gpt4 book ai didi

android - 如何使用设备的加速度计检测运动的增加然后突然停止?

转载 作者:行者123 更新时间:2023-11-29 18:46:45 26 4
gpt4 key购买 nike

我正在尝试检测两部手机相互碰撞时产生的运动。

我的问题是,加速度计是适合这种情况的传感器吗?

如果是这样,我将如何实现?

否则,我应该使用什么传感器以及我应该以什么方式使用它?

根据 https://developer.android.com/guide/topics/sensors/sensors_overview 处的指南, TYPE_LINEAR_ACCELERATION 似乎是正确的使用方式,但我不知道如何使用它。

最佳答案

这是您可以做到的。

1- 像这样初始化传感器对象并注册传感器更新事件回调

    private void initSensorObject() {
SensorManager sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor _Sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorMgr.registerListener(sensorEventListener, _Sensor,
SensorManager.SENSOR_DELAY_FASTEST);
}

2- 按照以下方式处理传感器回调,检测排除重力影响的线性加速度,然后突然停止运动(我写这个是为了两次敲击 Action ,比如敲击然后停止,再次敲击然后再次停止。我将解释整个过程以便更好地理解,您可以轻松修改它以仅进行一次点击检测。)我在代码中添加了注释以使其不言自明。

    /*
* Following are the parameters for Tap detection Algorithm
*/
private static float SPEED_THRESHOLD_RISE1;
private static float SPEED_THRESHOLD_DROP1;
private static float SPEED_THRESHOLD_RISE2;
private static float SPEED_THRESHOLD_DROP2;

private static int DROP_DELTA;
private static int RISE2_DELTA;
private int SENSITIVITY_INDEX = TapParam.SEN_DEFAULT;

private static final int TAP_STATE_RISE1 = 0;
private static final int TAP_STATE_DROP1 = 1;
private static final int TAP_STATE_RISE2 = 2;
private static final int TAP_STATE_DROP2 = 3;

private int tappingState = TAP_STATE_RISE1;
private boolean tapLastStateOnce = false;

private long lastSensorUpdate;
private long tap1DroppedAt = 0;
private int mathMeanIndex = 0;
private float[] lastLinearAcc = new float[3];
private float[] acceleSet = new float[TapParam.AM_SIZE];
private int acceleIndex = 0;
private float[] gravity = new float[3];
private float lastAccele = -99; // an arbitrarily very small value


/**
* onSensorChanged is called when the Motion Sensor value
* is changed and then run the algorithm to detect your desired motion.
*
* @return void
*/
private SensorEventListener sensorEventListener = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {

long curSensorTime = System.currentTimeMillis();
if ((curSensorTime - lastSensorUpdate) < TapParam.SENSOR_RE_READ_TIME)
return;
lastSensorUpdate = curSensorTime;

acceleSet[acceleIndex] = getMotionAcceleration(event, curSensorTime);
acceleIndex = (acceleIndex + 1) % TapParam.AM_SIZE;
if (mathMeanIndex < TapParam.AM_SIZE)
mathMeanIndex++;
float accele = Util.getArithmeticMean(acceleSet);

switch (tappingState) {
case TAP_STATE_RISE1:
if (accele > SPEED_THRESHOLD_RISE1) {
tappingState = TAP_STATE_DROP1;
resetTapStateDropping();
mathMeanIndex = 0;
}
break;
case TAP_STATE_DROP1:
if (accele <= SPEED_THRESHOLD_DROP1) {
tappingState = TAP_STATE_RISE2;
resetTapStateRise2();
tap1DroppedAt = curSensorTime;
mathMeanIndex = 0;
}
break;
case TAP_STATE_RISE2:
if (curSensorTime - tap1DroppedAt >= TapParam.DELAY_BETWEEN_TAPS) {
if (accele > SPEED_THRESHOLD_RISE2) {
tappingState = TAP_STATE_DROP2;
resetTapStateDropping();
mathMeanIndex = 0;
}
}
break;
case TAP_STATE_DROP2:
if ((!tapLastStateOnce) && (accele <= SPEED_THRESHOLD_DROP2)) {
tapLastStateOnce = true;
resetTapStateRise2();
mathMeanIndex = 0;

onTapTapDetected();
}
break;

default:
tappingState = TAP_STATE_RISE1;
break;
}
}

/**
* onAccuracyChanged inter shall be called when hardware IMU
* (Inertial Measurement Unit a.k.a Motion Sensor) of the device change
* its accuracy value.
*
* @return void
*/
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
setThresholdValues();

}
};
/**
* It shall return the Linear Acceleration of the device. The force of
* gravity shall be filtered out.
*
* @return float - Linear acceleration
*/
private float getMotionAcceleration(SensorEvent event, long curSensorTime) {

// In this code, alpha is calculated as t / (t + dT),
// where t is the low-pass filter's time-constant and
// dT is the event delivery rate.

final float alpha = 0.8f;
float[] linearAcc = new float[3];

// Isolate the force of gravity with the low-pass filter.
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

// Remove the gravity contribution with the high-pass filter.
linearAcc[0] = event.values[0] - gravity[0];
linearAcc[1] = event.values[1] - gravity[1];
linearAcc[2] = event.values[2] - gravity[2];

float accele = (Math.abs(lastLinearAcc[0] - linearAcc[0])
+ Math.abs(lastLinearAcc[1] - linearAcc[1]) + Math
.abs(lastLinearAcc[2] - linearAcc[2])) / 3;
lastLinearAcc = linearAcc;

return accele;
}

/**
* resetTapStateRise2 shall reset the tapping state if
* second Tap is not detected within TAP_RISE2_TIME time.
*
* @return void
*/
private void resetTapStateRise2() {
handleResetTapState.removeCallbacks(runResetTapState);
handleResetTapState.postDelayed(runResetTapState, RISE2_DELTA);
}
private Handler handleResetTapState = new Handler();
private Runnable runResetTapState = new Runnable() {

@Override
public void run() {
tappingState = TAP_STATE_RISE1;
tapLastStateOnce = false;
}
};

/**
* resetTapStateDropping shall reset the tapping state if
* Tap Drop is not detected within TAP_DROP_TIME time.
*
* @return void
*/
private void resetTapStateDropping() {
handleResetTapState.removeCallbacks(runResetTapState);
handleResetTapState.postDelayed(runResetTapState, DROP_DELTA);
}
private Handler handleResetTapState = new Handler();
private Runnable runResetTapState = new Runnable() {

@Override
public void run() {
tappingState = TAP_STATE_RISE1;
tapLastStateOnce = false;
}
};

3- 这是一个工作参数文件,可帮助您入门。 thresholds 数组定义了 10 个灵敏度级别,即您想要轻敲手机以检测为有效 Action 的力度或力度

TapParam.java

final class TapParam {

static final int SEN_DEFAULT = 4;
static final int SEN_MIN = 0;
static final int SEN_MAX = 9;
static final int DELAY_BETWEEN_TAPS = 75;
static final int SENSOR_RE_READ_TIME = 1;
static final int AM_SIZE = 5;

// Columns: A B Y D T1 T2 T3
private static final double[][] thresholds = new double[][]{
{0.8483763, 0.33935052, 0.5655842, 0.33935052, 175, 300, 175},
{0.95167595, 0.38067037, 0.6344506, 0.38067037, 175, 300, 175},
{1.0836192, 0.4334477, 0.7224128, 0.4334477, 175, 300, 175},
{1.8552876, 0.742115, 1.2368584, 0.742115, 175, 300, 175},
{2.4327612, 0.9731045, 1.6218408, 0.9731045, 175, 300, 175},
{3.5321822, 1.4128729, 2.354788, 1.4128729, 175, 300, 175},
{6.4446864, 2.5778747, 4.296458, 2.5778747, 175, 300, 175},
{8.2, 3.5, 5.4, 2.6, 175, 300, 175},
{9.8, 4.0, 6.0, 2.9, 175, 300, 175},
{12, 6.0, 8.0, 3.1, 175, 300, 175}
};

private static int indexLimiting(int index) {
return (index > SEN_MAX) ? SEN_MAX : (index < SEN_MIN) ? SEN_MIN : index;
}

static float getRISE1(int index) {
index = indexLimiting(index);
return (float) thresholds[index][0];
}

static float getDROP1(int index) {
index = indexLimiting(index);
return (float) thresholds[index][1];
}

static float getRISE2(int index) {
index = indexLimiting(index);
return (float) thresholds[index][2];
}

static float getDROP2(int index) {
index = indexLimiting(index);
return (float) thresholds[index][3];
}

static float getDROP_DELTA1(int index) {
index = indexLimiting(index);
return (float) thresholds[index][4];
}

static float getRISE_DELTA2(int index) {
index = indexLimiting(index);
return (float) thresholds[index][5];
}
}

更新:

/**
* setThresholdValues method shall calculate the Threshold values according
* to the accuracy value of the Motion Sensor.
*/
private void setThresholdValues() {

if (_Sensor == null)
return;

SPEED_THRESHOLD_RISE1 = TapParam.getRISE1(SENSITIVITY_INDEX);
SPEED_THRESHOLD_DROP1 = TapParam.getDROP1(SENSITIVITY_INDEX);
SPEED_THRESHOLD_RISE2 = TapParam.getRISE2(SENSITIVITY_INDEX);
SPEED_THRESHOLD_DROP2 = TapParam.getDROP2(SENSITIVITY_INDEX);
}

/**
* Method shall return the average (Arithmetic Mean) of the set of values
* passed as parameter.
*
* @param float[] set - the set of values
* @return float - arithmetic mean
*/
static float getArithmeticMean(float[] set) {
double sum = 0;
for (float aSet : set) {
sum += aSet;
}
return (float) sum / set.length;
}

更新 2:

在您的 Activity 的 onCreate() 中调用 setTapTapSensitivity()

    private void setTapTapSensitivity() {
setTapTapSensitivity(3); //You can try 0 to 9 for 10 levels of sensitivity defined in TapParam.java. I have tried 3 and it works for a moderate tap
}

private void setTapTapSensitivity(int sensitivityIndex) {
RISE2_DELTA = (int) TapParam.getRISE_DELTA2(sensitivityIndex);
DROP_DELTA = (int) TapParam.getDROP_DELTA1(sensitivityIndex);
SENSITIVITY_INDEX = sensitivityIndex;
}

关于android - 如何使用设备的加速度计检测运动的增加然后突然停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51574534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com