目标:运行一个 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
cxy@ubuntu2404-cxy:~/ros2_ws/src$ git clone https://github.com/ros/geometry_tutorials.git -b ros2
正克隆到 'geometry_tutorials'...
remote: Enumerating objects: 931, done.
remote: Counting objects: 100% (345/345), done.
remote: Compressing objects: 100% (153/153), done.
remote: Total 931 (delta 234), reused 254 (delta 186), pack-reused 586
接收对象中: 100% (931/931), 185.63 KiB | 141.00 KiB/s, 完成.
处理 delta 中: 100% (530/530), 完成.
cxy@ubuntu2404-cxy:~/ros2_ws/src$ cd ..
cxy@ubuntu2404-cxy:~/ros2_ws$ colcon build --packages-select turtle_tf2_py
Starting >>> turtle_tf2_py
Finished <<< turtle_tf2_py [7.04s]
Summary: 1 package finished [7.27s]
cxy@ubuntu2404-cxy:~/ros2_ws$ . install/setup.bash
cxy@ubuntu2404-cxy:~/ros2_ws$ ros2 launch turtle_tf2_py turtle_tf2_demo.launch.py
[INFO] [launch]: All log files can be found below /home/cxy/.ros/log/2024-07-10-21-38-28-986301-ubuntu2404-cxy-6658
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [turtlesim_node-1]: process started with pid [6661]
[INFO] [turtle_tf2_broadcaster-2]: process started with pid [6662]
[INFO] [turtle_tf2_broadcaster-3]: process started with pid [6663]
[INFO] [turtle_tf2_listener-4]: process started with pid [6664]
[turtlesim_node-1] [INFO] [1720618714.006422470] [sim]: Starting turtlesim with node name /sim
[turtlesim_node-1] [INFO] [1720618714.333972230] [sim]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]
[turtlesim_node-1] [INFO] [1720618716.096310214] [sim]: Spawning turtle [turtle2] at x=[4.000000], y=[2.000000], theta=[0.000000]
[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 以两只乌龟开始。
在第二个终端窗口中输入以下命令:
ros2 run turtlesim turtle_teleop_key
一旦启动了 turtlesim,您就可以使用键盘箭头键在 turtlesim 中驾驶中央的乌龟,选择第二个终端窗口,以便捕获您的击键来驾驶乌龟。
你可以看到一只乌龟在不断地移动,以跟随你正在驾驶的那只乌龟。
发生了什么事?
这个演示使用 tf2 库创建三个坐标框架:一个 world
框架,一个 turtle1
框架和一个 turtle2
框架。本教程使用 tf2 广播器发布乌龟坐标框架,并使用 tf2 监听器计算乌龟框架之间的差异,并移动一个乌龟以跟随另一个乌龟。
工具
现在让我们看看 tf2 是如何被用来创建这个演示的。我们可以使用 tf2_tools
来查看 tf2 在幕后的操作。
使用 view_frames
view_frames
可以创建 tf2 通过 ROS 广播的帧的图表。请注意,这个工具只适用于 Linux;如果您使用的是 Windows,请跳过以下的“使用 tf2_echo”部分。
cxy@ubuntu2404-cxy:~$ ros2 run tf2_tools view_frames
[INFO] [1720619364.100777916] [view_frames]: Listening to tf data for 5.0 seconds...
[INFO] [1720619369.109117862] [view_frames]: Generating graph...
[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")
[INFO] [1720619369.128438858] [view_frames]: Exporting graph in frames_2024-07-10_21.49.29.pdf file...
在这里,一个 tf2 监听器正在监听通过 ROS 广播的帧,并绘制一个帧如何连接的树状图。要查看该树,请用您最喜欢的 PDF 查看器打开结果 frames.pdf
。
在这里,我们可以看到三个由 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 广播的帧。
cxy@ubuntu2404-cxy:~$ ros2 run tf2_ros tf2_echo turtle2 turtle1
[INFO] [1720620455.699949690] [tf2_echo]: Waiting for transform turtle2 -> turtle1: Invalid frame ID "turtle2" passed to canTransform argument target_frame - frame does not exist
At time 1720620456.608651933
- Translation: [0.000, 0.000, 0.000]
- Rotation: in Quaternion [0.000, 0.000, -0.322, 0.947]
- Rotation: in RPY (radian) [0.000, 0.000, -0.655]
- Rotation: in RPY (degree) [0.000, 0.000, -37.538]
- Matrix:
0.793 0.609 0.000 0.000
-0.609 0.793 -0.000 0.000
-0.000 0.000 1.000 0.000
0.000 0.000 0.000 1.000
At time 1720620457.600902902
- Translation: [0.000, 0.000, 0.000]
- Rotation: in Quaternion [0.000, 0.000, -0.322, 0.947]
- Rotation: in RPY (radian) [0.000, 0.000, -0.655]
- Rotation: in RPY (degree) [0.000, 0.000, -37.538]
- Matrix:
0.793 0.609 0.000 0.000
-0.609 0.793 -0.000 0.000
-0.000 0.000 1.000 0.000
0.000 0.000 0.000 1.000
当你驾驶你的乌龟四处移动时,你会看到随着两只乌龟相对于彼此移动,变形也会发生改变。
rviz2 和 tf2
rviz2
是一个可视化工具,用于检查 tf2 帧。让我们使用 rviz2
来查看我们的海龟帧,通过使用 -d
选项启动它和一个配置文件:
ros2 run rviz2 rviz2 -d $(ros2 pkg prefix --share turtle_tf2_py)/rviz/turtle_rviz.rviz
cxy@ubuntu2404-cxy:~$ cd ~/ros2_ws
cxy@ubuntu2404-cxy:~/ros2_ws$ . install/setup.bash
cxy@ubuntu2404-cxy:~/ros2_ws$ ros2 run rviz2 rviz2 -d $(ros2 pkg prefix --share turtle_tf2_py)/rviz/turtle_rviz.rviz
[INFO] [1720620960.404437827] [rviz2]: Stereo is NOT SUPPORTED
[INFO] [1720620960.404563675] [rviz2]: OpenGl version: 4.6 (GLSL 4.6)
[INFO] [1720620960.558860478] [rviz2]: Stereo is NOT SUPPORTED
在侧边栏中,您将看到 tf2 广播的帧。当您驾驶乌龟在周围移动时,您将在 rviz 中看到帧的移动。