版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://cloud.tencent.com/developer/article/1508808
坐标变换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
).
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)。
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
gazebo
如果不支持gpu会报如下警告,编写代码时请避免使用gpu规范的虚拟传感器!
Wrn Conversion of sensor typegpu_ray not supported.
启动环境
复习一下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,查看数据可视化:
rviz2
这时候,rviz2中什么也没有,还有一个黄色警告。
tf数据没有,map坐标应该也是不对的。怎么办?先看看,激光的坐标吧?
使用ros2 topic echo,当然也可以查看源码确定具体坐标名称:
ray_link
laser no
依旧啥也没有,然后呢?还是有黄色警告。
显示可视化,需要勾选unreliable!
laser yes
消除警告使用:
ros2 run tf2_ros static_transform_publisher 0 0 0 0 0 0 world ray_link
point
最后合起来看一下:
gazebo | rviz2 |
---|---|
gazebo | rviz |
结合odom、imu等信息和slam算法,就可以建环境地图,并使用navigation2导航了。
下面再看一下,真实传感器的数据吧。预习链接:ROS2使用lidar激光雷达(rplidar和ydlidar)
终端
scan可视化
机器人建立环境地图和导航案例,稍后补充~