Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def pinSonar(self, pin):
- #print pin
- #print self.pinUse[pin]
- self.pinUse[pin] = self.PSONAR
- GPIO.setup(pin,GPIO.OUT)
- ti = time.time()
- # setup a list to hold 3 values and then do 3 distance calcs and store them
- #print 'sonar started'
- distlist = [0.0,0.0,0.0]
- distance = 0
- ts=time.time()
- try:
- for k in range(3):
- #print "sonar pulse" , k
- GPIO.output(pin, 0)
- time.sleep(0.05)#set pin low for 50ms as per spec
- GPIO.output(pin, 1) # Send Pulse high
- time.sleep(0.00001) # wait
- GPIO.output(pin, 0) # bring it back low - pulse over.
- t0=time.time() # remember current time
- GPIO.setup(pin,GPIO.IN)
- #PIN_USE[i] = PINPUT don't bother telling system
- t1=t0
- # This while loop waits for input pin (7) to be low but with a 0.04sec timeout
- while ((GPIO.input(pin)==0) and ((t1-t0) < 0.02)):
- #time.sleep(0.00001)
- t1=time.time()
- t1=time.time()
- #print 'low' , (t1-t0).microseconds
- t2=t1
- # This while loops waits for input pin to go high to indicate pulse detection
- # with 0.04 sec timeout
- while ((GPIO.input(pin)==1) and ((t2-t1) < 0.02)):
- #time.sleep(0.00001)
- t2=time.time()
- t2=time.time()
- #print 'high' , (t2-t1).microseconds
- t3=(t2-t1) # t2 contains time taken for pulse to return
- #print "total time " , t3
- distance=t3*343/2*100 # calc distance in cm
- distlist[k]=distance
- #print distance
- GPIO.setup(pin,GPIO.OUT)
- tf = time.time() - ts
- distance = sorted(distlist)[1] # sort the list and pick middle value as best distance
- except:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement