title: How to calculate and get the smooth value for pitch?
body: <p>I am trying to find a accurate angle (a) show in below image.</p>
  
  <p><a href="https://i.stack.imgur.com/j3Xko.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j3Xko.png" alt="enter image description here"></a></p>
  
  <p>By researching I understand that I can get the angle using <strong>CMotionManger</strong>. I created a singleton class which will give me updates regarding device movement. And getting the "<strong>Pitch</strong>" value using <strong>CMQuaternion</strong></p>
  
  <p>For better understanding what "Pitch" is with respect to axis, refer below image.</p>
  
  <p><a href="https://i.stack.imgur.com/HwVWU.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HwVWU.gif" alt="enter image description here"></a></p>
  
  <p>Here is my code in which I am getting expected value of pitch.is it the correct way to find the angle shown in 1st image ?</p>
  
  <pre><code>- (void) startMotionUpdate {
  
  if (self.motionManager == nil) {
      self.motionManager = [[CMMotionManager alloc] init];
  }
  
  self.motionManager.deviceMotionUpdateInterval = kUpdateInterval;
  
  [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical
                                                          toQueue:self.deviceQueue
                                                      withHandler:^(CMDeviceMotion *motion, NSError *error)
  {
      [[NSOperationQueue mainQueue] addOperationWithBlock:^{
          CGFloat x = motion.gravity.x;
          CGFloat y = motion.gravity.y;
          CGFloat z = motion.gravity.z;
  
  
          CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion;
          double yaw = asin(2*(quat.x*quat.z - quat.w*quat.y));
  
          DLog(@"Yaw ==&gt; %f", yaw);
  
          double myPitch = radiansToDegrees(atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z));
  
          DLog(@"myPitch ==&gt; %.2f degree", myPitch);
  
          self.motionLastPitch = myPitch;
      }];
  }];
  
  }
  </code></pre>
  
  <p>In Log I am getting this print :</p>
  
  <pre><code>    [Line 64] myPitch ==&gt; 74.71 degree
   [Line 60] Yaw ==&gt; -0.037314
   [Line 64] myPitch ==&gt; 74.68 degree
   [Line 60] Yaw ==&gt; -0.037849
   [Line 64] myPitch ==&gt; 74.67 degree
   [Line 60] Yaw ==&gt; -0.038531
   [Line 64] myPitch ==&gt; 74.69 degree
   [Line 60] Yaw ==&gt; -0.038637
   [Line 64] myPitch ==&gt; 74.71 degree
   [Line 60] Yaw ==&gt; -0.037314
   [Line 64] myPitch ==&gt; 74.68 degree
   [Line 60] Yaw ==&gt; -0.037849
   [Line 64] myPitch ==&gt; 75.65 degree
   [Line 60] Yaw ==&gt; -0.038531
   [Line 64] myPitch ==&gt; 76.90 degree
   [Line 60] Yaw ==&gt; -0.038637
  </code></pre>
  
  <p>I have tried to place my phone as per first image, so is the values are right ?  If yes then how can I get the stable values for Pitch ?</p>
  
  <p>Any help / Idea / guidance will be helpful.</p>