30

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..

Chandan Shetty SP
  • 5,087
  • 6
  • 42
  • 63
  • I found this clear video tutorial based on the MotionGraphs example from Apple: http://www.youtube.com/watch?v=Xk5cJlhePCI – mm24 Jun 13 '13 at 10:20

4 Answers4

59

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
cooperz
  • 13
  • 3
twerdster
  • 4,977
  • 3
  • 40
  • 70
  • 2
    does anyone know after all this code, how i simply grab the x,y,z values of the gyro ?? simply? not with a matrix and stuff ? – Curnelious Aug 31 '11 at 14:12
  • @Rant What do you need to do with the gyro? Do you just want what heading youre facing? – twerdster Aug 31 '11 at 16:01
  • I liked the formatting of the GLfloat matrix on this question: http://stackoverflow.com/questions/4449565/getting-displacement-from-accelerometer-data-with-core-motion GLfloat rotMat[] = { mat.m11, mat.m21, mat.m31, 0, mat.m12, mat.m22, mat.m32, 0, mat.m13, mat.m23, mat.m33, 0, 0, 0, 0, 1 }; – drewish Nov 14 '11 at 04:11
  • @twerdster Can i know how to use ur concept to find a solution to this question http://stackoverflow.com/questions/11106102/update-rotational-data-from-the-gyroscope-with-respect-to-a-predefined-orientati – sam Jul 22 '12 at 05:44
  • deviceMotion is always null, do I miss something ? – cyrilchampier Sep 27 '12 at 17:26
  • by using the yaw,pitch and roll how can we detect device rotation and the axis about which the device is rotating. – Madhu May 16 '13 at 11:59
  • i tried it, but when you first called enableMotion, your deviceMotion and attitude will be nil.. you need to wait a little bit for motionManager to return you the non-nil deviceMotion and set that to reference attitude so what I did was delay the assignment of that reference attitude assignment until your first non-nil deviceMotion & attitude returned – Ninji Nov 19 '14 at 17:44
  • Sometimes these answers irk me as a c++ programmer... source, but no API... so don't forget #import or @import CoreMotion; – Soylent Graham Apr 15 '15 at 10:27
15

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/

SamB
  • 2,621
  • 4
  • 34
  • 39
5

CoreMotion is how to get gyroscope data. Look at CMGyrodata for raw data or use the DeviceMotion attitude and rotation rate properties.

I'd recommend watching the 'Device Motion' WWDC session if you're a registered apple developer.

Michael Behan
  • 3,433
  • 2
  • 28
  • 38
  • no, its under a non disclosure agreement so only paid up devs can get it and the sample code in it cant be reproduced publicly. It will probably be a while before there is a lot of sample gyro code out there, might be worth the $99 if you have it. – Michael Behan Jul 14 '10 at 12:16
  • 7
    The videos do not require the paid iPhone developer. Use the same apple developer account used to download the sdk. The paid membership **is** required to test on an actual device. Given that you are targeting hardware features, you will have to upgrade. – falconcreek Jul 14 '10 at 15:44
  • This said, some videos remain conidential. At the bottom of each one, there is the following line: `"These are confidential sessions -- please refrain from streaming, blogging, or taking pictures"`. Moreover, on the left column of WWDC Videos webpage it says `"The content presented within the session videos and slides is Apple Confidential Information and is subject to the Registered Apple Developer Agreement."`. Apple want these videos to be available only for developers (free or paid) because they need to agree with their conditions. – mrcendre May 09 '13 at 18:56
3

Here's some of Apple's sample code for the CoreMotion framework: http://developer.apple.com/library/ios/#samplecode/pARk/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011083

And the older style UIAccelerometer (but it's got some good sample code for working with OpenGL): http://developer.apple.com/library/ios/#samplecode/GLGravity/Listings/Classes_GLGravityView_m.html#//apple_ref/doc/uid/DTS40007327-Classes_GLGravityView_m-DontLinkElementID_6

drewish
  • 9,042
  • 9
  • 38
  • 51