title: Gyroscope on iPhone
body: <p>I need some help using gyroscope on iPhone. I can't understand readings from CMAttitude regarding pitch, roll and yaw in a particular situation.</p>
  
  <p>This is my code</p>
  
  <pre><code>- (void)handleDeviceMotion:(CMDeviceMotion*)motion {
  
     NSLog(@"Yaw   %f ",attitude.yaw * 180 / M_PI);
     NSLog(@"Pitch %f ",attitude.pitch * 180 / M_PI);
     NSLog(@"Roll  %f ",attitude.roll * 180 / M_PI);
  }
  </code></pre>
  
  <p>Let's suppose that iPhone is laying down on a plane as in the following figure:</p>
  
  <p><img src="https://i.stack.imgur.com/MxkVk.png" alt="enter image description here"></p>
  
  <p>pitch, roll and yaw are (almost) 0 degrees and any turn around an axis returns understandable readings. For example, turning the device right, the Yaw decreases and Pitch and Roll remains at 0.</p>
  
  <p>Now the iPhone is in the following position:</p>
  
  <p><img src="https://i.stack.imgur.com/GEdol.png" alt="enter image description here"></p>
  
  <p>and measurement is started again.</p>
  
  <p>Readings are: Yaw = 0, Pitch = 90, Roll = 0 </p>
  
  <p>Since the devices rotated around this axis, Pitch increases.</p>
  
  <p>Moving the iPhone into this position:</p>
  
  <p><img src="https://i.stack.imgur.com/v2z7x.png" alt="enter image description here"></p>
  
  <p>readings are: Yaw = 30, Pitch = 90, Roll = 0</p>
  
  <p>Once again, since the device rotates around the Yaw axis, this value changes and the others not.</p>
  
  <p>Moving the device around the Roll axis:</p>
  
  <p><img src="https://i.stack.imgur.com/1FgZk.png" alt="enter image description here"></p>
  
  <p>readings are: Yaw = 0, Pitch = 90, Roll = -20.</p>
  
  <p>Now what I can't understand. Moving the iPhone around a circle of radius R (R > 0), like in this figure:</p>
  
  <p><img src="https://i.stack.imgur.com/NJwam.png" alt="enter image description here"></p>
  
  <p>Yaw changes meanwhile Pitch and Roll don't.</p>
  
  <p>I would have expected Yaw had remained unchanged and Roll had changed.</p>
  
  <p>How can compensate this since I am interested exclusively to rotations around Yaw axis made by the user ?</p>
  
  <p>Another problem I have is drifting. The iPhone is in the position described in the second figure, taken in my hand at rest for a long time (1 minute or more). The Yaw constantly increase. Any idea how to compensate the drifting ?</p>
  
  <p>Thank you in advance</p>
  
  <p><strong>UPDATE</strong>
  I have followed Kay suggestions but nothing changes. Some more detail on my code. I would like to use Yaw to rotated an UIImage only when user rotate the device around the Yaw axis.
  This works, but when the user rotates around its own vertical axis the Yaw changes. I suppose that is not correct since when the user moves around its vertical axis, the devices doesn't rotate around its own Yaw axis. May be I am wrong. This is my original code:</p>
  
  <pre><code>- (void)handleDeviceMotion:(CMDeviceMotion*)motion { 
  
     CMAttitude      *attitude = motion.attitude;
  
     NSLog(@"Yaw   %f ",attitude.yaw * 180 / M_PI);
     NSLog(@"Pitch %f ",attitude.pitch * 180 / M_PI);
     NSLog(@"Roll  %f ",attitude.roll * 180 / M_PI);
  
     image.transform = CGAffineTransformMakeRotation(-attitude.yaw);
  }
  </code></pre>
  
  <p>This is the code after Kay suggestion:</p>
  
  <pre><code>- (void)handleDeviceMotion:(CMDeviceMotion*)motion { 
  
     CMAttitude      *attitude = motion.attitude;        
  
     if (startAttitude == 0) {
  
        startAttitude = attitude;
     }
  
     [attitude multiplyByInverseOfAttitude:startAttitude];
  
     NSLog(@"Yaw   %f ",attitude.yaw * 180 / M_PI);
     NSLog(@"Pitch %f ",attitude.pitch * 180 / M_PI);
     NSLog(@"Roll  %f ",attitude.roll * 180 / M_PI);
  
  
     image.transform = CGAffineTransformMakeRotation(-attitude.yaw);
  }
  </code></pre>
  
  <p>I start device motion monitoring with</p>
  
  <pre><code> [motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical toQueue:[NSOperationQueue currentQueue]
                                                             withHandler: ^(CMDeviceMotion *motion, NSError *error){
  
                                                                 [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
                                                             }];
  </code></pre>