android从加速度计读数中去除重力 [英] android remove gravity from accelerometer readings

查看:57
本文介绍了android从加速度计读数中去除重力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Android 开发一个应用程序,我需要从加速度计读数中去除重力.我已经阅读了关于这个问题的多个讨论,我还发现了一个算法 here,但是没看懂.

I am developing an application for Android where I need to remove gravity from accelerometer readings. I have read multiple discussions on this problem, I have also found an algorithm here, but I didn't really understand it.

我想从每个轴过滤重力,而不是从总加速度.

I want to filter gravity from each axis, not from the total acceleration.

你能帮我一下吗?我的代码应该是这样的:

Could you please help me out? My code should be something like:

public void onSensorChanged(SensorEvent sensorEvent) {
    float vals[] = sensorEvent.values;
    float accelerationX = filterGravity(vals[0]);
    float accelerationY = filterGravity(vals[1]);
    float accelerationZ = filterGravity(vals[2]);
}

应该在 filterGravity() 方法中放置什么代码?

What code should I place in the filterGravity() method?

推荐答案

对于基本的解决方案,您需要一个 低通滤波器 卡尔曼滤波器等其他方法在背后的数学方面非常困难.Android 的一个简单示例是在 http://上单击一下您的链接developer.android.com/reference/android/hardware/SensorEvent.html#values.

For a basic solution you would need a low pass filter other approaches like a Kalman filter are pretty tough regarding the maths behind. A simple example for Android is one click away from your link at http://developer.android.com/reference/android/hardware/SensorEvent.html#values.

简单地说,低通滤波器根据您的所有历史值构建加权平均值.例如,如果您的过滤因子为 0.1,则意味着将当前值的 10% 添加到先前的平均值:newMeanValue = currentValue 的 10% + oldMeanValue 的 90%.这意味着即使有一个突然的峰值,它也只会因为 10% 而缓慢地推动你的平均值.

Simply spoken a low pass filter builds a weighted average from all your history values. If you have for example a filtering factor of 0.1 it means that 10% of your current value is added to the previous mean value: newMeanValue = 10% of currentValue + 90% of oldMeanValue. That means even if there is an abrupt peak it will only push your mean value slowly because of the 10%.

这篇关于android从加速度计读数中去除重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆