September 5, 2010

Prog: Accelerometer on iPhone/iPod Touch

In this post, I am going to talk about the accelerometer on the iPhone/iPod Touch and how to use it via SDK. The Wikipedia says that

An accelerometer is a device that measures proper acceleration, the acceleration experienced relative to freefall.

Well the accelerometer inside the device help us to detect the relative position in relation with the gravity, measuring the answer of the 3 movement axis: x, y and z, so this allow the device to detect its orientation (portrait, landscape, face up or down), a shake or the relative values in specific moment in time.


And accelerometer sensor is not only used on Phone devices, also many high end cameras uses for the image stabilization; the cars to detect the vehicle acceleration; watches for runners that include footpods; the Nintendo Wii with the accessory Wii Motion Plus or the six axis control of the PS3 includes one. Well let's talk about how to get the values of this sensor in side the iPhone/iPod.

Via the iPhone SDK, we are going to use UIAccelerometer and UIAcceleration clases to get the momentum of the axis. The first class helps us to receive acceleration-related data, it is a shared object, like UIApplication and UIDevice. The methods we use are:

UIAccelerometer *accelerationMeasure = [UIAccelerometer sharedAccelerometer];
accelerationMeasure.updateInterval = .1;
accelerationMeasure.delegate = self;

The first line access the accelerometer, doing it with another call to a shared-object method (sharedAccelerometer). The second line sets the interval of detection, this means that we are going to read the values of the accelerometer sensor 10 times per second, being the maximum value of 100 (or set the updateinterval = 1) which will detect until if the fly is around us however the battery will drain much faster.

With the third line, we are saying that the delegate for the accelerometer is going to be our defined interface. Also we must set the value < UIAccelerometerDelegate > in our @interface controller, like this:

@interface Controller : UIViewController
< UIAccelerometerDelegate >
{
UIAccelerometer *accelerationMeasure;
}

The delegate will need to respond to only one method, accelerometer: didAccelerate:, which sends a message containing a UIAcceleration object whenever acceleration occurs (caped to the limit of the updateInterval).

-(void)accelerometer: (UIAccelerometer *)accelerometer
didaccelerate: (UIAcceleration *)acceleration
{
NSString accelX = [NSString stringWithFormat:@"%f", acceleration.x];
NSString accelY = [NSString stringWithFormat:@"%f", acceleration.y];
NSString accelZ = [NSString stringWithFormat:@"%f", acceleration.z];
}

Any accelerometer program begins with the accelerometer: didAccelerate: method, which is accessed by setting the current program as a delegate to the Accelerometer shared action. In order to access the accelerometer object, all we do is look at the x, y, z coordinates of the UIAcceleration object, this way we can use these values for we are intending for.

Also we shouldn't forget to set to nil the delegate, like this:

accelerationMeasure.setDelegate = nil;

This way we are going to stop seeking for the accelerometer updates and save some battery.

No comments:

Post a Comment