How do I obtain the relative orientation given two orientations (represented by quaternions
Quaternion is another representation of axis angle. The solution is to create a new quaternion from the original quaternion that only has the needed components.
An axis angle representation can be converted to a quaternion using the following formula
q = cos(R/2); q = sin(R/2)*x; q = sin(R/2)*y; q = sin(R/2)*z;
R is the angle in radians, and
(x,y,z) represents the axis, and quaternion is (R,x,y,z).
So in order to create a new quaternion with only the pitch component you just zero out the other components and normalize the quaternion:
Quaternion q; // this is your original quaternion q.x = 0.0; q.z = 0.0; q.Normalize();
Edit based on your update:
Sensors AFAIK calculate orientation relative to gravity, in other words Y (or Z) is the direction of the gravity. You need take that into consideration. And I think you don't need to multiply it with the inverse initial orientation.
The relative orientation is obtained simply by division:
q = q0 / q1
Or, if division is not available:
q = q0 * inverse(q1)
Note that since the quaternions used to represent rotations are unit quaternions, the inverse of
q1 is simply its conjugate
q1*, and is obtained by flipping the sign of
z but not