Ros tf2_ros::Buffer.lookupTransform 运行报错lookupTransform base2laser error,"base_footprint" passed to

 示例代码:

tf2_ros::Buffer tfBuffer;
    tf2_ros::TransformListener tfListener(tfBuffer);
    geometry_msgs::TransformStamped base2laserTrans;
    ros::Time timeout(0.1);
    ros::Rate rate(2);
    try{
        //("/turtle2", "/turtle1", ros::Time(0), ros::Duration(3.0));
        //bool x = tfBuffer.canTransform( base_frame_id,laser_frame_id,timeout, nullptr);
        //rate.sleep();
        base2laserTrans = tfBuffer.lookupTransform( base_frame_id,laser_frame_id,ros::Time(0));
    }catch (tf2::TransformException &ex) {
        ROS_WARN("lookupTransform base2laser error,%s",ex.what());
        exit(1);
    }

 

执行之后报错:

[ WARN] [1588043299.086444605]: lookupTransform base2laser error,"base_footprint" passed to lookupTransform argument target_frame does not exist. 
 

Ros tf2_ros::Buffer.lookupTransform 运行报错lookupTransform base2laser error,

在官网的解释是:在listener计算两个框架之间的转换时,这两个主题的转换还没有发布和生成。

解决方法:

添加rate.sleep();

示例代码:

tf2_ros::Buffer tfBuffer;
    tf2_ros::TransformListener tfListener(tfBuffer);
    geometry_msgs::TransformStamped base2laserTrans;
    ros::Time timeout(0.1);
    ros::Rate rate(2);
    try{
        //("/turtle2", "/turtle1", ros::Time(0), ros::Duration(3.0));
        //bool x = tfBuffer.canTransform( base_frame_id,laser_frame_id,timeout, nullptr);
        rate.sleep();
        base2laserTrans = tfBuffer.lookupTransform( base_frame_id,laser_frame_id,ros::Time(0));
    }catch (tf2::TransformException &ex) {
        ROS_WARN("lookupTransform base2laser error,%s",ex.what());
        exit(1);
    }

Ros tf2_ros::Buffer.lookupTransform 运行报错lookupTransform base2laser error,

完整代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
string base_frame_id="base_footprint";
string laser_frame_id="velodyne";
class mySynchronizer{
public:
    ros::Publisher pubVelodyne;
    ros::Publisher pubImu;

    mySynchronizer();
    ~mySynchronizer(){};

    void callback(const sensor_msgs::PointCloud2::ConstPtr& , const nav_msgs::Odometry::ConstPtr& );

};

void mySynchronizer::callback(const sensor_msgs::PointCloud2::ConstPtr& ori_pointcloud, const nav_msgs::Odometry::ConstPtr& odom){
    cout << "*******************" << endl;
    sensor_msgs::PointCloud2 syn_pointcloud = *ori_pointcloud;
    nav_msgs::Odometry syn_imu = *odom;

    ROS_INFO("pointcloud stamp value is: %f", syn_pointcloud.header.stamp.toSec());
    ROS_INFO("imu stamp value is: %f", syn_imu.header.stamp.toSec());
    tf2_ros::Buffer tfBuffer;
    tf2_ros::TransformListener tfListener(tfBuffer);
    geometry_msgs::TransformStamped base2laserTrans;
    ros::Time timeout(0.1);
    ros::Rate rate(2);
    try{
        //("/turtle2", "/turtle1", ros::Time(0), ros::Duration(3.0));
        //bool x = tfBuffer.canTransform( base_frame_id,laser_frame_id,timeout, nullptr);
        rate.sleep();
        base2laserTrans = tfBuffer.lookupTransform( base_frame_id,laser_frame_id,ros::Time(0));
    }catch (tf2::TransformException &ex) {
        ROS_WARN("lookupTransform base2laser error,%s",ex.what());
        exit(1);
    }
    cout<("/Syn/imu/data", 1);
    //pubImu = nh.advertise("/Syn/velodyne_points", 1);

    message_filters::Subscriber imu_sub(nh, "odom", 1);
    message_filters::Subscriber velodyne_sub(nh, "velodyne_points", 1);

    typedef message_filters::sync_policies::ApproximateTime MySyncPolicy;
    message_filters::Synchronizer sync(MySyncPolicy(10), velodyne_sub, imu_sub);
    sync.registerCallback(boost::bind(&mySynchronizer::callback, this, _1, _2));

    ros::spin();
}


int main(int argc, char** argv)
{
    ros::init(argc, argv, "msg_synchronizer");
    ROS_INFO("\033[1;32m---->\033[0m Sync msgs node Started.");

    mySynchronizer wode;

    // ros::spin();

    return 0;
}//
// Created by allen on 2020/4/27.
//

 

cmake文件: 

cmake_minimum_required(VERSION 3.15)
project(MsgFilter)
find_package(catkin REQUIRED COMPONENTS
        roscpp
        std_msgs
        pcl_ros
        pcl_conversions
)
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
include_directories(include include/luneng_localization
        ${catkin_INCLUDE_DIRS}
        ${PCL_INCLUDE_DIRS}
        )
link_directories(
        ${catkin_LIBRARY_DIRS}
        ${PCL_LIBRARY_DIRS}
)

add_executable(MsgFilter demo1.cpp)
target_link_libraries(MsgFilter

        ${catkin_LIBRARIES}
        ${PCL_LIBRARIES}
        )

 

你可能感兴趣的:(ROS,c++,弹出设备,ros,tf转换,tf2_ros)