ros2 从零开始30 使用时间c++

发布时间:2026/7/13 8:33:17
ros2 从零开始30 使用时间c++ ros2 从零开始30 使用时间c前言背景在之前的教程中我们通过编写一个 tf2 广播器和一个 tf2 监听器重新创建了乌龟演示程序。我们还学习了如何向变换树中添加新的坐标系以及 tf2 如何跟踪一个坐标系树的变换关系。这个树会随时间变化tf2 会为每个变换存储一个时间快照默认最多保存 10 秒。到目前为止我们一直使用 lookupTransform() 函数来获取 tf2 树中可用的最新变换而不知道该变换是在什么时间记录的。本教程将教你如何获取特定时间的变换。实践本次不另外创建包我们使用《编写静态广播C》。1.更新turtle_tf2_listener.cpp在learning_tf2_cpp/src目录下打开文件turtle_tf2_listener.cpp,找到使用lookupTransform的地方t tf_buffer_-lookupTransform( toFrameRel, fromFrameRel, tf2::TimePointZero);[!TIP]tf2包有自己的时间类型tf2::TimePoint与rclcpp::Time不同tf2_ros的许多API接口都会自动转换tf2::TimePoint与rclcpp::Timerclcpp::Time(0, 0, this-get_clock()-get_clock_type())在tf2_ros使用时无例外地会被转换为tf2::TimePointZero我们指定时间参数为tf2::TimePointZero也就是0 对于tf2, 时间0代表最新后有效的变换。现在修改成当前时间this-get_clock()-now()rclcpp::Time now this-get_clock()-now(); t tf_buffer_-lookupTransform( toFrameRel, fromFrameRel, now);编译运行后我们看看输出rootbc2bf85b2e4a:~/ros2_ws# ros2 launch learning_tf2_cpp turtle_tf2_demo_launch.xml[turtle_tf2_listener-4][INFO][1782209830.117096765][listener]: Could not transform turtle2 to turtle1: Lookup would require extrapolation into the future. Requestedtime1782209830.116734but the latest data is attime1782209830.109609, when looking up transform from frame[turtle1]to frame[turtle2][turtle_tf2_listener-4][INFO][1782209831.118229913][listener]: Could not transform turtle2 to turtle1: Lookup would require extrapolation into the future. Requestedtime1782209831.118175but the latest data is attime1782209831.116723, when looking up transform from frame[turtle1]to frame[turtle2][turtle_tf2_listener-4][INFO][1782209832.120139239][listener]: Could not transform turtle2 to turtle1: Lookup would require extrapolation into the future. Requestedtime1782209832.120042but the latest data is attime1782209832.108938, when looking up transform from frame[turtle1]to frame[turtle2]输出的信息告诉我们帧数据不存在或者这个数据是未来时间点的。为了理解为什么会发生这种情况我们需要了解缓冲区是如何工作的。首先每个监听器都有一个缓冲区用于存储来自不同tf2广播器的所有坐标变换。其次当广播器发送一个变换时该变换需要一段时间通常几毫秒才能进入缓冲区。因此当你在当前时间请求一个帧变换时你应该等待几毫秒等待该信息到达。2 修复该问题tf2 提供一个很好的方法就是等待这个数据可用。我们将使用lookupTransform的超时参数修改如下rclcpp::Time now this-get_clock()-now(); t tf_buffer_-lookupTransform( toFrameRel, fromFrameRel, now, 50ms);现在lookupTransform会阻塞等待50ms。3 检查结果重新编译运行查看结果。rootbc2bf85b2e4a:~/ros2_ws# ros2 launch learning_tf2_cpp turtle_tf2_demo_launch.xml你应该注意到lookupTransform()确实会阻塞直到两只乌龟的帧数据有效通常会消耗几毫秒一旦达到超时50ms仍然无数据可用则依然会抛出异常。总结这个章节介绍了lookupTransform()的超时机制。