tf 四元素和欧拉角转换 获得机器人的实时位置

使用listener.lookupTransform()来获得机器人位置
主要代码如下:

//.h头文件中相关定义
#include <tf/transform_listener.h>

struct state
{
    float x;
    float y;
    float yaw;
};

tf::TransformListener listener;

state robot_pose;

//.cpp文件中主函数中的主循环
state robot_pose;
tf::StampedTransform transform;
while (node.ok()){ 
    try{
       listener.waitForTransform("map","base_link",ros::Time(0),ros::Duration(3.0));
       listener.lookupTransform("/map","/base_link",ros::Time(0),transform);
    }
    catch(tf::TransformException &ex){
        ROS_ERROR("%s",ex.what());
        ros::Duration(1.0).sleep();
        continue;
    }
    
    double roll,pitch,yaw;
    tf::Matrix3x3(transform.getRotation()).getEulerYPR(yaw,pitch,roll);

    //tf::Quaternion quat;
    //tf::quaternionMsgToTF(odom_msg.pose.pose.orientation,quat);
    //double roll,pitch,yaw;
    //tf::Matrix3x3(quat).getRPY(roll,pitch,yaw);

    robot_pose.x = transform.getOrigin().x();
    robot_pose.y = transform.getOrigin().y();
    robot_pose.yaw = yaw;

    return robot_pose;
}

注意: 其中lookupTransform:
不可以把ros::Time(0)改成ros::time::now(),因为监听做不到实时,会有几毫秒的延迟。ros::Time(0)指最近时刻存储的数据,ros::time::now()则指当下,如果非要使用ros::time::now,则需要结合waitForTransform()使用

一些相关链接:
https://blog.csdn.net/Hansry/article/details/84848884
https://blog.csdn.net/sru_alo/article/details/92804479
https://www.jianshu.com/p/17c016778879

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值