示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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.
在官网的解释是:在listener计算两个框架之间的转换时,这两个主题的转换还没有发布和生成。
解决方法:
添加rate.sleep();
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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); } |
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include <message_filters/subscriber.h> #include <message_filters/synchronizer.h> #include <message_filters/sync_policies/approximate_time.h> #include <sensor_msgs/PointCloud2.h> #include <sensor_msgs/Imu.h> #include <ros/ros.h> #include <nav_msgs/Odometry.h> #include <iostream> #include <geometry_msgs/TransformStamped.h> #include <tf2_ros/transform_listener.h> 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<<base2laserTrans.transform.rotation.x<<endl; //pubVelodyne.publish(syn_pointcloud); //pubImu.publish(syn_imu); } mySynchronizer::mySynchronizer() { ros::NodeHandle nh; //pubVelodyne = nh.advertise<sensor_msgs::PointCloud2>("/Syn/imu/data", 1); //pubImu = nh.advertise<sensor_msgs::Imu>("/Syn/velodyne_points", 1); message_filters::Subscriber<nav_msgs::Odometry> imu_sub(nh, "odom", 1); message_filters::Subscriber<sensor_msgs::PointCloud2> velodyne_sub(nh, "velodyne_points", 1); typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, nav_msgs::Odometry> MySyncPolicy; message_filters::Synchronizer<MySyncPolicy> 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文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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} ) |