Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 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. print math.degrees(math.atan2(t1, t2))
  34. if(math.degrees(math.atan2(t1, t2)) < 178.0 and c != None and d == False):
  35. vel_msg.angular.z = 0.5
  36. velocity_publisher.publish(vel_msg)
  37. elif(math.degrees(math.atan2(t1, t2)) >178.0 and d == False):
  38. vel_msg.angular.z = 0.0
  39. velocity_publisher.publish(vel_msg)
  40. d = True
  41.  
  42. if (data1.pose.pose.position.x > 0.000 and a == False and c != None and d ==True):
  43. vel_msg.linear.x = 0.2
  44. elif(x == True and a == False and c != None and d == True):
  45. vel_msg.linear.x = 0.0
  46. x = False
  47.  
  48. if (g == True and math.degrees(math.atan2(t1, t2)) < 178.0 and x == False):
  49. vel_msg.angular.z = -0.5
  50. velocity_publisher.publish(vel_msg)
  51. elif (g == True and x == False):
  52. vel_msg.angular.z = 0.0
  53. velocity_publisher.publish(vel_msg)
  54. g = False
  55.  
  56.  
  57. velocity_publisher.publish(vel_msg)
  58.  
  59.  
  60.  
  61. if __name__ == '__main__':
  62. velocity_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
  63. rospy.init_node('listener', anonymous=True)
  64. vel_msg = Twist()
  65. rospy.Subscriber("/scan", LaserScan, callback)
  66. rospy.Subscriber('/odom',Odometry,odomcallback)
  67. rospy.spin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement