Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #! usr#!/usr/bin/env python
  2.  
  3. import rospy
  4. from math import pi
  5. from sensor_msgs.msg import LaserScan
  6. from geometry_msgs.msg import Twist
  7. from time import sleep
  8.  
  9. def shutdown():
  10. """
  11. This function runs the shutdown procedure of the program when Ctrl + C is pressed
  12. """
  13. rospy.loginfo("Stopping TurtleBot...") #prints to hidden log file
  14. pub.publish(Twist()) #sends an empty velocity object to the robot's motor in the /cmd_vel topic
  15. rospy.sleep(1) #sleeps the program for 1 second to prevent interference from other nodes, and to give time for unfinished commands to finish
  16.  
  17.  
  18.  
  19. def callback(msg):
  20. print('=========================')
  21. #print('s1 [0]')
  22. #print(msg.ranges[0]) #distance at angle 0, which is directly in front of robot
  23.  
  24. #print('s2 [90]')
  25. #print(msg.ranges[90]) #distance at angle 90 degrees
  26.  
  27. #print('s3 [180]')
  28. #print(msg.ranges[180]) #distance at angle 180 degrees
  29.  
  30. print('s4 [270]')
  31. print(msg.ranges[270]) #distance at angle 270 degrees
  32.  
  33. #print('s5 [359]')
  34. #print(msg.ranges[359]) #distance at angle 359 degrees
  35.  
  36.  
  37. if 0.11 < msg.ranges[270] < 0.25:
  38. move.linear.x = 0.1
  39. move.angular.z = 0.0
  40. print('Moving forward')
  41. pub.publish(move)
  42. sleep(0.1)
  43. if msg.ranges[270] == 0.0 or msg.ranges[270] > 0.25:
  44. move.linear.x = 0.0
  45. move.angular.z = -1 * pi
  46. print('Turning')
  47. pub.publish(move)
  48. sleep(0.2)
  49. move.angular.z = 0.0
  50. pub.publish(move)
  51. if 0.01 < msg.ranges[0] == 0.1:
  52. move.linear.x = -0.1
  53. move.angular.z = 0.0
  54. print('Backward')
  55. pub.publish(move)
  56. sleep(0.2)
  57.  
  58.  
  59.  
  60.  
  61. #print('Obstable in front')
  62.  
  63.  
  64.  
  65.  
  66.  
  67. rospy.init_node('obstacle_avoidance') #Initiate node
  68. sub = rospy.Subscriber('/scan', LaserScan, callback) #Create subscriber to laser/scan topic
  69. pub = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
  70. move = Twist()
  71.  
  72. rospy.spin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement