如何使用Android加速度计检测步行 [英] How to detect walking with Android accelerometer
问题描述
我正在编写一个应用程序,我的目标是检测用户何时走路.我正在使用这样的卡尔曼滤波器:
I'm writing an application and my aim is to detect when a user is walking. I'm using a Kalman filter like this:
float kFilteringFactor=0.6f;
gravity[0] = (accelerometer_values[0] * kFilteringFactor) + (gravity[0] * (1.0f - kFilteringFactor));
gravity[1] = (accelerometer_values[1] * kFilteringFactor) + (gravity[1] * (1.0f - kFilteringFactor));
gravity[2] = (accelerometer_values[2] * kFilteringFactor) + (gravity[2] * (1.0f - kFilteringFactor));
linear_acceleration[0] = (accelerometer_values[0] - gravity[0]);
linear_acceleration[1] = (accelerometer_values[1] - gravity[1]);
linear_acceleration[2] = (accelerometer_values[2] - gravity[2]);
float magnitude = 0.0f;
magnitude = (float)Math.sqrt(linear_acceleration[0]*linear_acceleration[0]+linear_acceleration[1]*linear_acceleration[1]+linear_acceleration[2]*linear_acceleration[2]);
magnitude = Math.abs(magnitude);
if(magnitude>0.2)
//walking
数组gravity[]初始化为0s.
The array gravity[] is initialized with 0s.
我可以检测到用户是否在走路(查看加速度矢量的大小),但我的问题是当用户没有走路并且他移动手机时,他似乎在走路.
I can detect when a user is walking or not (looking at the value of the magnitude of the acceleration vector), but my problem is that when a user is not walking and he moves the phones, it seems that he is walking.
我是否使用了正确的过滤器?
Am I using the right filter?
是只看向量的大小还是我看单个值是正确的??
Is it right to watch only the magnitude of the vector or have I to look at the single values ??
推荐答案
Google 为此提供了一个名为 DetectedActivity
的 API,可以使用 ActivityRecognitionApi
获取该 API.可以在此处和此处.
Google provides an API for this called DetectedActivity
that can be obtained using the ActivityRecognitionApi
. Those docs can be accessed here and here.
DetectedActivity
有方法 public int getType()
来获取用户的当前活动,还有 public int getConfidence()
返回一个从 0 到 100 的值.getConfidence()
返回的值越高,API 就越确定用户正在执行返回的活动.
DetectedActivity
has the method public int getType()
to get the current activity of the user and also public int getConfidence()
which returns a value from 0 to 100. The higher the value returned by getConfidence()
, the more certain the API is that the user is performing the returned activity.
以下是 getType()
返回的内容的不断总结:
Here is a constant summary of what is returned by getType()
:
- int IN_VEHICLE 设备在车辆中,例如汽车.
- int ON_BICYCLE 设备在自行车上.
- int ON_FOOT 设备在步行或跑步的用户身上.
- int RUNNING 设备位于正在运行的用户身上.
- int STILL 设备静止(不移动).
- int TILTING 设备相对于重力的角度发生了显着变化.
- int UNKNOWN 无法检测到当前活动.
- int WALKING 设备在步行的用户身上.
这篇关于如何使用Android加速度计检测步行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!