计步算法

Step Counting Algorithm

我目前正在为带有 LSM9DS0 传感器的英特尔设备开发计步器应用程序。我的需求是开发一个计步器,应用代码intel发布在Native Application Guide

此代码可在以下共享 pdf 的第 6.1.4 节中找到

http://download.intel.com/support/edison/sb/edison_nag_331192003.pdf

事实上我找到了一些类似的代码但是android版本

public StepDetector() {
    int h = 480; // TODO: remove this constant
    mYOffset = h * 0.5f;
    mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
    mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
}

public void setSensitivity(float sensitivity) {
    mLimit = sensitivity; // 1.97  2.96  4.44  6.66  10.00  15.00  22.50  33.75  50.62
}

public void addStepListener(StepListener sl) {
    mStepListeners.add(sl);
}

//public void onSensorChanged(int sensor, float[] values) {
public void onSensorChanged(SensorEvent event) {
    Sensor sensor = event.sensor; 
    synchronized (this) {
        if (sensor.getType() == Sensor.TYPE_ORIENTATION) {
        }
        else {
            int j = (sensor.getType() == Sensor.TYPE_ACCELEROMETER) ? 1 : 0;
            if (j == 1) {
                float vSum = 0;
                for (int i=0 ; i<3 ; i++) {
                    final float v = mYOffset + event.values[i] * mScale[j];
                    vSum += v;
                }
                int k = 0;
                float v = vSum / 3;

                float direction = (v > mLastValues[k] ? 1 : (v < mLastValues[k] ? -1 : 0));
                if (direction == - mLastDirections[k]) {
                    // Direction changed
                    int extType = (direction > 0 ? 0 : 1); // minumum or maximum?
                    mLastExtremes[extType][k] = mLastValues[k];
                    float diff = Math.abs(mLastExtremes[extType][k] - mLastExtremes[1 - extType][k]);

                    if (diff > mLimit) {

                        boolean isAlmostAsLargeAsPrevious = diff > (mLastDiff[k]*2/3);
                        boolean isPreviousLargeEnough = mLastDiff[k] > (diff/3);
                        boolean isNotContra = (mLastMatch != 1 - extType);

                        if (isAlmostAsLargeAsPrevious && isPreviousLargeEnough && isNotContra) {
                            Log.i(TAG, "step");
                            for (StepListener stepListener : mStepListeners) {
                                stepListener.onStep();
                            }
                            mLastMatch = extType;
                        }
                        else {
                            mLastMatch = -1;
                        }
                    }
                    mLastDiff[k] = diff;
                }
                mLastDirections[k] = direction;
                mLastValues[k] = v;
            }
        }
    }
}

完整的 android 源代码在 Github

中可用

https://github.com/bagilevi/android-pedometer

我的问题是我无法使用这些代码制作完美的设计文档。我需要澄清

  1. 如何计算 Scale 值? 它看起来像这里使用的随机常数

    int h = 480; // TODO: remove this constant
    mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
    mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
    
  2. 这些mLimit值是如何计算的?

    if (diff > mLimit)

  3. 英特尔代码说

    v = vSum / 2300;

    但是 Android 代码说

    v = vSum / 3;

  4. 向量和的计算在两个代码中是相似的。这里实际遵循的是哪个等式?

请有人帮我弄清楚这些事情

我发现这个机制只是试验和运行算法

这些常量值可能会根据环境、设备、设备包装、加速度计的灵敏度等而发生变化。