Search code examples
iosobjective-caccelerometermeasure

Accelerometer to cm - Xcode


enter image description here

I have this result for my accelerometer , I would like to convert this to cm. what conversion would I have to do?

(.h)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIAccelerometerDelegate>
{
    IBOutlet UILabel *xlabel;
    IBOutlet UILabel *ylabel;
    IBOutlet UILabel *zlabel;

}
@end

(.m)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[UIAccelerometer sharedAccelerometer]setDelegate:self];
    //Do any additional setup after loading the view,typically from a nib
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:
  (UIAcceleration *)acceleration{
    [xlabel setText:[NSString stringWithFormat:@"%f",acceleration.x]];
    [ylabel setText:[NSString stringWithFormat:@"%f",acceleration.y]];
    [zlabel setText:[NSString stringWithFormat:@"%f",acceleration.z]];
}

@end

Is it possible to find the distance by using accelerometer?

Thanks for answers, thanks for people who took the time to help.


Solution

  • It is more like a physical question :)

    There are two situations:

    1.The iPhone device did not have any attitude adjustment while moving in 3D.

    2.The iPhone is falt on the table.

    Considering your y-value, I guess what you want it's more fit to the first situation.

    To calculate the displacement in a line, you can use the formula below.

    enter image description here

    While Vi - the velocity of iPhone at the beginning point,

    a - the acceleration in your direction (assume is uniform acceleration)

    Well, If you acclerate from motionless, Vi = 0, Then it became:

    distance = 1/2 * acceleration * time * time.

    And the UIAcceleration definition:

    UIAcceleration -

    This type is used to store acceleration values, which are specified as g-force values, where the value 1.0 corresponds to the normal acceleration caused by gravity at the Earth’s surface.

    timeStamp -

    This value indicates the time relative to the device CPU time base register. Compare acceleration event timestamps to determine the elapsed time between them. Do not use a timestamp to determine the exact time at which an event occurred.

    So with the x, y, z and timeStamp property in UIAcceleration Class,

    You can figure out the displacement of each single direction. To calculate the whole displacement in space:

    enter image description here

    See Also

    This is just a method how to calculate it under a number of assumptions, howerver, your question is not clear about the time, it can't work out in this condition you had provided so far.