Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rotation around Y-axis relative to how the user holds the device?

I remember from WWDC that there was a talk showing a teapot in OpenGL ES, which rotated with movement of device. It appeared like the teapot stood still in space.

When the app launched, the teapot started in a specific position. Then when device got rotated, the teapot rotated too to stand still in space.

At this talk, they mentioned that we must get the "reference frame" e.g. upon app launch, which tells us how the user initially held the device.

For instance here's the accelerometer axis:

accelerometer

I want to know rotation around Y axis, but relative to how the user holds the device. So when the user holds it upright and rotates around Y, I need to know that rotation value.

I think the key is removing the gravity from the readings? Also I target iPhone 4 / 4S with gyros, but I think CoreMotion would sensor-fusion them automatically.

How could I figure out by how much the user rotated the device around the Y-axis?

like image 442
Proud Member Avatar asked Oct 22 '22 21:10

Proud Member


1 Answers

From your other question Why is this CMDeviceMotionHandler never called by CoreMotion? I know that you working on iOS 4 - things have changed slightly in iOS5. In general gyro data or even better sensor fusion of accelerometer and gyro data as done in DeviceMotion is the best approach for getting proper results.

So if you got this up and running, you will need to work with CMAttitude's multiplyByInverseOfAttitude method to get all CMDeviceMotion results relative to your reference frame. Just store a reference of the very first CMAttitude in a class member and call multiplyByInverseOfAttitude with it on all subsequent calls. Then all members of CMDeviceMotion.attitude will refer to this reference frame.

For getting the rotation around Y axis, a first approach is to take Euler angles i.e. CMAttitude.roll. If you just need to track small motions this might be fine. If motions are more extensive, you will run into trouble regarding Gimbal Lock. Then you need advanced techniques like quaternion operation to get stable results, but this sounds like an own question.

like image 178
Kay Avatar answered Nov 01 '22 11:11

Kay