0

iPhone4からCoreMotionでジャイロスコープのデータを取得したいのですが、常にroll = 0、pitch = 0、yaw=0を取得します。徹底的に検索した後、私はそれを認識しました

if (motionManager.isDeviceMotionAvailable)
{
    [motionManager startDeviceMotionUpdates];
}

iPhone 4で実行しているのに、falseを返します。設定でジャイロを有効にする必要がありますか?または私は何を間違えることができますか...?

それは私の縮小されたインターフェースです:

@interface GameLayer : CCLayer {
    CMMotionManager *motionManager;
}

@property (nonatomic, retain) CMMotionManager *motionManager;

@end

そして、これは私がmotionManagerを実装する場所です:

- (id) init
{
    self=[super init];
    if(self)
    {
        self.motionManager = [[[CMMotionManager alloc] init] autorelease];
        motionManager.deviceMotionUpdateInterval = 1.0/60.0;
        if (motionManager.isDeviceMotionAvailable)
        {
            [motionManager startDeviceMotionUpdates];
        }
        else 
        {
            NSLog(@"No motion captured");
        }

        [self scheduleUpdate];
    }
    return self;
}

そしてそれが呼ばれるループ:

- (void) update:(ccTime)delta
{
    CMDeviceMotion *currentDeviceMotion = motionManager.deviceMotion;
    motionManager.showsDeviceMovementDisplay = YES;
    CMAttitude *currentDeviceAttitude = currentDeviceMotion.attitude;

    float roll = currentDeviceAttitude.roll;
    float pitch = currentDeviceAttitude.pitch;
    float yaw = currentDeviceAttitude.yaw;
}
4

1 に答える 1

1

このコードを使用して、CMMotionManager が現在のデバイスで利用できるかどうかを確認します。これで CMMotionManager インスタンスの初期化に失敗した場合は、iPhone シミュレーターなどのジャイロのないデバイスでアプリを実行していることがわかります。

// check if the motion manager class is available (available since iOS 4.0)
Class motionManagerClass = NSClassFromString(@"CMMotionManager");
if (motionManagerClass)
{
    motionManager = [[motionManagerClass alloc] init];
}
else
{
    NSLog(@"CMMotionManager not available");
}
于 2012-03-10T11:31:04.173 に答える