1

I have written this code to get the G value from UIAccelerometer in xcode. Could you tell me is it correct or not?

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    float gValue = sqrt(acceleration.x*acceleration.x+acceleration.y*acceleration.y+acceleration.z*acceleration.z);
    [gLabel setText:[NSString stringWithFormat:@"%.2f",gValue]];
}

Kindly help me

SRI
  • 1,514
  • 21
  • 39

1 Answers1

2

If G value is supposed to be Gravitational acceleration then this code is incorrect.

Problems:

  1. It could work but only if the device is not moving and the only acceleration is created by the gravitational force of the Earth. In all other cases the equation is incorrect.

  2. UIAcceleration values are scaled by G. That means that when device is idle, the values will be, for example (0, -1.0, 0) and your result is 1.0.

Also note that UIAccelerometer was deprecated and superseded by "Core Motion" framework.

EDIT: Use [CMDeviceMotion gravity].

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • You saved my life Sulthan and could you tell me how to calculate the Gravitational acceleration and Gravitational Force please? – SRI Sep 07 '12 at 10:13
  • Thank you In case of My device Moving Then Please tell me.Actually my intension is to get the Gravitational Force,distance and Spped when the user moves his device.Please tell me.Thanks in advance – SRI Sep 07 '12 at 10:30
  • @srinaidu Hey, this is a programming forum. I can't explain you basic physics. First advice - use `Core Motion` framework. Second advice - read the docs. Third advice - try it by yourself before asking questions. Forth advice - find the equations for everything you need before you even start programming. You have to understand how speed/acceleration vectors work. – Sulthan Sep 07 '12 at 10:35
  • @srinaidu Documentation, documentation, documentation. – Sulthan Sep 07 '12 at 14:02