9 This class encapsulates a type of acoustic ranger. In particular 10 the type of ranger with separate trigger and echo pins. 12 A pulse on the trigger initiates the sonar ping and shortly 13 afterwards a sonar pulse is transmitted and the echo pin 14 goes high. The echo pins stays high until a sonar echo is 15 received (or the response times-out). The time between 16 the high and low edges indicates the sonar round trip time. 21 The class is instantiated with the Pi to use and the 22 gpios connected to the trigger and echo pins. 37 pi.set_mode(self.
_trig, pigpio.OUTPUT)
38 pi.set_mode(self.
_echo, pigpio.INPUT)
41 self.
_cb = pi.callback(self.
_echo, pigpio.EITHER_EDGE, self.
_cbf)
45 def _cbf(self, gpio, level, tick):
46 if gpio == self.
_trig:
55 if self.
_high is not None:
62 Triggers a reading. The returned reading is the number 63 of microseconds for the sonar round-trip. 65 round trip cms = round trip time / 1000000.0 * 34030 69 self.pi.gpio_trigger(self.
_trig)
72 if (time.time()-start) > 5.0:
81 Cancels the ranger and returns the gpios to their 90 if __name__ ==
"__main__":
96 import sonar_trigger_echo
102 end = time.time() + 600.0
105 while time.time() < end:
107 print(
"{} {}".format(r, sonar.read()))
def __init__(self, pi, trigger, echo)
def _cbf(self, gpio, level, tick)