当前位置:   article > 正文

【ROS2】中级:tf2- 介绍

ros2 tf echo

目标:运行一个 turtlesim 演示,并通过使用 turtlesim 的多机器人示例,看到 tf2 的一些功能。

教程级别:中级

 时间:10 分钟

 目录

  •  安装演示

  •  运行演示

  •  发生了什么事?

  •  tf2 工具

    •  1. 使用视图框架

    •  2. 使用 tf2_echo

  •  rviz2 和 tf2

安装演示

让我们开始安装演示包及其依赖项。

Ubuntu包:

sudo apt-get install ros-jazzy-rviz2 ros-jazzy-turtle-tf2-py ros-jazzy-tf2-ros ros-jazzy-tf2-tools ros-jazzy-turtlesim

From source:

git clone https://github.com/ros/geometry_tutorials.git -b ros2
  1. cxy@ubuntu2404-cxy:~/ros2_ws/src$ git clone https://github.com/ros/geometry_tutorials.git -b ros2
  2. 正克隆到 'geometry_tutorials'...
  3. remote: Enumerating objects: 931, done.
  4. remote: Counting objects: 100% (345/345), done.
  5. remote: Compressing objects: 100% (153/153), done.
  6. remote: Total 931 (delta 234), reused 254 (delta 186), pack-reused 586
  7. 接收对象中: 100% (931/931), 185.63 KiB | 141.00 KiB/s, 完成.
  8. 处理 delta 中: 100% (530/530), 完成.
  9. cxy@ubuntu2404-cxy:~/ros2_ws/src$ cd ..
  10. cxy@ubuntu2404-cxy:~/ros2_ws$ colcon build --packages-select turtle_tf2_py
  11. Starting >>> turtle_tf2_py
  12. Finished <<< turtle_tf2_py [7.04s]
  13. Summary: 1 package finished [7.27s]
  14. cxy@ubuntu2404-cxy:~/ros2_ws$ . install/setup.bash
  15. cxy@ubuntu2404-cxy:~/ros2_ws$ ros2 launch turtle_tf2_py turtle_tf2_demo.launch.py
  16. [INFO] [launch]: All log files can be found below /home/cxy/.ros/log/2024-07-10-21-38-28-986301-ubuntu2404-cxy-6658
  17. [INFO] [launch]: Default logging verbosity is set to INFO
  18. [INFO] [turtlesim_node-1]: process started with pid [6661]
  19. [INFO] [turtle_tf2_broadcaster-2]: process started with pid [6662]
  20. [INFO] [turtle_tf2_broadcaster-3]: process started with pid [6663]
  21. [INFO] [turtle_tf2_listener-4]: process started with pid [6664]
  22. [turtlesim_node-1] [INFO] [1720618714.006422470] [sim]: Starting turtlesim with node name /sim
  23. [turtlesim_node-1] [INFO] [1720618714.333972230] [sim]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]
  24. [turtlesim_node-1] [INFO] [1720618716.096310214] [sim]: Spawning turtle [turtle2] at x=[4.000000], y=[2.000000], theta=[0.000000]
  25. [turtle_tf2_listener-4] [INFO] [1720618717.127165870] [listener]: Successfully spawned turtle2

运行演示

现在我们已经安装了 turtle_tf2_py 教程包,让我们运行演示。首先,打开一个新的终端并且源您的 ROS 2 安装,以便 ros2 命令能够工作。然后运行以下命令:

ros2 launch turtle_tf2_py turtle_tf2_demo.launch.py

您将看到 turtlesim 以两只乌龟开始。

9afd92f3c32b4d53080ae4ae5d649376.png

在第二个终端窗口中输入以下命令:

ros2 run turtlesim turtle_teleop_key

一旦启动了 turtlesim,您就可以使用键盘箭头键在 turtlesim 中驾驶中央的乌龟,选择第二个终端窗口,以便捕获您的击键来驾驶乌龟。

5516405602e108b581583fbb91a2d1ee.png

你可以看到一只乌龟在不断地移动,以跟随你正在驾驶的那只乌龟。

发生了什么事?

这个演示使用 tf2 库创建三个坐标框架:一个 world 框架,一个 turtle1 框架和一个 turtle2 框架。本教程使用 tf2 广播器发布乌龟坐标框架,并使用 tf2 监听器计算乌龟框架之间的差异,并移动一个乌龟以跟随另一个乌龟

工具

现在让我们看看 tf2 是如何被用来创建这个演示的。我们可以使用 tf2_tools 来查看 tf2 在幕后的操作。

使用 view_frames

view_frames 可以创建 tf2 通过 ROS 广播的帧的图表。请注意,这个工具只适用于 Linux;如果您使用的是 Windows,请跳过以下的“使用 tf2_echo”部分。

  1. cxy@ubuntu2404-cxy:~$ ros2 run tf2_tools view_frames
  2. [INFO] [1720619364.100777916] [view_frames]: Listening to tf data for 5.0 seconds...
  3. [INFO] [1720619369.109117862] [view_frames]: Generating graph...
  4. [INFO] [1720619369.122628719] [view_frames]: Result:tf2_msgs.srv.FrameGraph_Response(frame_yaml="turtle1: \n parent: 'world'\n broadcaster: 'default_authority'\n rate: 62.661\n most_recent_transform: 1720619369.104408\n oldest_transform: 1720619364.077389\n buffer_length: 5.027\nturtle2: \n parent: 'world'\n broadcaster: 'default_authority'\n rate: 62.661\n most_recent_transform: 1720619369.104446\n oldest_transform: 1720619364.077400\n buffer_length: 5.027\n")
  5. [INFO] [1720619369.128438858] [view_frames]: Exporting graph in frames_2024-07-10_21.49.29.pdf file...

在这里,一个 tf2 监听器正在监听通过 ROS 广播的帧,并绘制一个帧如何连接的树状图。要查看该树,请用您最喜欢的 PDF 查看器打开结果 frames.pdf 。

f0ae65c8146adc7ab6e1a44f6698fb49.png

8a33e70a09957bf5ee0bbe7c99d678e3.png

在这里,我们可以看到三个由 tf2 广播的框架: world 、 turtle1 和 turtle2 。 world 框架是 turtle1 和 turtle2 框架的父框架。 view_frames 还报告了一些诊断信息,比如最旧和最新的框架变换何时被接收,以及 tf2 框架发布到 tf2 的速度有多快,以便于调试。

使用 tf2_echo

tf2_echo 报告了通过 ROS 广播的任意两个帧之间的变换。

 用法:

ros2 run tf2_ros tf2_echo [source_frame] [target_frame]

让我们看看 turtle2 框架相对于 turtle1 框架的变换,这相当于:

ros2 run tf2_ros tf2_echo turtle2 turtle1

您将看到变换显示为 tf2_echo 监听器接收通过 ROS 2 广播的帧。

  1. cxy@ubuntu2404-cxy:~$ ros2 run tf2_ros tf2_echo turtle2 turtle1
  2. [INFO] [1720620455.699949690] [tf2_echo]: Waiting for transform turtle2 -> turtle1: Invalid frame ID "turtle2" passed to canTransform argument target_frame - frame does not exist
  3. At time 1720620456.608651933
  4. - Translation: [0.000, 0.000, 0.000]
  5. - Rotation: in Quaternion [0.000, 0.000, -0.322, 0.947]
  6. - Rotation: in RPY (radian) [0.000, 0.000, -0.655]
  7. - Rotation: in RPY (degree) [0.000, 0.000, -37.538]
  8. - Matrix:
  9. 0.793 0.609 0.000 0.000
  10. -0.609 0.793 -0.000 0.000
  11. -0.000 0.000 1.000 0.000
  12. 0.000 0.000 0.000 1.000
  13. At time 1720620457.600902902
  14. - Translation: [0.000, 0.000, 0.000]
  15. - Rotation: in Quaternion [0.000, 0.000, -0.322, 0.947]
  16. - Rotation: in RPY (radian) [0.000, 0.000, -0.655]
  17. - Rotation: in RPY (degree) [0.000, 0.000, -37.538]
  18. - Matrix:
  19. 0.793 0.609 0.000 0.000
  20. -0.609 0.793 -0.000 0.000
  21. -0.000 0.000 1.000 0.000
  22.   0.000  0.000  0.000  1.000

5091e169f3b57e6409ccb0c03bfcc70d.png

当你驾驶你的乌龟四处移动时,你会看到随着两只乌龟相对于彼此移动,变形也会发生改变。

rviz2 和 tf2 

rviz2 是一个可视化工具,用于检查 tf2 帧。让我们使用 rviz2 来查看我们的海龟帧,通过使用 -d 选项启动它和一个配置文件:

ros2 run rviz2 rviz2 -d $(ros2 pkg prefix --share turtle_tf2_py)/rviz/turtle_rviz.rviz

845418d162fe07a0e252bd9df8f9ae38.png

  1. cxy@ubuntu2404-cxy:~$ cd ~/ros2_ws
  2. cxy@ubuntu2404-cxy:~/ros2_ws$ . install/setup.bash
  3. cxy@ubuntu2404-cxy:~/ros2_ws$ ros2 run rviz2 rviz2 -d $(ros2 pkg prefix --share turtle_tf2_py)/rviz/turtle_rviz.rviz
  4. [INFO] [1720620960.404437827] [rviz2]: Stereo is NOT SUPPORTED
  5. [INFO] [1720620960.404563675] [rviz2]: OpenGl version: 4.6 (GLSL 4.6)
  6. [INFO] [1720620960.558860478] [rviz2]: Stereo is NOT SUPPORTED

在侧边栏中,您将看到 tf2 广播的帧。当您驾驶乌龟在周围移动时,您将在 rviz 中看到帧的移动。

d3a6d9d71b167d15a87963e3b1c70c43.png

07e552b1704c064bd772e08267ed4258.png

3daafde1b876437d7063a528a4a64a5e.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/885227
推荐阅读
相关标签
  

闽ICP备14008679号