英文:
Trying to make a turtlebot circle another moving turtlebot in ROS
问题
我试图让一个名为turtle2的小乌龟围绕一个静止的小乌龟turtle1画圈,然后让另一个小乌龟turtle3围绕turtle2画圈。Turtle2围绕turtle1画圈,半径为2,角速度为1,而turtle3应该围绕turtle2画圈,半径为0.5,角速度为2。然而,我无法想出如何使turtle3围绕turtle2画圈,同时又缩短了由turtle2运动造成的距离。这是我的ROS代码和我的结果的屏幕截图,以帮助您理解我的目标。任何帮助将不胜感激。
英文:
I am trying to make one turtlebot named turtle2 to circle around a stationary turtlebot and then have another turtlebot turtle3 circle around turtle 2. Turtle2 circles around turtle1 with a radius of 2 and an angular velocity of 1 and turtle3 should circle around turtle2 with a radius of 0.5 and an angular velocity of 2. However I cannot figure out how to make turtle3 circle around turtle2 while also closing the distance caused by turtle2's movement. Here's my ROS code and a screenshot of what I get with my results to help you understand my goal. Any help would be appreciated.
#!/usr/bin/env python
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)
# wait for the world to spawn
rospy.sleep(0.3)
(trans, rot) = listener.lookupTransform('/world', '/turtle1', rospy.Time(0))
r2 = 2
w2 = 1
# spawn second turtle along the orbit of the first still turtle
spawner(trans[0], trans[1] - r2, 0, 'turtle2')
turtle_vel2 = rospy.Publisher('turtle2/cmd_vel', geometry_msgs.msg.Twist,queue_size=1)
# wait for second turtle to spawn
rospy.sleep(0.3)
(trans, rot) = listener.lookupTransform('/world', '/turtle2', rospy.Time(0))
r3 = 0.5
w3 = 2
# spawn third turtle along the orbit of the second turtle
spawner(trans[0], trans[1] - r3, 0, 'turtle3')
turtle_vel3 = rospy.Publisher('turtle3/cmd_vel', geometry_msgs.msg.Twist,queue_size=1)
rate = rospy.Rate(10.0)
while not rospy.is_shutdown():
flag2 = True
flag3 = True
try:
(trans, rot) = listener.lookupTransform('/world', '/turtle1', rospy.Time(0))
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
flag2 = False
if(flag2):
angular2 = w2
linear2 = w2 * r2
cmd2 = geometry_msgs.msg.Twist()
cmd2.linear.x = linear2
cmd2.angular.z = angular2
turtle_vel2.publish(cmd2)
try:
(trans, rot) = listener.lookupTransform('/turtle3', '/turtle2', rospy.Time(0))
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
flag3 = False
if(flag3):
angular3 = w3
linear3 = w3 * r3
cmd3 = geometry_msgs.msg.Twist()
cmd3.linear.x = linear3
cmd3.angular.z = angular3
turtle_vel3.publish(cmd3)
rate.sleep()
答案1
得分: 0
这是我处理的方式:
- 获取turtle2的位置,我们称其为Pos2,它是一个向量(x2,y2)。
- 创建一个生成围绕(0, 0)旋转的点的函数,类似于SpinnerPos =(cx,cy)= DistanceToCenter *(cos(angle),sin(angle)),其中angle每帧增加,因此该点(也称为向量)会旋转。
- 将它们组合起来:Target = Pos2 + SpinnerPos。现在,如果绘制目标点,它将完美地围绕turtle2旋转。
- 现在,通过计算到目标点的距离(勾股定理)并根据距离调整其方向(三角函数atan2())和速度,使turtle3跟随目标点。
- 调整参数后,turtle3应该围绕turtle2旋转。
英文:
That's how I would approach it:
- Get turtle2 position let's call it Pos2 which is vector (x2, y2)
- Make a function which generates point which is circling around (0, 0) so it's something like SpinnerPos = (cx, cy) = DistanceToCenter * (cos(angle), sin(angle)) where angle is incremented every frame so the point (aka vector) would spin.
- Combine both: Target = Pos2 + SpinnerPos. Now if drawn Target point would perfectly spin around turtle2
- Now make turtle3 follow Target point by calculating distance to it (Pythagorean theorem) and adjusting it's orientation (trigonometry atan2()) and speed proportional to distance
- After tuning parameters turtle3 should circle turtle2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论