Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import rospy
  2. from threading import Thread
  3. from geometry_msgs.msg import Twist
  4. from sensor_msgs.msg import LaserScan
  5. from nav_msgs.msg import Odometry
  6. import math
  7.  
  8. a = True
  9. x = True
  10. b = False
  11. c = None
  12. d = False
  13. g = True
  14. def callback(data):
  15. global a, velocity_publisher, vel_msg, b
  16. if(data.ranges[0] != float('inf') and a == True):
  17. if(data.ranges[0] >= 0.39):
  18. vel_msg.linear.x = 0.2
  19. velocity_publisher.publish(vel_msg)
  20. else:
  21. vel_msg.linear.x = 0
  22. velocity_publisher.publish(vel_msg)
  23. a = False
  24. b = True
  25.  
  26. def odomcallback(data1):
  27. global velocity_publisher, x, vel_msg, b, c, d, g
  28. if(b == True):
  29. c = input()
  30. b = False
  31. t1 = +2.0 * (data1.pose.pose.orientation.w * data1.pose.pose.orientation.z + data1.pose.pose.orientation.x * data1.pose.pose.orientation.y)
  32. t2 = +1.0 - 2.0 * (data1.pose.pose.orientation.y ** 2 + data1.pose.pose.orientation.z**2)
  33. if(math.degrees(math.atan2(t1, t2)) < 173.0 and c != None and d == False):
  34. vel_msg.angular.z = 0.5
  35. velocity_publisher.publish(vel_msg)
  36. elif(math.degrees(math.atan2(t1, t2)) > 173.0 and d == False):
  37. vel_msg.angular.z = 0.0
  38. velocity_publisher.publish(vel_msg)
  39. d = True
  40.  
  41. if (data1.pose.pose.position.x > 0.000 and a == False and c != None and d ==True):
  42. vel_msg.linear.x = 0.2
  43. elif(x == True and a == False and c != None and d == True):
  44. vel_msg.linear.x = 0.0
  45. x = False
  46.  
  47. if (g == True and int(math.degrees(math.atan2(t1, t2))) != 0.0 and x == False):
  48. vel_msg.angular.z = 0.5
  49. velocity_publisher.publish(vel_msg)
  50. elif (g == True and x == False and int(math.degrees(math.atan2(t1, t2))) == 0):
  51. vel_msg.angular.z = 0.0
  52. velocity_publisher.publish(vel_msg)
  53. g = False
  54.  
  55.  
  56. velocity_publisher.publish(vel_msg)
  57.  
  58.  
  59.  
  60. if __name__ == '__main__':
  61. velocity_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
  62. rospy.init_node('listener', anonymous=True)
  63. vel_msg = Twist()
  64. rospy.Subscriber("/scan", LaserScan, callback)
  65. rospy.Subscriber('/odom',Odometry,odomcallback)
  66. rospy.spin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement