ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
The cylinder's axis if unrotated is originally pointing along the z axis of the referential so that will be the up direction (0, 0 1).
The cross product of the desired cylinder axis (normalized) and the up vector will give us the axis of rotation, perpendicular to the plane defined by the cylinder axis and the up vector.
The dot product of the cylinder axis and the up vector will provide the angle of rotation.
The following code snippet shows how to do this in ROS:
tf::Vector3 axis_vector(coef_cylinder_->values[3], coef_cylinder_->values[4], coef_cylinder_->values[5]);
tf::Vector3 up_vector(0.0, 0.0, 1.0);
tf::Vector3 right_vector = axis_vector.cross(up_vector);
right_vector.normalized();
tf::Quaternion q(right_vector, -1.0*acos(axis_vector.dot(up_vector)));
q.normalize();
geometry_msgs::Quaternion cylinder_orientation;
tf::quaternionTFToMsg(q, cylinder_orientation);
//(...)
marker.pose.orientation = cylinder_orientation;