ROS学习笔记(十一):tf坐标系广播与监听的编程实现

一、创建功能包

  1. $ cd ~/catkin_ws/src 要进入src文件夹才能创建功能包

  2. $ catkin_create_pkg learning_tf roscpp rospy turtlesim 创建learning_tf功能包

二、创建tf广播器代码

如何实现一个TF广播器

  • 定义TF广播器
  • 创建坐标变换值
  • 发布坐标变换

~/catkin_ws/src/learning_tf/src路径下创建turtle_tf_broadcaster.cpp

文件内容:

/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/

/**
 * 该例程产生tf数据,并计算、发布turtle2的速度指令
 */

#include 
#include 
#include 

std::string turtle_name;

void poseCallback(const turtlesim::PoseConstPtr& msg)
{
     
	// 创建tf的广播器
	static tf::TransformBroadcaster br;

	// 初始化tf数据
	tf::Transform transform;
	transform.setOrigin( tf::Vector3(msg->x, msg->y, 0.0) );
	tf::Quaternion q;
	q.setRPY(0, 0, msg->theta);
	transform.setRotation(q);

	// 广播world与海龟坐标系之间的tf数据
	br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "world", turtle_name));
}

int main(int argc, char** argv)
{
     
    // 初始化ROS节点
	ros::init(argc, argv, "my_tf_broadcaster");

	// 输入参数作为海龟的名字
	if (argc != 2)
	{
     
		ROS_ERROR("need turtle name as argument"); 
		return -1;
	}

	turtle_name = argv[1];

	// 订阅海龟的位姿话题
	ros::NodeHandle node;
	ros::Subscriber sub = node.subscribe(turtle_name+"/pose", 10, &poseCallback);

    // 循环等待回调函数
	ros::spin();

	return 0;
};


三、创建tf监听器代码

如何实现一个TF监听器

  • 定义TF监听器
  • 查找坐标变换

~/catkin_ws/src/learning_tf/src路径下创建turtle_tf_listener.cpp

文件内容:

/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/

/**
 * 该例程监听tf数据,并计算、发布turtle2的速度指令
 */

#include 
#include 
#include 
#include 

int main(int argc, char** argv)
{
     
	// 初始化ROS节点
	ros::init(argc, argv, "my_tf_listener");

    // 创建节点句柄
	ros::NodeHandle node;

	// 请求产生turtle2
	ros::service::waitForService("/spawn");
	ros::ServiceClient add_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");
	turtlesim::Spawn srv;
	add_turtle.call(srv);

	// 创建发布turtle2速度控制指令的发布者
	ros::Publisher turtle_vel = node.advertise<geometry_msgs::Twist>("/turtle2/cmd_vel", 10);

	// 创建tf的监听器
	tf::TransformListener listener;

	ros::Rate rate(10.0);
	while (node.ok())
	{
     
		// 获取turtle1与turtle2坐标系之间的tf数据
		tf::StampedTransform transform;
		try
		{
     
			listener.waitForTransform("/turtle2", "/turtle1", ros::Time(0), ros::Duration(3.0));
			listener.lookupTransform("/turtle2", "/turtle1", ros::Time(0), transform);
		}
		catch (tf::TransformException &ex) 
		{
     
			ROS_ERROR("%s",ex.what());
			ros::Duration(1.0).sleep();
			continue;
		}

		// 根据turtle1与turtle2坐标系之间的位置关系,发布turtle2的速度控制指令
		geometry_msgs::Twist vel_msg;
		vel_msg.angular.z = 4.0 * atan2(transform.getOrigin().y(),
				                        transform.getOrigin().x());
		vel_msg.linear.x = 0.5 * sqrt(pow(transform.getOrigin().x(), 2) +
				                      pow(transform.getOrigin().y(), 2));
		turtle_vel.publish(vel_msg);

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

四、配置tf广播器与监听器代码编译规则

打开src/learning_tf下的CMakeLists.txt文件,在

#############
## Install ##
#############

前面添加代码

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

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

五、编译并运行

  1. $ catkin_make

此时出现如下报错:

/usr/bin/ld: turtle_tf_listener.cpp:(.text+0x6dc): undefined reference to `tf::TransformListener::~TransformListener()'
/usr/bin/ld: turtle_tf_listener.cpp:(.text+0xb0e): undefined reference to `tf::TransformListener::~TransformListener()'
collect2: error: ld returned 1 exit status
make[2]: *** [learning_tf/CMakeFiles/turtle_tf_listener.dir/build.make:101/home/lacyexsale/catkin_ws/devel/lib/learning_tf/turtle_tf_listener] 错误 1
make[1]: *** [CMakeFiles/Makefile2:1752:learning_tf/CMakeFiles/turtle_tf_listener.dir/all] 错误 2
make: *** [Makefile:141:all] 错误 2

注意undefined reference to `tf,是提示没有找到tf依赖

解决方案:

  • 方法一:在开头创建功能包时把tf包含进去
  • 方法二:在CMakeLists.txt文件的find_package中添加 tf 依赖
  1. $ roscore

  2. $ rosrun turtlesim turtlesim_node

  3. $ rosrun learning_tf turtle_tf_broadcaster __name:=turtle1_tf_broadcaster /turtle1 这里是一个节点使用两次的方法,即共用同一个可执行文件,把两个节点分别重命名为turtle1_tf_broadcaster /turtle1 和 turtle2_tf_broadcaster /turtle2

  4. $ rosrun learning_tf turtle_tf_broadcaster __name:=turtle2_tf_broadcaster /turtle2

  5. $ rosrun learning_tf turtle_tf_listener 生成第turtle2,监听turtle1的状态并发布turtle2的速度指令

  6. $ rosrun turtlesim turtle_teleop_key 控制turtle1运动,可以看到turtle2在不断跟随

你可能感兴趣的:(ROS学习笔记,linux,ubuntu,slam)