ros tf2包学习

rosrun tf tf_echo [reference_frame] [target_frame] :
look at the transform of the [target_frame] frame with respect to [reference_frame] frame

tf2_ros::StaticTransformBroadcaster
该类可以被用作发布静态transform。在tf2_ros工具中已被封装过的static_transform_publisher可以作为命令行工具或者roslaunch中直接发布关于两个坐标系的变换。
例如:static_transform_publisher x y z yaw pitch roll frame_id child_frame_id
static_transform_publisher x y z qx qy qz qw frame_id child_frame_id

tf广播器:

static tf2_ros::TransformBroadcaster br;
br.sendTransform(transformStamped);

tf侦听器:

tf2_ros::Buffer tfBuffer;
tf2_ros::TransformListener tfListener(tfBuffer);

此两句,创建TransformListener对象。一旦创建了侦听器,它就开始通过连接接收tf2转换,并对它们进行长达10秒的缓冲

transformStamped = tfBuffer.lookupTransform(“target frame”, “source frame”,ros::Time(0));
代表从buffer中查询最近的两个坐标系间的变换。

命令行查看两个坐标系
rosrun tf tf_echo source_frame(参考) target_frame
查看targetframe在source_frame下的位姿。

添加固定坐标系到tf2

和广播一个坐标关系一样。
ros::Time::now().toSec()将时间转换为秒

tf2和时间(如何等待一段时间当使用lookupTransform()前)

This tutorial will teach you how to get a transform at a specific time。
对于 transformStamped = tfBuffer.lookupTransform(“turtle2”, “turtle1”, ros::Time(0));
第三个参数ros::Time(0)代表从buffer中获取“最新可获取时间”的变换。
note:每一个listener都有一个buffer储存来自来自不同tf2广播者的坐标系变换关系。这些变换进入缓冲区需要一段很小的时间,所以第三个参数不应该为ros::Time::now(),一般使用ros::Time(0)就很好。

Time travel with tf2

如何得到一个坐标系(现在时刻)相对于另一个坐标系(过去时刻的)的变换。
try{
ros::Time now = ros::Time::now();
ros::Time past = now - ros::Duration(5.0);
transformStamped = tfBuffer.lookupTransform(“turtle2”, now,
“turtle1”, past,
“world”, ros::Duration(1.0))
参数:
1 Give the transform from this frame,
2 at this time …
3… to this frame,
4 at this time.
5 Specify the frame that does not change over time, in this case the “/world” frame, and
6 the time-out.

C++数据类型的转换(使用tf2转换函数)

自动转换
MT代表消息类型,DT代表数据类型
inline MT toMsg(const DT& in)
inline toMsg(const DT& in,MT& msg)
inline void fromMsg(const MT& msg, DT& out)
自定义转换
要支持转换自定义数据类型,必须提供tf2/convert.h中定义的doTransform方法的模板具体化。
template
void doTransform(const T& data_in, T& data_out, const geometry_msgs::TransformStamped& transform);

猜你喜欢

转载自blog.csdn.net/qq_41986495/article/details/85274114