...
Code Block |
---|
def distance(self): GPIO.setmode(GPIO.BCM) # setting GPIO pins self.setup_ultrasonic() # set Trigger to HIGH GPIO.output(self.GPIO_TRIGGER, True) # set a small delay and change Trigger to LOW/False time.sleep(0.00001) GPIO.output(self.GPIO_TRIGGER, False) # get the start time StartTimestart_time = time.time() StopTimestop_time = time.time() while GPIO.input(self.GPIO_ECHO) == 0: StartTimestart_time = time.time() # get stop time while GPIO.input(self.GPIO_ECHO) == 1: StopTimestop_time = time.time() # time difference between start and arrival TimeElapsedtime_occured = StopTimestop_time - StartTime start_time # sound speed (34300 cm/s) distance = (TimeElapsedtime_occured * 34300) / 2 GPIO.cleanup() return distance |
How to get data from the sensors?
Create an Ultrasonic Interface object and pass in the TRIG and ECHO pins as arguments.
Code Block |
---|
ultrasonic_object = UltrasonicInterface(16,18) dist = ultrasonic_object.distance() |
...