开发者

Having a problem with Core Motion gyro rotation

I'm working on an app that n开发者_如何学JAVAeeds to detect a rotation gesture from the iPhone. I have written code that polls CMMotionManager for rotation data, but for some reason these values are constantly changing even though the phone is stationary on the table. I'm not sure what I am doing wrong here. I have consulted the Apple docs and it seems like I am doing what they suggest, and the thing does run without crashing, but the numbers that come out make no sense. Here is what I am doing:

    -(void)startDetectingMotion {

 if (!motionQueue){
  motionQueue = [[NSOperationQueue mainQueue] retain];
 }

 if (motionManager.isDeviceMotionAvailable) {

  CMDeviceMotionHandler motionHandler = ^ (CMDeviceMotion *motion, NSError *error) {
  [self processMotion:motion withError:error];
 };


 [motionManager startDeviceMotionUpdatesToQueue:motionQueue withHandler:motionHandler];

 }
 else {
  NSLog(@"motion not available");
 }

}

.....

-(void)processMotion:(CMDeviceMotion *)motion withError:(NSError *)error {

 CMRotationRate rotation = motion.rotationRate;
 if(rotation.y > 2 || rotation.y < -2) {
  NSLog(@"CM Motion X rotation:%f, Y rotation:%f, Z Rotation:%f", rotation.x, rotation.y, rotation.y);
  ....

  [self stopDetectingMotion];

 }
}

The point of trapping for y > 2 or < 2 is to look for rapid rotation of the iphone on a horizontal plane, which is what I am looking for.

The output looks like this:

2010-08-15 16:15:43.475 PokerFoldTest[539:307] CM Motion X rotation:11.415660, Y rotation:7.865920, Z Rotation:7.865920

2010-08-15 16:04:33.843 PokerFoldTest[539:307] CM Motion X rotation:8.925084, Y rotation:8.414384, Z Rotation:8.414384

2010-08-15 16:11:14.314 PokerFoldTest[539:307] CM Motion X rotation:10.245130, Y rotation:-8.243847, Z Rotation:-8.243847

2010-08-15 16:11:16.136 PokerFoldTest[539:307] CM Motion X rotation:10.212860, Y rotation:-4.303616, Z Rotation:-4.303616

2010-08-15 16:11:18.242 PokerFoldTest[539:307] CM Motion X rotation:9.988654, Y rotation:-7.074587, Z Rotation:-7.074587

2010-08-15 16:11:19.678 PokerFoldTest[539:307] CM Motion X rotation:16.092894, Y rotation:-10.562743, Z Rotation:-10.562743

2010-08-15 16:15:41.662 PokerFoldTest[539:307] CM Motion X rotation:12.854285, Y rotation:7.247667, Z Rotation:7.247667

As these numbers are supposed to be radians/sec rotation, they are suggesting that when the phone is stationary on the table it is spinning wildly. What the hell? Any chance my phone has a bad gyro?


  • Check that motion is not nil (and that error is nil). You get garbage data if you call struct-returning method on nil.
  • You're only printing the extreme values (it looks like you get one of these every few seconds, whereas the update rate should be much higher). If you print all the updates, you might have a better idea of what's going on.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜