# Problem with calculating relative orientation

I am using an IMU which provides absolute orientation of the sensors frame $$S$$ relative to an earth-fixed frame $$N$$ in quaternion form, $$^S_Nq$$. In my experiments, I first obtain an initial orientation (when the sensor is relatively static) $$^I_Nq$$, and I would like to obtain the relative orientation of the sensor in the form of ZYX Euler angles (more precisely $$ZY^{'}X^{''}$$).

Here is the procedure that I have tried:
First, I invert the initial orientation,
$$^N_Iq$$ = $$^I_Nq^{-1}$$
then, I use the result the obtain the relative quaternion as follows,
$$^S_Iq$$ = $$^S_Nq$$ $$\otimes$$ $$^N_Iq$$
Finally, to visualize the results, I convert the relative orientation to Euler angles. I also have reference trajectories calculated in a motion capture software which uses the same data. However, my result looks completely different (and wrong) as seen below,
Calculated vs. reference relative orientation

Curiously, if I manually set the $$Z$$ and $$Y$$ rotations of $$^I_Nq$$ to zero (and then convert the result back to quaternion form), the angle trajectories match exactly (except for the offset of $$Z$$ and $$Y$$).
Result with setting the first two rotations of initial orientation set to zero
What am I doing wrong?

By the way, this is the MATLAB code that I'm using. Note that initQ is $$^I_Nq$$ and relQ is $$^S_Iq$$.

% Average quaternion using meanrot to obtain initial orientation.
[q(1), q(2), q(3), q(4)] = parts(meanrot(quaternion(initData));
initQ = q;

% The second method, if I manually set the first two rotations to zero.
% initEul = quat2eul(q,'ZYX');
% initEul(2) = 0;
% initEul(1) = 0;
% initQ = eul2quat(initEul,'ZYX');

relQ = quatmultiply(mocapData,quatinv(initQ));
eulerAngles = quat2eul(quaternion(relQ),'ZYX')*180/pi;

• I believe you reversed the quaternion multiplication. So instead of ${}^S_Iq = {}^S_Nq \otimes {}^N_Iq$ you should use ${}^S_Iq = {}^N_Iq \otimes {}^S_Nq$. – fibonatic Jan 3 at 12:09
• I don't think so. For reference, see Eq. 5 in the paper "INS algorithm using quaternion model for low cost IMU". – m278 Jan 3 at 19:48

## 1 Answer

Referencing book "Robotics Modelling, Planning and control" by Bruno Siciliano from Springer publication. Chapter 3.7 Orientation error i.e relative orientation.

Relative Eulers angle can only use if you have a spherical joint i.e 3-revolute joints each abut different perpendicular axis. Check your configuration before using Eulers angle. For more refer referenced book to understand more.

Check out the similar solved question- Inverse kinematic orientation problem

• I don't see how that applies to my problem. That section is concerned with solving inverse kinematics of robots and it shows that Euler angles can be easily obtained in the case of the spherical wrist, but it suggests other representations for general mechanisms. In my case the IMU can freely rotate around any axis so in fact it can be considered a spherical joint and the problem is much simpler than what's being discussed in that book. – m278 Jan 1 at 17:12
• As you have a spherical joint, you are good with Euler angles. Verify your angles using this webiste andre-gaschler.com/rotationconverter . Also check eulerAngles = quat2eul(quaternion(relQ),'ZYX')*180/pi in this equation, Why are you writing quaternion(relQ), as relQ is already a quaternion. – manan kalsariya Jan 2 at 10:56