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変数はすぐには使用可能な値を保持しない (またはまったく保持しない) ことに注意してください。したがって、定期的に起動してそのプロパティを読み取るある種のタイマーが必要です。リンクされた記事も同様のことを行います。 CMMotionManagerdeviceMotionstartDeviceMotionUpdates
    • ジャイロスコープが起動している限り、プロパティは null になります (メッセージがゼロを返すdeviceMotionため、0.0 しか表示されません)。nil
  • [[CMDeviceMotion alloc] init]補足として、結果が割り当てられた変数を でオーバーライドするため、への呼び出しは役に立ちませんself.motionManager.deviceMotion
于 2013-10-29T10:34:02.897 に答える