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.
-
Do you mean 'skidpad' when you say 'g-force'? – Zaid Mar 02 '10 at 08:14
-
What sort of accuracies are you after for the speed and distance? – Zaid Mar 02 '10 at 08:33
-
Any luck with this? I'd be interested if you figured out a (reasonably) accurate way to do this. – Jerceratops Apr 19 '11 at 23:37
3 Answers
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.

- 17,012
- 8
- 67
- 94
-
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 Mar 02 '10 at 08:03
-
@Frank: Does the iPhone accelerometer automatically negate acceleration due to gravity? – Zaid Mar 02 '10 at 08:18
-
@Zaid thanks :) Also, Christian Severin's answered your question: first calibrate the accelerometer with your car at rest. – Frank Shearar Mar 02 '10 at 08:42
-
2@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 Mar 02 '10 at 21:05
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

- 1,793
- 19
- 38
-
+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 Mar 02 '10 at 08:44
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).

- 31
- 2