How to use deviceMotion attitude values (roll,pitch and yaw) to detect a motion

Madhu picture Madhu · May 17, 2013 · Viewed 10.4k times · Source

I'm using coreMotion in my app to detect a motion of iOS device. I know how to get the different sensor's data from coreMotion. I am getting roll, pitch, and yaw data as follows from the deviceMotion:

 float pitch =  (180/M_PI)*self.manager.deviceMotion.attitude.pitch];
 float roll = (180/M_PI)*self.manager.deviceMotion.attitude.roll];
 float yaw = (180/M_PI)*self.manager.deviceMotion.attitude.yaw];

How can I use these data to detect a motion? What are the calculations that I need to do for this?

Answer

iSeeker picture iSeeker · Sep 30, 2013

The attitude values need the device motion updates to be started as:

startDevicemotionUpdates

To monitor attitude values when device moves, use the attitude values to be displayed in labels:

[self.cmMotionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *devMotion, NSError *error) {

float pitch =  (180/M_PI)*self.manager.devMotion.attitude.pitch];
float roll = (180/M_PI)*self.manager.devMotion.attitude.roll];
float yaw = (180/M_PI)*self.manager.devMotion.attitude.yaw];

self.rollLabel.text = [NSString stringWithFormat:@"%f", roll];
self.pitchLabel.text = [NSString stringWithFormat:@"%f",pitch];
self.yawLabel.text = [NSString stringWithFormat:@"%f",yaw];
}

Also it is better not to use roll, pitch and yaw(aka Euler Angles). Use Quaternion or Rotation matrices for better accuracy. Euler angles have the tendency to be in a situation called gimbal lock which results in unreliable data.

Please check this link for how to use quaternion, and convert that value to the roll, pitch and yaw values: SO link