sBefore UIAccelerometer
was deprecated from iOS I used the data from x
, y
and z
from this class to calculate pitch, roll and yaw. I also had to do some filtering, but now I see that with the CoreMotion
library I can get these data from the CMAttitude
class and would really like to use these properties, but somehow I fail to do so.
Now, what I have done is to instantiate
CMMotionManager *motionManager;
CMDeviceMotion *deviceMotion;
CMAttitude *attitude;
...
deviceMotion = motionManager.deviceMotion;
attitude = deviceMotion.attitude;
motionManager.accelerometerUpdateInterval = 0.065; // 65ms
[motionManager startAccelerometerUpdates];
I am able to read x,y and z from motionManager.accelerometerData.acceleration.<x,y or z>
but trying to read from attitude.<roll,pitch,yaw>
gives me 0.
NSLog(@"Roll: %f", attitude.roll); // = 0
I read out the values in a method triggered by a continous timer each 100ms.
Any ideas on what I`m missing?
In order to use deviceMotion.attitude
you have to call [motionManager startDeviceMotionUpdates]
.
startAccelerometerUpdates
provides accelerometer data only like startGyroUpdates
will give you gyroData
. Note that device motion data is more than just accelerometer and gyro data as both of them will be combined (sensor fusion) to achieve more precision.