views:

611

answers:

3

Hi all,

I am working on app which is used to display the current G-Force value of a moving car on iPhone. I don't know how to calculate the G-force value using the iPhone accelerometer values. Also, I have to calculate distance and speed using these values.

Can anybody help me out to fix this problem?

Thanks in advance

+2  A: 

Have your class (ViewController, or whatever) implement the UIAccelerometerDelegate protocol. Then

-(void)startListening {
  UIAccelerometer *meter = [UIAccelerometer sharedAccelerometer];
  meter.updateInterval = 1.0; // One second
  meter.delegate = self;
}

Your delegate can then use the UIAcceleration object given it by the UIAccelerometer to do whatever it is you need. For instance, if you only need the magnitude of the iPhone's acceleration, you could, with a double accelMagnitude instance variable, have:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
  accelMagnitude = sqrt(acceleration.x * acceleration.x
                      + acceleration.y * acceleration.y
                      + acceleration.z * acceleration.z);
  [self refreshDisplay: accelMagnitude];
}

where refreshDisplay does whatever displaying you need.

Frank Shearar
Thanks for the answer. I can do this but, I want to calculate the G-force value for a car which is moving. Also, I have to calculate the distance and speed.
Vamshi
@Frank: Does the iPhone accelerometer automatically negate acceleration due to gravity?
Zaid
@Zaid thanks :) Also, Christian Severin's answered your question: first calibrate the accelerometer with your car at rest.
Frank Shearar
@Zaid The accelerometer does not negate gravity's acceleration, you get the raw values. You have to subtract it out yourself. Also remember you're only getting accelerations. If the car is moving at a constant speed and direction, no accelerating is taking place, only gravity will be registering.
willc2
+1  A: 

If you can calibrate your accelerometer while the car is at rest, you can later get the horizontal force component by simple vector arithmetics:

Fhorizontal = Ftotal - Fvertical

Christian Severin
+1 for the calibration tip. You'll need all three components: the vertical for speed bumps ( :) ) and both in the horizontal plane to account for turning.
Frank Shearar
A: 

I have read the accelerometer is not that accurate, so I think empirical fine tuning will be needed.

Once you calibrate, meassure/filter (and convert to the proper unit) the accelerometer data, speed can be aproximated by (Trapezoid Rule)

Speed_i+1 = Speed_i + (interval_time/2)* (Accel_i+Accel_i+1)

Same fashion for the distance. If you feel like having more accuracy you could try higher order quadrature formulas (like Simpson´s).

Javi Roman