Advertisement
Guest User

Untitled

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