头文件 tf,geometry_msgs
Broadcasting a Transform(广播变换)
利用tf::TransformBroadcaster sendTransform
进行坐标变换的广播tf::StampedTransform::StampedTransform( const tf::Transform & input, const ros::Time & timestamp,
const std::string & frame_id,
const std::string & child_frame_id
)
广播内容 :
第1个参数, 变换公式,Transform包括位姿pose和平移transform
第2个参数,需要给转换关系携带一个时间戳,标记为ros::Time::now();
第3个参数,需要传递parent节点的名字; //想要变成的坐标系
第4个参数,传递的是child节点的名字;
由于tf假设所有的转换都是从parent到child的(考虑原点变换),当收到child坐标系的坐标,完成转化变成parent坐标系的坐标
Transform 的构造方式:
tf::Transform::Transform(const Quaternion & q, //四元素
const Vector3 & c = Vector3(tfScalar(0), tfScalar(0), tfScalar(0)) )tf::Transform::Transform(const Matrix3x3 & b, //旋转矩阵
const Vector3 & c = Vector3(tfScalar(0), tfScalar(0),
tfScalar(0)))
tf::Transform::Transform(const Transform & othe//r)拷贝
#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
int main(int argc, char** argv){
ros::init(argc, argv, "robot_tf_publisher");
ros::NodeHandle n;
ros::Rate r(100);
tf::TransformBroadcaster broadcaster;
while(n.ok()){
broadcaster.sendTransform(
tf::StampedTransform(
tf::Transform(tf::Quaternion(0, 0, 0, 1), tf::Vector3(0.1, 0.0, 0.2)),
ros::Time::now(),"base_link", "base_laser"));
r.sleep();
}
}
Using a Transform(调用变换)
tf::TransformListener 该对象会自动订阅ROS中的tf消息,并且管理所有的变换关系数据。
回调函数void transformPoint(const tf::TransformListener& listener){ }
PointStamped 类包含Point 以及Stamp (frame_id 和 时间戳)利用
void TransformListener::transformPoint(const std::string &
target_frame,const geometry_msgs::PointStamped & stamped_in, geometry_msgs::PointStamped & stamped_out
)
void Transformer::lookupTransform(
const std::string & target_frame,
const std::string & source_frame,
const ros::Time & time,
StampedTransform & transform
)
//Get the transform between two frames by frame ID.
#include <ros/ros.h>
#include <geometry_msgs/PointStamped.h>
#include <tf/transform_listener.h>
void transformPoint(const tf::TransformListener& listener){
geometry_msgs::PointStamped laser_point;
laser_point.header.frame_id = "base_laser";
laser_point.header.stamp = ros::Time();
<strong>//just an arbitrary point in space</strong>
laser_point.point.x = 1.0;
laser_point.point.y = 0.2;
laser_point.point.z = 0.0;
}
int main(int argc, char** argv){
ros::init(argc, argv, "robot_tf_listener");
ros::NodeHandle n;
tf::TransformListener listener(ros::Duration(10));
//we'll transform a point once every second
ros::Timer timer = n.createTimer(ros::Duration(1.0), boost::bind(&transformPoint, boost::ref(listener)));
ros::spin();
}