使用加速度计获取相对于地平面的设备平面

2024-04-06

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

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

在.h中

double      accelerationXAverage;
double      accelerationYAverage;
double      accelerationZAverage;

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

在他们中

#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've added an image (from http://gotoandplay.freeblog.hu/ http://gotoandplay.freeblog.hu/) to make sure I'm talking about the same x- y- and z- axis.enter image description here


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

x、y 和 z 是相对于设备的。理论上,无论设备在桌面上的角度位置如何,角度都应该相同。然而,有几个因素会影响这里的准确性,包括如此小的角度下的反正切计算。我建议你尝试一下桌子的倾斜度(不与地面水平)。假设让你的桌子与地面成 30 度角并尝试一下。我假设你的 x,y,z 都是双精度的

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用加速度计获取相对于地平面的设备平面 的相关文章

随机推荐