clash
clash

Reputation: 532

No gyroscope in iPhone 4?

I just want to get some gyroscope data with Core Motion from my iPhone 4 but always get roll=0, pitch=0 and yaw=0. After intensive search I recognized that

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

returns false, although I run it on an iPhone 4. Do I have to enable the gyro in the settings? Or what could I do wrong…?

Thats my reduced interface:

@interface GameLayer : CCLayer {
    CMMotionManager *motionManager;
}

@property (nonatomic, retain) CMMotionManager *motionManager;

@end

And this is where I implement the 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;
}

and the loop where it's called:

- (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;
}

Upvotes: 0

Views: 1063

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

Use this code to check if CMMotionManager is available on the current device. If this fails to initialize the CMMotionManager instance, you know you're running the app on a device without a Gyro such as the iPhone Simulator:

// 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");
}

Upvotes: 1

Related Questions