Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
98
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.     lr2 = laser_range[0,front_angles]
  29.     nearestToOne = find_nearest(lr2,1.0)
  30.     rospy.loginfo(nearestToOne)
  31.     if nearestToOne - 1 <= 0.05 and nearestToOne -1 >= - 0.05:
  32.         pwm.ChangeDutyCycle(5)
  33.         GPIO.output(sol_point,GPIO.HIGH)
  34.         time.sleep(0.5)
  35.         GPIO.output(sol_point,GPIO.LOW)
  36.         time.sleep(0.5)
  37.         rospy.loginfo('Distance of %f located, firing plunger',nearestToOne)
  38.         time.sleep(0.5)
  39.  
  40. def scan_forServo():
  41.     rospy.init_node('scan_forServo', anonymous = True)
  42.  
  43.     rospy.Subscriber('scan', LaserScan, get_laserscan)
  44.  
  45.     rate.sleep()
  46.     rospy.spin()
  47.  
  48. if __name__ == '__main__':
  49.     try:
  50.         scan_forServo()
  51.     except ROSInterruptException:
  52.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement