How to calculate the angle between Quaternions

448 Views Asked by At

I have two IMU sensors giving me Euler angles. I convert them to Quaternion by following

quaternion_angle_1=tf.transformations.quaternion_from_euler(roll_1,pitch_1,yaw_1)
quaternion_angle_2=tf.transformations.quaternion_from_euler(roll_2,pitch_2,yaw_2)

Now I want to caculate the angle between these measurements from the IMU sensors. How can I do that?

2

There are 2 best solutions below

0
On

Sounds like you want the relative rotation between two quaternions. You could simple calculate the angle between the euler angles, then convert that to a quaternion. But if you need to do it strictly with quaternions tf allows you to do this directly:

relative_quat = quaternion_angle_1 * quaternion_angle_2.inverse()
0
On

An amazing python library for all quaternion/euler/matrix conversions and operations is transforms3d. To got from quaternion a, to quaternion b, you could just do -

quat_c = tf3.quaternions.qmult(quat_b, tf3.quaternions.qconjugate(quat_a))

Here is a github gist for the above example.

Or if you are using tf/transformations.py,

quat_c = quaternion_multiply(quat_b, quaternion_conjugate(quat_a))

Quaternion multiplication involves taking a hamiltonian product, therefore simply taking the dot/cross/element-wise multiplication of two quaternions will not be enough.