0

Right now I'm using the following code to get Euler values from the device's gyroscope. Is this how it's supposed to be used? Or is there a better way without using NSTimer?

- (void)viewDidLoad {
[super viewDidLoad];
CMMotionManager *motionManger = [[CMMotionManager alloc] init];
[motionManger startDeviceMotionUpdates];

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(1/6) target:self selector:@selector(read) userInfo:nil repeats:YES];
}

- (void)read {
CMAttitude *attitude;
CMDeviceMotion *motion = motionManger.deviceMotion;
attitude = motion.attitude;
int yaw = attitude.yaw; 
}
SahilS
  • 396
  • 5
  • 7
thisiscrazy4
  • 1,905
  • 8
  • 35
  • 58
  • My goal is to continuously monitor the yaw value to determine if the device has rotated 360 degrees along its center. What's the most efficient way to do this? – thisiscrazy4 Dec 21 '12 at 06:43

2 Answers2

1

You Can Use This...

    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error)
 {
     CMAttitude *attitude;
     attitude = motion.attitude;
     int yaw = attitude.yaw; 
 }];
0

Directly to quote the documentation:

Handling Motion Updates at Specified Intervals

To receive motion data at specific intervals, the app calls a “start” method that takes an operation queue (instance of NSOperationQueue) and a block handler of a specific type for processing those updates. The motion data is passed into the block handler. The frequency of updates is determined by the value of an “interval” property.

[...]

Device motion. Set the deviceMotionUpdateInterval property to specify an update interval. Call the or startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler: or startDeviceMotionUpdatesToQueue:withHandler: method, passing in a block of type CMDeviceMotionHandler. With the former method (new in iOS 5.0), you can specify a reference frame to be used for the attitude estimates. Rotation-rate data is passed into the block as CMDeviceMotion objects.

So e.g.

motionManger.deviceMotionUpdateInterval = 1.0/6.0; // not 1/6; 1/6 = 0
[motionManager 
    startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
    withHandler:
        ^(CMDeviceMotion *motion, NSError *error)
         {
             CMAttitude *attitude;
             attitude = motion.attitude;
             int yaw = attitude.yaw; 
         }];

I've lazily just used the main queue but that still may be a better solution than the NSTimer because it'll give the motion manager an explicit clue about how often you care to be updated.

Tommy
  • 99,986
  • 12
  • 185
  • 204