So, I'm pretty new to CoreMotion. I will do my best to explain why what you ask cannot be done, at least inaccurate.
Based on the Apple docs I read, there are 3 move handlers for iOS:
- Accelerometer
- Gyroscope
- magnetometer
(I believe that the latest devices have an altimeter?)
Based on my understanding of physics, I realized that an accelerometer would be most useful for your scenario, since the gyroscope and magnetometer are more oriented to rotation. I did some research and found this book . Chapter 9.5 in particular. From book:
While the accelerometer provides a measurement of forces in the x-, y- and z-axis can not measure rotation. On the other hand, a gyroscope is a speed changing device; when the phone rotates around an axis, it allows you to measure changes in such turns.
Following their code in accordance with Figure 9-5 to implement a simple application for viewing acceleration:
Then in .m viewDidLoad
- (void)viewDidLoad { [super viewDidLoad]; motionManager = [[CMMotionManager alloc] init]; motionManager.accelerometerUpdateInterval = 1.0/10.0; // Update at 10Hz if (motionManager.accelerometerAvailable) { NSLog(@"Accelerometer avaliable"); queue = [NSOperationQueue currentQueue]; [motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { CMAcceleration acceleration = accelerometerData.acceleration; xLabel.text = [NSString stringWithFormat:@"%f", acceleration.x]; xBar.progress = ABS(acceleration.x); yLabel.text = [NSString stringWithFormat:@"%f", acceleration.y]; yBar.progress = ABS(acceleration.y); zLabel.text = [NSString stringWithFormat:@"%f", acceleration.z]; zBar.progress = ABS(acceleration.z); }]; } }
After everything is connected and working on the phone, I see that the accelerometer measures the changes in my phone, which accelerate and decelerate when I move it to a flat table. Now we have acceleration, but in order to get a change in position (this is what you requested), it requires double integration (for example, fun calculus). While doable, I looked at what knowledgeable people in arduino have to say about it here , and boy is dark news. (They don’t discuss the iPhone’s accelerometer, but I extrapolated that this information applies to this issue as well).
Relevant parts that I posted here:
Getting a position from acceleration is a double integration, so the stack error is really evil. Obtaining good position data using IMU bolting down (what you're trying to do) is a truly difficult problem; The kind of accelerometers needed for this is very expensive.
and
What you are talking about is double integration, which is also too inaccurate with one accelerometer on this equipment. Even single integration will have a growing error per unit of time simply due to a lack of accuracy in this setting.
All this research leads to my conclusion that on the iPhone it is impossible to fulfill what you ask. Although, as I said, I am also a beginner! Sorry, this is not the answer you wanted to hear.