Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import rospy
  4. import random
  5. import numpy as np
  6. from std_msgs.msg import String
  7. from sensor_msgs.msg import LaserScan
  8. from geometry_msgs.msg import Twist
  9.  
  10. def callBack(msg):
  11. pos = Twist()
  12. pub = rospy.Publisher('cmd_vel_mux/input/navi', Twist, queue_size=10)
  13. size = len(msg.ranges) # get the length of scan data
  14. front = np.min(msg.ranges[size/2-30:size/2+30]) # front: middle of scan range +-30 and get the min
  15. right_range = msg.ranges[0:size/2 - 29] # left: left most to middle - 30
  16. left_range = msg.ranges[size/2+31:size] # right: middle + 30 to the right most
  17. state = random.randrange(0,3) # add a random element to the code, so it doesn't get stuck in some loops
  18. print(front, np.mean(right_range), state)
  19. if front < 0.7: # To prevent the robot from hitting the wall head on
  20. pos.linear.x = -front*1.2 # constant linear speed
  21. pos.angular.z = -2*np.mean(right_range) + np.mean(left_range) # some angulat speed just to get it out from a stuck state
  22. pub.publish(pos) # publish to cmd node
  23.  
  24. elif np.mean(right_range) > 2.1 and state == 1: # when there is space on the right AND the random element
  25. pos.linear.x = front*0.1 # constant linear speed
  26. pos.angular.z = -np.mean(right_range)
  27. pub.publish(pos) # publish to cmd node
  28.  
  29. elif np.mean(left_range) > 2.1 and state == 0: # when there is space on the left AND the random element
  30. pos.linear.x = front*0.1 # constant linear speed
  31. pos.angular.z = np.mean(left_range)
  32. pub.publish(pos) # publish to cmd node
  33.  
  34. else:
  35. pos.linear.x = front*0.7
  36. pos.angular.z = -np.mean(right_range) + np.mean(left_range) # take an averge of the ranges
  37. # on the left hemisphere and right hemispher of the scan
  38. # and take the differenece, causing the robot to go to the freest space on the map
  39. pub.publish(pos) # publish to cmd node
  40.  
  41. ''' This is the simple turn right algo
  42.  
  43. front = np.mean(msg.ranges[size/2-15:size/2+15])
  44. right = np.mean(msg.ranges[size/2 - 130 - 15:size/2 - 130 + 15])
  45. left = np.mean(msg.ranges[size/2 + 130 - 15:size/2 + 130 + 15])
  46. print(front, right, left)
  47. if right > 0.85:
  48. pos.linear.x = 0.4
  49. pos.angular.z = -right
  50. pub.publish(pos)
  51.  
  52. elif front < 0.7:
  53. pos.linear.x = 0
  54. pos.angular.z = 0.3
  55. pub.publish(pos)
  56.  
  57. else:
  58. pos.linear.x = 0.3
  59. pos.angular.z = left
  60. pub.publish(pos)
  61. '''
  62.  
  63.  
  64. def main():
  65. rospy.init_node('ForceMapper')
  66. rospy.Subscriber("/scan", LaserScan, callBack)
  67. # Spin until ctrl + c
  68. rospy.spin()
  69.  
  70. if __name__ == '__main__':
  71. main()
  72.  
  73.  
  74. #if object close to front (always pass string)
  75. #array of three F,L,R
  76. #If no longer object to right(entrance) turn right to follow wall
  77. #three parrallel while loops
  78. #all in feelforce
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement