1

我想将海龟绕圈移动两次,制作两个不同的圆圈,并且我正在使用 Twist() 但是当我为同一个发布者提供两种不同的扭曲和值时,即使我尝试过使用两个不同的发布者,但获得相同的输出。所以任何人都可以请告诉我如何发布具有两个不同值的 Twist()。

 pub = rospy.Publisher('turtle1/cmd_vel', Twist, queue_size=10)
 pub_1 = rospy.Publisher('turtle1/cmd_vel', Twist, queue_size=10)

 # Create a Twist message and add linear x and angular z values
 move_cmd = Twist()
 move_cmd.linear.x =   1.0
 move_cmd.linear.y =   0.0
 move_cmd.angular.z = -1.0
 move_cmd_1 = Twist()
 move_cmd_1.linear.x =  1.0
 move_cmd_1.linear.y =  0.0
 move_cmd_1.angular.z = 1.0

 # Save current time and set publish rate at 10 Hz
 now = rospy.Time.now()
 rate = rospy.Rate(10)

 # For the next 6 seconds publish cmd_vel move commands to Turtlesim
 while rospy.Time.now() < now + rospy.Duration.from_sec(10.5):
     pub.publish(move_cmd_1)

     pub_1.publish(move_cmd_1)
     rate.sleep()
4

1 回答 1

1

您的代码有一些问题。首先,您实际上是在两个发布者上发布相同的消息。其次,像这样在同一主题上发布两个值并不是一个好主意。如果订阅者正在缓存最新的值,那么它很有可能只会看到/使用发布的第二个值。如果您尝试替换,请尝试使用此代码。请注意,我降低了旋转速度,并且发布者在每个循环中交替。

 pub = rospy.Publisher('turtle1/cmd_vel', Twist, queue_size=10)

 # Create a Twist message and add linear x and angular z values
 move_cmd = Twist()
 move_cmd.linear.x =   1.0
 move_cmd.linear.y =   0.0
 move_cmd.angular.z = -1.0
 move_cmd_1 = Twist()
 move_cmd_1.linear.x =  1.0
 move_cmd_1.linear.y =  0.0
 move_cmd_1.angular.z = 1.0

 # Save current time and set publish rate at 10 Hz
 now = rospy.Time.now()
 rate = rospy.Rate(3)
 loop_count = 0

 # For the next 6 seconds publish cmd_vel move commands to Turtlesim
 while rospy.Time.now() < now + rospy.Duration.from_sec(10.5):
     if loop_count % 2 == 0:
         pub.publish(move_cmd)
     else:
         pub.publish(move_cmd_1)
     loop_count += 1
     rate.sleep()
于 2021-10-18T14:07:45.330 回答