CMQuaternion issue when trying to get Euler angles

in my application I would like to rotate a 3D model in 3 dimensions by rotating the iPhone. So my current method looks like this:

- (void)deviceMotionDataReceived:(CMDeviceMotion *)motion {

    CMQuaternion quat = motion.attitude.quaternion;

    NSLog(@"x:%f y:%f z:%f w%f", quat.x, quat.y, quat.z, quat.w);

    double myPitch = atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z);
    double myRoll = atan2(2*(quat.y*quat.w - quat.x*quat.z), 1 - 2*quat.y*quat.y - 2*quat.z*quat.z);
    double myYaw = asin(2*quat.x*quat.y + 2*quat.w*quat.z);

     NSLog(@"pitch:%f roll:%f yaw:%f", myPitch, myRoll, myYaw);

}

      

Everything looks good - the pitch, the shaft allows for 360 degree rotation, but when I change the dash - the pitch / shaft rotates 180 degrees when the yaw is 90 / -90 degrees. How can I get the correct lean / roll angles across all yaw boundaries? thanks in advance!

+3


source to share





All Articles