-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathranger.py
36 lines (30 loc) · 1.03 KB
/
ranger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import RPi.GPIO as GPIO
import time
class ranger:
""" Class defining an Ultrasonic Ranger
take in the pin number in BCM mode
has a single function measure to get the distance
"""
def __init__ (self, trigPin, echoPin) :
self.trigPin = trigPin
self.echoPin = echoPin
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.trigPin,GPIO.OUT)
GPIO.setup(self.echoPin,GPIO.IN)
def measure (self) :
""" Retrurns the ditance measured in CM
"""
GPIO.output(self.trigPin, GPIO.LOW)
#print "Waiting For Sensor To Settle"
time.sleep(0.05)
GPIO.output(self.trigPin, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(self.trigPin, GPIO.LOW)
while GPIO.input(self.echoPin)==0:
pulse_start = time.time()
while GPIO.input(self.echoPin)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
return (distance)