ROS2极简总结-坐标变换-TF

本文介绍了ROS2中的TF2库,它是TF的升级版,提供更快、更高效的时间同步和坐标变换功能。TF2跟踪树状结构中的所有坐标系,支持查询过去任意时间点的转换,并提供了广播和监听机制。通过rviz2和rqt_tf_tree工具可进行可视化。同时,文章展示了如何使用tf2_ros工具进行坐标变换的发布和接收,以及性能监控。

参考:


TF2 - 概述

  • TF2 是 TF (TransForm) 库的第二代。 事实上,ROS1 中的 TF 目前在后台调用 TF2。 ROS2 仅使用 TF2。
  • TF2 跟踪树结构中的所有坐标系。
  • 与 ROS2 中的大多数功能一样,TF2 API 仍在开发中,但基本功能已经可用。
  • TF2 vs TF - 更快、更高效、新功能和更容易扩展。

*此处重点,ROS1成熟,而ROS2还是深度开发中的实验版,LTS长期支持版目前也没有5年,而是3年,此版本不适合初学者学习!

rviz2中可视化

rqt_tf_tree

设计

TF2 核心独立于 ROS & BufferCore 类提供 API


时间同步

  • 用户可以查询任意给定时间点树中任意 2 帧之间的转换 - 即使在过去!
    • 对于异步/分布式系统必不可少


广播

提供坐标之间的转换(动态/静态)。

  • 例如:robot_state_publisher 提供基于 URDF 的 TF 数据
  • 例如:用户编写的具有专用广播器的节点


监听

可以访问变换树并查找 2 帧之间的特定变换(阻塞/非阻塞)。

buffer = tf2_ros.Buffer()
transform = buffer.lookup_transform("base_link","camera_link", rclpy.time.Time()) # Blocking
transform = await buffer.lookup_transform_async("base_link","camera_link",rclpy.time.Time())  #Non-Blocking

函数原型:lookup_transform(target_frame,source_frame,time)

使用 rclpy.time.Time() 提供最新的可用转换。


工具:

tf2_ros buffer_server.exe
tf2_ros static_transform_publisher.exe
tf2_ros tf2_echo.exe
tf2_ros tf2_monitor.exe
tf2_tools view_frames.py

ros2 launch dummy_robot_bringup dummy_robot_bringup.launch.py 

rviz2

发布坐标变换数据

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

接受坐标变换数据

ros2 run tf2_ros tf2_echo foo bar

数据:

[INFO] [1628841444.349748000] [tf2_echo]: Waiting for transform foo ->  bar: Invalid frame ID "foo" passed to canTransform argument target_frame - frame does not exist
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
At time 0.0
- Translation: [1.000, 2.000, 3.000]
- Rotation: in Quaternion [-0.475, -0.076, 0.240, 0.843]
ros2 run tf2_ros tf2_monitor
Gathering data on all frames for 10 seconds...



RESULTS: for all Frames

Frames:
Frame: bar, published by <no authority available>, Average Delay: 336.186, Max Delay: 336.186
Frame: single_rrbot_camera_link, published by <no authority available>, Average Delay: 0.0112712, Max Delay: 0.0168042
Frame: single_rrbot_hokuyo_link, published by <no authority available>, Average Delay: 0.011265, Max Delay: 0.016798
Frame: single_rrbot_link1, published by <no authority available>, Average Delay: 0.0112664, Max Delay: 0.0167992
Frame: single_rrbot_link2, published by <no authority available>, Average Delay: 0.0197751, Max Delay: 0.0335286
Frame: single_rrbot_link3, published by <no authority available>, Average Delay: 0.0197761, Max Delay: 0.033529

All Broadcasters:
Node: <no authority available> 34.0131 Hz, Average Delay: 0.940838 Max Delay: 336.186



RESULTS: for all Frames

Frames:
Frame: bar, published by <no authority available>, Average Delay: 336.186, Max Delay: 336.186
Frame: single_rrbot_camera_link, published by <no authority available>, Average Delay: 0.0111137, Max Delay: 0.0169733
Frame: single_rrbot_hokuyo_link, published by <no authority available>, Average Delay: 0.0111072, Max Delay: 0.0169675
Frame: single_rrbot_link1, published by <no authority available>, Average Delay: 0.0111086, Max Delay: 0.0169683
Frame: single_rrbot_link2, published by <no authority available>, Average Delay: 0.0197954, Max Delay: 0.0335286
Frame: single_rrbot_link3, published by <no authority available>, Average Delay: 0.0197964, Max Delay: 0.033529

ros2 run tf2_tools view_frames.py


 

 

 

 

ROS2中,`tf2-web-republisher` 是一个用于将 `tf2`(Transform)数据发布到Web端的工具,通常与 `rosbridge_suite` 配合使用,以便在Web应用中访问和处理机器人坐标变换信息。通过 `tf2-web-republisher`,开发者可以将机器人系统中的坐标变换数据通过 WebSocket 协议传输到前端应用中,从而实现可视化或远程控制功能。 ### 功能概述 `tf2-web-republisher` 的主要功能是订阅 `/tf` 和 `/tf_static` 话题,并将其中的坐标变换数据通过 `rosbridge` 发送到 Web 端。这一功能在基于 Web 的机器人可视化系统中非常有用,例如结合 `RViz` 或自定义的三维可视化前端展示机器人运动轨迹、传感器坐标变换等。 ### 安装方式 在 ROS2 环境下,可以通过以下命令安装 `tf2-web-republisher`: ```bash sudo apt install ros-<ros2-distro>-tf2-web-republisher ``` 其中 `<ros2-distro>` 替换为当前使用的 ROS2 发行版名称,例如 `foxy` 或 `humble`。 ### 启动方法 启动 `tf2-web-republisher` 通常需要先运行 `rosbridge_websocket` 服务,然后运行 `tf2-web-republisher` 节点。以下是一个典型的启动流程: ```bash # 启动 rosbridge websocket 服务 ros2 run rosbridge_server rosbridge_websocket # 启动 tf2-web-republisher 节点 ros2 run tf2_web_republisher tf2_web_republisher ``` 上述命令启动后,`tf2-web-republisher` 会将 `/tf` 和 `/tf_static` 数据通过 WebSocket 提供给前端应用访问。 ### 数据访问方式 前端应用可以通过 `roslibjs` 库连接到 `rosbridge` 并订阅 `/tf` 数据。以下是一个使用 `roslibjs` 获取 `tf` 数据的示例代码: ```javascript // 创建 ROS 连接 const ros = new ROSLIB.Ros({ url: 'ws://localhost:9090' // rosbridge websocket 地址 }); // 订阅 TF 数据 const tfClient = new ROSLIB.TFClient({ ros: ros, angularThres: 0.01, transThres: 0.01 }); tfClient.on('tf_callback', function(tf) { console.log('Received TF data:', tf); }); ``` ### 项目特点 - **实时性**:支持实时传输 `/tf` 和 `/tf_static` 数据。 - **轻量化**:资源占用低,适合嵌入式设备和远程 Web 通信场景。 - **兼容性**:与 `rosbridge_suite` 深度集成,适用于多种机器人平台。 - **扩展性**:可结合 `roslibjs` 在前端实现复杂的数据处理和可视化功能。 ### 应用场景 - **机器人可视化**:在 Web 端展示机器人本体、传感器数据及坐标变换- **远程监控**:通过 Web 浏览器远程监控机器人状态。 - **教育与演示**:用于教学或演示中,展示机器人系统的坐标变换逻辑。 ### 示例配置 如果需要自定义话题名称或调整参数,可以通过 `tf2_web_republisher` 的参数配置实现。例如,在启动时指定不同的 TF 话题: ```bash ros2 run tf2_web_republisher tf2_web_republisher --ros-args -p tf_topic:=/custom_tf -p tf_static_topic:=/custom_tf_static ``` ### 性能优化建议 - **减少数据量**:通过调整 `angularThres` 和 `transThres` 参数过滤不必要的变换数据。 - **压缩传输**:启用 `rosbridge` 的压缩功能以减少带宽占用。 - **合理使用静态变换**:对于不经常变化的坐标变换,使用 `/tf_static` 话题以降低通信负载。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangrelay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值