Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Gyroscope API

I am learning to write an app using the gyroscope sensor in iOS. Are there classes for dealing with the gyroscope similar to UIAcceleration/UIAccelerometer/UIAccelerometerDelegate for the accelerometer?

like image 774
user682765 Avatar asked Aug 21 '11 02:08

user682765


People also ask

Does iOS gyroscope?

Many iOS devices have a three-axis gyroscope, which delivers rotation values in each of the three axes shown in Figure 1. Rotation values are measured in radians per second around the given axis. Rotation values may be positive or negative depending on the direction of rotation.

How do I get gyroscope on my iPhone?

To toggle this on/off, swipe upwards from the bottom of the screen to activate the control center. The orientation lock is the last icon on the right of the top row. Tap on, tap off.

How accurate is iPhone gyroscope?

The comparison of the measurement results shows that the inaccuracies of a calibrated smartphone gyroscope for various movements are between 0.42° and 1.15°.

What is iOS accelerometer?

An accelerometer measures changes in velocity along one axis. All iOS devices have a three-axis accelerometer, which delivers acceleration values in each of the three axes shown in Figure 1. The values reported by the accelerometers are measured in increments of the gravitational acceleration, with the value 1.


1 Answers

First import CoreMotion framework

#import <CoreMotion/CoreMotion.h>

    self.motionManager = [[CMMotionManager alloc] init];


    //Gyroscope
    if([self.motionManager isGyroAvailable])
    {
        /* Start the gyroscope if it is not active already */ 
        if([self.motionManager isGyroActive] == NO)
        {
            /* Update us 2 times a second */
            [self.motionManager setGyroUpdateInterval:1.0f / 2.0f];

            /* Add on a handler block object */

            /* Receive the gyroscope data on this block */
            [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue]
             withHandler:^(CMGyroData *gyroData, NSError *error)
            {
                NSString *x = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.x];
                self.gyro_xaxis.text = x;

                NSString *y = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.y];
                self.gyro_yaxis.text = y;

                NSString *z = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.z];
                self.gyro_zaxis.text = z;
            }];
        }
    }
    else
    {
        NSLog(@"Gyroscope not Available!");
    }

As the code says, first I create an instance of motion manager. Then I see if the device supports Gyroscope. If not die gracefully, else set gyroscope update interval & then register to get updates from gyroscope. With these updates you need to define your custom logic of what you want to do with the values. That's it you are good to go...

like image 177
Srikar Appalaraju Avatar answered Sep 19 '22 06:09

Srikar Appalaraju