I'm asking them at 50Hz / 50 times per second for data. When I suddenly flip the device on the x-axis by 90 degrees while the device was flat on a table with display facing up bevore, the values move pretty slowly to the "target" value for that position.
我问他们每秒50Hz / 50次数据。当我突然将设备在x轴上翻转90度而设备在显示器面朝上的桌子上是平的时,值会缓慢移动到该位置的“目标”值。
Now the weird thing is: If I increase the measurement-rate, the value will move faster to that new value upon suddenly flipping the device by 90 degrees. But if I just ask once per second for the new value, it take's very long until the value reaches the target. What can be the reason for this?
现在奇怪的是:如果我提高测量速率,在突然将设备翻转90度时,该值将更快地移动到该新值。但是,如果我每秒只询问一次新值,则需要很长时间才能达到目标值。这可能是什么原因?
I don't do any kind of data aggregation, and don't accumulate anything. I just do some simple filtering to get rid of the noise. My method looks like this:
我没有做任何类型的数据聚合,也没有积累任何东西。我只是做一些简单的过滤来摆脱噪音。我的方法看起来像这样:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// Use a basic low-pass filter to only keep the gravity in the accelerometer values for the X and Y axes
// accelerationX is an instance variable
accelerationX = acceleration.x * 0.05 + accelerationX * (1.0 - 0.05);
// round
int i = accelerationX * 100;
float clippedAccelerationValue = i;
clippedAccelerationValue /= 100;
[self moveViews:clippedAccelerationValue];
}
later on, in my -moveViews: method, I do this:
稍后,在我的-moveViews:方法中,我这样做:
-(IBAction)moveSceneForPseudo3D:(float)accelerationValue {
if(fabs(lastAccelerationValue - accelerationValue) > 0.02) { // some little treshold to prevent flickering when it lays on a table
float viewAccelerationOffset = accelerationValue * 19 * -1;
newXPos = initialViewOrigin + viewAccelerationOffset;
myView.frame = CGRectMake(newXPos, myView.frame.origin.y, myView.frame.size.width, myView.frame.size.height);
lastAccelerationValue = accelerationValue;
}
}
As a result, of the device gets turned 90 degrees on the x-achsis, or 180 degrees, the view just moves pretty slowly to it's target position. I don't know if that's because of the physics of the accelerometers, or if it's a bug in my filtering code. I only know that there are fast paced games where the accelerometers are used for steering, so I almost can't imagine that's a hardware problem.
结果,当设备在x-achsis上旋转90度或180度时,视图移动得非常缓慢到达目标位置。我不知道这是因为加速度计的物理特性,还是我的过滤代码中的错误。我只知道有快节奏的游戏,加速度计用于转向,所以我几乎无法想象这是一个硬件问题。
1 个解决方案
#1
8
This line:
accelerationX = acceleration.x * 0.05 + accelerationX * (1.0 - 0.05);
is a low-pass filter, which works by computing a moving average of the x acceleration. In other words, each time that callback is called, you're only moving the accelerationX
by 5% towards the new accelerometer value. That's why it takes many iterations before accelerationX
reflects the new orientation.
是一个低通滤波器,它通过计算x加速度的移动平均值来工作。换句话说,每次调用回调时,您只需将accelerationX向新的加速度计值移动5%。这就是为什么在accelerationX反映新方向之前需要多次迭代的原因。
What you should do is increase the 0.05
value, to say 0.2
. I'd make a global #define
and play around with different values along with different refresh rates.
你应该做的是增加0.05值,比如0.2。我会创建一个全局#define并使用不同的值和不同的刷新率。
#1
8
This line:
accelerationX = acceleration.x * 0.05 + accelerationX * (1.0 - 0.05);
is a low-pass filter, which works by computing a moving average of the x acceleration. In other words, each time that callback is called, you're only moving the accelerationX
by 5% towards the new accelerometer value. That's why it takes many iterations before accelerationX
reflects the new orientation.
是一个低通滤波器,它通过计算x加速度的移动平均值来工作。换句话说,每次调用回调时,您只需将accelerationX向新的加速度计值移动5%。这就是为什么在accelerationX反映新方向之前需要多次迭代的原因。
What you should do is increase the 0.05
value, to say 0.2
. I'd make a global #define
and play around with different values along with different refresh rates.
你应该做的是增加0.05值,比如0.2。我会创建一个全局#define并使用不同的值和不同的刷新率。