You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ROS Noetic中tf2 lookup_transform返回时间戳规则及自定义需求

TF2 lookup_transform 组合变换的时间戳规则与自定义时间戳实现

组合变换的时间戳判定逻辑

当通过lookup_transform查询A→B的变换,且依赖变换链A→X→B时:

  • 若你使用Time(0)(查询最新可用变换),tf2会分别获取变换链中每个环节的最新有效变换,最终组合得到的A→B变换的时间戳,会取这些环节变换中**时间戳最大(最晚)**的那个。
  • 比如你提到的场景:A→X时间戳为T,X→B为T+1,组合后的结果时间戳就是T+1;如果A→X是T+1、X→B是T,结果时间戳则为T+1。

如何强制结果使用A→X的时间戳

要实现这个需求,不能直接用Time(0)查询A→B,需要分三步手动处理:

  1. 获取A→X的最新变换及时间戳:先查询A到X的最新变换,记录它的时间戳ts_A_X。
  2. 用该时间戳查询全链路变换:使用ts_A_X作为统一查询时间,分别获取A→X和X→B在该时间点的变换(需确保X→B在该时间点有可用数据,可设置合理超时避免报错)。
  3. 组合变换并固定时间戳:将两个变换组合得到A→B,同时将结果的时间戳设置为ts_A_X。

代码示例(C++)

// 1. 获取A到X的最新变换及时间戳
geometry_msgs::TransformStamped A_to_X = tf_buffer.lookup_transform("X", "A", tf2::TimePointZero);
tf2::TimePoint ts_A_X = A_to_X.header.stamp;

// 2. 用A→X的时间戳查询X到B的变换(设置0.1秒超时)
geometry_msgs::TransformStamped X_to_B = tf_buffer.lookup_transform("B", "X", ts_A_X, ros::Duration(0.1));

// 3. 组合变换并设置目标时间戳
geometry_msgs::TransformStamped A_to_B;
A_to_B.header.frame_id = "B";
A_to_B.child_frame_id = "A";
A_to_B.header.stamp = ts_A_X;
tf2::doTransform(A_to_X.transform, A_to_B.transform, X_to_B);

代码示例(Python)

# 1. 获取A到X的最新变换及时间戳
A_to_X = tf_buffer.lookup_transform('X', 'A', rclpy.time.Time())
ts_A_X = A_to_X.header.stamp

# 2. 用A→X的时间戳查询X到B的变换(设置0.1秒超时)
X_to_B = tf_buffer.lookup_transform('B', 'X', ts_A_X, timeout=rclpy.duration.Duration(seconds=0.1))

# 3. 组合变换并设置目标时间戳
A_to_B = tf_transformations.transform_times(A_to_X.transform, X_to_B.transform)
A_to_B.header.stamp = ts_A_X
A_to_B.header.frame_id = 'B'
A_to_B.child_frame_id = 'A'

内容的提问来源于stack exchange,提问作者Sharath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 21:45:33