参考wiki的ros tf demo教程 纯粹科研精华~~~
美学乌龟效果图
#!/usr/bin/env python
import roslib
roslib.load_manifest('sustc_follow')
import rospy #rospy 必须import的包
import math #数学计算包
import tf #TF包
import geometry_msgs.msg #这个包含乌龟的几何信息
import turtlesim.srv #这个是“龟模拟“仿真器
if __name__ == '__main__':
rospy.init_node('turtle_tf_listener') #初始化turtle_listener的节点名称
listener = tf.TransformListener() #实例化坐标监听器
# 使用spawn命令生出多只小乌龟来
rospy.wait_for_service('spawn')
spawner = rospy.ServiceProxy('spawn', turtlesim.srv.Spawn)
# Add TMD new turtle!
spawner(4, 2, 0, 'turtle2') #第一只小乌龟已经默认生成,这里从第二只开始
spawner(2, 2, 0, 'turtle3') #前面的三个数字是在地图上的初始坐标
spawner(1, 2, 0, 'turtle4')
# Set TMD turtle twist value(by TMD publisher)
#这是每一只龟的消息发布器
turtle1_vel = rospy.Publisher('turtle1/cmd_vel', geometry_msgs.msg.Twist, queue_size=1)
turtle2_vel = rospy.Publisher('turtle2/cmd_vel', geometry_msgs.msg.Twist, queue_size=1)
turtle3_vel = rospy.Publisher('turtle3/cmd_vel', geometry_msgs.msg.Twist, queue_size=1)
turtle4_vel = rospy.Publisher('turtle4/cmd_vel', geometry_msgs.msg.Twist, queue_size=1)
#每秒操作10次
rate = rospy.Rate(10.0)
while not rospy.is_shutdown():
#这里做一个小循环,可以分别对每一只龟进行操作
for i in range(3):
try:
#此处返回的,是前后两只乌龟的坐标变换数据
(trans, rot) = listener.lookupTransform('/turtle'+bytes(i+2), '/turtle'+bytes(i+1), rospy.Time(0))
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
continue
#我们定义一下,cmd是一种Twist类型的msg
cmd = geometry_msgs.msg.Twist()
#这里是坐标变换
angular = 4 * math.atan2(trans[1], trans[0])
linear = 0.5 * math.sqrt(trans[0] ** 2 + trans[1] ** 2)
cmd.linear.x = linear
cmd.angular.z = angular
#向当前的乌龟发布新的线速度和角速度
if i == 0:
turtle2_vel.publish(cmd)
if i == 1:
turtle3_vel.publish(cmd)
if i == 2:
turtle4_vel.publish(cmd)
rate.sleep()
#!/usr/bin/env python
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')
print 'turtlename: ' + turtlename
rospy.Subscriber('/%s/pose' % turtlename,
turtlesim.msg.Pose,
handle_turtle_pose,
turtlename)
rospy.spin()
if __name__ == '__main__':
rospy.init_node('turtle_tf_broadcaster')
turtlename = rospy.get_param('~turtle')
print 'turtlename: ' + turtlename
rospy.Subscriber('/%s/pose' % turtlename,
turtlesim.msg.Pose,
handle_turtle_pose,
turtlename)
rospy.spin()
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")
rospy.wait_for_service('spawn')
spawner = rospy.ServiceProxy('spawn', turtlesim.srv.Spawn)
# Add TMD new turtle!
spawner(4, 2, 0, 'turtle2')
spawner(3, 2, 0, 'turtle3')
spawner(2, 2, 0, 'turtle4')
spawn使用效果图
$ rosservice type spawn| rossrv show
# Add TMD new turtle!
spawner(4, 2, 0, 'turtle2')
spawner(3, 2, 0, 'turtle3')
spawner(2, 2, 0, 'turtle4')
$ rosservice call spawn 2 2 0 "turtle2"
[ERROR] [1532226272.126496492]: A turtled named [turtle1] already exists
listener = tf.TransformListener()
rate = rospy.Rate(10.0)
while not rospy.is_shutdown():
for i in range(3):
try:
(trans, rot) = listener.lookupTransform('/turtle'+bytes(i+2), '/turtle'+bytes(i+1), rospy.Time(0))
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
continue
angular = 3 * 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
if i == 0:
turtle2_vel.publish(cmd)
if i == 1:
turtle3_vel.publish(cmd)
if i == 2:
turtle4_vel.publish(cmd)