Xcoder
Xcoder

Reputation: 199

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.

Upvotes: 1

Views: 1222

Answers (2)

Mark Bessey
Mark Bessey

Reputation: 19782

You can't use the accelerometer to measure XYZ displacement, without additional constraints. The iPhone lacks a high-quality gyroscope, so it's unable to determine if the phone rotates.

For simplicity, imagine the phone sitting on a table. The (x,y,z) acceleration values you get from UIAcceleration will read something close to (0,0,1) [or maybe -1, I don't remember which way the Z axis points]. If you move the phone from left to right, the X-axis values will change, and you can integrate those changes over the time between updates to get an approximation of the X-coordinate position of the phone. However, you can then rotate the phone 90 degrees, and then when you move it left and right, the Y values will change instead. There's no way to tell that this has happened, so you can't tell is the phone is "actually" moving in a particular direction.

You can use the compass to get rotation information, but it's not a very good reference (it's affected by nearby metal, and doesn't update as quickly as the accelerometer).

Upvotes: 0

Zigii Wong
Zigii Wong

Reputation: 7816

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.

Upvotes: 1

Related Questions