使用加速度计获取相对于地平面的设备平面 [英] Get device plane relative to ground plane using accelerometer

查看:98
本文介绍了使用加速度计获取相对于地平面的设备平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设iPhone放在一张平台上。我想确定桌面的角度,其中0的角度意味着桌子与重力矢量完全垂直。我使用以下公式:

Let's say the iPhone is placed on a flat table. I'd like to determine the angle of the tabletop plane, where an angle of 0 would mean the table is exactly perpendicular to the gravity vector. I'm using the following formula:

    radians = atanf (z / sqrt (x^2 + y^2)

在.h

double      accelerationXAverage;
double      accelerationYAverage;
double      accelerationZAverage;

double      accelerationXSum;
double      accelerationYSum;
double      accelerationZSum;
int         readingsCount;

在.m

#define kThresholdMovementHigh 0.05

- (void)accelerometer:(UIAccelerometer *)accelerometer
        didAccelerate:(UIAcceleration *)acceleration 
{
    // did device move a lot? if so, reset sums
    if (fabsf(acceleration.x - accelerationXAverage) > kThresholdMovementHigh ||
        fabsf(acceleration.y - accelerationYAverage) > kThresholdMovementHigh ||
        fabsf(acceleration.z - accelerationZAverage) > kThresholdMovementHigh   )
    {
        NSLog(@"deviceDidMove a lot");
        accelerationXSum = acceleration.x;
        accelerationYSum = acceleration.y;
        accelerationZSum = acceleration.z;
        readingsCount = 1;
    }

    else 
    {
        // because the device is at rest, we can take an average of readings
        accelerationXSum += acceleration.x;
        accelerationYSum += acceleration.y;
        accelerationZSum += acceleration.z;
        readingsCount ++;        
    }

    accelerationXAverage = accelerationXSum / readingsCount;
    accelerationYAverage = accelerationYSum / readingsCount;
    accelerationZAverage = accelerationZSum / readingsCount;

    float angle = RadiansToDegrees(atanf(accelerationZAverage/sqrtf(pow(accelerationXAverage, 2) + pow(accelerationYAverage, 2)))) + 90;

    labelAngle.text = [NSString stringWithFormat:@"%.2f°", angle];
}

我通过平均加速度计读数来滤除噪音。加速度计更新间隔为1/60,现在在试验时,我让设备静置10秒钟(因此平均有600个读数)。

I'm filtering out noise by averaging the accelerometer readings. The accelerometer update interval is 1/60, and for now while experimenting, I've let the device sit for 10 seconds (so it's an average of 600 readings).

这个公式似乎有用,它给了我关于我期望的角度。但是,我也期待如果我尝试将设备旋转到不同的静态位置,同时仍然平放在桌面上,我应该得到相同的答案(因为相对于重力矢量,角度没有改变) 。然而,当我尝试时,这不是我得到的。角度变化几度。

This formula appears to work, it gives me angles about what I'd expect. However, I'm also expecting that if I try this with the device rotated to a different static position while still flat on the tabletop, that I should get the same answer (cause relative to the gravity vector, the angle hasn't changed). However, that's not what I get when I try it out. The angles vary by a couple of degrees.

我使用正确的公式?为什么同一桌面上不同位置的角度会有所不同?它只是误差范围吗?

I am using the right formula? Why would the angle differ for different placements on the same tabletop? Is it just the margin of error?

我添加了一张图片(来自 http://gotoandplay.freeblog.hu/ )以确保我在谈论相同的x-y轴和z轴。

I've added an image (from http://gotoandplay.freeblog.hu/) to make sure I'm talking about the same x- y- and z- axis.

推荐答案

您的方法和公式是正确的。

Your approach and formula are correct.

x,y和z是相对于设备的。理论上,无论设备在桌面上的角度位置如何,角度都应该相同。然而,这里有几个因素有助于精确度,包括在如此小的角度下的arctan计算。我建议你尝试一下台面倾斜度(不要与地面水平)。说保持你的桌子与地面成30度角并尝试。我假设你的x,y,z都是双倍的

The x, y and z are relative to the device. Theoretically the angle should be the same irrespective of the angular position of the device on the table top. However there are several factors contributing to the accuracy here including the arctan computation at such a small angle. I would suggest you to try it out with the table inclination (not horizontal to the ground). Say keep your table at angle of 30 degrees from the ground and try out. I assume your x,y,z all are in double

这篇关于使用加速度计获取相对于地平面的设备平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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