ROS tf变换之得到两个坐标之间的转换关系

1、two_tf_transform.cpp

#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <geometry_msgs/Twist.h>
#include <turtlesim/Spawn.h>
#include <iostream>
#include <cmath>
using namespace std;
double x,y,z,ww,zz,hh,ii,Aww,Azz,Ahh,Aii;
double theta;
int main(int argc, char** argv){
  ros::init(argc, argv, "two_tf_transform");
 
  ros::NodeHandle node;
  tf::TransformListener listener;
  ros::Rate rate(10.0);
  while (node.ok()){
    tf::StampedTransform transform;
    try{
        //得到坐标odom和坐标base_link之间的关系
      listener.waitForTransform("odom", "base_link", ros::Time(0), ros::Duration(3.0));
      listener.lookupTransform("odom", "base_link",
                               ros::Time(0), transform);
    }
    catch (tf::TransformException &ex) {
      ROS_ERROR("%s",ex.what());
      ros::Duration(1.0).sleep();
    }
 
    x=transform.getOrigin().x();
    y=transform.getOrigin().y();
    z=transform.getOrigin().z();
    cout<<"x: "<<x<<endl;
    cout<<"y: "<<y<<endl;
    cout<<"z: "<<z<<endl;
    //两种不同的表示方法,来表示getRotation
     ww=transform.getRotation()[0];
     zz=transform.getRotation()[1];
     hh=transform.getRotation()[2];
     ii=transform.getRotation()[3];
     cout<<"ww: "<<ww<<endl;
     cout<<"zz: "<<zz<<endl;
     cout<<"hh: "<<hh<<endl;
     cout<<"ii: "<<ii<<endl;
     Aww=transform.getRotation().getX();
     Azz=transform.getRotation().getY();
     Ahh=transform.getRotation().getZ();
     Aii=transform.getRotation().getW();
     theta=2*acos(transform.getRotation().getW())/3.1415926*180;
     cout<<"Aww: "<<Aww<<endl;
     cout<<"Azz: "<<Azz<<endl;
     cout<<"Ahh: "<<Ahh<<endl;
     cout<<"Aii: "<<Aii<<endl;
     cout<<"Theta: "<<theta<<endl;
     cout<<endl;

    rate.sleep();
  }
  return 0;
};

2、CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(learn_tf)


find_package(catkin REQUIRED COMPONENTS
  geometry_msgs
  roscpp
  tf
)


catkin_package(

)


## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)


add_executable(two_tf_transform src/two_tf_transform.cpp)
target_link_libraries(two_tf_transform ${catkin_LIBRARIES})

3、package.xml

<?xml version="1.0"?>
<package format="2">
  <name>learn_tf</name>
  <version>0.0.0</version>
  <description>The learn_tf package</description>

  <maintainer email="patience@todo.todo">patience</maintainer>
  <license>TODO</license>


  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>geometry_msgs</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>tf</build_depend>
  <build_export_depend>geometry_msgs</build_export_depend>
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>tf</build_export_depend>
  <exec_depend>geometry_msgs</exec_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>tf</exec_depend>


  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->

  </export>
</package>

 

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值