TF quaternion rotation problem

My goal is to rotate a quaternion by a yaw angle. q1 is the quaternion that I want to rotate

q1 = quaternion_from_euler(math.pi/2, math.pi/4, math.pi/4)

I want to rotate q1 by 1/4 pi yaw so I create another quaternion q2 for the purpose

q2 = quaternion_from_euler(0, 0, math.pi/4)

I then rotate q1 and I expect the result to be (pi/2, pi/4, pi/2) roll-pitch-yaw

q3 = quaternion_multiply(q1, q2)

But when I convert the result to Euler below is the value.

e3 = euler_from_quaternion(q3)
print e3
(1.5707963267948966, -0.0, 0.7853981633974484)

Looks like it rotates pitch by -pi/4 instead of the yaw by pi/4. Am I doing something wrong? Thanks for any help.

edit retag close merge delete

Sort by » oldest newest most voted Try q3 = quaternion_multiply(q2, q1). Order matters.

more To expand on Andy's answer, the order determines whether you're applying a global or local rotation.

q3 = quaternion_multiply(q1, q2)

Here q3 is the local rotation of q2 on q1
or
the global rotation of q1 on q2

q3 = quaternion_multiply(q2, q1)

Here q3 is the local rotation of q1 on q2
or
the global rotation of q2 on q1

In your case, you wanted to apply a global rotation of q2 on q1 -
i.e. apply the rotation q2 according to q1's parent coordinate frame (the coordinate frame on which q1 was applied initially)

Further reading / maths:

more