MATLAB: Simscape Multibody quaternion calculation does not match hand calculation

Simscape Multibody

I have noticed a potential issue regarding the Simscape block "Transform Sensor".
I am simulating the rotation of a simple body by using a gimbal joint. The inputs into the gimbal joint are three desired angles (roll, pitch, yaw). Now I am using the "Transform Sensor" to output the quaternion "Q" describing the motion between the base frame (World) and the follower frame (Rotating Body). At the same time, I am calculating the quaternion via a MATLAB Function block with the equations I found from reference to convert Euler angles to quaternion. For example, see the section "Euler angles to quaternion conversion" in the following link:
Now if I compare the quaternion entries computed by the Transform Sensor block and the quaternion computed from the Euler angle equations I use, there is no difference as long as I only have a single rotation about roll, pitch, or yaw. But once I combine two or more movements, there is an error between the two quaternion calculations.
Can you explain the reason for the difference? Is there a different convention I need to apply? Or do I have to change the settings of the transform sensor?

Best Answer

I suspect the reason for this is due to a difference in convention in how the Euler angle rotations are ordered. Euler angles are non-unique and order of rotation matters. Rotating about x first then y will give a different resulting orientation then rotating by y then x. In Simscape Multibody for the Gimbal joint, it appears the convention for order of rotation is x,y, then z.
Looking over the Wikipedia article it appears that they use an order of rotation of z, y, then x. The reverse of that used for Simscape Multibody.
This may be the potential reason for the difference and is consistent with your description. This would cause any rotation in any single dimension to be the same but the results will differ when combining rotations in two or more directions.