博客参考:https://www.ncnynl.com/archives/201702/1306.html
ROS与C++入门教程-tf-坐标变换
说明:
- 介绍在c++实现TF的坐标变换
概念:
- Coodinate Frames (坐标框架,坐标系)
- Transforms (变换)
- TF
参考:
- TF中的几何对象表示的TF类型,这是相当于相应的bullet类型;见TF数据类型。
- Bullet类参考transforms和quaternions
Frames and Points
- Frame是坐标系统,在ROS里都是以3D形式存在,右手原则,X向前,Y向左,Z向上.
- Points在坐标系以tf::Point形式表达,等同与bullet类型的btVector3。
- 在坐标系W里,坐标的点p,可以用:Wp
其余参考原文:http://wiki.ros.org/tf/Overview/Transformations
ROS与C++入门教程-tf-广播变换
说明:
- 介绍如何在roscpp的节点广播变换关系。
广播变换:
- 在节点里广播变换,推荐使用tf::TransformBroadcaster.
构造器
- 无参数构造器:
tf::TransformBroadcaster();
发送变换
- 发送变换通过调用sendTransform()函数实现,传递StampedTransform或geometry_msgs::TransformStamped为参数
- 示例代码:
void sendTransform(const StampedTransform & transform); void sendTransform(const geometry_msgs::TransformStamped & transform);
ROS与C++入门教程-tf-使用已发布的变换
说明:
- 介绍roscpp节点获取和使用TF的变换。
TransformListener
- 通过调用 tf::TransformListener类来处理变换,它继承自tf::Transformer。
核心方法
(1)构造函数
- 示例代码:
TransformListener(const ros::NodeHandle &nh, ros::Duration max_cache_time=ros::Duration(DEFAULT_CACHE_TIME), bool spin_thread=true) TransformListener(ros::Duration max_cache_time=ros::Duration(DEFAULT_CACHE_TIME), bool spin_thread=true)
(2)辅助方法
- 示例代码:
std::string tf::TransformListener::resolve (const std::string &frame_id)
- 结合tf_prefix获取解析的frame_id,查阅geometry/CoordinateFrameConventions.
(3)canTransform()函数
- 返回bool ,判断能否实现变换。不会抛出异常,如果出错,会返回error_msg的内容。
- 基本API:
bool tf::TransformListener::canTransform (const std::string &target_frame, const std::string &source_frame, const ros::Time &time, std::string *error_msg=NULL) const
- 检查在时间time,source_frame能否变换到target_frame。
- 高级API:
bool tf::TransformListener::canTransform (const std::string &target_frame, const ros::Time &target_time, const std::string &source_frame, const ros::Time &source_time, const std::string &fixed_frame, std::string *error_msg=NULL) const
- 检查在时间source_time,source_frame能否变换到fixed_frame,那么再实现在target_time变换到target_frame。
(4)waitForTransform()函数
- 返回bool值,评估变换是否有效。
- 基本API:
bool tf::TransformListener::waitForTransform (const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration &timeout,
const ros::Duration &polling_sleep_duration=ros::Duration(0.01), std::string *error_msg=NULL) const
- 检查在时间time, source_frame能否变换到target_frame
- 它将休眠并重试每个polling_duration,直到超时的持续时间已经过去。 它不会抛出异常。 在一个错误的情况下,如果你传递一个非NULL字符串指针,它会填充字符串error_msg。 (注意:这需要大量的资源来生成错误消息。)
- 高级API
bool tf::TransformListener::waitForTransform (const std::string &target_frame, const ros::Time &target_time, const std::string &source_frame, const ros::Time &source_time,
const std::string &fixed_frame, const ros::Duration &timeout, const ros::Duration &polling_sleep_duration=ros::Duration(0.01), std::string *error_msg=NULL) const
- 测试在source_time时间,source_frame能否变换到fixed_frame,那么在target_time,变换到target_frame。
(5)lookupTransform
-
lookupTransform()是一个更底层的方法用于返回两个坐标系的变换。
-
这个方法是 tf库的核心方法。大部分transform的方法都是终端用户使用,而这个方法设计在transform()方法内使用的。
-
返回的变换的方向将从target_frame到source_frame。 如果应用于数据,将把source_frame中的数据转换为target_frame。查阅geometry/CoordinateFrameConventions#Transform_Direction
-
这个方法会抛出TF的异常
-
基本API:
void tf::TransformListener::lookupTransform (const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
- 在时间time上使用从source_frame到target_frame的变换填充transform
- 高级API:
void tf::TransformListener::lookupTransform (const std::string &target_frame, const ros::Time &target_time, const std::string &source_frame, const ros::Time &source_time,
const std::string &fixed_frame, StampedTransform &transform) const
- 在source_time使用从source_frame到fixed_frame的变换填充transform,在target_time从fixed_frame到target_frame的链接变换。
transformDATA 方法
- tf::TransformListener类的主要目的是在坐标系间进行变换数据。
- 支持的数据类型:
C++ Method Name |
Python Method Name |
Full Datatype |
transformQuaternion() |
none |
tf::Stamped<tf::Quaternion> |
transformVector() |
none |
tf::Stamped<tf::Vector3> |
transformPoint() |
none |
tf::Stamped<tf::Point> |
transformPose() |
none |
tf::Stamped<tf::Pose> |
transformQuaternion() |
transformQuaternion() |
|
transformVector() |
transformVector3() |
|
transformPoint() |
transformPoint() |
|
transformPose() |
transformPose() |
|
transformPointCloud() |
transformPointCloud() |
- 基本API:
void tf::TransformListener::transformDATATYPE (const std::string &target_frame, const geometry_msgs::DATATYPEStamped &stamped_in, geometry_msgs::DATATYPEStamped &stamped_out) const
- 将数据stamped_in转换为target_frame,使用标记的数据类型中的frame_id和stamp作为源。
- 高级API:
void tf::TransformListener::transformDATATYPE (const std::string &target_frame, const ros::Time &target_time, const geometry_msgs::DATATYPEStamped &pin,
const std::string &fixed_frame, geometry_msgs::DATATYPEStamped &pout) const
- 将数据stamped_in转换为fixed_frame。 使用标记数据类型中的frame_id和stamp作为源。 然后在target_time从fixed_frame变换到target_frame。
ROS与C++入门教程-tf-异常
说明:
- 介绍roscpp的tf的异常类型及作用
TF异常
- 所有在TF里异常继承自tf::TransformException,它继承自std::runtime_error。
异常类型:
- 异常类:tf::ConnectivityException
- 作用:如果由于两个坐标系ID不在同一个连接的树中而无法完成请求,则抛出。
- 异常类:tf::ExtrapolationException
- 作用:如果请求的坐标系id之间存在连接,但一个或多个变换已过期,则抛出。
- 异常类:tf::InvalidArgument
- 作用:如果参数无效则抛出。 最常见的情况是非规范化的四元数。
- 异常类:tf::LookupException
- 作用:如果引用了未发布的坐标系ID,则抛出。
原文链接: https://www.cnblogs.com/flyinggod/p/10810166.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/294664
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!