博客參考: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,則拋出。