坐标变换tf2对于发布各类传感器消息非常重要。一些环境辨识相关的传感器都是含有坐标信息,其位置与机器人的关系,对应到机器人对环境的感知,就需要坐标变换。

参考文献:

Using tf2 with ROS 2 在ROS 2中使用tf2

There is preliminary support for tf2 in ROS 2. We rely heavily on tf2 in ROS 1 to manage data about coordinate transforms, and we expect to continue to use extensively in ROS 2. ROS 2中有tf2的初步支持。依赖ROS 1中的tf2来管理坐标变换的数据,期望继续在ROS 2中广泛使用。

Here’s how to try it out. In each shell, be sure to start by sourcing the ROS 2 setup file as usual 如何使用它?在每个shell中,一定要像往常一样导入ROS 2安装文件环境(例如在Linux 或Windows上分别如下)。

(e.g. on Linux . ~/ros2_ws/install/setup.bash or on Windows call C:\dev\ros2\install\setup.bat).

Publishing transform data 发布变换数据

First run the static_transform_publisher to generate tf2 data: 首先运行static_transform_publisher以生成tf2数据:

ros2 run tf2_ros static_transform_publisher 1 2 3 0.5 0.1 -1.0 foo bar

That tool will publish a static transform from the parent frame foo to the child frame bar with (X, Y, Z) translation (1, 2, 3) and (roll, pitch, yaw) rotation (0.5, 0.1, -1.0). 该工具将发布从父帧foo到子帧bar的静态变换,其中(X,Y,Z)平移(1,2,3)和(滚动,俯仰,偏航)旋转(0.5,0.1,-1.0)。

Receiving transform data 接收变换数据

Now we can check whether it’s possible to receive that transform data with tf2_echo: 现在检查是否可以接收转换数据, 使用tf2_echo:

ros2 run tf2_ros tf2_echo foo bar

You should see repeated output similar to this: 应该看到类似于此的重复输出:

At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]

Note that tf2_echo is reporting the rotation as a quaternion as opposed to roll, pitch, and yaw. 请注意,tf2_echo将旋转角度输出为四元数而不是滚动,俯仰和偏航。


以上是基本概念,具体如何使用呢?遇到问题如何进行排查呢?来看一个案例吧:

在学习这个案例之前,需要先了解Gazebo,详细内容可以参考:Gazebo和ROS2的使用说明(部分翻译)

打开一个gazebo环境,查看其中传感器数据:

gazebo --verbose /opt/ros/dashing/share/gazebo_plugins/worlds/gazebo_ros_ray_sensor_demo.world

ROS2学习tf2坐标变换_ROS2 gazebo

如果不支持gpu会报如下警告,编写代码时请避免使用gpu规范的虚拟传感器!

[Wrn] [msgs.cc:1852] Conversion of sensor type[gpu_ray] not supported.

ROS2学习tf2坐标变换_rviz2_02 启动环境

复习一下ros2 topic指令和rqt_gui:

$ ros2 topic list 
/clock
/gpu_ray/laserscan
/gpu_ray/parameter_events
/gpu_ray/pointcloud
/gpu_ray/pointcloud2
/gpu_ray/range
/gpu_ray/rosout
/parameter_events
/ray/laserscan
/ray/parameter_events
/ray/pointcloud
/ray/pointcloud2
/ray/range
/ray/rosout
/rosout
打开rviz2,查看数据可视化:

ROS2学习tf2坐标变换_数据_03 rviz2

这时候,rviz2中什么也没有,还有一个黄色警告。

tf数据没有,map坐标应该也是不对的。怎么办?先看看,激光的坐标吧?

使用ros2 topic echo,当然也可以查看源码确定具体坐标名称:

ROS2学习tf2坐标变换_tf2_04 ray_link ROS2学习tf2坐标变换_Gazebo_05 laser no

依旧啥也没有,然后呢?还是有黄色警告。

显示可视化,需要勾选unreliable!

ROS2学习tf2坐标变换_tf2_06 laser yes

消除警告使用:

ros2 run tf2_ros static_transform_publisher 0 0 0 0 0 0 world ray_link
ROS2学习tf2坐标变换_Gazebo_07 point

最后合起来看一下:

gazebo rviz2
ROS2学习tf2坐标变换_数据_08 gazebo ROS2学习tf2坐标变换_Gazebo_09 rviz

结合odom、imu等信息和slam算法,就可以建环境地图,并使用navigation2导航了。

下面再看一下,真实传感器的数据吧。预习链接:ROS2使用lidar激光雷达(rplidar和ydlidar)

ROS2学习tf2坐标变换_rviz2_10 终端 ROS2学习tf2坐标变换_ROS2_11 scan可视化

机器人建立环境地图和导航案例,稍后补充~


 

更多文章请关注《万象专栏》