Reputation: 1002
I was wondering how to get the iphones orientation with respect to cardinal direction using Core Motion. I think the best route to go is to use the attitude of the device with respect to the magnetic field given by the magnetometer. From the appl e documentation it describes the attitude as:
A CMAttitude object represents a measurement of attitude—that is, the orientation of a body relative to a given frame of reference.
I was wondering how I could make this "given frame of reference" the cardinal directions. Maybe by using using CMCalibratedMagneticField?
Can anybody tell me how to do this or go an easier alternate route.
Upvotes: 2
Views: 428
Reputation: 130193
I'm not 100% sure if this is what you're looking for, but you can get device attitude values from the motion manager using the CMAttitudeReferenceFrameXMagneticNorthZVertical
reference frame with the following. This code will dump the attitude of the device to log 30 times per second. If seems to me that yaw values are around 0 facing east, and extend to 180 degrees in both directions, positive values being counter clockwise, and negative being clockwise.
- (void)startMonitoring
{
if (!self.motionManager) {
self.motionManager = [CMMotionManager new];
[self.motionManager setDeviceMotionUpdateInterval:1.0 / 30.0];
}
NSOperationQueue *currentQueue = [NSOperationQueue currentQueue];
[self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical
toQueue:currentQueue
withHandler:^(CMDeviceMotion *motion, NSError *error) {
NSLog(@"%@",motion.attitude);
}];
}
Upvotes: 2