如何使用加速度计检测 iPhone 在太空中的运动? [英] How to detect iPhone movement in space using accelerometer?

查看:27
本文介绍了如何使用加速度计检测 iPhone 在太空中的运动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个应用程序,该应用程序可以使用加速度计检测您用 iPhone 制作的形状类型.例如,如果您用手拿着 iPhone 画一个圆圈,该应用程序将能够在屏幕上重新绘制它.这也适用于正方形,甚至更复杂的形状.我见过这样做的唯一应用程序示例是 AirPaint (http://vimeo.com/2276713),但它似乎无法实时执行.

I am trying to make an application that would detect what kind of shape you made with your iPhone using accelerometer. As an example, if you draw a circle with your hand holding the iPhone, the app would be able to redraw it on the screen. This could also work with squares, or even more complicated shapes. The only example of application I've seen doing such a thing is AirPaint (http://vimeo.com/2276713), but it doesn't seems to be able to do it in real time.

我的第一次尝试是对来自加速度计的 X 和 Y 参数应用低通滤波器,并使指针移向这些值,与屏幕大小成比例.但这显然还不够,我的精度非常低,如果我摇动设备它也会使指针移动...

My first try is to apply a low-pass filter on the X and Y parameters from the accelerometer, and to make a pointer move toward these values, proportionally to the size of the screen. But this is clearly not enought, I have a very low accuracy, and if I shake the device it also makes the pointer move...

对此有什么想法吗?您认为加速度计数据足以做到吗?还是应该考虑使用其他数据,例如指南针?

Any ideas about that ? Do you think accelerometer data is enought to do it ? Or should I consider using other data, such as the compass ?

提前致谢!

推荐答案

好的,我找到了一些似乎有效的方法,但我仍然有一些问题.这是我继续的方式(承认设备垂直):

OK I have found something that seems to work, but I still have some problems. Here is how I proceed (admiting the device is hold verticaly) :

1 - 我有默认的 x、y 和 z 值.
2 - 我使用低通滤波器从此数据中提取重力矢量.
3 - 我从每个 x、y 和 z 中减去归一化的重力矢量,并得到运动加速度.
4 - 然后,我将这个加速度值与时间积分,所以我得到了速度.
5 - 我再次将这个速度与时间积分,并找到一个位置.

1 - I have my default x, y, and z values.
2 - I extract the gravity vector from this data using a low pass filter.
3 - I substract the normalized gravity vector from each x, y, and z, and get the movement acceleration.
4 - Then, I integrate this acceleration value with respect to time, so I get the velocity.
5 - I integrate this velocity again with respect to time, and find a position.

以下所有代码都包含在我的控制器的加速计:didAccelerate:委托中.我试图让球根据我找到的位置移动.这是我的代码:

All of the below code is into the accelerometer:didAccelerate: delegate of my controller. I am trying to make a ball moving according to the position i found. Here is my code :

NSTimeInterval interval = 0;
NSDate *now = [NSDate date];
if (previousDate != nil)
{
    interval = [now timeIntervalSinceDate:previousDate];
}
previousDate = now;

//Isolating gravity vector
gravity.x = currentAcceleration.x * kFileringFactor + gravity.x * (1.0 - kFileringFactor);
gravity.y = currentAcceleration.y * kFileringFactor + gravity.y * (1.0 - kFileringFactor);
gravity.z = currentAcceleration.z * kFileringFactor + gravity.z * (1.0 - kFileringFactor);
float gravityNorm = sqrt(gravity.x * gravity.x + gravity.y * gravity.y + gravity.z * gravity.z);

//Removing gravity vector from initial acceleration
filteredAcceleration.x = acceleration.x - gravity.x / gravityNorm;
filteredAcceleration.y = acceleration.y - gravity.y / gravityNorm;
filteredAcceleration.z = acceleration.z - gravity.z / gravityNorm;

//Calculating velocity related to time interval
velocity.x = velocity.x + filteredAcceleration.x * interval;
velocity.y = velocity.y + filteredAcceleration.y * interval;
velocity.z = velocity.z + filteredAcceleration.z * interval;

//Finding position
position.x = position.x + velocity.x * interval * 160;
position.y = position.y + velocity.y * interval * 230;

如果我执行这个,我会得到很好的值,我的意思是我可以看到加速度根据我所做的动作变成正值或负值.但是当我尝试将该位置应用到我的球视图时,我可以看到它正在移动,但倾向于在一个方向上比另一个方向移动得更多.这意味着,例如,如果我用我的设备画圆圈,我会看到球描述朝向屏幕左上角的曲线.类似的东西:http://img685.imageshack.us/i/capturedcran20100422133.png/

If I execute this, I get quite good values, I mean I can see the acceleration going into positive or negative values according to the movements I make. But when I try to apply that position to my ball view, I can see it is moving, but with a propencity to go more in one direction than the other. This means, for example, if I draw circles with my device, i will see the ball describing curves towards the top-left corner of the screen. Something like that : http://img685.imageshack.us/i/capturedcran20100422133.png/

你对正在发生的事情有什么想法吗?提前致谢!

Do you have any ideas about what is happening ? Thanks in advance !

这篇关于如何使用加速度计检测 iPhone 在太空中的运动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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