Why are my accelerometers responding so slowly?
I am asking them at 50Hz / 50 times per second for data. When I suddenly flip the device along the x-axis 90 degrees until the device is flat on the table with the display facing up, the values move rather slowly towards the "target" value for that position.
Now the weird thing is that if I increase the measurement speed, the value will move faster towards this new value when the device is suddenly flipped 90 degrees. But if I just ask once per second for a new value, it will take a very long time until the value reaches the target. What could be causing this?
I am not doing any data aggregation and am not accumulating anything. I am just doing 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, in my -moveViews: method, I do this:
-(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, the device rotates 90 degrees x-achsis or 180 degrees, the view just moves rather slowly to the target position. I don't know what it is because of the physics of the accelerometers, or if this is a bug in my filtering code. I only know that there are fast games that use accelerometers for control, so I can hardly imagine what the problem is with the hardware.
a source to share
This line:
accelerationX = acceleration.x * 0.05 + accelerationX * (1.0 - 0.05);
is a low-pass filter that works by calculating a moving average acceleration x. In other words, every time the callback is called, you move accelerationX
5% towards the new accelerometer value. This requires many iterations before accelerationX
reflecting the new orientation.
What you need to do is increase the value 0.05
, say 0.2
. I would do it globally #define
and play around with different values and different refresh rates.
a source to share