Advertisement
Guest User

Turtlebots

a guest
Oct 23rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #! usr#!/usr/bin/env python
  2.  
  3. import rospy
  4. from sensor_msgs.msg import LaserScan
  5. from geometry_msgs.msg import Twist
  6.  
  7.  
  8.  
  9.  
  10. def callback(msg):
  11. print('=========================')
  12. print('s1 [0]')
  13. print(msg.ranges[0]) #distance at angle 0, which is directly in front of robot
  14.  
  15. print('s2 [90]')
  16. print(msg.ranges[90]) #distance at angle 90 degrees
  17.  
  18. print('s3 [180]')
  19. print(msg.ranges[180]) #distance at angle 180 degrees
  20.  
  21. print('s4 [270]')
  22. print(msg.ranges[270]) #distance at angle 270 degrees
  23.  
  24. print('s5 [359]')
  25. print(msg.ranges[359]) #distance at angle 359 degrees
  26.  
  27.  
  28. c1 = 0
  29. c2 = 0
  30. a1 = 345
  31. a2 = 0
  32. while a1 < 359:
  33. a1 += 1
  34. r = msg.ranges[a1] > 0.3
  35. if r == False:
  36. c1 += 1
  37.  
  38. while a2 < 15:
  39. a2 += 1
  40. r = msg.ranges[a2] > 0.3
  41. if r == False:
  42. c2 += 1
  43.  
  44. if c1 > 0 or c2 > 0:
  45. move.linear.x = 0.0
  46. move.angular.z = 0.0
  47. print('Obstacle in front')
  48. print("Turning")
  49. move.linear.x = 0.0
  50. move.angular.z = 0.22
  51. if msg.ranges[90] > 0.5:
  52. move.linear.x = 0.0
  53. move.angular.z = -0.22
  54.  
  55.  
  56. else:
  57. move.linear.x = 0.22
  58. move.angular.z = 0.0
  59. print('Moving Forward')
  60.  
  61.  
  62. pub.publish(move)
  63.  
  64.  
  65. rospy.init_node('obstacle_avoidance') #Initiate node
  66. sub = rospy.Subscriber('/scan', LaserScan, callback) #Create subscriber to laser/scan topic
  67. pub = rospy.Publisher('/cmd_vel', Twist)
  68. move = Twist()
  69.  
  70. rospy.spin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement