1

我无法弄清楚为什么在记录时滚动、俯仰和偏航值给出 0.0000.. 我确定这是我想念的东西,但我无法弄清楚..

这是代码:

//ViewController.m

#import "ViewController.h"
@interface ViewController (){

}
@property (nonatomic) CMMotionManager *motionManager;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.motionManager = [[CMMotionManager alloc] init];
    CMDeviceMotion *devMotion = [[CMDeviceMotion alloc]init];

    if ([self.motionManager isDeviceMotionAvailable]) {
        NSLog(@"Device Motion Available");
        [self.motionManager setDeviceMotionUpdateInterval:1.0/30.0];
        // Pull mechanism is used
        [self.motionManager startDeviceMotionUpdates];
    }
    devMotion = self.motionManager.deviceMotion;

    NSLog(@"Roll Pitch and Yaw are %f, %f, %f",devMotion.attitude.roll, devMotion.attitude.pitch, devMotion.attitude.yaw);

}

我已经解决了这个类似的问题:SO Question

请帮助我理解这一点..

谢谢..


更新代码:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.motionManager = [[CMMotionManager alloc] init];

    if ([self.motionManager isDeviceMotionAvailable]) {
        NSLog(@"Device Motion Available");
        [self.motionManager setDeviceMotionUpdateInterval:1.0/30.0];
        // Pull mechanism is used
        [self.motionManager startDeviceMotionUpdates];
    }
    CMDeviceMotion *devMotion = self.motionManager.deviceMotion;
    NSLog(@"*Roll,Pitch and Yaw are %f, %f, %f",devMotion.attitude.roll, devMotion.attitude.pitch, devMotion.attitude.yaw);

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(updateValues:) userInfo:nil repeats:YES];
}

-(void) updateValues:(NSTimer *)timer{
    CMDeviceMotion *currDeviceMotion = self.motionManager.deviceMotion;

    NSLog(@"Roll Pitch and Yaw are %f, %f, %f",currDeviceMotion.attitude.roll, currDeviceMotion.attitude.pitch, currDeviceMotion.attitude.yaw);


}

这段代码也有很大一部分初始值为 0.000000 。之后它开始获得价值......所以我想startDeviceMotionUpdates为提供价值有一些延迟deviceMotion。所以看起来我需要弄清楚如何保存第一个非零值。

4

1 回答 1

0
  • 请注意,您的devMotion变量不会立即保存可用值(或任何值),因为CMMotionManager在. 所以你应该有某种计时器,它会定期触发并读取该属性。您的链接文章做了类似的事情。 deviceMotionstartDeviceMotionUpdates
    • 只要陀螺仪正在启动,该deviceMotion属性就会为空(您只会看到 0.0,因为消息nil返回零)。
  • 作为旁注:您的调用[[CMDeviceMotion alloc] init]是无用的,因为您随后用 覆盖了结果分配给的变量self.motionManager.deviceMotion
于 2013-10-29T10:34:02.897 回答