Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth UIAccelerometer

Tags:

iphone

How can I smooth the accelerometer of the iPhone/iPod Touch?

like image 997
isiaatz Avatar asked Jan 23 '23 06:01

isiaatz


1 Answers

You can smooth the accelerometer data by applying a filter to the incoming data before using it. The first thing you'll want to do it set up a constant for your filter.

#define kFilteringFactor   0.1

In your didAccelerate method, you'll need to add the following filtering code

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
    sx = acceleration.x * kFilteringFactor + sx * (1.0 - kFilteringFactor);
    sy = acceleration.y * kFilteringFactor + sy * (1.0 - kFilteringFactor);
    sz = acceleration.z * kFilteringFactor + sz * (1.0 - kFilteringFactor);
}

The code above should smooth the data for you. The sx, sy and sz values are of type UIAccelerationValue.

There's lots of related information in Apple's documentation that you may find similarly useful in respect to this.

like image 164
Paul McCabe Avatar answered Jan 31 '23 07:01

Paul McCabe