Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import time
  2. import RPi.GPIO as GPIO
  3. import numpy as np
  4. from sensor_msgs.msg import LaserScan
  5.  
  6. GPIO.setmode(GPIO.BOARD)
  7. laser_range = np.array([])
  8. servo_point = 32
  9. sol_point = 40
  10. front_angle = 1
  11. front_angles = range(-front_angle, front_angle+1, 1)
  12.  
  13. GPIO.setup(servo_point,GPIO.OUT)
  14. GPIO.setup(sol_point,GPIO.OUT)
  15.  
  16. pwm = GPIO.PWM(servo_point,50)
  17. pwm.start(2.5)
  18. time.sleep(1)
  19.  
  20. def find_nearest(array,value):
  21.     array = np.asarray(array)
  22.     idx = (np.abs(array-value)).argmin()
  23.     return array[idx]
  24.  
  25. def get_laserscan(msg):
  26.     global laser_range
  27.     laser_range = np.array([msg.range])
  28.  
  29.  
  30. def scan_forServo():
  31.     rospy.init_node('scan_forServo', anonymous = True)
  32.  
  33.     rospy.Subscriber('scan', LaserScan, get_laserscan)
  34.  
  35.     lr2 = laser_range[0,front_angles]
  36.     nearestToOne = find_nearest(lr2,1.0)
  37.     rospy.loginfo(nearestToOne)
  38.     if nearestToOne - 1 <= 0.05 and nearestToOne -1 >= - 0.05:
  39.         pwm.ChangeDutyCycle(5)
  40.         GPIO.output(sol_point,GPIO.HIGH)
  41.         time.sleep(0.5)
  42.         GPIO.output(sol_point,GPIO.LOW)
  43.         time.sleep(0.5)
  44.         rospy.loginfo('Distance of %f located, firing plunger',nearestToOne)
  45.         time.sleep(0.5)
  46.  
  47.     rate.sleep()
  48.     rospy.spin()
  49.  
  50. if __name__ == '__main__':
  51.     try:
  52.         scan_forServo()
  53.     except ROSInterruptException:
  54.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement