Advertisement
MagicWinnie

Untitled

Dec 5th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import rospy
  3. import math
  4. from geometry_msgs.msg import Twist
  5. from turtlesim.msg import Pose
  6. global flag
  7. flag = False
  8. def callback(msg):
  9.     if msg.linear_velocity == 0 and msg.angular_velocity != 0:
  10.         rospy.loginfo("x: %.2f, y: %.2f" % (msg.x, msg.y))
  11.        
  12. def move():
  13.     rospy.init_node('robot', anonymous=True)
  14.     velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
  15.     sub = rospy.Subscriber('/turtle1/pose', Pose, callback)
  16.     speed = 0.5
  17.     vel_msg = Twist()
  18.     vel_msg.linear.x = abs(speed)
  19.     vel_msg.linear.y = 0
  20.     vel_msg.linear.z = 0
  21.     vel_msg.angular.x = 0
  22.     vel_msg.angular.y = 0
  23.     pose = Pose()
  24.     num_of_ver = input("Kol-vo vershin: ")
  25.     distance = input("Dlina:")
  26.    
  27.     for i in range(num_of_ver):
  28.         q=(360/num_of_ver)*2*math.pi/360
  29.         t0 = rospy.Time.now().to_sec()
  30.         current_distance = 0
  31.         while(current_distance < distance):
  32.             velocity_publisher.publish(vel_msg)
  33.             t1=rospy.Time.now().to_sec()
  34.             current_distance = speed*(t1-t0)
  35.         vel_msg.angular.z = abs(speed)
  36.         vel_msg.linear.x = 0
  37.         current_angle = 0
  38.         t0 = rospy.Time.now().to_sec()
  39.         while(current_angle < q):
  40.             velocity_publisher.publish(vel_msg)
  41.             t1 = rospy.Time.now().to_sec()
  42.             current_angle = 0.5*(t1-t0)
  43.         vel_msg.angular.z = 0
  44.         vel_msg.linear.x = speed
  45.     velocity_publisher.publish(vel_msg)
  46. if __name__ == '__main__':
  47.     try:
  48.         move()
  49.     except rospy.ROSInterruptException: pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement