将两个UIAccelerationValue加在一起?

时间:2013-05-06 05:01:21

标签: ios accelerometer addition

我不确定这是不是一个愚蠢的问题,所以请在这里忍受我。无论如何,我有两个UIAccelerationValue's。当我单独NSLog他们的结果时,我分别获得这样的值:

1. Value: -0.016186

2. Value2: -0.033460

但是,当我将两个UIAccelerationValue's加在一起或相减时,我会得到这样的值:

Value: -571.142049
Value: -1085.174017
Value: -2061.834754
Value: -3917.490156
Value: -7443.235420
Value: -14142.151420

正如您所看到的,这些值只是呈指数级增长。

无论如何,如何在没有我现在得到的结果的情况下简单地将两个UIAccelerationValues相加或相减?

2 个答案:

答案 0 :(得分:3)

我认为你正在存储加法/减法的结果并且一遍又一遍地加/减,因此增加了大值

UIAccelerationValue只是一个双倍(见UIAccelerometer.h):

typedef double UIAccelerationValue;

使用以下示例获得预期输出:

UIAccelerationValue xOne = -0.016186;
UIAccelerationValue xTwo = -0.033460;

NSLog(@"%f", xOne + xTwo); // prints out -0.049646
NSLog(@"%f", xOne - xTwo); // prints out 0.017274

答案 1 :(得分:0)

我不是UIAcceleration的专家,但这里是示例代码的示例。希望它有所帮助。

#define kAccelerometerFrequency        50.0 //Hz
#define kFilteringFactor 0.1


UIAccelerationValue gravX;
UIAccelerationValue gravY;
UIAccelerationValue gravZ;
gravX = (acceleration.x * kFilteringFactor) + (gravX * (1.0 - kFilteringFactor));
gravY = (acceleration.y * kFilteringFactor) + (gravY * (1.0 - kFilteringFactor));
gravZ = (acceleration.z * kFilteringFactor) + (gravZ * (1.0 - kFilteringFactor));

UIAccelerationValue accelX = acceleration.x - ( (acceleration.x * kFilteringFactor) + (gravX * (1.0 - kFilteringFactor)) );

UIAccelerationValue accelY = acceleration.y - ( (acceleration.y * kFilteringFactor) + (gravY * (1.0 - kFilteringFactor)) );
UIAccelerationValue accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (gravZ * (1.0 - kFilteringFactor)) );
accelX *= 9.81f;
accelY *= 9.81f;
accelZ *= 9.81f;
accelX = [self tendToZero:accelX];
accelY = [self tendToZero:accelY];
accelZ = [self tendToZero:accelZ];

UIAccelerationValue vector = sqrt(pow(accelX,2)+pow(accelY,2)+pow(accelZ, 2));
UIAccelerationValue acce = vector - prevVelocity;
UIAccelerationValue velocity = (((acce - prevAcce)/2) * (1/kAccelerometerFrequency)) + prevVelocity;

NSLog(@"X %g Y %g Z %g, Vector %g, Velocity %g",accelX,accelY,accelZ,vector,velocity);