0

I am trying to find a accurate angle (a) show in below image.

enter image description here

By researching I understand that I can get the angle using CMotionManger. I created a singleton class which will give me updates regarding device movement. And getting the "Pitch" value using CMQuaternion

For better understanding what "Pitch" is with respect to axis, refer below image.

enter image description here

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 ?

- (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 ==> %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 ==> %.2f degree", myPitch);

        self.motionLastPitch = myPitch;
    }];
}];

}

In Log I am getting this print :

    [Line 64] myPitch ==> 74.71 degree
 [Line 60] Yaw ==> -0.037314
 [Line 64] myPitch ==> 74.68 degree
 [Line 60] Yaw ==> -0.037849
 [Line 64] myPitch ==> 74.67 degree
 [Line 60] Yaw ==> -0.038531
 [Line 64] myPitch ==> 74.69 degree
 [Line 60] Yaw ==> -0.038637
 [Line 64] myPitch ==> 74.71 degree
 [Line 60] Yaw ==> -0.037314
 [Line 64] myPitch ==> 74.68 degree
 [Line 60] Yaw ==> -0.037849
 [Line 64] myPitch ==> 75.65 degree
 [Line 60] Yaw ==> -0.038531
 [Line 64] myPitch ==> 76.90 degree
 [Line 60] Yaw ==> -0.038637

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 ?

Any help / Idea / guidance will be helpful.

Wolverine
  • 4,264
  • 1
  • 27
  • 49

1 Answers1

0

I think below are good reference this may help you.

Using quaternion instead of roll, pitch and yaw to track device motion

iPhone - understanding iPhone rotation

https://en.wikipedia.org/wiki/Gimbal_lock

Community
  • 1
  • 1
Patrick R
  • 6,621
  • 1
  • 24
  • 27