ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

It's funny you should ask this question. I have been looking into something similar for a quadrotor. Specifically I want to extract the yaw rotation from a quaternion for some simple visual servoing. At this point I believe this can be done by the following procedure:

1) Set the x and y components of the quaternion to 0 2) Normalize the quaternion. In pseudo-code

mag = sqrt(q[0]^2 + q[4]^2)
q[0] /= mag

3) Convert the quaternion to a rotation matrix

It's funny you should ask this question. I have been looking into something similar for a quadrotor. Specifically I want to extract the yaw rotation from a quaternion for some simple visual servoing. At this point I believe this can be done by the following procedure:

1) Set the x and y components of the quaternion to 0 2) Normalize the quaternion. In pseudo-code

mag = sqrt(q[0]^2 + q[4]^2)
q[0] /= mag
q[1] /= mag

3) Convert the quaternion to a rotation matrix

matrix. Again in pseudo-code (as per the Rotation matrices section of Conversion between quaternions and Euler angles -- Wikipedia

R[0][0] = q[0]^2 + q[1]^2 - q[2]^2 - q[3]^2
R[1][2] = 2*(q[1]*q[2] - q[0]*q[3])
R[1][3] = 2*(q[1]*q[3] + q[0]*q[2])
R[2][1] = 2*(q[1]*q[2] + q[0]*q[3])
R[2][2] = q[0]^2 - q[1]^2 + q[2]^2 - q[3]^2
R[2][3] = 2*(q[2]*q[3] - q[0]*q[1])
R[3][1] = 2*(q[1]*q[3] - q[0]*q[2])
R[3][2] = 2*(q[2]*q[3] + q[0]*q[1])
R[3][3] = q[0]^2 - q[1]^2 - q[2]^2 + q[3]^2

If you actually want all the Euler angles then the Quaternion section of the Rotation Matrix article may be helpful. However beware that the angles you get out may not be accurate due to the shortcomings of Euler angles. For example, if you create a rotation matrix R = RotZ(pi/2)*RotY(pi/2), convert it to a quaternion and convert it back you will get RotY(pi/2). I.e. the process doesn't return pi/2 for the rotation about z.

It's funny you should ask this question. I have been looking into something similar for a quadrotor. Specifically I want to extract the yaw rotation from a quaternion for some simple visual servoing. At this point I believe this can be done by the following procedure:

1) Set the x and y components of the quaternion to 0 2) Normalize the quaternion. In pseudo-code

mag = sqrt(q[0]^2 + q[4]^2)
q[0] /= mag
q[1] /= mag

3) Convert the quaternion to a rotation matrix. Again in pseudo-code (as per the Rotation matrices section of Conversion between quaternions and Euler angles -- Wikipedia

R[0][0] = q[0]^2 + q[1]^2 - q[2]^2 - q[3]^2
R[1][2] = 2*(q[1]*q[2] - q[0]*q[3])
R[1][3] = 2*(q[1]*q[3] + q[0]*q[2])
R[2][1] = 2*(q[1]*q[2] + q[0]*q[3])
R[2][2] = q[0]^2 - q[1]^2 + q[2]^2 - q[3]^2
R[2][3] = 2*(q[2]*q[3] - q[0]*q[1])
R[3][1] = 2*(q[1]*q[3] - q[0]*q[2])
R[3][2] = 2*(q[2]*q[3] + q[0]*q[1])
R[3][3] = q[0]^2 - q[1]^2 - q[2]^2 + q[3]^2

If you actually want all the Euler angles then the Quaternion section of the Rotation MatrixMatrix -- Wikipedia article may be helpful. However beware that the angles you get out may not be accurate due to the shortcomings of Euler angles. For example, if you create a rotation matrix R = RotZ(pi/2)*RotY(pi/2), convert it to a quaternion and convert it back you will get RotY(pi/2). I.e. the process doesn't return pi/2 for the rotation about z.