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

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

问题描述

假设将 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 与设备相关.理论上,无论设备在桌面上的角度位置如何,角度都应该相同.然而,这里的准确性有几个因素,包括在如此小的角度下的反正切计算.我建议您尝试使用桌子倾斜度(不与地面水平).假设您的桌子与地面成 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天全站免登陆