Пример #1
0
def main():

    GPIO_TRIGGER = 66  #P8.7
    GPIO_ECHO = 67  #P8.8
    PWM_PORT = 6  #P8.13

    LPF_A = 0.5

    MIN_DIST = 50.0
    MAX_DIST = 150.0
    DIST_RANGE = MAX_DIST - MIN_DIST

    MIN_FREQ = 220
    MAX_FREQ = 880
    FREQ_RANGE = MAX_FREQ - MIN_FREQ

    try:
        print("Press Ctrl+C to finish")
        meter = Ultrasonic(GPIO_TRIGGER, GPIO_ECHO)
        buzz = Buzzer(6)

        dist = meter.read()
        time.sleep(0.5)
        while True:

            distRead = meter.read()

            if distRead != Ultrasonic.OUT_OF_RANGE:

                dist += (distRead - dist) * LPF_A
                print("~ {0:.3f} cm".format(dist))

                if dist > MIN_DIST and dist < MAX_DIST:
                    freq = (((dist - MIN_DIST) / DIST_RANGE) *
                            FREQ_RANGE) + MIN_FREQ
                    buzz.playNote(freq, 0.5)

                else:
                    time.sleep(0.5)

            else:
                print("Out of range!")
                time.sleep(0.5)

    except KeyboardInterrupt:
        print("\nCtrl+C pressed.")

    finally:
        print("Bye!")
        meter.cleanup()
        buzz.cleanup()
Пример #2
0
def main():
    '''
    Makes a LED to blink according to the distance meassured
    by an ultrasonic sensor.
    Finish when user press a toggle key.
    '''

    blinker = Blinker(27, MAX_DELAY)  #P8.17
    ultrasonic = Ultrasonic(66, 69)  #P8.7, P8.9
    key = Gpio(65, Gpio.IN)  #P8.18

    try:

        print("Ready. Press toggle key to start.")

        #Wait for key down event
        while key.getValue() == Gpio.LOW:

            sleep(0.2)

        #Wait for key up event
        while key.getValue() == Gpio.HIGH:

            sleep(0.2)

        print("Started. Press toggle key again to finish.")
        blinker.start()

        while key.getValue() == Gpio.LOW:
            dist = ultrasonic.read()
            delay = calculateDelay(dist)
            blinker.setDelay(delay)
            sleep(0.2)

        print("Bye!")

    finally:
        key.cleanup()
        blinker.stop()
        blinker.cleanup()
        ultrasonic.cleanup()
Пример #3
0
class DistanceErrorApp(ExperimentApp):

    GPIO_TRIGGER = 66  #P8.7
    GPIO_ECHO = 67  #P8.8

    def __init__(self):

        super().__init__("Distance Error")
        self.setPollingPeriod(1)

        self._meter = Ultrasonic(DistanceErrorApp.GPIO_TRIGGER,
                                 DistanceErrorApp.GPIO_ECHO)

    def getValues(self):

        distRead = self._meter.read()
        if distRead == Ultrasonic.OUT_OF_RANGE:
            distRead = 0

        return (100, int(distRead))

    def cleanup(self):

        self._meter.cleanup()