Search code examples
iosrotationaccelerometergyroscopecore-motion

Rotation on Y axis in degree or radians in iOS


I need to get Rotation for iDevice on portrait mode, around Y axis of accelerometer, as per shown in this image, (reference: https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/motion_event_basics/motion_event_basics.html) enter image description here

How can i get rotation performed around Y axis, either in positive or negative. I've implemented few codes from stackoverflow/Apple code, i get no difference in roll, pitch and yaw when i rotate on Y axis.

[self.motionManager setGyroUpdateInterval:0.1f];
[self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData *gyroData, NSError *error) {
     dispatch_async(dispatch_get_main_queue(), ^{
          NSString *strYaw = [NSString stringWithFormat:@"Yaw: %.02f",gyroData.rotationRate.z];
          [self.lblYaw setText:strYaw];

          NSString *strPitch = [NSString stringWithFormat:@"Pitch: %.02f",gyroData.rotationRate.x];
          [self.lblPitch setText:strPitch];

          NSString *strRoll = [NSString stringWithFormat:@"Roll: %.02f",gyroData.rotationRate.y];
          [self.lblRoll setText:strRoll];

      });

 }];

And this,

roll  = atan2(2*y*w - 2*x*z, 1 - 2*y*y - 2*z*z);
pitch = atan2(2*x*w - 2*y*z, 1 - 2*x*x - 2*z*z);
yaw   =  asin(2*x*y + 2*z*w);

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

Any help will be much appreciated.


Solution

  • I found the rotation of the device using Compass, created locationManager

    /**
     *  Location manager class instance
     */
    @property (strong, nonatomic) CLLocationManager *locationManager;
    

    Then initializing it

    - (void) initLocationManager
    {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.headingFilter = 0.1;
        self.locationManager.delegate = self;
        [self.locationManager startUpdatingHeading];
    }
    

    And implementing the delegate method

    - (void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
    {
       // newHeading.trueHeading is the property that i needed.
    }