项目场景:
tf2_ros::buffer.lookupTransform()用法及注意事项
问题描述
在tf树已经查到坐标之间的关系,但是在监听其中两个坐标之间关系时,却显示:运行报错,提示运行报错 lookupTransform base2laser error,“base_footprint” passed to lookupTransform argument target_frame does not exist.
原因分析:
// 参考tf2源码: https://docs.ros.org/en/noetic/api/tf2_ros/html/c++/classtf2__ros_1_1Buffer.html
用法: Buffer::lookupTransform(const std::string& target_frame, const std::string& source_frame,
const ros::Time& time, const ros::Duration timeout) const
作用:Get the transform between two frames by frame ID.
参数意义:
target_frame The frame to which data should be transformed(目标坐标系)
source_frame The frame where the data originated(原坐标系)
time The time at which the value of the transform is desired. (0 will get the latest)
timeout How long to block before failing(设置最长等待时间)
Returns
The transform between the frames
虽然在tf树中可以看到各个坐标之间的已经存在,但是却 Buffer::lookupTransform()时报错,主要是因为在使用时,大家都默认不填写第四个参数timeout或填写的时间比较短而导致的。这个参数表示最长等待监听时间,如果没有这个参数,那么监听一次,没有查询到数据,就失败,但是,有时候lookupTransform()函数处理时间比较长,所以导致出错。
解决方案:
Buffer::lookupTransform(const std::string& target_frame, const std::string& source_frame, const ros::Time& time, const ros::Duration timeout) const
函数第四个参数填写完整,如 ros::Duration(1)
,最长等待1s,基本就可以解决该问题。当然,可以在该函数前后设置休眠时间来等待。