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

how to construct a vector from quaternion

asked 2012-06-14 04:05:27 -0500

blejzzz gravatar image

Messages like Pose or PoseStamped have a orientation which is a quaternion. I would like to construct a vector3 of length 1 facing in the direction of the orientation from the quaternion. Is there a helper function in tf to acomplish this?


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-06-14 04:55:14 -0500

Lorenz gravatar image

When using the TF library, you can use the method tf::quatRotate to rotate your vector by the rotation represented by the quaternion. E.g. the following code rotates your vector by 180 degrees around the x axis:

tf::Quaternion rotation(1, 0, 0, 0);
tf::Vector3 vector(0, 1, 0);
tf::Vector3 rotated_vector = tf::quatRotate(rotation, vector);

Don't forget to include tf/tf.h.

edit flag offensive delete link more


so for instance if i set a goal in rviz and use your code (and replace the values of rotation with the goal orientation) and construct a new point like so: goal.position + rotatedVector; it will result in a point 1 meter forward in the goal direction/orientation?

blejzzz gravatar image blejzzz  ( 2012-06-14 05:25:41 -0500 )edit

Yes. I think so. Alternatively, you can convert your goal to a tf::Stamped<tf::Pose> and multiply it with another tf::Pose with origin 1 0 0 and rotation 0 0 0 1. Same maths but less code. Also see for convertion functions:

Lorenz gravatar image Lorenz  ( 2012-06-14 05:35:34 -0500 )edit

it works. thanks alot! Just one note in electric quatRotate isnt in tf namespace it is in btQuaternion.h.

blejzzz gravatar image blejzzz  ( 2012-06-15 06:30:02 -0500 )edit

Question Tools


Asked: 2012-06-14 04:05:27 -0500

Seen: 4,846 times

Last updated: Jun 14 '12