tf常用函数和geometry_msgs的互相转换

来自transform_datatypes.h,这个头文件真是太有用了

1
2
typedef tf::Vector3 	tf::Point
typedef tf::Transform tf::Pose

  • static tf::Quaternion tf::createQuaternionFromRPY (double roll, double pitch, double yaw)

  • static geometry_msgs::Quaternion tf::createQuaternionMsgFromRollPitchYaw (double roll, double pitch, double yaw)

  • static geometry_msgs::Quaternion tf::createQuaternionMsgFromYaw (double yaw)

  • static double tf::getYaw (const geometry_msgs::Quaternion &orientation)

  • static void tf::transformTFToMsg (const Transform &bt, geometry_msgs::Transform &msg)

  • static void tf::transformStampedMsgToTF (const geometry_msgs::TransformStamped &msg, StampedTransform &bt)

  • static void tf::transformMsgToTF (const geometry_msgs::Transform &msg, Transform &bt)

  • static void tf::quaternionTFToMsg (const Quaternion &bt, geometry_msgs::Quaternion &msg)

  • static void tf::poseTFToMsg (const Pose &bt, geometry_msgs::Pose &msg)

  • static void tf::poseStampedMsgToTF (const geometry_msgs::PoseStamped &msg, Stamped< Pose > &bt)

1
2
3
4
5
6
#include "tf2_geometry_msgs/tf2_geometry_msgs.h"

tf2::Quaternion quat_tf;
quat_tf.setRPY(1.0, 0.0, 0.0);
geometry_msgs::Quaternion quat_msg;
tf2::convert(quat_tf, quat_msg);
1
2
3
4
5
geometry_msgs::PoseStamped  p;
// 对 p 赋值
// ......
tf2::Quaternion q;
tf2::convert(p.pose.orientation, q);
  • geometry_msgs::TransformStampedtf::StampedTransform, tf2::Transform
1
2
3
4
5
6
geometry_msgs::TransformStamped   transform;
tf::StampedTransform tfTransform;
transformStampedTFToMsg(tfTransform , transform);

tf2::Transform tf2_transform;
tf2::convert(transform.transform, tf2_transform);

tf::StampedTransformgeometry_msgs/TransformStamped的区别:前者是C++中的一个类,只能用于C++中,而后者则是ROS的消息,只依赖于ROS,与语言无关,都可以使用。