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

系统Ubuntu 18.04

ROS版本: Melodic

1 创建功能包

cd ~/catkin_ws/src
catkin_create_pkg learning_tf roscpp rospy tf turtlesim

2创建Python文件

1 创建scripts文件夹和.py文件

touch turtle_tf_broadcaster.py
touch turtle_tf_listener.py
ROS学习笔记(18)tf坐标系广播与监听的编程实现_第1张图片

修改.py文件属性(允许作为执行文件)

ROS学习笔记(18)tf坐标系广播与监听的编程实现_第2张图片

2 实现一个tf广播器(turtle_tf_broadcaster.py)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

########################################################################
####                              广播器                             ###
########################################################################
import roslib
roslib.load_manifest('learning_tf')
import rospy

import tf
import turtlesim.msg

def handle_turtle_pose(msg, turtlename):
    br = tf.TransformBroadcaster()
    br.sendTransform((msg.x, msg.y, 0),
                     tf.transformations.quaternion_from_euler(0, 0, msg.theta),
                     rospy.Time.now(),
                     turtlename,
                     "world")

if __name__ == '__main__':
    rospy.init_node('turtle_tf_broadcaster')

#获取海龟的名字
    turtlename = rospy.get_param('~turtle') 

#订阅海龟的位置
    rospy.Subscriber('/%s/pose' % turtlename,
                     turtlesim.msg.Pose,
                     handle_turtle_pose,
                     turtlename)
    rospy.spin()

3 实现一个tf监听器(turtle_tf_listener.py)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

########################################################################
####                             tf监听器                             ###
########################################################################

import roslib
roslib.load_manifest('learning_tf')
import rospy
import math
import tf
import geometry_msgs.msg
import turtlesim.srv

if __name__ == '__main__':
    rospy.init_node('turtle_tf_listener')

    listener = tf.TransformListener()

    rospy.wait_for_service('spawn')
    spawner = rospy.ServiceProxy('spawn', turtlesim.srv.Spawn)
    spawner(4, 2, 0, 'turtle2')

    turtle_vel = rospy.Publisher('turtle2/cmd_vel', geometry_msgs.msg.Twist,queue_size=1)

    rate = rospy.Rate(10.0)
    while not rospy.is_shutdown():
        try:
            (trans,rot) = listener.lookupTransform('/turtle2', '/turtle1', rospy.Time(0))
        except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
            continue

        angular = 4 * math.atan2(trans[1], trans[0])
        linear = 0.5 * math.sqrt(trans[0] ** 2 + trans[1] ** 2)
        cmd = geometry_msgs.msg.Twist()
        cmd.linear.x = linear
        cmd.angular.z = angular
        turtle_vel.publish(cmd)

        rate.sleep()

3.运行

使用python文件可以省略编译部分,直接启动。

启动ROS核心、海龟仿真器、键盘节点

roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key

启动代码文件

source devel/setup.bash
rosrun learning_tf turtle_tf_broadcaster __name:=turtle1_tf_broadcaster /turtle1

source devel/setup.bash
rosrun learning_tf turtle_tf_broadcaster __name:=turtle2_tf_broadcaster /turtle2

source devel/setup.bash
rosrun learning_tf turtle_tf_listener

4 效果

ROS学习笔记(18)tf坐标系广播与监听的编程实现_第3张图片

查看节点控制图:

rqt_graph
ROS学习笔记(18)tf坐标系广播与监听的编程实现_第4张图片

代码的部分解释:

ROS学习笔记(18)tf坐标系广播与监听的编程实现_第5张图片
ROS学习笔记(18)tf坐标系广播与监听的编程实现_第6张图片

在math库函数中,atan2(y,x)表示求y/x(弧度表示)的反正切值。则速度表示如下:

ROS学习笔记(18)tf坐标系广播与监听的编程实现_第7张图片

你可能感兴趣的:(ROS入门,python)