Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple gyroscope sample code

I am planning to develop an gyroscope based project like rotating an opengl texture using gyroscope data, is there any sample code released from apple about gyroscope or any tutorial about integrating gyroscope with openGL... I searched google i didn't find anything except core motion guide and event handling guide.

Updated: Please let me know if any sample available..

like image 718
Chandan Shetty SP Avatar asked Jul 14 '10 11:07

Chandan Shetty SP


People also ask

How do I get a gyroscope on my iPhone?

The feature can be found on your iPhone or iPad, in Settings > Safari > Privacy & Security > Motion & Orientation Access, and it's disabled by default.

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.

What gyroscope does iPhone use?

The iPhone 4 utilizes a microscopic, electronic version of a vibrational gyroscope, called a MEMS gyroscope. A microelectromechanical system (MEMS) is an embedded system that integrates electronic and mechanical components at a very small scale.

What is gyroscope XYZ?

The Gyroscope block has three output ports: X , Y , and Z . Each port outputs a single -precision scalar value. For any positive axis on the device, clockwise rotation outputs negative values, and counterclockwise rotation outputs positive values.


2 Answers

To get gyro updates you need to create a motion manager object and optionally (but recommended) a reference attitude object

So in your interface definition you add:

CMMotionManager *motionManager; CMAttitude *referenceAttitude; 

According to the docs you should only create one of these managers per application. I recommend making the motionManager accesible through a singleton but thats some extra work which you might not need to do if you only instantiate your class once.

Then in your init method you should allocate the motion manager object like so;

motionManager = [[CMMotionManager alloc] init]; referenceAttitude = nil;  

When you want to enable the motion updates you could create an enableMotion method or just call it from the init method. The following will store the initial device attitude and cause the device to continue sampling the gyro and updating its attitude property.

-(void) enableMotion{         CMDeviceMotion *deviceMotion = motionManager.deviceMotion;               CMAttitude *attitude = deviceMotion.attitude;         referenceAttitude = [attitude retain];         [motionManager startDeviceMotionUpdates]; } 

For virtual reality applications using the gyro and OpenGL is pretty simple. You need to get the current gyro attitude (rotation) and then store it in an OpenGL compatible matrix. The code below retrieves and saves the current device motion.

GLfloat rotMatrix[16];  -(void) getDeviceGLRotationMatrix  {         CMDeviceMotion *deviceMotion = motionManager.deviceMotion;               CMAttitude *attitude = deviceMotion.attitude;          if (referenceAttitude != nil) [attitude multiplyByInverseOfAttitude:referenceAttitude];         CMRotationMatrix rot=attitude.rotationMatrix;         rotMatrix[0]=rot.m11; rotMatrix[1]=rot.m21; rotMatrix[2]=rot.m31;  rotMatrix[3]=0;         rotMatrix[4]=rot.m12; rotMatrix[5]=rot.m22; rotMatrix[6]=rot.m32;  rotMatrix[7]=0;         rotMatrix[8]=rot.m13; rotMatrix[9]=rot.m23; rotMatrix[10]=rot.m33; rotMatrix[11]=0;         rotMatrix[12]=0;      rotMatrix[13]=0;      rotMatrix[14]=0;       rotMatrix[15]=1; } 

Depending on what you want to do with that you may have to invert it which is very easy. The inverse of a rotation is just its transpose which means swapping the columns and rows. So the above becomes:

rotMatrix[0]=rot.m11; rotMatrix[4]=rot.m21; rotMatrix[8]=rot.m31;  rotMatrix[12]=0; rotMatrix[1]=rot.m12; rotMatrix[5]=rot.m22; rotMatrix[9]=rot.m32;  rotMatrix[13]=0; rotMatrix[2]=rot.m13; rotMatrix[6]=rot.m23; rotMatrix[10]=rot.m33; rotMatrix[14]=0; rotMatrix[3]=0;       rotMatrix[7]=0;       rotMatrix[11]=0;       rotMatrix[15]=1; 

If you want the yaw, pitch and roll angles then you can access them easily using

attitude.yaw attitude.pitch attitude.roll 
like image 61
twerdster Avatar answered Sep 29 '22 16:09

twerdster


I have been searching for some sample code as a very simple project. After several days of searching, I finally found it. Here you go you guys!

http://cs491f10.wordpress.com/2010/10/28/core-motion-gyroscope-example/

like image 36
SamB Avatar answered Sep 29 '22 17:09

SamB