views:

154

answers:

0

From the code below, I am having a problem with Core Motion that I could not get a reliable yaw data. To reproduce the problem on iPhone 4, you can put the phone on a table, run the code and rotate the phone about the vertical axis between 2 points. On my phone, the yaw value of the points tends to move away from the initial values. For example, the range started from 0 to 50. After rotating back and forth for several times, the range changed to -10 to 45. Is there anything I can do to adjust the attitude values?

- (void)viewDidLoad {
 startStopButtonStatus = 1;
 if (!motionManager) {
  motionManager = [[CMMotionManager alloc] init];
 }
    [super viewDidLoad];
}

- (IBAction)startStopButtonPressed:(id)sender {
 if (startStopButtonStatus == 1) {
  startStopButtonStatus = 0;
  if (motionManager.deviceMotionAvailable) {
   motionManager.deviceMotionUpdateInterval = 0.01;
   [motionManager startDeviceMotionUpdates];
   timer = [NSTimer scheduledTimerWithTimeInterval:0.01 
              target:self selector:@selector(showCMDat) userInfo:nil repeats:YES];
  }
 }
 else {
  startStopButtonStatus = 1;
  if (timer != nil) {
   [timer invalidate];
   timer = nil;
  }
  if (motionManager.isDeviceMotionActive) { 
   [motionManager stopDeviceMotionUpdates];
  }
 }
}

-(void)showCMDat {
 pitchLabel.text = [NSString stringWithFormat:@"Pitch: %6.4f",motionManager.deviceMotion.attitude.pitch*57.2958];
 rollLabel.text = [NSString stringWithFormat:@"Roll: %6.4f",motionManager.deviceMotion.attitude.roll*57.2958];
 yawLabel.text = [NSString stringWithFormat:@"Yaw: %6.4f",motionManager.deviceMotion.attitude.yaw*57.2958];
}