# Quaternion representation of the rotation of a sphere into plane displacement

This is a mathematical problem I'm facing these days.. here you go.

I do have a sphere of known radius which does have a coordinate frame rigidly attached to it. Let's call the coordinate frame attached to the sphere XYZ_{s}. The sphere can be rotated and displaced arbitrarily with respect to a fixed coordinate frame (world) called $XYZ_w$. Let's be XYZ_{w} the coordinate frame of the world where X points the north, Z points the sky and Y is orthogonal to the other 2 axis.

I'm capable of knowing the orientation of the sphere with respect to the XYZ_{w} and I have such orientation represented using a quaternion which we'll call Q^{s}_{w} (that is the quaternion representing the orientation of XYZ_{s} with respect to XYZ_{w}). By knowing Q^{s}_{w} I can easily compute Q^{w}_{s} (that is the quaternion representing the orientation of XYZ_{w} with respect to XYZ_{s}) by computing the quaternion conjugate of Q^{s}_{w}.

Now, let's suppose to place the sphere on a XY_{w} plane, so that by rotating the sphere a displacement of XYZ_{s} with respect to XY_{w} is produced. You can visualize this by thinking of placing a ball on a table and rotating it so that it moves on then table plane.

Now, the problem is, by being able to sample Q^{s}_{w}, getting to know the displacement of XYZ_{s} on XY_{w} after an indefinite rotation.

This is a practical problem araised when using PALLA (as described in chapter 8 of my MoS thesis), a spherical device which contains an accelerometer, a gyroscope and magnetometer, from which doing a sensor fusion I'm able to compute it's orientation with respect to the world. Now, once placed it on a table with the user rotating it to displace it, I'd like to compute how much it is being displaced as described in math language above.

Of course, I'm not looking for the solution to the problem, I'm just looking for hints on possible paths to the solution.

Any suggestions?

**UPDATE 2011-06-16:** I had to ask this on http://math.stackexchange.com/ as I wasn't without ideas. Steven Stadnicki replied me the following:

If I understand the problem correctly (that is, for a given orientation of the sphere, you're asking 'what rolling displacement gave this orientation'), then I'm afraid the problem has no answer; in fact, it's possible to end up in any orientation of the sphere anywhere on the plane. The rough explanation for this is because of the non-commutativity of the spherical rotations (or equivalently, if more complicatedly, the non-vanishing of the Riemann curvature tensor); essentially, rolling the sphere in a small square (up-Y, right-X, down-Y, left-X) will leave it in a different orientation than it started, and by composing various of these 'identity rolls' you can get the sphere back to its starting point in any orientation.

From a more practical perspective, though, things shouldn't be too bad because you have relatively continuous sensor readings. At each step you can find the minimal rotation that carries the sphere from one orientation to the next recorded by essentially taking the product of the 'new' quaternion orientation with the inverse of the previous orientation; that will give you the axis of instantaneous rotation, and the cross product of that with the planar normal will give the (literal) translation of the motion down to the plane (suitably scaled). This won't be 100% accurate, though, and worse it'll get progressively less accurate over time, so you may need some sort of spatial (translational) tracking in addition, at least to be able to 'reset' the sphere's position every few thousand frames.

I'll let you know how this goes..

## Update on gimbal lock

Hmmm. I created a video of the issue and also replicated it using the Toxilibs meaning you are correct about the Euler Angles not introducing gimbal lock. I'm not sure what this is? Could it be the filter getting confused?

See here:

http://www.youtube.com/watch?v=8pqqRxnurek

## One more thing

I'm now working on adding a tilt compensation to your library for the magnetometer. I believe that will clean up some of the yaw/roll wandering when the imu is at any angle other than level.

## Some ideas

Hi Fabio

I've just finished refactoring your FreeIMU cube sample to use Toxilib (http://hg.postspectacular.com/toxiclibs) Quaternion class. It works famously. Your implementation (self-proclaimed 10 minute code) suffers from gimbal lock issues faced by most implementations of Euler angles. In other words, using FreeIMU (at least in my experience) and pointing the x-axis directly upward or downward with a few hand rotations to add some confusion will result in an endlessly rotating cube (like it's stuck spinning). The Toxilib (Quaternion).toAxisAngle(); conversion is pretty solid and is not susceptible. I'll repost the code after I get a few more things done. Here are a couple of key lines:

//global

import toxi.processing.*;

import toxi.geom.*;

Quaternion q = new Quaternion();

//drawCube

//replacesrotateZ(-Euler[2]);rotateX(-Euler[1]);rotateY(-Euler[0]);

axis = q.toAxisAngle();

rotate(axis[0], axis[1], axis[3], axis[2]); //note axis swapping

I'm telling you this because: 1) I think you could easily add this code to your libraries/demos, 2) I'm happy to have figured this out, and 3) I found some references to problems similar to yours (above) in my research on Quaternions. http://tanagr.am/oktu5y (no not spam)

Cheers,

J

## I never experienced the

I never experienced the gimbal lock problem you are describing, in fact given that I'm getting Euler angles from the quaternion my code shouldn't be affected by Gimbal lock. Anyway, I'll test it a bit more and I'm looking forward so have access to your implementations using toxiclibs.

Thanks for your efforts.