当前位置:   article > 正文

ROS学习笔记(六)ROS TF初探

ROS学习笔记(六)ROS TF初探

 StampedTransform函数参数定义

  1. ros::Time stamp_; ///< The timestamp associated with this transform
  2. std::string frame_id_; ///< The frame_id of the coordinate frame in which this transform is defined
  3. std::string child_frame_id_; ///< The frame_id of the coordinate frame this transform defines
  4. StampedTransform(const tf::Transform& input, const ros::Time& timestamp, const std::string & frame_id, const std::string & child_frame_id):
  5. tf::Transform (input), stamp_ ( timestamp ), frame_id_ (frame_id), child_frame_id_(child_frame_id){ }

 transformPoint函数参数定义

  1. /** \brief Transform a Stamped Point Message into the target frame
  2. * This can throw all that lookupTransform can throw as well as tf::InvalidTransform */
  3. void transformPoint(const std::string& target_frame, const geometry_msgs::PointStamped& stamped_in, geometry_msgs::PointStamped& stamped_out) const;

参数收发的小例子: 

tf_broadcaster.cpp

  1. #include <ros/ros.h>
  2. #include <tf/transform_broadcaster.h>
  3. int main(int argc, char** argv) {
  4. ros::init(argc, argv, "robot_tf_publisher");
  5. ros::NodeHandle n;
  6. ros::Rate r(100);
  7. tf::TransformBroadcaster broadcaster;
  8. while (n.ok()) {
  9. tf::Transform transform; //这里transform为一个类
  10. tf::Quaternion q; // Quaternion是一个类
  11. q.setRPY(0.0, 0.0, 0.0);
  12. // setRPY是类Quaternion的一个成员函数 rpy 转换为四元数
  13. transform.setRotation(q);
  14. transform.setOrigin(tf::Vector3(0.1, 0.0, 0.2));
  15. broadcaster.sendTransform(tf::StampedTransform(transform, ros::Time::now(),
  16. "base_link", "base_laser"));
  17. r.sleep();
  18. }
  19. }

tf_listener.cpp

  1. #include <geometry_msgs/PointStamped.h>
  2. #include <ros/ros.h>
  3. #include <tf/transform_listener.h>
  4. void transformPoint(const tf::TransformListener& listener) {
  5. // we'll create a point in the base_laser frame that we'd like to transform to
  6. // the base_link frame
  7. geometry_msgs::PointStamped laser_point;
  8. laser_point.header.frame_id = "base_laser";
  9. // we'll just use the most recent transform available for our simple example
  10. laser_point.header.stamp = ros::Time();
  11. // just an arbitrary point in space
  12. static int x = 1;
  13. laser_point.point.x = x++;
  14. laser_point.point.y = 0.2;
  15. laser_point.point.z = 0.0;
  16. try {
  17. geometry_msgs::PointStamped base_point;
  18. listener.transformPoint("base_link", laser_point, base_point);
  19. ROS_INFO(
  20. "base_laser: (%.2f, %.2f. %.2f) -----> base_link: (%.2f, %.2f, %.2f) "
  21. "at time %.2f",
  22. laser_point.point.x, laser_point.point.y, laser_point.point.z,
  23. base_point.point.x, base_point.point.y, base_point.point.z,
  24. base_point.header.stamp.toSec());
  25. } catch (tf::TransformException& ex) {
  26. ROS_ERROR(
  27. "Received an exception trying to transform a point from \"base_laser\" "
  28. "to \"base_link\": %s",
  29. ex.what());
  30. }
  31. }
  32. int main(int argc, char** argv) {
  33. ros::init(argc, argv, "robot_tf_listener");
  34. ros::NodeHandle n;
  35. tf::TransformListener listener(ros::Duration(10));
  36. // we'll transform a point once every second
  37. ros::Timer timer = n.createTimer(
  38. ros::Duration(1.0), boost::bind(&transformPoint, boost::ref(listener)));
  39. ros::spin();
  40. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/395219
推荐阅读
相关标签
  

闽ICP备14008679号