anandvgeorge

ultrasonic.py

May 6th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import rospy
  4. import roslib
  5. import sys
  6. import RPi.GPIO as GPIO
  7. import time
  8. roslib.load_manifest('camera')
  9. from camera.msg import ball
  10. from std_msgs.msg import Int8
  11.  
  12. distance = 0
  13. MOVE = 1 #moving
  14.  
  15. TRIG = 23                                  #Associate pin 23 to TRIG
  16. ECHO = 24                                  #Associate pin 24 to ECHO
  17.  
  18. def findDistance():
  19.     global distance
  20.     GPIO.output(TRIG, False)                 #Set TRIG as LOW
  21.     # print "Waitng For Sensor To Settle"
  22.     time.sleep(2)                            #Delay of 2 seconds
  23.  
  24.     GPIO.output(TRIG, True)                  #Set TRIG as HIGH
  25.     time.sleep(0.00001)                      #Delay of 0.00001 seconds
  26.     GPIO.output(TRIG, False)                 #Set TRIG as LOW
  27.  
  28.     while GPIO.input(ECHO)==0:               #Check whether the ECHO is LOW
  29.         pulse_start = time.time()              #Saves the last known time of LOW pulse
  30.  
  31.     while GPIO.input(ECHO)==1:               #Check whether the ECHO is HIGH
  32.         pulse_end = time.time()                #Saves the last known time of HIGH pulse
  33.  
  34.     pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable
  35.  
  36.     distance = pulse_duration * 17150        #Multiply pulse duration by 17150 to get distance
  37.     distance = round(distance, 2)            #Round to two decimal points
  38.  
  39.     if distance > 1 and distance < 400:      #Check whether the distance is within range
  40.         print "Distance:",distance - 1.2,"cm"  #Print distance with 0.5 cm calibration
  41.     else:
  42.         print "Out Of Range"                   #display out of range
  43.     return
  44.  
  45. def callback(data):
  46.     global MOVE
  47.     pub = rospy.Publisher('stop', Int8, queue_size = 10)
  48.     # start measuring
  49.     # print "callback!!"
  50.     findDistance()
  51.     # set GPIO pin
  52.     # print("\ntest_callback")
  53.     if distance <= 12:
  54.         pub.publish(not MOVE) # not moving
  55.         # stop robot
  56.         # reset GPIO pins
  57.         return
  58.     pub.publish(MOVE)
  59.  
  60. def listener():
  61.     rospy.Subscriber("ball_pos", ball, callback)
  62.     # print("\ntest")
  63.  
  64.     # spin() simply keeps python from exiting until this node is stopped
  65.     rospy.spin()
  66.  
  67. if __name__ == '__main__':
  68.     # Initialize to use BCM numbering.
  69.     GPIO.setmode(GPIO.BCM)
  70.    
  71.     GPIO.setup(TRIG,GPIO.OUT)                  #Set pin as GPIO out
  72.     GPIO.setup(ECHO,GPIO.IN)                   #Set pin as GPIO in
  73.  
  74.     rospy.init_node('ultrasonic', anonymous=True)
  75.     print ("working")
  76.     listener()
Add Comment
Please, Sign In to add comment