ROS学习之tf基本用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SUNFC_nbu/article/details/52424589

主要细节参见wiki,这里我写一下它的broadcaster和listener做个记录:

tf_broadcaster.cpp:

#include<ros/ros.h>
#include<tf/transform_broadcaster.h>

int main(int argc, char** argv){
	ros::init(argc, argv, "robot_tf_publisher");
	ros::NodeHandle node;
	
	ros::Rate rate(100);

	tf::TransformBroadcaster broadcaster;
	while(node.ok()){
		broadcaster.sendTransform(
			tf::StampedTransform(
				tf::Transform(
					tf::Quaternion(0,0,0,1),
					tf::Vector3(0.1,0.0,0.2)
				),
				ros::Time::now(),
				"base_link",
				"base_laser"
			)
		);
		rate.sleep();
	}
}

在broadcaster中我们主要描述的是两个坐标frame之间的位置关系,这里是base_link与base_laser之间的位置关系,表base_laser在base_link的x=0.1,y=0.0,z=0.2的位置,旋转方面quaternion置0,没有变化。


tf_listener.cpp:

#include<ros/ros.h>
#include<tf/transform_listener.h>
#include<geometry_msgs/PointStamped.h>

void transformPoint(const tf::TransformListener& listener){
        //这里先随意设置一个点,当做是laser坐标的点,然后我们将其转换成base_link坐标上的位置
        geometry_msgs::PointStamped laser_point;
	laser_point.header.frame_id = "base_laser";
	
	laser_point.header.stamp = ros::Time();
	laser_point.point.x = 1.0;
	laser_point.point.y = 0.2;
	laser_point.point.z = 0.0;

	try{
                //创建一个点,用来存放当前base_laser的点在base_link坐标系中的位置
                geometry_msgs::PointStamped base_point;
                //下面这句话,它会自动为我们进行相应的转换,并将结果放入base_point,由于laser_point的frame_id为base_laser,这里我们先写将转换到的坐标系base_link.                
                listener.transformPoint("base_link", laser_point, base_point);
		ROS_INFO("base_laser: (%.2f, %.2f. %.2f) -----> base_link: (%.2f, %.2f, %.2f) at time %.2f",
		     laser_point.point.x, laser_point.point.y, laser_point.point.z,
		     base_point.point.x, base_point.point.y, base_point.point.z, base_point.header.stamp.toSec());
	}catch(tf::TransformException& ex){
		ROS_ERROR("Received an exception trying to transform a point from \"base_laser\" to \"base_link\": %s", ex.what());
	}
}



int main(int argc, char** argv){
	ros::init(argc, argv, "robot_tf_listener");
	ros::NodeHandle node;

	tf::TransformListener listener(ros::Duration(10));
	//定时每隔1秒查询进行一次:
	ros::Timer timer = node.createTimer( ros::Duration(1.0), boost::bind( &transformPoint, boost::ref(listener) ) );
	ros::spin();
}

在CMakeLists文件中加入:

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

依次运行,会发现出现结果:

[ INFO] [1472897890.623220105]: base_laser: (1.00, 0.20. 0.00) -----> base_link: (1.10, 0.20, 0.20) at time 1472897890.61
[ INFO] [1472897891.623229008]: base_laser: (1.00, 0.20. 0.00) -----> base_link: (1.10, 0.20, 0.20) at time 1472897891.61
[ INFO] [1472897892.624179159]: base_laser: (1.00, 0.20. 0.00) -----> base_link: (1.10, 0.20, 0.20) at time 1472897892.61
[ INFO] [1472897893.622589774]: base_laser: (1.00, 0.20. 0.00) -----> base_link: (1.10, 0.20, 0.20) at time 1472897893.61
[ INFO] [1472897894.624529454]: base_laser: (1.00, 0.20. 0.00) -----> base_link: (1.10, 0.20, 0.20) at time 1472897894.61

显示出了两坐标关系~~



展开阅读全文

没有更多推荐了,返回首页