Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 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 std_msgs.msg import Float64
  8. from sensor_msgs.msg import LaserScan
  9. from geometry_msgs.msg import Twist
  10.  
  11. prev_dist = 0
  12. state = 0
  13. i = 0
  14.  
  15. def callBack_dist(msg):
  16. global prev_dist, state, i
  17. new_dist = msg.data
  18. arr = np.arange(-15,15)
  19. if new_dist == prev_dist:
  20. state = arr[i % len(arr)]
  21. i += 1
  22. prev_dist = new_dist
  23.  
  24. def callBack_scan(msg):
  25. pos = Twist()
  26. pub = rospy.Publisher('cmd_vel_mux/input/navi', Twist, queue_size=10)
  27.  
  28. size = len(msg.ranges) # get the length of scan data
  29. norm = np.linalg.norm(np.multiply(msg.ranges, np.arange(-size/2,size/2)))
  30. rot = sum(np.multiply(msg.ranges, np.arange(-size/2,size/2))/norm)
  31. print(sum(msg.ranges))
  32. if min(msg.ranges[size/2-50:size/2+50]) < 0.6:
  33. pos.linear.x = 0
  34. pos.angular.z = state*0.5
  35. pub.publish(pos)
  36.  
  37. elif sum(msg.ranges) > 1450:
  38. pos.linear.x = 0.2*min(msg.ranges[size/2-40:size/2+40])
  39. pos.angular.z = state*0.5
  40. pub.publish(pos)
  41.  
  42. elif max(msg.ranges[size/2-40:size/2+40]) >= 0.7*max(msg.ranges):
  43. pos.linear.x = min(msg.ranges[size/2-40:size/2+40])
  44. pos.angular.z = 0
  45. pub.publish(pos)
  46.  
  47. else:
  48. pos.linear.x = min(msg.ranges[size/2-40:size/2+40])
  49. pos.angular.z = rot
  50. pub.publish(pos)
  51.  
  52.  
  53.  
  54. def main():
  55. rospy.init_node('ForceMapper')
  56. rospy.Subscriber("/scan", LaserScan, callBack_scan)
  57. rospy.Subscriber("/dist", Float64, callBack_dist)
  58. # Spin until ctrl + c
  59. rospy.spin()
  60.  
  61. if __name__ == '__main__':
  62. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement